-
Notifications
You must be signed in to change notification settings - Fork 5
/
chat_test.go
101 lines (81 loc) · 2.37 KB
/
chat_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
package main
import (
"bufio"
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"log"
"net"
"os"
"testing"
)
func init() {
fmt.Fprintln(os.Stderr, "init()")
dbType = "sqlite3"
dbConnectionString = "erosd.sqlite3"
initDb()
initChat()
initClientCaches()
}
func createTestRoom(fixed bool) *ChatRoom {
room := createChatRoom("test", "test", "test", true, fixed)
room.logger = log.New(os.Stdout, fmt.Sprintf("chat-%d:", room.id), log.Ldate|log.Ltime|log.Lshortfile)
return room
}
func fakeClient(t *testing.T, id int64, write *bytes.Buffer) *ClientConnection {
// Find a port that would be used as a local address.
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
conn, err := net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatal(err)
}
c := &ClientConnection{
id: id,
authenticated: false,
chatRooms: make(map[string]*ChatRoom),
conn: conn,
connType: CLIENT_CONNECTION_TYPE_SOCKET,
client: &Client{Id: 1},
logger: log.New(os.Stdout, fmt.Sprintf("chat-%d:", id), log.Ldate|log.Ltime|log.Lshortfile),
writer: bufio.NewWriter(write),
}
return c
}
func TestAddingMemberToChatRoom(t *testing.T) {
written := &bytes.Buffer{}
cr := createTestRoom(false)
fc := fakeClient(t, 1, written)
defer fc.conn.(net.Conn).Close()
added := cr.addMemberIfNew(fc)
assert.True(t, added, "a new member should be added and return true")
_, ok := cr.members[fc.id]
assert.True(t, ok, "a member should be there after being added")
addedTwice := cr.addMemberIfNew(fc)
assert.False(t, addedTwice, "a new member should not be added twice and return false")
}
func TestJoiningRoom(t *testing.T) {
written := &bytes.Buffer{}
cr := createTestRoom(false)
fc := fakeClient(t, 1, written)
defer fc.conn.(net.Conn).Close()
cr.ClientJoin(fc)
_, ok := cr.members[fc.id]
assert.True(t, ok, "a member should be there after joining")
}
func TestLeavingRoom(t *testing.T) {
written := &bytes.Buffer{}
cr := createTestRoom(false)
fc := fakeClient(t, 1, written)
defer fc.conn.(net.Conn).Close()
cr.ClientJoin(fc)
_, ok := cr.members[fc.id]
assert.True(t, ok, "a member should be there after joining")
log.Printf("member %v joined ok", fc.id)
cr.ClientLeave(fc)
_, ok = cr.members[fc.id]
assert.False(t, ok, "a member not should be there after leaving")
log.Printf("member %v left ok", fc.id)
}