-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_test.go
More file actions
48 lines (43 loc) · 1.15 KB
/
message_test.go
File metadata and controls
48 lines (43 loc) · 1.15 KB
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
package gocomet
import (
"runtime"
"testing"
)
func TestClientLifeCycle(t *testing.T) {
b := newBroker()
ch := b.register("client")
b.deregister("client")
_, ok := <-ch
assert(!ok, t, "channel should be closed after deregister")
}
func assert(ok bool, t *testing.T, format string, args ...interface{}) {
if !ok {
t.Errorf(format, args...)
}
}
func TestMessageBroadcast(t *testing.T) {
b := newBroker()
ch := b.register("client")
var msg *Message
go func() { msg = <-ch }()
b.broadcast("/foo/bar", "hello")
assert(len(ch) == 0, t, "nothing should happens")
b.subscribe("client", "/foo/bar")
b.broadcast("/foo/bar", "hello again")
runtime.Gosched()
assert(msg.data == "hello again", t, "failed to receive message")
}
func TestChannelUnsubscribe(t *testing.T) {
b := newBroker()
clientId := "client"
ch := b.register(clientId)
var msg *Message
go func() { msg = <-ch }()
b.subscribe(clientId, "/foo/bar")
b.broadcast("/foo/bar", "hello")
runtime.Gosched()
assert(msg.data == "hello", t, "failed to receive message")
b.unsubscribe(clientId, "/foo/bar")
b.broadcast("/foo/bar", "hello again")
assert(len(ch) == 0, t, "nothing should happens")
}