-
Notifications
You must be signed in to change notification settings - Fork 14
/
msg_queue_test.go
133 lines (118 loc) · 2.69 KB
/
msg_queue_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
132
133
package pbft
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMsgQueue_RoundChangeState(t *testing.T) {
m := newMsgQueue()
// insert non round change messages
{
m.pushMessage(createMessage("A", MessageReq_Prepare, ViewMsg(1, 1)))
m.pushMessage(createMessage("B", MessageReq_Commit, ViewMsg(1, 1)))
// we only read round state messages
assert.Nil(t, m.readMessage(RoundChangeState, ViewMsg(1, 0)))
}
// insert old round change messages
{
m.pushMessage(createMessage("C", MessageReq_RoundChange, ViewMsg(1, 1)))
// the round change message is old
assert.Nil(t, m.readMessage(RoundChangeState, ViewMsg(2, 0)))
assert.Zero(t, m.roundChangeStateQueue.Len())
}
// insert two valid round change messages with an old one in the middle
{
m.pushMessage(createMessage("D", MessageReq_RoundChange, ViewMsg(2, 2)))
m.pushMessage(createMessage("E", MessageReq_RoundChange, ViewMsg(1, 1)))
m.pushMessage(createMessage("F", MessageReq_RoundChange, ViewMsg(2, 1)))
msg1 := m.readMessage(RoundChangeState, ViewMsg(2, 0))
assert.NotNil(t, msg1)
assert.Equal(t, msg1.From, NodeID("F"))
msg2 := m.readMessage(RoundChangeState, ViewMsg(2, 0))
assert.NotNil(t, msg2)
assert.Equal(t, msg2.From, NodeID("D"))
}
// insert future messages to the queue => such messages should not be retrieved back
{
m = newMsgQueue()
m.pushMessage(createMessage("A", MessageReq_RoundChange, ViewMsg(3, 1)))
assert.Nil(t, m.readMessage(RoundChangeState, ViewMsg(1, 1)))
m.pushMessage(createMessage("A", MessageReq_Commit, ViewMsg(3, 1)))
assert.Nil(t, m.readMessage(CommitState, ViewMsg(1, 1)))
}
}
func Test_msgToState(t *testing.T) {
expectedResult := map[MsgType]State{
MessageReq_RoundChange: RoundChangeState,
MessageReq_Preprepare: AcceptState,
MessageReq_Prepare: ValidateState,
MessageReq_Commit: ValidateState,
}
for msgType, st := range expectedResult {
assert.Equal(t, st, msgToState(msgType))
}
}
func TestCmpView(t *testing.T) {
var cases = []struct {
x, y *View
expectedResult int
}{
{
&View{
Sequence: 1,
Round: 1,
},
&View{
Sequence: 2,
Round: 1,
},
-1,
},
{
&View{
Sequence: 2,
Round: 1,
},
&View{
Sequence: 1,
Round: 1,
},
1,
},
{
&View{
Sequence: 1,
Round: 1,
},
&View{
Sequence: 1,
Round: 2,
},
-1,
},
{
&View{
Sequence: 1,
Round: 2,
},
&View{
Sequence: 1,
Round: 1,
},
1,
},
{
&View{
Sequence: 1,
Round: 1,
},
&View{
Sequence: 1,
Round: 1,
},
0,
},
}
for _, c := range cases {
assert.Equal(t, cmpView(c.x, c.y), c.expectedResult)
}
}