@@ -50,7 +50,7 @@ class ParseLiveQueryTests: XCTestCase {
50
50
masterKey: " masterKey " ,
51
51
serverURL: url,
52
52
testing: true )
53
- ParseLiveQuery . client = try ? ParseLiveQuery ( isDefault: true )
53
+ ParseLiveQuery . setDefault ( try ParseLiveQuery ( isDefault: true ) )
54
54
}
55
55
56
56
override func tearDownWithError( ) throws {
@@ -314,24 +314,28 @@ class ParseLiveQueryTests: XCTestCase {
314
314
XCTFail ( " Should be able to get client " )
315
315
return
316
316
}
317
- client. isSocketEstablished = true // Socket neets to be true
317
+ client. isSocketEstablished = true // Socket needs to be true
318
318
client. isConnecting = true
319
319
client. attempts = 50
320
320
client. isConnected = true
321
321
client. clientId = " yolo "
322
-
323
- XCTAssertEqual ( client. isSocketEstablished, true )
324
- XCTAssertEqual ( client. isConnecting, false )
325
- XCTAssertEqual ( client. clientId, " yolo " )
326
- XCTAssertEqual ( client. attempts, 1 )
322
+ let expectation1 = XCTestExpectation ( description: " Synch " )
323
+ client. synchronizationQueue. sync {
324
+ XCTAssertEqual ( client. isSocketEstablished, true )
325
+ XCTAssertEqual ( client. isConnecting, false )
326
+ XCTAssertEqual ( client. clientId, " yolo " )
327
+ XCTAssertEqual ( client. attempts, 1 )
328
+ expectation1. fulfill ( )
329
+ }
330
+ wait ( for: [ expectation1] , timeout: 20.0 )
327
331
}
328
332
329
333
func testDisconnectedState( ) throws {
330
334
guard let client = ParseLiveQuery . getDefault ( ) else {
331
335
XCTFail ( " Should be able to get client " )
332
336
return
333
337
}
334
- client. isSocketEstablished = true // Socket neets to be true
338
+ client. isSocketEstablished = true // Socket needs to be true
335
339
client. isConnecting = true
336
340
client. isConnected = true
337
341
client. clientId = " yolo "
@@ -352,7 +356,7 @@ class ParseLiveQueryTests: XCTestCase {
352
356
XCTFail ( " Should be able to get client " )
353
357
return
354
358
}
355
- client. isSocketEstablished = true // Socket neets to be true
359
+ client. isSocketEstablished = true // Socket needs to be true
356
360
client. isConnecting = true
357
361
client. isConnected = true
358
362
client. clientId = " yolo "
@@ -384,11 +388,13 @@ class ParseLiveQueryTests: XCTestCase {
384
388
XCTAssertEqual ( client. clientId, " yolo " )
385
389
client. close ( )
386
390
387
- XCTAssertEqual ( client. isSocketEstablished, true )
388
- XCTAssertEqual ( client. isConnected, false )
389
- XCTAssertEqual ( client. isConnecting, false )
390
- XCTAssertNil ( client. clientId)
391
- XCTAssertEqual ( client. isDisconnectedByUser, true )
391
+ DispatchQueue . main. asyncAfter ( deadline: . now( ) ) {
392
+ XCTAssertEqual ( client. isSocketEstablished, true )
393
+ XCTAssertEqual ( client. isConnected, false )
394
+ XCTAssertEqual ( client. isConnecting, false )
395
+ XCTAssertNil ( client. clientId)
396
+ XCTAssertEqual ( client. isDisconnectedByUser, true )
397
+ }
392
398
}
393
399
394
400
func testReconnectInterval( ) throws {
@@ -584,6 +590,87 @@ class ParseLiveQueryTests: XCTestCase {
584
590
wait ( for: [ expectation1, expectation2] , timeout: 20.0 )
585
591
}
586
592
593
+ func testSubscribeCloseSubscribe( ) throws {
594
+ let query = GameScore . query ( " score " > 9 )
595
+ let handler = SubscriptionCallback ( query: query)
596
+ var subscription = try Query< GameScore> . subscribe( handler)
597
+
598
+ guard let client = ParseLiveQuery . getDefault ( ) else {
599
+ XCTFail ( " Should be able to get client " )
600
+ return
601
+ }
602
+ XCTAssertEqual ( subscription. query, query)
603
+
604
+ let expectation1 = XCTestExpectation ( description: " Subscribe Handler " )
605
+ let expectation2 = XCTestExpectation ( description: " Resubscribe Handler " )
606
+ var count = 0
607
+ var originalTask : URLSessionWebSocketTask ?
608
+ subscription. handleSubscribe { subscribedQuery, isNew in
609
+ XCTAssertEqual ( query, subscribedQuery)
610
+ if count == 0 {
611
+ XCTAssertTrue ( isNew)
612
+ XCTAssertEqual ( client. pendingSubscriptions. count, 0 )
613
+ XCTAssertEqual ( client. subscriptions. count, 1 )
614
+ XCTAssertNotNil ( ParseLiveQuery . client? . task)
615
+ originalTask = ParseLiveQuery . client? . task
616
+ expectation1. fulfill ( )
617
+ } else if count == 2 {
618
+ XCTAssertNotNil ( ParseLiveQuery . client? . task)
619
+ XCTAssertFalse ( originalTask == ParseLiveQuery . client? . task)
620
+ expectation2. fulfill ( )
621
+ return
622
+ }
623
+
624
+ ParseLiveQuery . client? . close ( )
625
+ ParseLiveQuery . client? . synchronizationQueue. sync {
626
+ XCTAssertNil ( ParseLiveQuery . client? . task)
627
+ if let socketEstablished = ParseLiveQuery . client? . isSocketEstablished {
628
+ XCTAssertFalse ( socketEstablished)
629
+ } else {
630
+ XCTFail ( " Should have socket that isn't established " )
631
+ }
632
+
633
+ //Resubscribe
634
+ do {
635
+ count += 1
636
+ subscription = try Query< GameScore> . subscribe( handler)
637
+ } catch {
638
+ XCTFail ( " \( error) " )
639
+ }
640
+
641
+ try ? self . pretendToBeConnected ( )
642
+
643
+ let response2 = PreliminaryMessageResponse ( op: . subscribed,
644
+ requestId: 2 ,
645
+ clientId: " yolo " ,
646
+ installationId: " naw " )
647
+ guard let encoded2 = try ? ParseCoding . jsonEncoder ( ) . encode ( response2) else {
648
+ expectation2. fulfill ( )
649
+ return
650
+ }
651
+ client. received ( encoded2)
652
+ }
653
+ }
654
+
655
+ XCTAssertFalse ( try client. isSubscribed ( query) )
656
+ XCTAssertTrue ( try client. isPendingSubscription ( query) )
657
+ XCTAssertEqual ( client. subscriptions. count, 0 )
658
+ XCTAssertEqual ( client. pendingSubscriptions. count, 1 )
659
+ try pretendToBeConnected ( )
660
+ let response = PreliminaryMessageResponse ( op: . subscribed,
661
+ requestId: 1 ,
662
+ clientId: " yolo " ,
663
+ installationId: " naw " )
664
+ let encoded = try ParseCoding . jsonEncoder ( ) . encode ( response)
665
+ client. received ( encoded)
666
+ XCTAssertTrue ( try client. isSubscribed ( query) )
667
+ XCTAssertFalse ( try client. isPendingSubscription ( query) )
668
+ XCTAssertEqual ( client. subscriptions. count, 1 )
669
+ XCTAssertEqual ( client. pendingSubscriptions. count, 0 )
670
+
671
+ wait ( for: [ expectation1, expectation2] , timeout: 20.0 )
672
+ }
673
+
587
674
func testServerRedirectResponse( ) throws {
588
675
guard let client = ParseLiveQuery . getDefault ( ) else {
589
676
XCTFail ( " Should be able to get client " )
0 commit comments