forked from hashicorp/memberlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_test.go
172 lines (148 loc) · 4.44 KB
/
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package memberlist
import (
"testing"
)
func TestTransmitLimited_Queue(t *testing.T) {
q := &TransmitLimitedQueue{RetransmitMult: 1, NumNodes: func() int { return 1 }}
q.QueueBroadcast(&memberlistBroadcast{"test", nil, nil})
q.QueueBroadcast(&memberlistBroadcast{"foo", nil, nil})
q.QueueBroadcast(&memberlistBroadcast{"bar", nil, nil})
if len(q.bcQueue) != 3 {
t.Fatalf("bad len")
}
if q.bcQueue[0].b.(*memberlistBroadcast).node != "test" {
t.Fatalf("missing test")
}
if q.bcQueue[1].b.(*memberlistBroadcast).node != "foo" {
t.Fatalf("missing foo")
}
if q.bcQueue[2].b.(*memberlistBroadcast).node != "bar" {
t.Fatalf("missing bar")
}
// Should invalidate previous message
q.QueueBroadcast(&memberlistBroadcast{"test", nil, nil})
if len(q.bcQueue) != 3 {
t.Fatalf("bad len")
}
if q.bcQueue[0].b.(*memberlistBroadcast).node != "foo" {
t.Fatalf("missing foo")
}
if q.bcQueue[1].b.(*memberlistBroadcast).node != "bar" {
t.Fatalf("missing bar")
}
if q.bcQueue[2].b.(*memberlistBroadcast).node != "test" {
t.Fatalf("missing test")
}
}
func TestTransmitLimited_GetBroadcasts(t *testing.T) {
q := &TransmitLimitedQueue{RetransmitMult: 3, NumNodes: func() int { return 10 }}
// 18 bytes per message
q.QueueBroadcast(&memberlistBroadcast{"test", []byte("1. this is a test."), nil})
q.QueueBroadcast(&memberlistBroadcast{"foo", []byte("2. this is a test."), nil})
q.QueueBroadcast(&memberlistBroadcast{"bar", []byte("3. this is a test."), nil})
q.QueueBroadcast(&memberlistBroadcast{"baz", []byte("4. this is a test."), nil})
// 2 byte overhead per message, should get all 4 messages
all := q.GetBroadcasts(2, 80)
if len(all) != 4 {
t.Fatalf("missing messages: %v", all)
}
// 3 byte overhead, should only get 3 messages back
partial := q.GetBroadcasts(3, 80)
if len(partial) != 3 {
t.Fatalf("missing messages: %v", partial)
}
}
func TestTransmitLimited_GetBroadcasts_Limit(t *testing.T) {
q := &TransmitLimitedQueue{RetransmitMult: 1, NumNodes: func() int { return 10 }}
// 18 bytes per message
q.QueueBroadcast(&memberlistBroadcast{"test", []byte("1. this is a test."), nil})
q.QueueBroadcast(&memberlistBroadcast{"foo", []byte("2. this is a test."), nil})
q.QueueBroadcast(&memberlistBroadcast{"bar", []byte("3. this is a test."), nil})
q.QueueBroadcast(&memberlistBroadcast{"baz", []byte("4. this is a test."), nil})
// 3 byte overhead, should only get 3 messages back
partial1 := q.GetBroadcasts(3, 80)
if len(partial1) != 3 {
t.Fatalf("missing messages: %v", partial1)
}
partial2 := q.GetBroadcasts(3, 80)
if len(partial2) != 3 {
t.Fatalf("missing messages: %v", partial2)
}
// Only two not expired
partial3 := q.GetBroadcasts(3, 80)
if len(partial3) != 2 {
t.Fatalf("missing messages: %v", partial3)
}
// Should get nothing
partial5 := q.GetBroadcasts(3, 80)
if len(partial5) != 0 {
t.Fatalf("missing messages: %v", partial5)
}
}
func TestTransmitLimited_Prune(t *testing.T) {
q := &TransmitLimitedQueue{RetransmitMult: 1, NumNodes: func() int { return 10 }}
ch1 := make(chan struct{}, 1)
ch2 := make(chan struct{}, 1)
// 18 bytes per message
q.QueueBroadcast(&memberlistBroadcast{"test", []byte("1. this is a test."), ch1})
q.QueueBroadcast(&memberlistBroadcast{"foo", []byte("2. this is a test."), ch2})
q.QueueBroadcast(&memberlistBroadcast{"bar", []byte("3. this is a test."), nil})
q.QueueBroadcast(&memberlistBroadcast{"baz", []byte("4. this is a test."), nil})
// Keep only 2
q.Prune(2)
if q.NumQueued() != 2 {
t.Fatalf("bad len")
}
// Should notify the first two
select {
case <-ch1:
default:
t.Fatalf("expected invalidation")
}
select {
case <-ch2:
default:
t.Fatalf("expected invalidation")
}
if q.bcQueue[0].b.(*memberlistBroadcast).node != "bar" {
t.Fatalf("missing bar")
}
if q.bcQueue[1].b.(*memberlistBroadcast).node != "baz" {
t.Fatalf("missing baz")
}
}
func TestLimitedBroadcastSort(t *testing.T) {
bc := limitedBroadcasts([]*limitedBroadcast{
&limitedBroadcast{
transmits: 0,
},
&limitedBroadcast{
transmits: 10,
},
&limitedBroadcast{
transmits: 3,
},
&limitedBroadcast{
transmits: 4,
},
&limitedBroadcast{
transmits: 7,
},
})
bc.Sort()
if bc[0].transmits != 10 {
t.Fatalf("bad val %v", bc[0])
}
if bc[1].transmits != 7 {
t.Fatalf("bad val %v", bc[7])
}
if bc[2].transmits != 4 {
t.Fatalf("bad val %v", bc[2])
}
if bc[3].transmits != 3 {
t.Fatalf("bad val %v", bc[3])
}
if bc[4].transmits != 0 {
t.Fatalf("bad val %v", bc[4])
}
}