-
Notifications
You must be signed in to change notification settings - Fork 2
/
agree_test.go
81 lines (61 loc) · 1.52 KB
/
agree_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
77
78
79
80
81
package agree
import (
"fmt"
"os/exec"
"testing"
//"time"
)
func init() {
fmt.Println("Cleaning up previous Raft state...")
exec.Command("rm", "-rf", "snapshots/*")
exec.Command("rm", "raft.db")
}
type testStruct struct {
Value string
}
func (t *testStruct) Set(newValue string) {
t.Value = newValue
}
func (t *testStruct) Get() string {
return t.Value
}
func TestSingleNode(t *testing.T) {
s := &testStruct{}
wt, err := Wrap(s, &Config{})
var changed bool
if err != nil {
t.Fatalf("Failed to wrap: %s", err.Error())
}
notify := func(mutation Mutation) {
if len(mutation.MethodArgs) != 1 {
t.Fatalf("Wrong number of arguments: expected %d but got %d", 1, len(mutation.MethodArgs))
}
argVal, ok := mutation.MethodArgs[0].(string)
if !ok {
t.Fatal("Arg had incorrect type")
}
if argVal != "hello" {
t.Fatalf("Expected arg to be %s but got %s", "hello", argVal)
}
changed = true
}
wt.SubscribeFunc("Set", notify)
err = wt.Mutate("Set", "hello")
//time.Sleep(time.Second * 3)
if err != nil {
t.Fatalf("Failed to mutate: %s", err.Error())
}
if !changed {
t.Fatal("Callback did not get called")
}
if v, err := wt.Read("Get", Any); err != nil {
t.Fatalf("Received unexpected error %v", v)
} else if v.(string) != "hello" {
t.Fatalf("Expected %s, received %s", "hello", v.(string))
}
if v, err := wt.Read("Get", Consistent); err != nil {
t.Fatalf("Received unexpected error %v", v)
} else if v.(string) != "hello" {
t.Fatalf("Expected %s, received %s", "hello", v.(string))
}
}