-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconn_test.go
477 lines (391 loc) · 11.1 KB
/
conn_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package cleisthenes_test
import (
"bytes"
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
"strings"
"sync"
"testing"
"github.com/DE-labtory/cleisthenes/test/util"
"github.com/DE-labtory/cleisthenes"
"github.com/DE-labtory/cleisthenes/pb"
"github.com/DE-labtory/cleisthenes/test/mock"
)
type MockHandler struct {
done chan struct{}
ServeRequestFunc func(msg cleisthenes.Message)
}
func NewMockHandler(done chan struct{}) *MockHandler {
return &MockHandler{
done: done,
}
}
func (h *MockHandler) ServeRequest(msg cleisthenes.Message) {
h.ServeRequestFunc(msg)
}
func TestGrpcConnection_Send(t *testing.T) {
cAddress := cleisthenes.Address{"127.0.0.1", util.GetAvailablePort(8000)}
mockStreamWrapper := mock.NewStreamWrapper()
conn, err := cleisthenes.NewConnection(cAddress, "127.0.0.1:8081", mockStreamWrapper)
if err != nil {
t.Fatal(err)
}
done := make(chan struct{}, 1)
mockHandler := NewMockHandler(done)
mockHandler.ServeRequestFunc = func(msg cleisthenes.Message) {
if msg.GetRbc().Type != pb.RBC_VAL {
t.Fatalf("expected message type is %s, but got %s", pb.RBCType_name[int32(pb.RBC_VAL)], msg.GetRbc().Type)
}
if !bytes.Equal(msg.GetRbc().Payload, []byte("kim")) {
t.Fatalf("expected message payload is %s, but got %s", "kim", string(msg.GetRbc().Payload))
}
done <- struct{}{}
}
conn.Handle(mockHandler)
go func() {
if err := conn.Start(); err != nil {
conn.Close()
}
}()
msg := pb.Message{
Payload: &pb.Message_Rbc{
Rbc: &pb.RBC{
Payload: []byte("kim"),
Type: pb.RBC_VAL,
},
},
}
conn.Send(msg, nil, nil)
// wait until receive the msg
<-done
}
func TestGrpcConnection_GetIP(t *testing.T) {
cAddress := cleisthenes.Address{"127.0.0.1", util.GetAvailablePort(8000)}
mockStreamWrapper := mock.NewStreamWrapper()
conn, err := cleisthenes.NewConnection(cAddress, "127.0.0.1:8081", mockStreamWrapper)
if err != nil {
t.Fatal(err)
}
address := conn.Ip()
if strings.Compare(cAddress.Ip, address.Ip) != 0 || cAddress.Port != address.Port {
t.Fatal("not equal address")
}
}
func TestGrpcConnection_GetID(t *testing.T) {
id := "someNetworkID"
mockStreamWrapper := mock.NewStreamWrapper()
conn, err := cleisthenes.NewConnection(cleisthenes.Address{}, id, mockStreamWrapper)
if err != nil {
t.Fatal(err)
}
connId := conn.Id()
if strings.Compare(id, connId) != 0 {
t.Fatal("not equal id")
}
}
func TestGrpcConnection_Close(t *testing.T) {
cAddress := cleisthenes.Address{"127.0.0.1", util.GetAvailablePort(8000)}
wg := sync.WaitGroup{}
wg.Add(1)
mockStreamWrapper := mock.NewStreamWrapper()
conn, err := cleisthenes.NewConnection(cAddress, "127.0.0.1:8081", mockStreamWrapper)
if err != nil {
t.Fatal(err)
}
go func() {
<-mockStreamWrapper.CloseChan
wg.Done()
}()
go func() {
if err := conn.Start(); err != nil {
t.Fatal(err)
}
}()
conn.Close()
wg.Wait()
}
type samplePayload struct {
Data string
Id int
}
type messageRecvChecker struct {
lock sync.RWMutex
checkList []bool
}
func newMessageRecvChecker(checkSize int) *messageRecvChecker {
return &messageRecvChecker{
lock: sync.RWMutex{},
checkList: make([]bool, checkSize),
}
}
func (m *messageRecvChecker) check(i int) {
m.lock.Lock()
defer m.lock.Unlock()
m.checkList[i] = true
}
func (m *messageRecvChecker) result() []bool {
return m.checkList
}
type messageRecvCounter struct {
lock sync.RWMutex
counter int
}
func newMessageRecvCounter() *messageRecvCounter {
return &messageRecvCounter{
lock: sync.RWMutex{},
}
}
func (m *messageRecvCounter) count() {
m.lock.Lock()
defer m.lock.Unlock()
m.counter++
}
func (m *messageRecvCounter) result() int {
return m.counter
}
type connectionTester struct {
connListSize int
msgRecvChecker *messageRecvChecker
msgRecvCounter *messageRecvCounter
}
func newConnectionTester(connListSize int) *connectionTester {
return &connectionTester{
connListSize: connListSize,
msgRecvChecker: newMessageRecvChecker(connListSize),
msgRecvCounter: newMessageRecvCounter(),
}
}
func (c *connectionTester) setupConnectionPool() (*cleisthenes.ConnectionPool, error) {
connPool := cleisthenes.NewConnectionPool()
for i := 0; i < c.connListSize; i++ {
addr := cleisthenes.Address{"127.0.0.1", uint16(8000 + i)}
conn, err := cleisthenes.NewConnection(addr, "TestConnection"+strconv.Itoa(i+1), mock.NewStreamWrapper())
if err != nil {
return nil, err
}
connPool.Add(addr, conn)
}
return connPool, nil
}
func (c *connectionTester) setupHandler(servRequestFunc func(message cleisthenes.Message)) *MockHandler {
mockHandler := NewMockHandler(nil)
mockHandler.ServeRequestFunc = servRequestFunc
return mockHandler
}
func (c *connectionTester) run(connPool *cleisthenes.ConnectionPool, handler *MockHandler) {
for _, conn := range connPool.GetAll() {
conn.Handle(handler)
}
for _, conn := range connPool.GetAll() {
go func(conn cleisthenes.Connection) {
if err := conn.Start(); err != nil {
conn.Close()
}
}(conn)
}
}
func (c *connectionTester) checkResult() []bool {
return c.msgRecvChecker.result()
}
func (c *connectionTester) countResult() int {
return c.msgRecvCounter.result()
}
func executeDistributeMessage(t *testing.T, textList []string, connListSize int) []bool {
wg := &sync.WaitGroup{}
wg.Add(int(math.Min(float64(len(textList)), float64(connListSize))))
tester := newConnectionTester(connListSize)
connPool, err := tester.setupConnectionPool()
if err != nil {
t.Fatalf("failed to setup connection pool: err=%s", err)
}
handler := tester.setupHandler(func(msg cleisthenes.Message) {
if msg.GetRbc().Type != pb.RBC_VAL {
t.Fatalf(fmt.Sprintf("expected message type is %s, but got %s", string(pb.RBCType_name[int32(pb.RBC_VAL)]), string(msg.GetRbc().Type)))
}
payload := &samplePayload{}
if err := json.Unmarshal(msg.GetRbc().Payload, payload); err != nil {
t.Fatalf("failed to unmarshal payload: err=%s", err)
}
if payload.Data != textList[payload.Id] {
t.Fatalf(fmt.Sprintf("expected message is %s, but got %s", textList[payload.Id], payload.Data))
}
tester.msgRecvChecker.check(payload.Id)
wg.Done()
})
tester.run(connPool, handler)
msgList := make([]pb.Message, len(textList))
for i, _ := range msgList {
payload := &samplePayload{
Data: textList[i],
Id: i,
}
data, err := json.Marshal(payload)
if err != nil {
t.Fatalf("failed to marshal sample payload: err=%s", err)
}
msgList[i] = pb.Message{
Payload: &pb.Message_Rbc{
Rbc: &pb.RBC{
Payload: data,
Type: pb.RBC_VAL,
},
},
}
}
connPool.DistributeMessage(msgList)
wg.Wait()
return tester.checkResult()
}
func executeShareMessage(t *testing.T, text string, connListSize int) int {
wg := &sync.WaitGroup{}
wg.Add(connListSize)
tester := newConnectionTester(connListSize)
connPool, err := tester.setupConnectionPool()
if err != nil {
t.Fatalf("failed to setup connection pool: err=%s", err)
}
handler := tester.setupHandler(func(msg cleisthenes.Message) {
if !bytes.Equal(msg.GetRbc().Payload, []byte(text)) {
t.Fatalf("expected message payload is %s, but got %s", msg.GetRbc().Payload, []byte(text))
}
tester.msgRecvCounter.count()
wg.Done()
})
tester.run(connPool, handler)
connPool.ShareMessage(pb.Message{
Payload: &pb.Message_Rbc{
Rbc: &pb.RBC{
Payload: []byte(text),
Type: pb.RBC_VAL,
},
},
})
wg.Wait()
return tester.countResult()
}
// Text case for ShareMessage with NodeNum = 3
func TestConnectionPool_ShareMessage(t *testing.T) {
result := executeShareMessage(t, "jang", 3)
if result != 3 {
t.Fatalf("expected result is %d, but got %d", 3, result)
}
}
// Test case for NodeNum == MessageNum (Both of them are 3) at DistributeMessage
func TestConnectionPool_DistributeMessage_EqualNum(t *testing.T) {
textList := []string{"jang", "gun", "hee"}
result := executeDistributeMessage(t, textList, 3)
trueCount := 0
for _, check := range result {
if check {
trueCount++
}
}
if trueCount != 3 {
t.Fatalf("expected trueCount is %d, but got %d", 3, trueCount)
}
}
// Test case for NodeNum < MessageNum (NodeNum is 3, MessageNum is 4) at DistributeMessage
func TestConnectionPool_DistributeMessage_MessageNumBigger(t *testing.T) {
textList := []string{"jang", "gun", "hee", "kim"}
result := executeDistributeMessage(t, textList, 3)
trueCount := 0
for _, check := range result {
if check {
trueCount++
}
}
if trueCount != 3 {
t.Fatalf("expected trueCount is %d, but got %d", 3, trueCount)
}
}
// Test case for NodeNum > MessageNum (NodeNum is 3, MessageNum is 2) at DistributeMessage
func TestConnectionPool_DistributeMessage_NodeNumBigger(t *testing.T) {
textList := []string{"jang", "gun"}
result := executeDistributeMessage(t, textList, 3)
trueCount := 0
for _, check := range result {
if check {
trueCount++
}
}
if trueCount != len(textList) {
t.Fatalf("expected trueCount is %d, but got %d", 3, trueCount)
}
}
func TestConnectionPool_Add(t *testing.T) {
mockStreamWrapper := mock.NewStreamWrapper()
cp := cleisthenes.NewConnectionPool()
connNum := 5
connList := make([]cleisthenes.Connection, connNum)
var err error
for i, _ := range connList {
connId := "TestConnection" + strconv.Itoa(i+1)
addr := cleisthenes.Address{Ip: "127.0.0.1", Port: uint16(8000 + i)}
connList[i], err = cleisthenes.NewConnection(addr, connId, mockStreamWrapper)
if err != nil {
t.Fatal(err)
}
}
for _, conn := range connList {
cp.Add(conn.Ip(), conn)
}
if connNum != len(cp.GetAll()) {
t.Fatalf("expected connection length is %d, but got %d", connNum, len(cp.GetAll()))
}
for i, getConn := range cp.GetAll() {
checkInclude := false
for _, inputConn := range connList {
if reflect.DeepEqual(inputConn, getConn) {
checkInclude = true
break
}
}
if checkInclude == false {
t.Fatalf("test[%d] failed : Connection from connection pool is not a value of input", i)
}
}
}
func TestConnectionPool_Remove(t *testing.T) {
mockStreamWrapper := mock.NewStreamWrapper()
cp := cleisthenes.NewConnectionPool()
connNum := 5
connList := make([]cleisthenes.Connection, connNum)
var err error
for i, _ := range connList {
connId := "TestConnection" + strconv.Itoa(i+1)
addr := cleisthenes.Address{Ip: "127.0.0.1", Port: uint16(8000 + i)}
connList[i], err = cleisthenes.NewConnection(addr, connId, mockStreamWrapper)
if err != nil {
t.Fatal(err)
}
}
for _, conn := range connList {
cp.Add(conn.Ip(), conn)
}
deleteNum := 2
deleteConnList := make([]cleisthenes.Address, deleteNum)
for i, _ := range deleteConnList {
deleteConnList[i] = connList[i].Ip()
}
for _, deleteConnId := range deleteConnList {
cp.Remove(deleteConnId)
}
if connNum-deleteNum != len(cp.GetAll()) {
t.Fatalf("expected connection length is %d, but got %d", connNum-deleteNum, len(cp.GetAll()))
}
for _, getConn := range cp.GetAll() {
checkInclude := true
for _, deleteConnId := range deleteConnList {
if reflect.DeepEqual(deleteConnId, getConn) {
checkInclude = false
break
}
}
if checkInclude == false {
t.Fatalf("connection from connection pool is removed connection: error occurred when removing connection pool")
}
}
}