@@ -68,6 +68,7 @@ class ParseLiveQueryTests: XCTestCase {
68
68
try KeychainStore . shared. deleteAll ( )
69
69
#endif
70
70
try ParseStorage . shared. deleteAll ( )
71
+ URLSession . liveQuery. closeAll ( )
71
72
}
72
73
73
74
func testWebsocketURL( ) throws {
@@ -89,10 +90,9 @@ class ParseLiveQueryTests: XCTestCase {
89
90
XCTAssertTrue ( client. url. absoluteString. contains ( " ws " ) )
90
91
91
92
let expectation1 = XCTestExpectation ( description: " Socket delegate " )
92
- client. synchronizationQueue. async {
93
+ client. synchronizationQueue. asyncAfter ( deadline : . now ( ) + 2 ) {
93
94
let socketDelegates = URLSession . liveQuery. delegates
94
95
XCTAssertNotNil ( socketDelegates [ client. task] )
95
- client. close ( )
96
96
expectation1. fulfill ( )
97
97
}
98
98
wait ( for: [ expectation1] , timeout: 20.0 )
@@ -118,10 +118,9 @@ class ParseLiveQueryTests: XCTestCase {
118
118
XCTAssertTrue ( client. url. absoluteString. contains ( " ws " ) )
119
119
XCTAssertNotEqual ( client, defaultClient)
120
120
let expectation1 = XCTestExpectation ( description: " Socket delegate " )
121
- client. synchronizationQueue. async {
121
+ client. synchronizationQueue. asyncAfter ( deadline : . now ( ) + 2 ) {
122
122
let socketDelegates = URLSession . liveQuery. delegates
123
123
XCTAssertNotNil ( socketDelegates [ client. task] )
124
- client. close ( )
125
124
expectation1. fulfill ( )
126
125
}
127
126
wait ( for: [ expectation1] , timeout: 20.0 )
@@ -138,10 +137,9 @@ class ParseLiveQueryTests: XCTestCase {
138
137
XCTAssertTrue ( client. url. absoluteString. contains ( " ws " ) )
139
138
XCTAssertEqual ( client, defaultClient)
140
139
let expectation1 = XCTestExpectation ( description: " Socket delegate " )
141
- client. synchronizationQueue. async {
140
+ client. synchronizationQueue. asyncAfter ( deadline : . now ( ) + 2 ) {
142
141
let socketDelegates = URLSession . liveQuery. delegates
143
142
XCTAssertNotNil ( socketDelegates [ client. task] )
144
- client. close ( )
145
143
expectation1. fulfill ( )
146
144
}
147
145
wait ( for: [ expectation1] , timeout: 20.0 )
@@ -162,10 +160,9 @@ class ParseLiveQueryTests: XCTestCase {
162
160
client = nil
163
161
XCTAssertNotNil ( ParseLiveQuery . getDefault ( ) )
164
162
let expectation1 = XCTestExpectation ( description: " Socket delegate " )
165
- defaultClient. synchronizationQueue. async {
163
+ defaultClient. synchronizationQueue. asyncAfter ( deadline : . now ( ) + 2 ) {
166
164
let socketDelegates = URLSession . liveQuery. delegates
167
165
XCTAssertNotNil ( socketDelegates [ defaultClient. task] )
168
- defaultClient. close ( )
169
166
expectation1. fulfill ( )
170
167
}
171
168
wait ( for: [ expectation1] , timeout: 20.0 )
@@ -189,7 +186,6 @@ class ParseLiveQueryTests: XCTestCase {
189
186
XCTAssertNotNil ( URLSession . liveQuery. authenticationDelegate)
190
187
client. authenticationDelegate = nil
191
188
XCTAssertNil ( URLSession . liveQuery. authenticationDelegate)
192
- client. close ( )
193
189
}
194
190
195
191
func testStandardMessageEncoding( ) throws {
@@ -324,7 +320,6 @@ class ParseLiveQueryTests: XCTestCase {
324
320
client. isConnected = true
325
321
XCTAssertEqual ( client. isConnecting, false )
326
322
XCTAssertEqual ( client. isConnected, false )
327
- client. close ( )
328
323
}
329
324
330
325
func testConnectedState( ) throws {
@@ -356,7 +351,6 @@ class ParseLiveQueryTests: XCTestCase {
356
351
XCTAssertEqual ( client. isConnecting, false )
357
352
XCTAssertEqual ( client. clientId, " yolo " )
358
353
XCTAssertEqual ( client. attempts, ParseLiveQueryConstants . maxConnectionAttempts + 1 )
359
- client. close ( )
360
354
}
361
355
362
356
func testDisconnectedState( ) throws {
@@ -378,7 +372,6 @@ class ParseLiveQueryTests: XCTestCase {
378
372
XCTAssertEqual ( client. isConnected, false )
379
373
XCTAssertEqual ( client. isConnecting, false )
380
374
XCTAssertNil ( client. clientId)
381
- client. close ( )
382
375
}
383
376
384
377
func testSocketDisconnectedState( ) throws {
@@ -399,7 +392,6 @@ class ParseLiveQueryTests: XCTestCase {
399
392
XCTAssertEqual ( client. isConnected, false )
400
393
XCTAssertEqual ( client. isConnecting, false )
401
394
XCTAssertNil ( client. clientId)
402
- client. close ( )
403
395
}
404
396
405
397
func testUserClosedConnectionState( ) throws {
@@ -424,7 +416,6 @@ class ParseLiveQueryTests: XCTestCase {
424
416
XCTAssertEqual ( client. isConnecting, false )
425
417
XCTAssertNil ( client. clientId)
426
418
XCTAssertEqual ( client. isDisconnectedByUser, true )
427
- client. close ( )
428
419
}
429
420
430
421
func testOpenSocket( ) throws {
@@ -437,7 +428,6 @@ class ParseLiveQueryTests: XCTestCase {
437
428
client. open ( isUserWantsToConnect: true ) { error in
438
429
XCTAssertNotNil ( error) //Should always fail since WS isn't intercepted.
439
430
expectation1. fulfill ( )
440
- client. close ( )
441
431
}
442
432
wait ( for: [ expectation1] , timeout: 20.0 )
443
433
}
@@ -457,7 +447,6 @@ class ParseLiveQueryTests: XCTestCase {
457
447
XCTAssertEqual ( delegate. code, . goingAway)
458
448
XCTAssertNil ( delegate. reason)
459
449
XCTAssertTrue ( client. task. state == . completed)
460
- client. close ( )
461
450
expectation1. fulfill ( )
462
451
}
463
452
wait ( for: [ expectation1] , timeout: 20.0 )
@@ -526,11 +515,21 @@ class ParseLiveQueryTests: XCTestCase {
526
515
527
516
func testCloseAll( ) throws {
528
517
let client = try ParseLiveQuery ( )
518
+ guard let originalTask = client. task else {
519
+ XCTFail ( " Should not be nil " )
520
+ return
521
+ }
529
522
XCTAssertTrue ( client. task. state == . running)
523
+ client. isSocketEstablished = true
524
+ client. isConnected = true
530
525
client. closeAll ( )
531
526
let expectation1 = XCTestExpectation ( description: " Close all " )
532
527
client. synchronizationQueue. asyncAfter ( deadline: . now( ) + 2 ) {
533
528
XCTAssertTrue ( client. task. state == . suspended)
529
+ XCTAssertFalse ( client. isSocketEstablished)
530
+ XCTAssertFalse ( client. isConnected)
531
+ XCTAssertNil ( URLSession . liveQuery. delegates [ originalTask] )
532
+ XCTAssertNotNil ( URLSession . liveQuery. delegates [ client. task] )
534
533
expectation1. fulfill ( )
535
534
}
536
535
wait ( for: [ expectation1] , timeout: 20.0 )
@@ -552,7 +551,6 @@ class ParseLiveQueryTests: XCTestCase {
552
551
}
553
552
XCTAssertEqual ( parseError. code, ParseError . Code. unknownError)
554
553
XCTAssertTrue ( parseError. message. contains ( " socket status " ) )
555
- client. close ( )
556
554
expectation1. fulfill ( )
557
555
}
558
556
wait ( for: [ expectation1] , timeout: 20.0 )
@@ -572,7 +570,6 @@ class ParseLiveQueryTests: XCTestCase {
572
570
client. sendPing { error in
573
571
XCTAssertEqual ( client. isSocketEstablished, true )
574
572
XCTAssertNotNil ( error) // Should have error because testcases don't intercept websocket
575
- client. close ( )
576
573
expectation1. fulfill ( )
577
574
}
578
575
wait ( for: [ expectation1] , timeout: 20.0 )
@@ -589,7 +586,6 @@ class ParseLiveQueryTests: XCTestCase {
589
586
XCTAssertGreaterThan ( time, - 1 )
590
587
client. attempts += index
591
588
}
592
- client. close ( )
593
589
}
594
590
595
591
func testRandomIdGenerator( ) throws {
@@ -601,7 +597,6 @@ class ParseLiveQueryTests: XCTestCase {
601
597
let idGenerated = client. requestIdGenerator ( )
602
598
XCTAssertEqual ( idGenerated. value, index)
603
599
}
604
- client. close ( )
605
600
}
606
601
607
602
func testSubscribeNotConnected( ) throws {
@@ -621,7 +616,6 @@ class ParseLiveQueryTests: XCTestCase {
621
616
XCTAssertEqual ( client. pendingSubscriptions. count, 1 )
622
617
XCTAssertNoThrow ( try client. removePendingSubscription ( query) )
623
618
XCTAssertEqual ( client. pendingSubscriptions. count, 0 )
624
- client. close ( )
625
619
}
626
620
627
621
func pretendToBeConnected( ) throws {
@@ -675,7 +669,6 @@ class ParseLiveQueryTests: XCTestCase {
675
669
XCTAssertEqual ( query, unsubscribed)
676
670
XCTAssertNil ( subscription. subscribed)
677
671
XCTAssertNil ( subscription. event)
678
- client. close ( )
679
672
expectation2. fulfill ( )
680
673
}
681
674
@@ -742,7 +735,6 @@ class ParseLiveQueryTests: XCTestCase {
742
735
XCTAssertTrue ( client. pendingSubscriptions. isEmpty)
743
736
XCTAssertTrue ( client. subscriptions. isEmpty)
744
737
XCTAssertFalse ( client. isSocketEstablished)
745
- client. close ( )
746
738
expectation2. fulfill ( )
747
739
}
748
740
XCTAssertNotNil ( try ? query. unsubscribe ( ) )
@@ -810,7 +802,6 @@ class ParseLiveQueryTests: XCTestCase {
810
802
} else {
811
803
XCTAssertNotNil ( ParseLiveQuery . client? . task)
812
804
XCTAssertFalse ( originalTask == ParseLiveQuery . client? . task)
813
- client. close ( )
814
805
expectation2. fulfill ( )
815
806
return
816
807
}
@@ -883,7 +874,6 @@ class ParseLiveQueryTests: XCTestCase {
883
874
let encoded = try ParseCoding . jsonEncoder ( ) . encode ( response)
884
875
client. received ( encoded)
885
876
XCTAssertEqual ( client. url, url)
886
- client. close ( )
887
877
}
888
878
889
879
func testServerErrorResponse( ) throws {
@@ -908,7 +898,6 @@ class ParseLiveQueryTests: XCTestCase {
908
898
XCTAssertNotNil ( delegate. error)
909
899
XCTAssertEqual ( delegate. error? . code, ParseError . Code. internalServer)
910
900
XCTAssertTrue ( delegate. error? . message. contains ( " message " ) != nil )
911
- client. close ( )
912
901
expectation1. fulfill ( )
913
902
}
914
903
wait ( for: [ expectation1] , timeout: 20.0 )
@@ -939,11 +928,10 @@ class ParseLiveQueryTests: XCTestCase {
939
928
expectation1. fulfill ( )
940
929
}
941
930
let expectation2 = XCTestExpectation ( description: " Client closed " )
942
- client. synchronizationQueue. async {
931
+ client. synchronizationQueue. asyncAfter ( deadline : . now ( ) + 2 ) {
943
932
XCTAssertTrue ( client. isDisconnectedByUser)
944
933
XCTAssertFalse ( client. isConnected)
945
934
XCTAssertFalse ( client. isConnecting)
946
- client. close ( )
947
935
expectation2. fulfill ( )
948
936
}
949
937
wait ( for: [ expectation1, expectation2] , timeout: 20.0 )
@@ -980,7 +968,6 @@ class ParseLiveQueryTests: XCTestCase {
980
968
default :
981
969
XCTFail ( " Should have receeived event " )
982
970
}
983
- client. close ( )
984
971
expectation1. fulfill ( )
985
972
}
986
973
@@ -1033,7 +1020,6 @@ class ParseLiveQueryTests: XCTestCase {
1033
1020
default :
1034
1021
XCTFail ( " Should have receeived event " )
1035
1022
}
1036
- client. close ( )
1037
1023
expectation1. fulfill ( )
1038
1024
}
1039
1025
@@ -1086,7 +1072,6 @@ class ParseLiveQueryTests: XCTestCase {
1086
1072
default :
1087
1073
XCTFail ( " Should have receeived event " )
1088
1074
}
1089
- client. close ( )
1090
1075
expectation1. fulfill ( )
1091
1076
}
1092
1077
@@ -1139,7 +1124,6 @@ class ParseLiveQueryTests: XCTestCase {
1139
1124
default :
1140
1125
XCTFail ( " Should have receeived event " )
1141
1126
}
1142
- client. close ( )
1143
1127
expectation1. fulfill ( )
1144
1128
}
1145
1129
@@ -1192,7 +1176,6 @@ class ParseLiveQueryTests: XCTestCase {
1192
1176
default :
1193
1177
XCTFail ( " Should have receeived event " )
1194
1178
}
1195
- client. close ( )
1196
1179
expectation1. fulfill ( )
1197
1180
}
1198
1181
@@ -1257,7 +1240,6 @@ class ParseLiveQueryTests: XCTestCase {
1257
1240
XCTAssertFalse ( subscribed. isNew)
1258
1241
XCTAssertEqual ( client. subscriptions. count, 1 )
1259
1242
XCTAssertEqual ( client. pendingSubscriptions. count, 0 )
1260
- client. close ( )
1261
1243
expectation2. fulfill ( )
1262
1244
return
1263
1245
}
@@ -1356,7 +1338,6 @@ class ParseLiveQueryTests: XCTestCase {
1356
1338
XCTAssertNil ( subscription. unsubscribed)
1357
1339
XCTAssertEqual ( client. subscriptions. count, 1 )
1358
1340
XCTAssertEqual ( client. pendingSubscriptions. count, 0 )
1359
- client. close ( )
1360
1341
expectation2. fulfill ( )
1361
1342
return
1362
1343
}
@@ -1414,7 +1395,6 @@ class ParseLiveQueryTests: XCTestCase {
1414
1395
default :
1415
1396
XCTFail ( " Should have receeived event " )
1416
1397
}
1417
- client. close ( )
1418
1398
expectation1. fulfill ( )
1419
1399
}
1420
1400
@@ -1458,7 +1438,6 @@ class ParseLiveQueryTests: XCTestCase {
1458
1438
default :
1459
1439
XCTFail ( " Should have receeived event " )
1460
1440
}
1461
- client. close ( )
1462
1441
expectation1. fulfill ( )
1463
1442
}
1464
1443
@@ -1502,7 +1481,6 @@ class ParseLiveQueryTests: XCTestCase {
1502
1481
default :
1503
1482
XCTFail ( " Should have receeived event " )
1504
1483
}
1505
- client. close ( )
1506
1484
expectation1. fulfill ( )
1507
1485
}
1508
1486
@@ -1546,7 +1524,6 @@ class ParseLiveQueryTests: XCTestCase {
1546
1524
default :
1547
1525
XCTFail ( " Should have receeived event " )
1548
1526
}
1549
- client. close ( )
1550
1527
expectation1. fulfill ( )
1551
1528
}
1552
1529
@@ -1590,7 +1567,6 @@ class ParseLiveQueryTests: XCTestCase {
1590
1567
default :
1591
1568
XCTFail ( " Should have receeived event " )
1592
1569
}
1593
- client. close ( )
1594
1570
expectation1. fulfill ( )
1595
1571
}
1596
1572
@@ -1635,7 +1611,6 @@ class ParseLiveQueryTests: XCTestCase {
1635
1611
XCTAssertFalse ( isNew)
1636
1612
XCTAssertEqual ( client. subscriptions. count, 1 )
1637
1613
XCTAssertEqual ( client. pendingSubscriptions. count, 0 )
1638
- client. close ( )
1639
1614
expectation2. fulfill ( )
1640
1615
return
1641
1616
}
@@ -1706,7 +1681,6 @@ class ParseLiveQueryTests: XCTestCase {
1706
1681
XCTAssertTrue ( isNew)
1707
1682
XCTAssertEqual ( client. subscriptions. count, 1 )
1708
1683
XCTAssertEqual ( client. pendingSubscriptions. count, 0 )
1709
- client. close ( )
1710
1684
expectation2. fulfill ( )
1711
1685
return
1712
1686
}
0 commit comments