-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection_manager_test.go
229 lines (182 loc) · 4.95 KB
/
connection_manager_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
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
223
224
225
226
227
228
229
package chatty
import (
"sync"
"testing"
"time"
"github.com/pborman/uuid"
"github.com/ryan-berger/chatty/connection"
"github.com/ryan-berger/chatty/operators"
"github.com/ryan-berger/chatty/repositories"
)
type testData struct {
*repositories.MockConversationRepo
*repositories.MockConversantRepo
*repositories.MockMessageRepo
*operators.MockAuther
*operators.MockNotifier
}
func newTestData() *testData {
return &testData{
&repositories.MockConversationRepo{},
&repositories.MockConversantRepo{},
&repositories.MockMessageRepo{},
&operators.MockAuther{},
&operators.MockNotifier{},
}
}
func makeConn(id string) *connection.MockConn {
mockConn := &connection.MockConn{}
mockConn.Conversant = func() repositories.Conversant {
return repositories.Conversant{
ID: id,
}
}
mockConn.Auth = func() error {
return nil
}
mockConn.Request = func() chan connection.Request {
return nil
}
mockConn.Leaver = func() chan struct{} {
return nil
}
return mockConn
}
func makeMockManager() *ConnectionManager {
return &ConnectionManager{
connections: make(map[string][]connection.Conn),
connectionMu: &sync.RWMutex{},
messageChan: make(chan messageRequest, 10),
shutdownChan: make(chan struct{}, 1),
chatInteractor: &chatInteractor{},
}
}
func TestConnectionManager_JoinAuthorize(t *testing.T) {
manager := makeMockManager()
manager.startup()
conversantRepo := &repositories.MockConversantRepo{
Upsert: func(conversant repositories.Conversant) (*repositories.Conversant, error) {
return &conversant, nil
},
}
manager.chatInteractor.conversantRepo = conversantRepo
conn := makeConn(uuid.New())
manager.Join(conn)
if len(manager.connections) != 1 {
t.Fatalf("Connections didn't join")
}
}
func TestConnectionManager_NotifyInMemory(t *testing.T) {
manager := makeMockManager()
manager.startup()
senderID := uuid.New()
receiverID := uuid.New()
m := &repositories.MockMessageRepo{
Create: func(message repositories.Message) (message2 *repositories.Message, e error) {
return &message, nil
},
}
c := &repositories.MockConversationRepo{
GetConvo: func(conversationId string) (conversants []repositories.Conversant, e error) {
return []repositories.Conversant{
{ID: receiverID},
{ID: senderID},
}, nil
},
}
manager.chatInteractor = newChatInteractor(m, c, nil)
connA := makeConn(senderID)
connB := makeConn(receiverID)
resp := make(chan connection.Response, 2)
connA.Resp = func() chan connection.Response {
return resp
}
connB.Resp = func() chan connection.Response {
return resp
}
manager.addConn(connA)
manager.addConn(connB)
manager.sendMessage(connA, connection.SendMessageRequest{ConversationID: uuid.New(), Message: "test"})
select {
case response := <-resp:
if response.Type == connection.Error {
t.Fatalf("received response error")
}
return
case <-time.After(10 * time.Second):
t.Error("Didn't receive message")
}
}
func TestConnectionManager_Notifier(t *testing.T) {
manager := makeMockManager()
manager.startup()
m := &repositories.MockMessageRepo{
Create: func(message repositories.Message) (message2 *repositories.Message, e error) {
return &message, nil
},
}
c := &repositories.MockConversationRepo{
GetConvo: func(conversationId string) (conversants []repositories.Conversant, e error) {
return []repositories.Conversant{
{ID: "b"},
}, nil
},
}
manager.chatInteractor = newChatInteractor(m, c, nil)
conn := makeConn("a")
notified := make(chan struct{}, 1)
requests := make(chan connection.Request)
conn.Request = func() chan connection.Request {
return requests
}
conn.Resp = func() chan connection.Response {
return make(chan connection.Response)
}
manager.notifier = &operators.MockNotifier{
SendNotification: func(id string, message repositories.Message) error {
notified <- struct{}{}
return nil
},
}
manager.addConn(conn)
requests <- connection.Request{Type: connection.SendMessage, Data: connection.SendMessageRequest{ConversationID: uuid.New(), Message: "Test"}}
select {
case <-notified:
return
case <-time.After(10 * time.Millisecond):
t.Error("didn't receive notification")
}
}
func TestManager_Leave(t *testing.T) {
td := newTestData()
manager := NewManager(td, td, td, td, td)
manager.chatInteractor.conversantRepo = &repositories.MockConversantRepo{
Upsert: func(conversant repositories.Conversant) (*repositories.Conversant, error) {
return &conversant, nil
},
}
conn1 := makeConn("a")
closer := make(chan struct{})
conn1.Leaver = func() chan struct{} {
return closer
}
conn1.Resp = func() chan connection.Response {
return nil
}
conn1.Request = func() chan connection.Request {
return nil
}
conn1.Auth = func() error {
return nil
}
manager.Join(conn1)
if len(manager.connections) == 0 {
t.Error("conn didn't join")
}
closer <- struct{}{}
manager.connectionMu.Lock()
if len(manager.connections) != 0 {
t.Error("conn didn't leave")
}
manager.connectionMu.Unlock()
}