/
gotrigga_test.go
174 lines (166 loc) · 3.24 KB
/
gotrigga_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// This package tests both the server and the client.
// Assumes a runnin server on port 127.0.0.1:8912
package gotrigga_test
import(
gt "github.com/opesun/gotrigga"
"testing"
"time"
"encoding/json"
)
type Waiter struct {
interval time.Duration
limit time.Duration
}
func (w *Waiter) Interval(i time.Duration) *Waiter {
w.interval = i
return w
}
func (w *Waiter) Limit(i time.Duration) *Waiter {
w.limit = i
return w
}
// Returns true if f returned true under given time.
func (w *Waiter) Wait(f func() bool) bool {
t := time.Now()
for {
if f() == true {
return true
}
if time.Since(t) > w.limit {
return false
}
time.Sleep(w.interval)
}
panic("Should never happen.")
}
func TestSameconnSendRec(t *testing.T) {
roomACounter := 0
da := "hello"
read := func(rec *gt.Room, c *int) {
for {
dat, err := rec.Read()
if err != nil {
t.Fatal(err)
return
}
if string(dat) == da {
*c++
} else {
t.Fatal("Wrong data:", string(dat))
}
}
}
conn, err := gt.Connect("127.0.0.1:8912")
if err != nil {
t.Fatal(err)
}
roomA := conn.Room("roomA")
roomA.Subscribe()
go read(roomA, &roomACounter)
conn.Room("roomA").Publish(da)
v := &Waiter{}
ok := v.Interval(50*time.Millisecond).Limit(2*time.Second).Wait(func() bool {
return roomACounter == 1
})
if !ok {
t.Fatal(roomACounter)
}
}
func TestMultiroom(t *testing.T) {
roomACounter := 0
roomBCounter := 0
da := "hello"
read := func(rec *gt.Room, c *int) {
for {
dat, err := rec.Read()
if err != nil {
t.Fatal(err)
return
}
if string(dat) == da {
*c++
} else {
t.Fatal("Wrong data:", string(dat))
}
}
}
conn, err := gt.Connect("127.0.0.1:8912")
if err != nil {
t.Fatal(err)
}
conn1, err := gt.Connect("127.0.0.1:8912")
if err != nil {
t.Fatal(err)
}
roomA := conn.Room("roomA")
err = roomA.Subscribe()
if err != nil {
panic(err)
}
roomB := conn.Room("roomB")
err = roomB.Subscribe()
if err != nil {
panic(err)
}
go read(roomA, &roomACounter)
go read(roomB, &roomBCounter)
sendn := 1000
for i:=0;i<sendn;i++{
conn1.Room("roomA").Publish(da)
}
for i:=0;i<sendn;i++{
conn1.Room("roomB").Publish(da)
}
v := &Waiter{}
ok := v.Interval(50*time.Millisecond).Limit(2*time.Second).Wait(func() bool {
return roomACounter == sendn && roomBCounter == sendn
})
if !ok {
t.Fatal(roomACounter, roomBCounter)
}
}
func TestJson(t *testing.T) {
roomACounter := 0
m := map[string]interface{}{
"key": "value",
"key1": "value1",
}
da, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}
read := func(rec *gt.Room, c *int) {
for {
dat, err := rec.Read()
if err != nil {
t.Fatal(err)
return
}
var v interface{}
err = json.Unmarshal(dat, &v)
if err != nil {
t.Fatal(err)
}
ma := v.(map[string]interface{})
if len(ma) != 2 || ma["key"] != "value" || ma["key1"] != "value1" {
t.Fatal(ma)
}
*c++
}
}
conn, err := gt.Connect("127.0.0.1:8912")
if err != nil {
t.Fatal(err)
}
roomA := conn.Room("roomA")
roomA.Subscribe()
go read(roomA, &roomACounter)
conn.Room("roomA").Publish(string(da))
v := &Waiter{}
ok := v.Interval(50*time.Millisecond).Limit(2*time.Second).Wait(func() bool {
return roomACounter == 1
})
if !ok {
t.Fatal(roomACounter)
}
}