-
Notifications
You must be signed in to change notification settings - Fork 2
/
messenger_test.go
179 lines (144 loc) · 4.43 KB
/
messenger_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
package messenger
import (
"fmt"
"math/rand"
"testing"
"time"
"code.google.com/p/gogoprotobuf/proto"
"github.com/go-distributed/messenger/codec"
example "github.com/go-distributed/messenger/codec/testexample"
"github.com/go-distributed/messenger/transporter"
"github.com/go-distributed/testify/assert"
)
var count1 int
var count2 int
var count3 int
var count4 int
// Simple message handlers used for testing.
func handler1(msg interface{}) {
count1++
}
func handler2(msg interface{}) {
count2++
}
func handler3(msg interface{}) {
count3++
}
func handler4(msg interface{}) {
count4++
}
// A simple echo server used for testing.
type echoServer struct {
m *Messenger
peerAddr string
}
func (e *echoServer) msgHandler(msg interface{}) {
e.m.Send(e.peerAddr, msg)
}
func generateMessages(n int) []proto.Message {
var m []proto.Message
for i := 0; i < n; i++ {
m1 := &example.GoGoProtobufTestMessage1{
F0: proto.Int32(int32(rand.Int())),
F1: proto.String(fmt.Sprintf("%10d", rand.Int())),
F2: proto.Float32(rand.Float32()),
}
m = append(m, m1)
m2 := &example.GoGoProtobufTestMessage2{
F0: proto.Int32(int32(rand.Int())),
F1: proto.String(fmt.Sprintf("%10d", rand.Int())),
F2: proto.Float32(rand.Float32()),
}
m = append(m, m2)
m3 := &example.GoGoProtobufTestMessage3{
F0: proto.Int32(int32(rand.Int())),
F1: proto.String(fmt.Sprintf("%10d", rand.Int())),
F2: proto.String(fmt.Sprintf("%10d", rand.Int())),
}
m = append(m, m3)
m4 := &example.GoGoProtobufTestMessage4{
F0: proto.Int32(int32(rand.Int())),
F1: proto.String(fmt.Sprintf("%10d", rand.Int())),
}
m = append(m, m4)
}
// Shuffle the messages.
for i := range m {
index := rand.Intn(i + 1)
m[i], m[index] = m[index], m[i]
}
return m
}
// Test Send() and Recv() of the messenger.
func TestSendRecv(t *testing.T) {
// Create the sender.
c := codec.NewGoGoProtobufCodec()
assert.NotNil(t, c)
tr := transporter.NewHTTPTransporter("localhost:8008")
// Should fail to create the messenger.
assert.Nil(t, New(c, tr, false, false))
m := New(c, tr, true, true)
assert.NotNil(t, m)
assert.NoError(t, m.RegisterMessage(&example.GoGoProtobufTestMessage1{}))
assert.NoError(t, m.RegisterMessage(&example.GoGoProtobufTestMessage2{}))
assert.NoError(t, m.RegisterMessage(&example.GoGoProtobufTestMessage3{}))
assert.NoError(t, m.RegisterMessage(&example.GoGoProtobufTestMessage4{}))
assert.NoError(t, m.RegisterHandler(&example.GoGoProtobufTestMessage1{}, handler1))
assert.NoError(t, m.RegisterHandler(&example.GoGoProtobufTestMessage2{}, handler2))
assert.NoError(t, m.RegisterHandler(&example.GoGoProtobufTestMessage3{}, handler3))
assert.NoError(t, m.RegisterHandler(&example.GoGoProtobufTestMessage4{}, handler4))
// Create the echo server.
c = codec.NewGoGoProtobufCodec()
assert.NotNil(t, c)
tr = transporter.NewHTTPTransporter("localhost:8009")
n := New(c, tr, false, true)
assert.NotNil(t, n)
e := &echoServer{
m: n,
peerAddr: "localhost:8008",
}
assert.NoError(t, n.RegisterMessage(&example.GoGoProtobufTestMessage1{}))
assert.NoError(t, n.RegisterMessage(&example.GoGoProtobufTestMessage2{}))
assert.NoError(t, n.RegisterMessage(&example.GoGoProtobufTestMessage3{}))
assert.NoError(t, n.RegisterMessage(&example.GoGoProtobufTestMessage4{}))
assert.NoError(t, n.RegisterHandler(&example.GoGoProtobufTestMessage1{}, e.msgHandler))
assert.NoError(t, n.RegisterHandler(&example.GoGoProtobufTestMessage2{}, e.msgHandler))
assert.NoError(t, n.RegisterHandler(&example.GoGoProtobufTestMessage3{}, e.msgHandler))
assert.NoError(t, n.RegisterHandler(&example.GoGoProtobufTestMessage4{}, e.msgHandler))
assert.NoError(t, m.Start())
assert.NoError(t, n.Start())
cnt := 10
messages := generateMessages(cnt)
go func() {
for i := range messages {
m.Send("localhost:8009", messages[i])
}
}()
var recvMessages []interface{}
wait := make(chan struct{})
go func() {
for {
select {
case <-wait:
return
default:
}
msg, err := m.Recv()
assert.NoError(t, err)
recvMessages = append(recvMessages, msg)
}
}()
<-time.After(time.Second * 5)
for i := range messages {
assert.Equal(t, messages[i], recvMessages[i])
}
// Verify that the handlers are called.
assert.Equal(t, cnt, count1)
assert.Equal(t, cnt, count2)
assert.Equal(t, cnt, count3)
assert.Equal(t, cnt, count4)
assert.NoError(t, m.Stop())
assert.NoError(t, n.Stop())
assert.NoError(t, m.Destroy())
assert.NoError(t, n.Destroy())
}