forked from yosuke-furukawa/socket.io-go-emitter
/
emitter_test.go
166 lines (156 loc) · 3.45 KB
/
emitter_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
package SocketIO
import (
"time"
"bytes"
"github.com/garyburd/redigo/redis"
"strings"
"testing"
)
func TestHasBinary(t *testing.T) {
hasBin := HasBinary("string")
if hasBin != false {
t.Error("string is not binary")
}
hasBin = HasBinary(123)
if hasBin != false {
t.Error("integer is not binary")
}
hasBin = HasBinary([]byte("abc"))
if hasBin != true {
t.Error("[]byte is binary")
}
var data []interface{}
data = append(data, 1)
data = append(data, "2")
data = append(data, 3)
hasBin = HasBinary(data)
if hasBin != false {
t.Error("string array is not binary")
}
data = append(data, []byte("aaa"))
hasBin = HasBinary(data)
if hasBin != true {
t.Error("data has binary")
}
dataMap := make(map[string]interface{})
dataMap["hoge"] = "bbb"
hasBin = HasBinary(dataMap)
if hasBin != false {
t.Error("data doesnot have binary")
}
dataMap["fuga"] = []byte("bbb")
hasBin = HasBinary(dataMap)
if hasBin != true {
t.Error("data has binary")
}
dataMap["fuga"] = data
hasBin = HasBinary(dataMap)
if hasBin != true {
t.Error("data has binary")
}
}
func TestPublish(t *testing.T) {
emitter, _ := NewEmitter(&EmitterOpts{
Host: "localhost",
Port: 6379,
})
if emitter == nil {
t.Error("emitter is nil")
}
c, _ := redis.Dial("tcp", "localhost:6379")
defer c.Close()
psc := redis.PubSubConn{Conn: c}
psc.Subscribe("socket.io#emitter")
emitter.Emit("text", "hogefuga")
for {
switch v := psc.Receive().(type) {
case redis.Message:
isContain := strings.Contains(string(v.Data), "hogefuga")
if !isContain {
t.Errorf("%s not contains hogefuga", v.Data)
return
} else {
return
}
}
}
}
func TestPublishJson(t *testing.T) {
time.Sleep(1 * time.Second)
emitter, _ := NewEmitter(&EmitterOpts{
Host: "localhost",
Port: 6379,
})
if emitter == nil {
t.Error("emitter is nil")
}
c, _ := redis.Dial("tcp", "localhost:6379")
defer c.Close()
psc := redis.PubSubConn{Conn: c}
psc.Subscribe("socket.io#emitter")
emitter.Emit("jsondata", []byte(`{"name":"a","age":1,"bin":"abc"}`))
for {
switch v := psc.Receive().(type) {
case redis.Message:
isContain := strings.Contains(string(v.Data), "abc")
if !isContain {
t.Errorf("%s not contains abc", v.Data)
return
} else {
return
}
}
}
}
func TestPublishBinary(t *testing.T) {
time.Sleep(1 * time.Second)
emitter, _ := NewEmitter(&EmitterOpts{
Host: "localhost",
Port: 6379,
})
if emitter == nil {
t.Error("emitter is nil")
}
c, _ := redis.Dial("tcp", "localhost:6379")
defer c.Close()
psc := redis.PubSubConn{Conn: c}
psc.Subscribe("socket.io#emitter")
val := bytes.NewBufferString("aaabbbccc")
emitter.EmitBinary("bin", val.Bytes())
for {
switch v := psc.Receive().(type) {
case redis.Message:
isContain := strings.Contains(string(v.Data), "aaabbbccc")
if !isContain {
t.Errorf("%s not contains aaabbbccc", v.Data)
return
} else {
return
}
}
}
}
func TestPublishEnd(t *testing.T) {
time.Sleep(1 * time.Second)
emitter, _ := NewEmitter(&EmitterOpts{
Host: "localhost",
Port: 6379,
})
c, _ := redis.Dial("tcp", "localhost:6379")
defer c.Close()
psc := redis.PubSubConn{Conn: c}
psc.Subscribe("socket.io#emitter")
emitter.Emit("finish")
for {
switch v := psc.Receive().(type) {
case redis.Message:
isContain := strings.Contains(string(v.Data), "finish")
if !isContain {
t.Errorf("%s not contains end", v.Data)
return
} else {
return
}
}
}
}