-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscomm_test.go
131 lines (102 loc) · 2.57 KB
/
scomm_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package spaxos
import (
"encoding/json"
"fmt"
"os"
"testing"
// pb "spaxos/spaxospb"
pb "github.com/dengoswei/spaxos/spaxospb"
)
func TestFakeSwitch(t *testing.T) {
printIndicate()
selfid := uint64(1)
testid := uint64(2)
fswitch := NewFakeSwitch(selfid)
assert(nil != fswitch)
sendc := fswitch.GetSendChan()
assert(nil != sendc)
recvc := fswitch.GetRecvChan()
assert(nil != recvc)
tsendc := make(chan pb.Message)
done := make(chan struct{})
go fswitch.run(tsendc, done)
defer close(done)
smsg := pb.Message{From: selfid, To: testid}
sendc <- smsg
trmsg := <-tsendc
assert(trmsg.From == selfid)
assert(trmsg.To == testid)
rmsg := pb.Message{From: testid, To: selfid}
fswitch.crecvc <- rmsg
tsmsg := <-recvc
assert(tsmsg.To == selfid)
assert(tsmsg.From == testid)
}
func TestFakeSwitchCenter(t *testing.T) {
printIndicate()
groups := make(map[uint64]bool)
groups[uint64(1)] = true
groups[uint64(2)] = true
groups[uint64(3)] = true
fcenter := NewFakeSwitchCenter(groups)
assert(nil != fcenter)
for id, _ := range groups {
fswitch := fcenter.Get(id)
assert(nil != fswitch)
assert(fswitch.id == id)
}
go fcenter.Run()
defer fcenter.Stop()
msg := pb.Message{From: 1, To: 2}
fswitch := fcenter.Get(1)
fswitch.GetSendChan() <- msg
rmsg := <-fcenter.Get(2).GetRecvChan()
assert(rmsg.From == 1)
assert(rmsg.To == 2)
msg.From = 3
msg.To = 1
fcenter.Get(3).GetSendChan() <- msg
rmsg = <-fcenter.Get(1).GetRecvChan()
assert(rmsg.From == 3)
assert(rmsg.To == 1)
}
func TestFakeStorage(t *testing.T) {
printIndicate()
store := NewFakeStorage()
assert(nil != store)
assert(nil != store.table)
hs := randHardState()
assert(0 != hs.Index)
err := store.Store([]pb.HardState{hs})
assert(nil == err)
newhs, err := store.Get(hs.Index)
assert(nil == err)
assert(true == hs.Equal(&newhs))
newhs, err = store.Get(hs.Index + 1)
assert(nil != err)
assert(err == IndexNotExist)
assert(0 == newhs.Index)
}
func TestGroupEntry(t *testing.T) {
printIndicate()
entry := &GroupEntry{Id: 1, Ip: "127.0.0.1", Port: 10001}
s, err := json.Marshal(entry)
assert(nil == err)
os.Stdout.Write(s)
println()
conststr := `{"id": 1, "ip": "127.0.0.1", "port": 10001}`
newentry := &GroupEntry{}
err = json.Unmarshal([]byte(conststr), newentry)
assert(nil == err)
fmt.Printf("%v\n", newentry)
}
func TestReadConfig(t *testing.T) {
printIndicate()
c := NewDefaultConfig()
fmt.Printf("%d %v\n", len(c.Groups), c)
assert(0 < c.Selfid)
groups := c.GetGroupIds()
assert(len(groups) == len(c.Groups))
entry := c.GetEntry(c.Selfid)
assert(c.Selfid == entry.Id)
}