-
Notifications
You must be signed in to change notification settings - Fork 34
/
room_test.go
109 lines (88 loc) · 1.96 KB
/
room_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
package main
import (
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRoomManager(t *testing.T) {
rooms := NewRoomManager(pgClient)
roomIDs := []string{}
wg := &sync.WaitGroup{}
for i := 0; i < 10; i++ {
roomID := fmt.Sprintf("room-%d", i)
roomIDs = append(roomIDs, roomID)
wg.Add(1)
go func(roomID string) {
rooms.GetRoom(roomID)
wg.Done()
}(roomID)
}
wg.Wait()
for _, roomID := range roomIDs {
assert.NotNil(t, rooms.rooms[roomID], fmt.Sprintf("RoomID: %v\nRooms: %v\n", roomID, rooms))
}
}
func TestRoomMessages(t *testing.T) {
r := NewRoom("testing-room-testing-room-testing-room-testing", pgClient)
wg := &sync.WaitGroup{}
msgs := [][]Message{}
expectedMsgs := []Message{}
for i := 0; i < 10; i++ {
msgs = append(msgs, []Message{})
for n := 0; n < 10; n++ {
msgs[i] = append(msgs[i], Message{'a'})
expectedMsgs = append(expectedMsgs, Message{'a'})
}
}
wg.Add(len(msgs) * 2)
ttlSecs := 60
for i := 0; i < 10; i++ {
go func(i int) {
err := r.AddMessages(msgs[i], &ttlSecs)
if err != nil {
t.Logf("Error from AddMessages: %s", err)
// FALLTHROUGH
}
wg.Done()
}(i)
go func() {
_, err := r.GetMessages()
if err != nil {
t.Logf("Error from GetMessages: %s", err)
// FALLTHROUGH
}
wg.Done()
}()
}
wg.Wait()
gotMsgs, _ := r.GetMessages()
assert.Equal(t, expectedMsgs, gotMsgs)
}
func TestRoomClients(t *testing.T) {
r := NewRoom("testing-room-testing-room-testing-room-testing", pgClient)
clients := []*Client{}
wg := &sync.WaitGroup{}
for i := 0; i < 10; i++ {
client := &Client{}
clients = append(clients, client)
wg.Add(1)
go func(c *Client) {
r.AddClient(c)
wg.Done()
}(client)
}
wg.Wait()
assert.Equal(t, clients, r.Clients)
for _, client := range clients {
wg.Add(1)
go func(c *Client) {
r.RemoveClient(c)
wg.Done()
}(client)
}
wg.Wait()
assert.Empty(t, r.Clients)
}
// TODO
func TestRoomBroadcastMessages(t *testing.T) {}