Skip to content

Commit 2d7c97b

Browse files
author
Jannis Pohlmann
committed
subscriptions: Add tests for the subscription manager
1 parent fa25316 commit 2d7c97b

File tree

1 file changed

+338
-0
lines changed

1 file changed

+338
-0
lines changed

subscriptions_test.go

Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
package graphqlws_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/functionalfoundry/graphqlws"
7+
"github.com/graphql-go/graphql"
8+
log "github.com/sirupsen/logrus"
9+
)
10+
11+
// Mock connection
12+
13+
type mockWebSocketConnection struct {
14+
user string
15+
id string
16+
}
17+
18+
func (c *mockWebSocketConnection) ID() string {
19+
return c.id
20+
}
21+
22+
func (c *mockWebSocketConnection) User() interface{} {
23+
return c.user
24+
}
25+
26+
func (c *mockWebSocketConnection) SendData(
27+
opID string,
28+
data *graphqlws.DataMessagePayload,
29+
) {
30+
// Do nothing
31+
}
32+
33+
func (c *mockWebSocketConnection) SendError(err error) {
34+
// Do nothing
35+
}
36+
37+
// Tests
38+
39+
func TestMain(m *testing.M) {
40+
log.SetLevel(log.ErrorLevel)
41+
}
42+
43+
func TestSubscriptions_NewSubscriptionManagerCreatesInstance(t *testing.T) {
44+
schema, _ := graphql.NewSchema(graphql.SchemaConfig{})
45+
46+
sm := graphqlws.NewSubscriptionManager(&schema)
47+
if sm == nil {
48+
t.Fatal("NewSubscriptionManager fails in creating a new instance")
49+
}
50+
}
51+
52+
func TestSubscriptions_SubscriptionsAreEmptyInitially(t *testing.T) {
53+
schema, _ := graphql.NewSchema(graphql.SchemaConfig{})
54+
sm := graphqlws.NewSubscriptionManager(&schema)
55+
56+
if len(sm.Subscriptions()) > 0 {
57+
t.Fatal("The subscriptions of SubscriptionManager are not empty initially")
58+
}
59+
}
60+
61+
func TestSubscriptions_AddingInvalidSubscriptionsFails(t *testing.T) {
62+
schema, _ := graphql.NewSchema(graphql.SchemaConfig{})
63+
sm := graphqlws.NewSubscriptionManager(&schema)
64+
65+
conn := mockWebSocketConnection{
66+
id: "1",
67+
}
68+
69+
// Try adding a subscription with nothing set
70+
errors := sm.AddSubscription(&conn, &graphqlws.Subscription{})
71+
72+
if len(errors) == 0 {
73+
t.Error("AddSubscription does not fail when adding an empty subscription")
74+
}
75+
76+
if len(sm.Subscriptions()) > 0 {
77+
t.Fatal("AddSubscription unexpectedly adds empty subscriptions")
78+
}
79+
80+
// Try adding a subscription with an invalid query
81+
errors = sm.AddSubscription(&conn, &graphqlws.Subscription{
82+
Query: "<<<Fooo>>>",
83+
})
84+
85+
if len(errors) == 0 {
86+
t.Error("AddSubscription does not fail when adding an invalid subscription")
87+
}
88+
89+
if len(sm.Subscriptions()) > 0 {
90+
t.Fatal("AddSubscription unexpectedly adds invalid subscriptions")
91+
}
92+
93+
// Try adding a subscription with a query that doesn't match the schema
94+
errors = sm.AddSubscription(&conn, &graphqlws.Subscription{
95+
Query: "subscription { foo }",
96+
})
97+
98+
if len(errors) == 0 {
99+
t.Error("AddSubscription doesn't fail if the query doesn't match the schema")
100+
}
101+
102+
if len(sm.Subscriptions()) > 0 {
103+
t.Fatal("AddSubscription unexpectedly adds invalid subscriptions")
104+
}
105+
}
106+
107+
func TestSubscriptions_AddingValidSubscriptionsWorks(t *testing.T) {
108+
schema, _ := graphql.NewSchema(graphql.SchemaConfig{
109+
Subscription: graphql.NewObject(graphql.ObjectConfig{
110+
Name: "Subscription",
111+
Fields: graphql.Fields{
112+
"users": &graphql.Field{
113+
Type: graphql.NewList(graphql.String),
114+
},
115+
},
116+
})})
117+
sm := graphqlws.NewSubscriptionManager(&schema)
118+
119+
conn := mockWebSocketConnection{
120+
id: "1",
121+
}
122+
123+
// Add a valid subscription
124+
sub1 := graphqlws.Subscription{
125+
ID: "1",
126+
Connection: &conn,
127+
Query: "subscription { users }",
128+
SendData: func(s *graphqlws.Subscription, msg *graphqlws.DataMessagePayload) {
129+
// Do nothing
130+
},
131+
}
132+
errors := sm.AddSubscription(&conn, &sub1)
133+
134+
if len(errors) > 0 {
135+
t.Error(
136+
"AddSubscription fails adding valid subscriptions. Unexpected errors:",
137+
errors,
138+
)
139+
}
140+
141+
if len(sm.Subscriptions()) != 1 ||
142+
len(sm.Subscriptions()[&conn]) != 1 ||
143+
sm.Subscriptions()[&conn]["1"] != &sub1 {
144+
t.Fatal("AddSubscription doesn't add valid subscriptions properly")
145+
}
146+
147+
// Add another valid subscription
148+
sub2 := graphqlws.Subscription{
149+
ID: "2",
150+
Connection: &conn,
151+
Query: "subscription { users }",
152+
SendData: func(s *graphqlws.Subscription, msg *graphqlws.DataMessagePayload) {
153+
// Do nothing
154+
},
155+
}
156+
errors = sm.AddSubscription(&conn, &sub2)
157+
if len(errors) > 0 {
158+
t.Error(
159+
"AddSubscription fails adding valid subscriptions.",
160+
"Unexpected errors:", errors,
161+
)
162+
}
163+
if len(sm.Subscriptions()) != 1 ||
164+
len(sm.Subscriptions()[&conn]) != 2 ||
165+
sm.Subscriptions()[&conn]["1"] != &sub1 ||
166+
sm.Subscriptions()[&conn]["2"] != &sub2 {
167+
t.Fatal("AddSubscription doesn't add valid subscriptions properly")
168+
}
169+
}
170+
171+
func TestSubscriptions_AddingSubscriptionsTwiceFails(t *testing.T) {
172+
schema, _ := graphql.NewSchema(graphql.SchemaConfig{
173+
Subscription: graphql.NewObject(graphql.ObjectConfig{
174+
Name: "Subscription",
175+
Fields: graphql.Fields{
176+
"users": &graphql.Field{
177+
Type: graphql.NewList(graphql.String),
178+
},
179+
},
180+
})})
181+
sm := graphqlws.NewSubscriptionManager(&schema)
182+
183+
conn := mockWebSocketConnection{
184+
id: "1",
185+
}
186+
187+
// Add a valid subscription
188+
sub := graphqlws.Subscription{
189+
ID: "1",
190+
Connection: &conn,
191+
Query: "subscription { users }",
192+
SendData: func(s *graphqlws.Subscription, msg *graphqlws.DataMessagePayload) {
193+
// Do nothing
194+
},
195+
}
196+
sm.AddSubscription(&conn, &sub)
197+
198+
// Try adding the subscription for a second time
199+
errors := sm.AddSubscription(&conn, &sub)
200+
201+
if len(errors) == 0 {
202+
t.Error(
203+
"AddSubscription doesn't fail when adding subscriptions a second time.",
204+
"Unexpected errors:", errors,
205+
)
206+
}
207+
208+
if len(sm.Subscriptions()) != 1 ||
209+
len(sm.Subscriptions()[&conn]) != 1 ||
210+
sm.Subscriptions()[&conn]["1"] != &sub {
211+
t.Fatal("AddSubscription unexpectedly adds subscriptions twice")
212+
}
213+
}
214+
215+
func TestSubscriptions_RemovingSubscriptionsWorks(t *testing.T) {
216+
schema, _ := graphql.NewSchema(graphql.SchemaConfig{
217+
Subscription: graphql.NewObject(graphql.ObjectConfig{
218+
Name: "Subscription",
219+
Fields: graphql.Fields{
220+
"users": &graphql.Field{
221+
Type: graphql.NewList(graphql.String),
222+
},
223+
},
224+
})})
225+
sm := graphqlws.NewSubscriptionManager(&schema)
226+
227+
conn := mockWebSocketConnection{
228+
id: "1",
229+
}
230+
231+
// Add two valid subscriptions
232+
sub1 := graphqlws.Subscription{
233+
ID: "1",
234+
Connection: &conn,
235+
Query: "subscription { users }",
236+
SendData: func(s *graphqlws.Subscription, msg *graphqlws.DataMessagePayload) {
237+
// Do nothing
238+
},
239+
}
240+
sm.AddSubscription(&conn, &sub1)
241+
sub2 := graphqlws.Subscription{
242+
ID: "2",
243+
Connection: &conn,
244+
Query: "subscription { users }",
245+
SendData: func(s *graphqlws.Subscription, msg *graphqlws.DataMessagePayload) {
246+
// Do nothing
247+
},
248+
}
249+
sm.AddSubscription(&conn, &sub2)
250+
251+
// Remove the first subscription
252+
sm.RemoveSubscription(&conn, &sub1)
253+
254+
// Verify that only one subscription is left
255+
if len(sm.Subscriptions()) != 1 || len(sm.Subscriptions()[&conn]) != 1 {
256+
t.Error("RemoveSubscription does not remove subscriptions")
257+
}
258+
259+
// Remove the second subscription
260+
sm.RemoveSubscription(&conn, &sub2)
261+
262+
// Verify that there are no subscriptions left
263+
if len(sm.Subscriptions()) != 0 {
264+
t.Error("RemoveSubscription does not remove subscriptions")
265+
}
266+
}
267+
268+
func TestSubscriptions_RemovingSubscriptionsOfAConnectionWorks(t *testing.T) {
269+
schema, _ := graphql.NewSchema(graphql.SchemaConfig{
270+
Subscription: graphql.NewObject(graphql.ObjectConfig{
271+
Name: "Subscription",
272+
Fields: graphql.Fields{
273+
"users": &graphql.Field{
274+
Type: graphql.NewList(graphql.String),
275+
},
276+
},
277+
})})
278+
sm := graphqlws.NewSubscriptionManager(&schema)
279+
280+
conn1 := mockWebSocketConnection{id: "1"}
281+
conn2 := mockWebSocketConnection{id: "2"}
282+
283+
// Add four valid subscriptions
284+
sub1 := graphqlws.Subscription{
285+
ID: "1",
286+
Connection: &conn1,
287+
Query: "subscription { users }",
288+
SendData: func(s *graphqlws.Subscription, msg *graphqlws.DataMessagePayload) {
289+
// Do nothing
290+
},
291+
}
292+
sm.AddSubscription(&conn1, &sub1)
293+
sub2 := graphqlws.Subscription{
294+
ID: "2",
295+
Connection: &conn1,
296+
Query: "subscription { users }",
297+
SendData: func(s *graphqlws.Subscription, msg *graphqlws.DataMessagePayload) {
298+
// Do nothing
299+
},
300+
}
301+
sm.AddSubscription(&conn1, &sub2)
302+
sub3 := graphqlws.Subscription{
303+
ID: "1",
304+
Connection: &conn2,
305+
Query: "subscription { users }",
306+
SendData: func(s *graphqlws.Subscription, msg *graphqlws.DataMessagePayload) {
307+
// Do nothing
308+
},
309+
}
310+
sm.AddSubscription(&conn2, &sub3)
311+
sub4 := graphqlws.Subscription{
312+
ID: "2",
313+
Connection: &conn2,
314+
Query: "subscription { users }",
315+
SendData: func(s *graphqlws.Subscription, msg *graphqlws.DataMessagePayload) {
316+
// Do nothing
317+
},
318+
}
319+
sm.AddSubscription(&conn2, &sub4)
320+
321+
// Remove subscriptions of the first connection
322+
sm.RemoveSubscriptions(&conn1)
323+
324+
// Verify that only the subscriptions of the second connection remain
325+
if len(sm.Subscriptions()) != 1 ||
326+
len(sm.Subscriptions()[&conn2]) != 2 ||
327+
sm.Subscriptions()[&conn1] != nil {
328+
t.Error("RemoveSubscriptions doesn't remove subscriptions of connections")
329+
}
330+
331+
// Remove subscriptions of the second connection
332+
sm.RemoveSubscriptions(&conn2)
333+
334+
// Verify that there are no subscriptions left
335+
if len(sm.Subscriptions()) != 0 {
336+
t.Error("RemoveSubscriptions doesn't remove subscriptions of connections")
337+
}
338+
}

0 commit comments

Comments
 (0)