-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraft_test.go
76 lines (62 loc) · 1.69 KB
/
raft_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"io/ioutil"
"log"
"os"
"testing"
"time"
"github.com/tidwall/buntdb"
)
// Test_StoreOpen tests that the store can be opened.
func Test_StoreOpen(t *testing.T) {
// Open the data.db file. It will be created if it doesn't exist.
db, err := buntdb.Open(":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
tmpDir, _ := ioutil.TempDir("", "store_test")
defer os.RemoveAll(tmpDir)
s := NewStore(tmpDir, "127.0.0.1:0", db).(*rStore)
if err := s.Open(false, "node0"); err != nil {
t.Fatalf("failed to open store: %s", err)
}
}
// Test_Storage tests that a command can be applied to the log
func Test_Storage(t *testing.T) {
// Open the data.db file. It will be created if it doesn't exist.
db, err := buntdb.Open(":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
tmpDir, _ := ioutil.TempDir("", "store_test")
defer os.RemoveAll(tmpDir)
s := NewStore(tmpDir, "127.0.0.1:0", db).(*rStore)
if err := s.Open(true, "node0"); err != nil {
t.Fatalf("failed to open store: %s", err)
}
// Simple way to ensure there is a leader.
time.Sleep(3 * time.Second)
if err := s.Set("foo", "bar"); err != nil {
t.Fatalf("failed to set key: %s", err.Error())
}
// Wait for committed log entry to be applied.
time.Sleep(500 * time.Millisecond)
value, err := s.Get("foo")
if err != nil {
t.Fatalf("failed to get key: %s", err.Error())
}
if value != "bar" {
t.Fatalf("key has wrong value: %s", value)
}
// Wait for committed log entry to be applied.
time.Sleep(500 * time.Millisecond)
value, err = s.Get("foo")
if err != nil {
t.Fatalf("failed to get key: %s", err.Error())
}
if value != "bar" {
t.Fatalf("key has wrong value: %s", value)
}
}