-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathfunctest.go
222 lines (177 loc) · 4.78 KB
/
functest.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"time"
"github.com/YuriyNasretdinov/social-net/protocol"
"golang.org/x/net/websocket"
)
const TEST_PASSWORD = "test"
const TEST_MSG_TEXT = "Hello from test"
const TEST_USER_ID = 1
func setFlag(fl chan bool) {
select {
case fl <- true:
default:
}
}
func haveFlag(fl chan bool) bool {
select {
case <-fl:
return true
default:
}
return false
}
func setupAndGetConnection(addr string) (*websocket.Conn, error) {
time.Sleep(time.Millisecond * 100)
conf, err := websocket.NewConfig("ws://"+addr+"/events", "/")
if err != nil {
return nil, err
}
name := fmt.Sprintf("test%d", time.Now().Unix())
email := name + "@example.org"
err, dup := registerUser(email, TEST_PASSWORD, name)
if err != nil {
return nil, err
}
if dup {
return nil, errors.New("User already exists")
}
sessionId, err := loginUser(email, TEST_PASSWORD)
if err != nil {
return nil, err
}
conf.Header.Add("Cookie", "id="+sessionId)
return websocket.DialConfig(conf)
}
func responseReaderThread(c *websocket.Conn, online, connected, newmsg chan bool, respChan chan map[string]interface{}) {
rd := json.NewDecoder(c)
for {
var value map[string]interface{}
if err := rd.Decode(&value); err != nil {
time.Sleep(time.Second)
log.Fatalln("Could not decode value: ", err.Error())
}
msgType, ok := value["Type"].(string)
if ok {
switch msgType {
case "EVENT_ONLINE_USERS_LIST":
setFlag(online)
case "EVENT_USER_CONNECTED":
setFlag(connected)
case "EVENT_NEW_MESSAGE":
if value["UserFrom"].(string) != fmt.Sprint(TEST_USER_ID) {
time.Sleep(time.Second)
log.Fatalf("Improper event new message, expected UserFrom=%d: %+v", TEST_USER_ID, value)
}
setFlag(newmsg)
default:
respChan <- value
}
}
}
}
type TestConn struct {
c *websocket.Conn
respChan chan map[string]interface{}
seqId int
}
func (p *TestConn) callMethod(msg, respMsg string, req, resp interface{}) error {
data, err := json.Marshal(req)
if err != nil {
return err
}
fmt.Fprintf(p.c, "%s %d\n%s\n", msg, p.seqId, data)
rawResp := <-p.respChan
if rawResp["Type"].(string) != respMsg {
return fmt.Errorf("Invalid response, expected %s for %s: %+v\n", respMsg, msg, rawResp)
}
if rawResp["SeqId"].(float64) != float64(p.seqId) {
return fmt.Errorf("Invalid SeqId: %+v\n", rawResp)
}
data, err = json.Marshal(rawResp)
if err != nil {
return err
}
json.Unmarshal(data, resp)
log.Printf("Reply to %s: %+v", msg, resp)
p.seqId++
return nil
}
func testGetMessages(p *TestConn) {
var reply protocol.ReplyMessagesList
log.Printf("Testing get messages")
err := p.callMethod("REQUEST_GET_MESSAGES", "REPLY_MESSAGES_LIST", &protocol.RequestGetMessages{Limit: 10, UserTo: TEST_USER_ID}, &reply)
if err != nil {
panic(err)
}
if len(reply.Messages) != 1 {
log.Panicf("Received unexpected number of messages (%d) instead of 1", len(reply.Messages))
}
msg := reply.Messages[0]
if msg.UserFrom != fmt.Sprint(TEST_USER_ID) {
log.Panicf("Unexpected user from: requested user id=%d, got %s", TEST_USER_ID, msg.UserFrom)
}
if msg.Text != TEST_MSG_TEXT {
log.Panicf("Unexpected msg text: expected '%s', got '%s'", TEST_MSG_TEXT, msg.Text)
}
if msg.IsOut != protocol.MSG_TYPE_OUT {
log.Panicf("Unexpected msg type: expected '%v', got '%v'", protocol.MSG_TYPE_OUT, msg.IsOut)
}
}
func testSendMessage(p *TestConn) error {
var reply protocol.ReplyGeneric
log.Printf("Testing send message")
err := p.callMethod("REQUEST_SEND_MESSAGE", "REPLY_GENERIC", &protocol.RequestSendMessage{UserTo: TEST_USER_ID, Text: TEST_MSG_TEXT}, &reply)
if err != nil {
panic(err)
}
return nil
}
func checkFlag(fl chan bool, name string) {
if !haveFlag(fl) {
panic("Did not receive " + name)
}
}
func testConvertUnderscoreToCamelCase() {
exp := "RequestGetMessages"
res := convertUnderscoreToCamelCase("REQUEST_GET_MESSAGES")
if res != exp {
log.Panicf("Unexpected result from convertUnderscoreToCamelCase, expected '%s', got '%s'", exp, res)
}
}
func runTest(addr string) (err error) {
defer func() {
if r := recover(); r != nil {
switch v := r.(type) {
case error:
err = v
case string:
err = errors.New(v)
default:
panic(r)
}
}
}()
c, err := setupAndGetConnection(addr)
if err != nil {
return
}
testConvertUnderscoreToCamelCase()
online := make(chan bool, 1)
connected := make(chan bool, 1)
newmsg := make(chan bool, 1)
respChan := make(chan map[string]interface{}, 1)
go responseReaderThread(c, online, connected, newmsg, respChan)
p := &TestConn{c: c, respChan: respChan, seqId: 0}
testSendMessage(p)
testGetMessages(p)
time.Sleep(time.Millisecond * 100)
checkFlag(online, "online users list")
checkFlag(connected, "user connected")
checkFlag(newmsg, "new message event")
return nil
}