-
Notifications
You must be signed in to change notification settings - Fork 4
/
client_test.go
66 lines (51 loc) · 1.4 KB
/
client_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
package main
import (
"io"
"net"
"testing"
msg "github.com/abdelq/lode-runner/message"
)
func TestRead(t *testing.T) {
serverConn, clientConn := net.Pipe()
client := &client{conn: serverConn, out: make(chan *msg.Message, 1)}
go client.read()
/* TODO Tests */
sendMsg(t, clientConn, message{})
sendMsg(t, clientConn, message{"test", []byte(`"TestRead"`)})
}
func TestWrite(t *testing.T) {
serverConn, clientConn := net.Pipe()
client := &client{conn: serverConn, out: make(chan *msg.Message, 1)}
go client.write()
/* TODO Tests */
client.out <- &msg.Message{}
receiveMsg(t, clientConn, message{"", []byte(`null`)})
client.out <- &msg.Message{"test", []byte(`"TestWrite"`)}
receiveMsg(t, clientConn, message{"test", []byte(`"TestWrite"`)})
}
func TestClose(t *testing.T) {
conn, _ := net.Pipe()
client := newClient(conn)
// Join rooms
for _, name := range []string{"Buzz", "Rex", "Bo"} {
newRoom(name).join <- &join{client, nil}
}
client.close() // First
client.close() // Second
// Verify rooms are left
rooms.Range(func(n, r interface{}) bool {
if _, ok := r.(*room).clients[client]; ok {
t.Errorf("client still in a room")
return false
}
return true
})
// Verify output channel is closed
/*if _, ok := <-client.out; ok {
t.Error("output channel not closed")
}*/
// Verify connection is closed
if _, err := conn.Read(nil); err != io.ErrClosedPipe {
t.Error(err)
}
}