-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
159 lines (138 loc) · 4.3 KB
/
server_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
package gocomet
import (
"log"
"runtime"
"testing"
"time"
)
func TestHandshake(t *testing.T) {
log.Println("Testing handshake...")
s := newServer()
c1, err := s.handshake()
assert(err == nil, t, "simple handshake should not fail")
c2, err := s.handshake()
assert(err == nil, t, "simple handshake should not fail")
assert(c1 != c2, t, "client ID should not conflict")
}
func TestConnect(t *testing.T) {
log.Println("Testing connect...")
s := newServer()
c1, _ := s.handshake()
_, _, ok := s.connect(c1)
assert(ok, t, "failed to connect simple client")
_, _, ok = s.connect("invalid")
assert(!ok, t, "invalid client should not connect")
}
func TestDisconnect(t *testing.T) {
log.Println("Testing disconnect...")
s := newServer()
_, ok := s.disconnect("invalid")
assert(!ok, t, "cannot disconnect an non-exist client")
c1, _ := s.handshake()
_, ok = s.disconnect(c1)
assert(ok, t, "failed to disconnect the client")
c2, _ := s.handshake()
ch, _, _ := s.connect(c2)
_, ok = s.disconnect(c2)
assert(ok, t, "failed to disconnect a connected client")
runtime.Gosched()
_, ok = <-ch
assert(!ok, t, "channel should be closed after disconnect")
}
func TestSubscribe(t *testing.T) {
log.Println("Testing subscribe...")
s := newServer()
_, ok := s.subscribe("invalid", "/foo/bar")
assert(!ok, t, "cannot subscribe w/o client ID")
c1, _ := s.handshake()
_, ok = s.subscribe(c1, "/foo/bar")
assert(ok, t, "failed to subscribe w/o connect")
s.connect(c1)
_, ok = s.subscribe(c1, "/foo/bar")
assert(ok, t, "failed to subscribe")
}
func TestUnsubscribe(t *testing.T) {
log.Println("Testing unsubscribe...")
s := newServer()
_, ok := s.unsubscribe("invalid", "/foo/bar")
assert(!ok, t, "cannot unsubscribe w/o client ID")
c1, _ := s.handshake()
_, ok = s.unsubscribe(c1, "/foo/bar")
assert(!ok, t, "cannot unsubscribe w/o connect first")
s.connect(c1)
_, ok = s.unsubscribe(c1, "/foo/bar")
assert(!ok, t, "cannot unsubscribe w/o subscribe first")
s.subscribe(c1, "/foo/bar")
_, ok = s.unsubscribe(c1, "/foo/bar")
assert(ok, t, "failed to unsubscribe")
}
func TestPublish(t *testing.T) {
log.Println("Testing publish...")
s := newServer()
_, ok := s.publish("invalid", "/foo/bar", "ping")
assert(!ok, t, "cannot publish with invalid client ID")
c1, _ := s.handshake()
_, ok = s.publish(c1, "/foo/bar", "ping")
assert(ok, t, "failed to publish w/o connect")
c2, _ := s.handshake()
ch, _, ok := s.connect(c2)
var msg string
go func() { msg = (<-ch).data }()
s.subscribe(c2, "/foo/bar")
s.publish(c1, "/foo/bar", "ping")
time.Sleep(10 * time.Millisecond)
assert(msg == "ping", t, "failed to receive the delivered message (got %v)", msg)
}
func TestWhisper(t *testing.T) {
log.Println("Testing whisper...")
s := newServer()
s.whisper("/foo/bar", "ping")
c1, _ := s.handshake()
ch, _, _ := s.connect(c1)
var msg string
go func() { msg = (<-ch).data }()
s.subscribe(c1, "/foo/bar")
s.whisper("/foo/bar", "ping")
time.Sleep(10 * time.Millisecond) // give msg receiver a chance
assert(msg == "ping", t, "failed to receive whipered message (got %v)", msg)
}
func TestTwoConnectionRestrict(t *testing.T) {
log.Println("Testing 2 connections restrict...")
s := newServer()
c1, _ := s.handshake()
ch1, timeout, _ := s.connect(c1)
var msg string
go func() { msg = (<-ch1).data }()
ch2, _ := s.subscribe(c1, "/foo/bar")
_, ok := <-ch2
assert(!ok, t, "only one active channel is allowed")
c2, _ := s.handshake()
s.connect(c2)
s.publish(c2, "/foo/bar", "ping")
time.Sleep(10 * time.Millisecond)
assert(msg == "ping", t, "failed to receive message from previous active connect")
timeout <- true
_, ok = <-ch1
assert(!ok, t, "active channel should be closed after disconnect")
s.subscribe(c1, "/foo/bar/2")
ch4, _, _ := s.connect(c1)
msg = ""
go func() { msg = (<-ch4).data }()
s.publish(c2, "/foo/bar/2", "ping")
time.Sleep(10 * time.Millisecond)
assert(msg == "ping", t, "failed to receive message from new active connect (got %v)", msg)
}
func TestAvoidReuseClientId(t *testing.T) {
log.Println("Testing client ID reuse...")
s := newServer()
names := make(map[string]bool)
var id string
var err error
for i := 1; i <= 10000; i++ {
id, err = s.handshake()
if _, ok := names[id]; ok || err != nil {
t.Fatalf("Client IDs should not conflict... for at least 1 million trials.")
}
names[id] = true
}
}