/
general_test.go
209 lines (154 loc) · 3.99 KB
/
general_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
package main_test
import(
"log"
"time"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/sech90/go-message-hub/hub"
"github.com/sech90/go-message-hub/client"
"github.com/sech90/go-message-hub/syncmap"
"github.com/sech90/go-message-hub/message"
"github.com/sech90/go-message-hub/testutils"
)
const(
Port = 9999
Addr = "localhost"
TimeoutTime = 5 * time.Second
ClientsNum = 25
PayloadBytes = 1024 * 10
MessagesPerCLient = 10
)
var climap = syncmap.NewSyncMap()
var server = hub.NewHub(Port)
var testBody = testutils.GenPayload(PayloadBytes)
var allCliId []uint64
func TestInit(t *testing.T){
go server.Run()
}
func TestIdentity(t *testing.T){
assert := assert.New(t)
var wg sync.WaitGroup
wg.Add(ClientsNum)
for i:=0; i< ClientsNum; i++ {
go func(){
c := client.NewClient()
err := c.Connect(Addr,Port)
assert.Nil(err, "Connection should be valid")
ans := <- c.IncomingId()
assert.NotNil(ans, "Should receive an answer from ID channel")
climap.Set(c.Id(), c)
wg.Done()
}()
}
wg.Wait()
allCliId = climap.GetKeys()
}
func TestList(t *testing.T){
assert := assert.New(t)
var wg sync.WaitGroup
mex := message.NewRequest(message.List)
wg.Add(ClientsNum)
for i:=0; i< ClientsNum; i++ {
val,_ := climap.Get(allCliId[i])
go func(cli *client.Client){
cli.Send(mex)
ans := <- cli.IncomingList()
assert.NotNil(ans, "Should receive an answer from List channel")
assert.False(testutils.IsInList(cli.Id(),cli.List()))
diff := testutils.DiffLists(allCliId, cli.List())
assert.Len(diff, 1, "diff list should contain only the client ID")
assert.Equal(cli.Id(), diff[0], "diff list should contain only the client ID")
wg.Done()
}(val.(*client.Client))
}
wg.Wait()
}
func TestRelayTwo(t *testing.T){
if len(allCliId) < 2 {
return
}
val,_ := climap.Get(allCliId[0])
c1 := val.(*client.Client)
val,_ = climap.Get(allCliId[1])
c2 := val.(*client.Client)
var wg sync.WaitGroup
wg.Add(MessagesPerCLient)
var counter int
req := message.NewRelayRequest([]uint64{c2.Id()}, testBody)
buf := make([]byte,8)
for i:=0; i<MessagesPerCLient; i++ {
message.Uint64ToByteArray(buf, uint64(i))
req.Body = buf
c1.Send(req)
}
for i:=0; i<MessagesPerCLient; i++ {
counter++
wg.Done()
}
wg.Wait()
assert.Equal(t,MessagesPerCLient, counter,"Client2 should have received all messages")
}
func TestRelay(t *testing.T){
assert := assert.New(t)
var wg sync.WaitGroup
var expectedMex int = MessagesPerCLient * (ClientsNum-1)
wg.Add(ClientsNum)
for i:=0; i< ClientsNum; i++ {
cli,_ := climap.Get(allCliId[i])
go func(cli *client.Client){
received := ForwardAndListen(cli)
assert.Equal(expectedMex, received, "Client didnt receive the correct number of messages")
wg.Done()
}(cli.(*client.Client))
}
wg.Wait()
}
func TestDisconnect(t *testing.T){
for _, id := range allCliId {
val,_ := climap.Get(id)
cli := val.(*client.Client)
cli.Disconnect()
}
}
func TestPostDisconnect(t *testing.T){
assert := assert.New(t)
cli := client.NewClient()
err := cli.Connect(Addr, Port)
assert.Nil(err, "new client should connect")
//wait for id
<- cli.IncomingId()
cli.Send(message.NewRequest(message.List))
//wait for list
<- cli.IncomingList()
if len(cli.List()) > 0 {
time.Sleep(time.Millisecond * 200)
cli.Send(message.NewRequest(message.List))
//wait for list
<- cli.IncomingList()
}
assert.Len(cli.List(), 0, "Server should return empty list")
cli.Disconnect()
}
func TestEnd(t *testing.T){
server.Stop()
}
func ForwardAndListen(cli *client.Client) int {
var received int
req := message.NewRelayRequest(cli.List(), testBody)
incoming := cli.IncomingRelay()
var expectedIncoming int = MessagesPerCLient * (ClientsNum-1)
for i:=0; i<MessagesPerCLient; i++ {
go cli.Send(req)
}
for i:=0; i<expectedIncoming; i++ {
select{
case <- incoming:
received++
case <- time.After( TimeoutTime ):
log.Println(cli.Id(),"timed out")
return received
}
}
return received
}