/
hub_test.go
76 lines (67 loc) · 1.33 KB
/
hub_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
package hub
import (
"testing"
"time"
)
func TestHub(t *testing.T) {
h := New(5)
done := make(map[string]time.Time)
go func() {
hr := <-h.QueryCh()
time.Sleep(100 * time.Millisecond)
done["query"] = time.Now()
hr.Done()
}()
go func() {
hr := <-h.DrawCh()
switch v := hr.Data(); v.(type) {
case string, bool, nil:
// OK
default:
t.Errorf("Expected data to be nil, got %s", v)
}
time.Sleep(100 * time.Millisecond)
done["draw"] = time.Now()
hr.Done()
}()
go func() {
hr := <-h.StatusMsgCh()
r := hr.Data().(*statusMsgReq)
if r.Message() != "Hello, World!" {
t.Errorf("Expected data to be 'Hello World!', got '%s'", r.Message())
}
time.Sleep(100 * time.Millisecond)
done["status"] = time.Now()
hr.Done()
}()
go func() {
hr := <-h.PagingCh()
time.Sleep(100 * time.Millisecond)
done["paging"] = time.Now()
hr.Done()
}()
h.Batch(func() {
h.SendQuery("Hello World!")
h.SendDraw(true)
h.SendStatusMsg("Hello, World!")
h.SendPaging(1)
}, true)
phases := []string{
"query",
"draw",
"status",
"paging",
}
max := len(phases) - 1
for i := range phases {
if max == i {
break
}
cur := phases[i]
next := phases[i+1]
t.Logf("Checkin if %s was fired before %s", cur, next)
if done[next].Before(done[cur]) {
t.Errorf("%s executed before %s?!", next, cur)
}
}
}