-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
84 lines (82 loc) · 1.8 KB
/
node_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
82
83
84
package bullyelection
import (
"bytes"
"strings"
"testing"
)
func TestNodeMeta(t *testing.T) {
t.Run("toJSON/fromJSON", func(tt *testing.T) {
sb := strings.Builder{}
if err := toJSON(&sb, nodeMeta{
ID: "test1",
Addr: "testaddr",
}); err != nil {
tt.Fatalf("toJSON: %+v", err)
}
nodeMeta, err := fromJSON(strings.NewReader(sb.String()))
if err != nil {
tt.Fatalf("fromJSON: %+v", err)
}
if nodeMeta.ID != "test1" {
tt.Errorf("unserialize from json")
}
if nodeMeta.Addr != "testaddr" {
tt.Errorf("unserialize from json")
}
})
t.Run("metaToNode/full", func(tt *testing.T) {
node := nodeMetaToNode(nodeMeta{
ID: "testid",
Addr: "testaddr",
IsVoter: true,
UserMetadata: []byte("testusermetadata"),
LeaderID: "testid",
ULID: "7890",
})
if node.ID() != "testid" {
tt.Errorf("ID")
}
if node.Addr() != "testaddr" {
tt.Errorf("Addr")
}
if node.IsVoter() != true {
tt.Errorf("IsVoterNode")
}
if bytes.Equal(node.UserMetadata(), []byte("testusermetadata")) != true {
tt.Errorf("UserMetadata")
}
if node.IsLeader() != true {
tt.Errorf("IsLeader")
}
vn := node.(internalVoterNode)
if vn.getULID() != "7890" {
tt.Errorf("getULID")
}
})
t.Run("metaToNode/partial", func(tt *testing.T) {
node := nodeMetaToNode(nodeMeta{
ID: "testid",
Addr: "testaddr",
IsVoter: true,
})
if node.ID() != "testid" {
tt.Errorf("ID")
}
if node.Addr() != "testaddr" {
tt.Errorf("Addr")
}
if node.IsVoter() != true {
tt.Errorf("IsVoterNode")
}
if bytes.Equal(node.UserMetadata(), []byte{}) != true {
tt.Errorf("initial value")
}
if node.IsLeader() {
tt.Errorf("IsLeader")
}
vn := node.(internalVoterNode)
if vn.getULID() != "" {
tt.Errorf("initial value")
}
})
}