@@ -4,7 +4,7 @@ import Nimble
44@testable import CogsSDK
55
66class PubSubFullSweepTests : QuickSpec {
7-
7+
88 override func spec( ) {
99 let testChannel = " Test channel "
1010 let testMessage = " Test message "
@@ -37,25 +37,6 @@ class PubSubFullSweepTests: QuickSpec {
3737
3838 let defaultOptions = PubSubOptions . defaultOptions
3939
40- func getSessionUUID( _ connectionHandle: PubSubConnectionHandle , completion: @escaping ( String ) -> Void ) {
41- connectionHandle. getSessionUuid ( ) { outcome in
42- switch outcome {
43- case . pubSubSuccess( let object) :
44- if let uuid = object as? String {
45- expect ( uuid) . toNot ( beNil ( ) )
46- expect ( uuid) . toNot ( beEmpty ( ) )
47-
48- completion ( uuid)
49- } else {
50- fail ( " Expected String, got \( object) " )
51- }
52-
53- default :
54- fail ( " Expected success, got error response " )
55- }
56- }
57- }
58-
5940 describe ( " Full Sweep Test " ) {
6041
6142 it ( " pubsub successfully connects, subscribes, lists subscribtions, publishes and receives message, closes the connection " ) {
@@ -117,10 +98,10 @@ class PubSubFullSweepTests: QuickSpec {
11798
11899 default :
119100 fail ( " Expected success, got error response " )
120-
101+
121102 done ( )
122103 }
123-
104+
124105 connectionHandle. close ( )
125106
126107 connectionHandle. onClose = { ( error) in
@@ -245,23 +226,35 @@ class PubSubFullSweepTests: QuickSpec {
245226
246227 waitUntil ( timeout: 15 ) { done in
247228 let socket = PubSubSocket ( keys: allKeys, options: defaultOptions)
248- let connectionHandle = PubSubService . connect ( socket: socket)
229+ var connectionHandle : PubSubConnectionHandle ! = PubSubService . connect ( socket: socket)
249230
250- connectionHandle. onNewSession = { _ in
251- getSessionUUID ( connectionHandle) { uuid in
252- sessionUUID = uuid
231+ connectionHandle. onNewSession = { uuid in
232+ sessionUUID = uuid
253233
254- connectionHandle. dropConnection ( )
255- }
234+ connectionHandle. dropConnection ( )
256235 }
257236
258237 connectionHandle. onReconnect = {
259- getSessionUUID ( connectionHandle) { uuid in
238+ connectionHandle. getSessionUuid { outcome in
239+ switch outcome {
240+ case . pubSubSuccess( let object) :
241+ if let uuid = object as? String {
242+ expect ( uuid) . toNot ( beNil ( ) )
243+ expect ( uuid) . toNot ( beEmpty ( ) )
260244
261- expect ( uuid == sessionUUID) . to ( beTruthy ( ) )
245+ expect ( uuid == sessionUUID) . to ( beTruthy ( ) )
262246
263- connectionHandle. close ( )
264- done ( )
247+ connectionHandle. close ( )
248+ connectionHandle = nil
249+ done ( )
250+
251+ } else {
252+ fail ( " Expected String, got \( object) " )
253+ }
254+
255+ default :
256+ fail ( " Expected success, got error response " )
257+ }
265258 }
266259 }
267260 }
@@ -294,22 +287,36 @@ class PubSubFullSweepTests: QuickSpec {
294287 describe ( " Get Session Uuid Test " ) {
295288
296289 it ( " returns the same uuid when session is restored " ) {
290+ var oldUuid : String !
291+
297292 waitUntil ( timeout: 15 ) { done in
298293 let socket = PubSubSocket ( keys: allKeys, options: defaultOptions)
299- let connectionHandle = PubSubService . connect ( socket: socket)
294+ var connectionHandle : PubSubConnectionHandle ! = PubSubService . connect ( socket: socket)
300295
301- connectionHandle. onNewSession = { _ in
302- getSessionUUID ( connectionHandle) { oldUuid in
296+ connectionHandle. onNewSession = { uuid in
297+ oldUuid = uuid
298+ connectionHandle. dropConnection ( )
303299
304- connectionHandle. dropConnection ( )
300+ connectionHandle. onReconnect = {
301+ connectionHandle. getSessionUuid { outcome in
302+ switch outcome {
303+ case . pubSubSuccess( let object) :
304+ if let newUuid = object as? String {
305+ expect ( newUuid) . toNot ( beNil ( ) )
306+ expect ( newUuid) . toNot ( beEmpty ( ) )
305307
306- connectionHandle. onReconnect = {
307- getSessionUUID ( connectionHandle) { newUuid in
308+ expect ( oldUuid == newUuid) . to ( beTruthy ( ) )
308309
309- expect ( oldUuid == newUuid) . to ( beTruthy ( ) )
310+ connectionHandle. close ( )
311+ connectionHandle = nil
312+ done ( )
310313
311- connectionHandle. close ( )
312- done ( )
314+ } else {
315+ fail ( " Expected String, got \( object) " )
316+ }
317+
318+ default :
319+ fail ( " Expected success, got error response " )
313320 }
314321 }
315322 }
@@ -401,7 +408,7 @@ class PubSubFullSweepTests: QuickSpec {
401408 }
402409 }
403410 }
404-
411+
405412 context ( " when write key is not supplied " ) {
406413 var isEmitting : Bool = false
407414 var error : PubSubErrorResponse ?
@@ -414,7 +421,7 @@ class PubSubFullSweepTests: QuickSpec {
414421 connectionHandle. onNewSession = { _ in
415422 connectionHandle. publish ( channel: testChannel, message: testMessage) { _ in }
416423 }
417-
424+
418425 connectionHandle. onErrorResponse = { responseError in
419426 isEmitting = true
420427 error = responseError
@@ -423,19 +430,19 @@ class PubSubFullSweepTests: QuickSpec {
423430 done ( )
424431 }
425432 }
426-
433+
427434 waitUntil ( timeout: 11 ) { done in
428435 expect ( isEmitting == true ) . to ( beTruthy ( ) )
429436 expect ( error) . toNot ( beNil ( ) )
430437 expect ( error!. action == PubSubAction . publish. rawValue) . to ( beTruthy ( ) )
431438 expect ( error!. code == PubSubResponseCode . unauthorised. rawValue) . to ( beTruthy ( ) )
432-
439+
433440 done ( )
434441 }
435442 }
436443 }
437444 }
438-
445+
439446 describe ( " onReconnect Test " ) {
440447 var isEmitting : Bool = false
441448
@@ -455,10 +462,10 @@ class PubSubFullSweepTests: QuickSpec {
455462 done ( )
456463 }
457464 }
458-
465+
459466 waitUntil ( timeout: 16 ) { done in
460467 expect ( isEmitting == true ) . to ( beTruthy ( ) )
461-
468+
462469 done ( )
463470 }
464471 }
@@ -468,18 +475,18 @@ class PubSubFullSweepTests: QuickSpec {
468475
469476 it ( " emits close event " ) {
470477 var isEmitting : Bool = false
471-
478+
472479 waitUntil ( timeout: defaultTimeout) { done in
473480 let socket = PubSubSocket ( keys: allKeys, options: defaultOptions)
474481 let connectionHandle = PubSubService . connect ( socket: socket)
475-
482+
476483 connectionHandle. onNewSession = { _ in
477484 connectionHandle. close ( )
478485 }
479486
480487 connectionHandle. onClose = { ( error) in
481488 isEmitting = true
482-
489+
483490 connectionHandle. close ( )
484491 done ( )
485492 }
@@ -494,17 +501,17 @@ class PubSubFullSweepTests: QuickSpec {
494501 }
495502
496503 describe ( " onNewSession Test " ) {
497-
504+
498505 it ( " emits on new session event " ) {
499506 var isEmitting : Bool = false
500-
507+
501508 waitUntil ( timeout: defaultTimeout) { done in
502509 let socket = PubSubSocket ( keys: allKeys, options: defaultOptions)
503510 let connectionHandle = PubSubService . connect ( socket: socket)
504511
505512 connectionHandle. onNewSession = { _ in
506513 isEmitting = true
507-
514+
508515 connectionHandle. close ( )
509516 done ( )
510517 }
0 commit comments