forked from myzhan/boomer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_gomq_test.go
135 lines (117 loc) · 2.81 KB
/
client_gomq_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
// +build !goczmq
package boomer
import (
"fmt"
"log"
"runtime/debug"
"testing"
"github.com/zeromq/gomq"
"github.com/zeromq/gomq/zmtp"
)
type testServer struct {
bindHost string
pushPort int
pullPort int
fromClient chan *message
toClient chan *message
pushSocket *gomq.PushSocket
pullSocket *gomq.PullSocket
shutdownSignal chan bool
}
func newTestServer(bindHost string, pushPort, pullPort int) (server *testServer) {
return &testServer{
bindHost: bindHost,
pushPort: pushPort,
pullPort: pullPort,
fromClient: make(chan *message, 100),
toClient: make(chan *message, 100),
shutdownSignal: make(chan bool, 1),
}
}
func (s *testServer) send() {
for {
select {
case <-s.shutdownSignal:
s.pushSocket.Close()
return
case msg := <-s.toClient:
s.sendMessage(msg)
}
}
}
func (s *testServer) sendMessage(msg *message) {
defer func() {
// don't panic
err := recover()
if err != nil {
log.Printf("%v\n", err)
debug.PrintStack()
}
}()
serializedMessage, err := msg.serialize()
if err != nil {
log.Println("Msgpack encode fail:", err)
return
}
err = s.pushSocket.Send(serializedMessage)
if err != nil {
log.Printf("Error sending to client: %v\n", err)
}
}
func (s *testServer) recv() {
for {
select {
case <-s.shutdownSignal:
s.pullSocket.Close()
return
default:
msg, err := s.pullSocket.Recv()
if err != nil {
log.Printf("Error reading: %v\n", err)
} else {
msgFromClient, err := newMessageFromBytes(msg)
if err != nil {
log.Println("Msgpack decode fail:", err)
} else {
s.fromClient <- msgFromClient
}
}
}
}
}
func (s *testServer) close() {
close(s.shutdownSignal)
}
func (s *testServer) start() {
pushAddr := fmt.Sprintf("tcp://%s:%d", s.bindHost, s.pushPort)
pullAddr := fmt.Sprintf("tcp://%s:%d", s.bindHost, s.pullPort)
pushSocket := gomq.NewPush(zmtp.NewSecurityNull())
pullSocket := gomq.NewPull(zmtp.NewSecurityNull())
go pushSocket.Bind(pushAddr)
go pullSocket.Bind(pullAddr)
s.pushSocket = pushSocket
s.pullSocket = pullSocket
go s.recv()
go s.send()
}
func TestPingPong(t *testing.T) {
masterHost := "127.0.0.1"
masterPort := 5557
server := newTestServer(masterHost, masterPort+1, masterPort)
defer server.close()
server.start()
// start client
client := newClient(masterHost, masterPort)
client.connect()
defer client.close()
client.sendChannel() <- newMessage("ping", nil, "testing ping pong")
msg := <-server.fromClient
if msg.Type != "ping" || msg.NodeID != "testing ping pong" {
t.Error("server doesn't recv ping message")
}
server.toClient <- newMessage("pong", nil, "testing ping pong")
msg = <-client.recvChannel()
if msg.Type != "pong" || msg.NodeID != "testing ping pong" {
t.Error("client doesn't recv pong message")
}
}