@@ -9,11 +9,9 @@ import (
9
9
"io"
10
10
"net/http"
11
11
"net/http/httptest"
12
- "sync"
13
12
"testing"
14
13
"time"
15
14
16
- "github.com/cespare/xxhash/v2"
17
15
"github.com/stretchr/testify/assert"
18
16
"github.com/stretchr/testify/require"
19
17
@@ -8076,70 +8074,10 @@ var errSubscriptionClientFail = errors.New("subscription client fail error")
8076
8074
8077
8075
type FailingSubscriptionClient struct {}
8078
8076
8079
- func (f * FailingSubscriptionClient ) Subscribe (ctx * resolve.Context , options GraphQLSubscriptionOptions , updater resolve. SubscriptionUpdater ) error {
8077
+ func (f FailingSubscriptionClient ) Subscribe (_ * resolve.Context , _ GraphQLSubscriptionOptions , _ chan <- [] byte ) error {
8080
8078
return errSubscriptionClientFail
8081
8079
}
8082
8080
8083
- func (f * FailingSubscriptionClient ) UniqueRequestID (ctx * resolve.Context , options GraphQLSubscriptionOptions , hash * xxhash.Digest ) (err error ) {
8084
- return errSubscriptionClientFail
8085
- }
8086
-
8087
- type testSubscriptionUpdater struct {
8088
- updates []string
8089
- done bool
8090
- mux sync.Mutex
8091
- }
8092
-
8093
- func (t * testSubscriptionUpdater ) AwaitUpdates (tt * testing.T , timeout time.Duration , count int ) {
8094
- ticker := time .NewTicker (timeout )
8095
- defer ticker .Stop ()
8096
- for {
8097
- time .Sleep (10 * time .Millisecond )
8098
- select {
8099
- case <- ticker .C :
8100
- tt .Fatalf ("timed out waiting for updates" )
8101
- default :
8102
- t .mux .Lock ()
8103
- if len (t .updates ) == count {
8104
- t .mux .Unlock ()
8105
- return
8106
- }
8107
- t .mux .Unlock ()
8108
- }
8109
- }
8110
- }
8111
-
8112
- func (t * testSubscriptionUpdater ) AwaitDone (tt * testing.T , timeout time.Duration ) {
8113
- ticker := time .NewTicker (timeout )
8114
- defer ticker .Stop ()
8115
- for {
8116
- time .Sleep (10 * time .Millisecond )
8117
- select {
8118
- case <- ticker .C :
8119
- tt .Fatalf ("timed out waiting for done" )
8120
- default :
8121
- t .mux .Lock ()
8122
- if t .done {
8123
- t .mux .Unlock ()
8124
- return
8125
- }
8126
- t .mux .Unlock ()
8127
- }
8128
- }
8129
- }
8130
-
8131
- func (t * testSubscriptionUpdater ) Update (data []byte ) {
8132
- t .mux .Lock ()
8133
- defer t .mux .Unlock ()
8134
- t .updates = append (t .updates , string (data ))
8135
- }
8136
-
8137
- func (t * testSubscriptionUpdater ) Done () {
8138
- t .mux .Lock ()
8139
- defer t .mux .Unlock ()
8140
- t .done = true
8141
- }
8142
-
8143
8081
func TestSubscriptionSource_Start (t * testing.T ) {
8144
8082
chatServer := httptest .NewServer (subscriptiontesting .ChatGraphQLEndpointHandler ())
8145
8083
defer chatServer .Close ()
@@ -8182,86 +8120,84 @@ func TestSubscriptionSource_Start(t *testing.T) {
8182
8120
}
8183
8121
8184
8122
t .Run ("should return error when input is invalid" , func (t * testing.T ) {
8185
- source := SubscriptionSource {client : & FailingSubscriptionClient {}}
8123
+ source := SubscriptionSource {client : FailingSubscriptionClient {}}
8186
8124
err := source .Start (resolve .NewContext (context .Background ()), []byte (`{"url": "", "body": "", "header": null}` ), nil )
8187
8125
assert .Error (t , err )
8188
8126
})
8189
8127
8190
8128
t .Run ("should return error when subscription client returns an error" , func (t * testing.T ) {
8191
- source := SubscriptionSource {client : & FailingSubscriptionClient {}}
8129
+ source := SubscriptionSource {client : FailingSubscriptionClient {}}
8192
8130
err := source .Start (resolve .NewContext (context .Background ()), []byte (`{"url": "", "body": {}, "header": null}` ), nil )
8193
8131
assert .Error (t , err )
8194
8132
assert .Equal (t , resolve .ErrUnableToResolve , err )
8195
8133
})
8196
8134
8197
8135
t .Run ("invalid json: should stop before sending to upstream" , func (t * testing.T ) {
8136
+ next := make (chan []byte )
8198
8137
ctx := resolve .NewContext (context .Background ())
8199
8138
defer ctx .Context ().Done ()
8200
8139
8201
- updater := & testSubscriptionUpdater {}
8202
-
8203
8140
source := newSubscriptionSource (ctx .Context ())
8204
8141
chatSubscriptionOptions := chatServerSubscriptionOptions (t , `{"variables": {}, "extensions": {}, "operationName": "LiveMessages", "query": "subscription LiveMessages { messageAdded(roomName: "#test") { text createdBy } }"}` )
8205
- err := source .Start (ctx , chatSubscriptionOptions , updater )
8142
+ err := source .Start (ctx , chatSubscriptionOptions , next )
8206
8143
require .ErrorIs (t , err , resolve .ErrUnableToResolve )
8207
8144
})
8208
8145
8209
8146
t .Run ("invalid syntax (roomNam)" , func (t * testing.T ) {
8147
+ next := make (chan []byte )
8210
8148
ctx := resolve .NewContext (context .Background ())
8211
8149
defer ctx .Context ().Done ()
8212
8150
8213
- updater := & testSubscriptionUpdater {}
8214
-
8215
8151
source := newSubscriptionSource (ctx .Context ())
8216
8152
chatSubscriptionOptions := chatServerSubscriptionOptions (t , `{"variables": {}, "extensions": {}, "operationName": "LiveMessages", "query": "subscription LiveMessages { messageAdded(roomNam: \"#test\") { text createdBy } }"}` )
8217
- err := source .Start (ctx , chatSubscriptionOptions , updater )
8153
+ err := source .Start (ctx , chatSubscriptionOptions , next )
8218
8154
require .NoError (t , err )
8219
- updater .AwaitUpdates (t , time .Second , 1 )
8220
- assert .Len (t , updater .updates , 1 )
8221
- assert .Equal (t , `{"errors":[{"message":"Unknown argument \"roomNam\" on field \"Subscription.messageAdded\". Did you mean \"roomName\"?","locations":[{"line":1,"column":29}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}},{"message":"Field \"messageAdded\" argument \"roomName\" of type \"String!\" is required, but it was not provided.","locations":[{"line":1,"column":29}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}]}` , updater .updates [0 ])
8222
- assert .Equal (t , true , updater .done )
8155
+
8156
+ msg , ok := <- next
8157
+ assert .True (t , ok )
8158
+ assert .Equal (t , `{"errors":[{"message":"Unknown argument \"roomNam\" on field \"Subscription.messageAdded\". Did you mean \"roomName\"?","locations":[{"line":1,"column":29}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}},{"message":"Field \"messageAdded\" argument \"roomName\" of type \"String!\" is required, but it was not provided.","locations":[{"line":1,"column":29}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}]}` , string (msg ))
8159
+ _ , ok = <- next
8160
+ assert .False (t , ok )
8223
8161
})
8224
8162
8225
8163
t .Run ("should close connection on stop message" , func (t * testing.T ) {
8164
+ next := make (chan []byte )
8226
8165
subscriptionLifecycle , cancelSubscription := context .WithCancel (context .Background ())
8227
8166
resolverLifecycle , cancelResolver := context .WithCancel (context .Background ())
8228
8167
defer cancelResolver ()
8229
8168
8230
- updater := & testSubscriptionUpdater {}
8231
-
8232
8169
source := newSubscriptionSource (resolverLifecycle )
8233
8170
chatSubscriptionOptions := chatServerSubscriptionOptions (t , `{"variables": {}, "extensions": {}, "operationName": "LiveMessages", "query": "subscription LiveMessages { messageAdded(roomName: \"#test\") { text createdBy } }"}` )
8234
- err := source .Start (resolve .NewContext (subscriptionLifecycle ), chatSubscriptionOptions , updater )
8171
+ err := source .Start (resolve .NewContext (subscriptionLifecycle ), chatSubscriptionOptions , next )
8235
8172
require .NoError (t , err )
8236
8173
8237
8174
username := "myuser"
8238
8175
message := "hello world!"
8239
8176
go sendChatMessage (t , username , message )
8240
8177
8241
- updater .AwaitUpdates (t , time .Second , 1 )
8178
+ nextBytes := <- next
8179
+ assert .Equal (t , `{"data":{"messageAdded":{"text":"hello world!","createdBy":"myuser"}}}` , string (nextBytes ))
8242
8180
cancelSubscription ()
8243
- updater .AwaitDone (t , time .Second * 5 )
8244
- assert .Len (t , updater .updates , 1 )
8245
- assert .Equal (t , `{"data":{"messageAdded":{"text":"hello world!","createdBy":"myuser"}}}` , updater .updates [0 ])
8181
+ _ , ok := <- next
8182
+ assert .False (t , ok )
8246
8183
})
8247
8184
8248
8185
t .Run ("should successfully subscribe with chat example" , func (t * testing.T ) {
8186
+ next := make (chan []byte )
8249
8187
ctx := resolve .NewContext (context .Background ())
8250
8188
defer ctx .Context ().Done ()
8251
8189
8252
- updater := & testSubscriptionUpdater {}
8253
-
8254
8190
source := newSubscriptionSource (ctx .Context ())
8255
8191
chatSubscriptionOptions := chatServerSubscriptionOptions (t , `{"variables": {}, "extensions": {}, "operationName": "LiveMessages", "query": "subscription LiveMessages { messageAdded(roomName: \"#test\") { text createdBy } }"}` )
8256
- err := source .Start (ctx , chatSubscriptionOptions , updater )
8192
+ err := source .Start (ctx , chatSubscriptionOptions , next )
8257
8193
require .NoError (t , err )
8258
8194
8259
8195
username := "myuser"
8260
8196
message := "hello world!"
8261
8197
go sendChatMessage (t , username , message )
8262
- updater . AwaitUpdates ( t , time . Second , 1 )
8263
- assert . Len ( t , updater . updates , 1 )
8264
- assert .Equal (t , `{"data":{"messageAdded":{"text":"hello world!","createdBy":"myuser"}}}` , updater . updates [ 0 ] )
8198
+
8199
+ nextBytes := <- next
8200
+ assert .Equal (t , `{"data":{"messageAdded":{"text":"hello world!","createdBy":"myuser"}}}` , string ( nextBytes ) )
8265
8201
})
8266
8202
}
8267
8203
@@ -8309,65 +8245,60 @@ func TestSubscription_GTWS_SubProtocol(t *testing.T) {
8309
8245
}
8310
8246
8311
8247
t .Run ("invalid syntax (roomNam)" , func (t * testing.T ) {
8248
+ next := make (chan []byte )
8312
8249
ctx := resolve .NewContext (context .Background ())
8313
8250
defer ctx .Context ().Done ()
8314
8251
8315
- updater := & testSubscriptionUpdater {}
8316
-
8317
8252
source := newSubscriptionSource (ctx .Context ())
8318
8253
chatSubscriptionOptions := chatServerSubscriptionOptions (t , `{"variables": {}, "extensions": {}, "operationName": "LiveMessages", "query": "subscription LiveMessages { messageAdded(roomNam: \"#test\") { text createdBy } }"}` )
8319
- err := source .Start (ctx , chatSubscriptionOptions , updater )
8254
+ err := source .Start (ctx , chatSubscriptionOptions , next )
8320
8255
require .NoError (t , err )
8321
8256
8322
- updater . AwaitUpdates ( t , time . Second , 1 )
8323
- assert .Len (t , updater . updates , 1 )
8324
- assert .Equal (t , `{"errors":[{"message":"Unknown argument \"roomNam\" on field \"Subscription.messageAdded\". Did you mean \"roomName\"?","locations":[{"line":1,"column":29}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}},{"message":"Field \"messageAdded\" argument \"roomName\" of type \"String!\" is required, but it was not provided.","locations":[{"line":1,"column":29}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}]}` , updater . updates [ 0 ] )
8325
- updater . AwaitDone ( t , time . Second )
8326
- assert .Equal (t , true , updater . done )
8257
+ msg , ok := <- next
8258
+ assert .True (t , ok )
8259
+ assert .Equal (t , `{"errors":[{"message":"Unknown argument \"roomNam\" on field \"Subscription.messageAdded\". Did you mean \"roomName\"?","locations":[{"line":1,"column":29}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}},{"message":"Field \"messageAdded\" argument \"roomName\" of type \"String!\" is required, but it was not provided.","locations":[{"line":1,"column":29}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}]}` , string ( msg ) )
8260
+ _ , ok = <- next
8261
+ assert .False (t , ok )
8327
8262
})
8328
8263
8329
8264
t .Run ("should close connection on stop message" , func (t * testing.T ) {
8265
+ next := make (chan []byte )
8330
8266
subscriptionLifecycle , cancelSubscription := context .WithCancel (context .Background ())
8331
8267
resolverLifecycle , cancelResolver := context .WithCancel (context .Background ())
8332
8268
defer cancelResolver ()
8333
8269
8334
- updater := & testSubscriptionUpdater {}
8335
-
8336
8270
source := newSubscriptionSource (resolverLifecycle )
8337
8271
chatSubscriptionOptions := chatServerSubscriptionOptions (t , `{"variables": {}, "extensions": {}, "operationName": "LiveMessages", "query": "subscription LiveMessages { messageAdded(roomName: \"#test\") { text createdBy } }"}` )
8338
- err := source .Start (resolve .NewContext (subscriptionLifecycle ), chatSubscriptionOptions , updater )
8272
+ err := source .Start (resolve .NewContext (subscriptionLifecycle ), chatSubscriptionOptions , next )
8339
8273
require .NoError (t , err )
8340
8274
8341
8275
username := "myuser"
8342
8276
message := "hello world!"
8343
8277
go sendChatMessage (t , username , message )
8344
8278
8345
- updater .AwaitUpdates (t , time .Second , 1 )
8346
- assert .Len (t , updater .updates , 1 )
8347
- assert .Equal (t , `{"data":{"messageAdded":{"text":"hello world!","createdBy":"myuser"}}}` , updater .updates [0 ])
8279
+ nextBytes := <- next
8280
+ assert .Equal (t , `{"data":{"messageAdded":{"text":"hello world!","createdBy":"myuser"}}}` , string (nextBytes ))
8348
8281
cancelSubscription ()
8349
- updater . AwaitDone ( t , time . Second * 5 )
8350
- assert .Equal (t , true , updater . done )
8282
+ _ , ok := <- next
8283
+ assert .False (t , ok )
8351
8284
})
8352
8285
8353
8286
t .Run ("should successfully subscribe with chat example" , func (t * testing.T ) {
8287
+ next := make (chan []byte )
8354
8288
ctx := resolve .NewContext (context .Background ())
8355
8289
defer ctx .Context ().Done ()
8356
8290
8357
- updater := & testSubscriptionUpdater {}
8358
-
8359
8291
source := newSubscriptionSource (ctx .Context ())
8360
8292
chatSubscriptionOptions := chatServerSubscriptionOptions (t , `{"variables": {}, "extensions": {}, "operationName": "LiveMessages", "query": "subscription LiveMessages { messageAdded(roomName: \"#test\") { text createdBy } }"}` )
8361
- err := source .Start (ctx , chatSubscriptionOptions , updater )
8293
+ err := source .Start (ctx , chatSubscriptionOptions , next )
8362
8294
require .NoError (t , err )
8363
8295
8364
8296
username := "myuser"
8365
8297
message := "hello world!"
8366
8298
go sendChatMessage (t , username , message )
8367
8299
8368
- updater .AwaitUpdates (t , time .Second , 1 )
8369
- assert .Len (t , updater .updates , 1 )
8370
- assert .Equal (t , `{"data":{"messageAdded":{"text":"hello world!","createdBy":"myuser"}}}` , updater .updates [0 ])
8300
+ nextBytes := <- next
8301
+ assert .Equal (t , `{"data":{"messageAdded":{"text":"hello world!","createdBy":"myuser"}}}` , string (nextBytes ))
8371
8302
})
8372
8303
}
8373
8304
0 commit comments