-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrtm_loop_impl_test.go
92 lines (78 loc) · 1.64 KB
/
rtm_loop_impl_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
package bearychat
import (
"sync"
"testing"
)
const (
testRTMWSHost = "foobar"
)
func TestNewRTMLoop(t *testing.T) {
l, err := NewRTMLoop(testRTMWSHost)
if err != nil {
t.Errorf("unexpected error: %+v", err)
}
if l.wsHost != testRTMWSHost {
t.Errorf("unexpected wsHost: %s", l.wsHost)
}
if l.callId != 0 {
t.Errorf("unexpected call id: %d", l.callId)
}
}
func TestRTMLoop_ReadC_Closed(t *testing.T) {
l, err := NewRTMLoop(testRTMWSHost)
if err != nil {
t.Errorf("unexpected error: %+v", err)
}
if _, err := l.ReadC(); err != ErrRTMLoopClosed {
t.Errorf("unexpected error: %+v", err)
}
}
func TestRTMLoop_ErrC_Closed(t *testing.T) {
l, err := NewRTMLoop(testRTMWSHost)
if err != nil {
t.Errorf("unexpected error: %+v", err)
}
if c := l.ErrC(); c == nil {
t.Errorf("expected error channel")
}
}
func TestRTMLoop_advanceCallId(t *testing.T) {
l, err := NewRTMLoop(testRTMWSHost)
if err != nil {
t.Errorf("unexpected error: %+v", err)
}
for i := uint64(1); i < 11; i = i + 1 {
newCallId := l.advanceCallId()
if l.callId != i || newCallId != l.callId {
t.Errorf(
"unexpected callId: %d, %d, %d",
i,
newCallId,
l.callId,
)
}
}
}
func TestRTMLoop_advanceCallId_Race(t *testing.T) {
l, err := NewRTMLoop(testRTMWSHost)
if err != nil {
t.Errorf("unexpected error: %+v", err)
}
per := 15
times := 10
var wg sync.WaitGroup
advance := func() {
for i := 0; i < per; i = i + 1 {
l.advanceCallId()
}
wg.Done()
}
for i := 0; i < times; i = i + 1 {
wg.Add(1)
go advance()
}
wg.Wait()
if l.callId != uint64(per*times) {
t.Errorf("unexepcted call id after data race: %d", l.callId)
}
}