@@ -68,6 +68,7 @@ class ParseLiveQueryTests: XCTestCase {
6868 try KeychainStore . shared. deleteAll ( )
6969 #endif
7070 try ParseStorage . shared. deleteAll ( )
71+ URLSession . liveQuery. closeAll ( )
7172 }
7273
7374 func testWebsocketURL( ) throws {
@@ -89,10 +90,9 @@ class ParseLiveQueryTests: XCTestCase {
8990 XCTAssertTrue ( client. url. absoluteString. contains ( " ws " ) )
9091
9192 let expectation1 = XCTestExpectation ( description: " Socket delegate " )
92- client. synchronizationQueue. async {
93+ client. synchronizationQueue. asyncAfter ( deadline : . now ( ) + 2 ) {
9394 let socketDelegates = URLSession . liveQuery. delegates
9495 XCTAssertNotNil ( socketDelegates [ client. task] )
95- client. close ( )
9696 expectation1. fulfill ( )
9797 }
9898 wait ( for: [ expectation1] , timeout: 20.0 )
@@ -118,10 +118,9 @@ class ParseLiveQueryTests: XCTestCase {
118118 XCTAssertTrue ( client. url. absoluteString. contains ( " ws " ) )
119119 XCTAssertNotEqual ( client, defaultClient)
120120 let expectation1 = XCTestExpectation ( description: " Socket delegate " )
121- client. synchronizationQueue. async {
121+ client. synchronizationQueue. asyncAfter ( deadline : . now ( ) + 2 ) {
122122 let socketDelegates = URLSession . liveQuery. delegates
123123 XCTAssertNotNil ( socketDelegates [ client. task] )
124- client. close ( )
125124 expectation1. fulfill ( )
126125 }
127126 wait ( for: [ expectation1] , timeout: 20.0 )
@@ -138,10 +137,9 @@ class ParseLiveQueryTests: XCTestCase {
138137 XCTAssertTrue ( client. url. absoluteString. contains ( " ws " ) )
139138 XCTAssertEqual ( client, defaultClient)
140139 let expectation1 = XCTestExpectation ( description: " Socket delegate " )
141- client. synchronizationQueue. async {
140+ client. synchronizationQueue. asyncAfter ( deadline : . now ( ) + 2 ) {
142141 let socketDelegates = URLSession . liveQuery. delegates
143142 XCTAssertNotNil ( socketDelegates [ client. task] )
144- client. close ( )
145143 expectation1. fulfill ( )
146144 }
147145 wait ( for: [ expectation1] , timeout: 20.0 )
@@ -162,10 +160,9 @@ class ParseLiveQueryTests: XCTestCase {
162160 client = nil
163161 XCTAssertNotNil ( ParseLiveQuery . getDefault ( ) )
164162 let expectation1 = XCTestExpectation ( description: " Socket delegate " )
165- defaultClient. synchronizationQueue. async {
163+ defaultClient. synchronizationQueue. asyncAfter ( deadline : . now ( ) + 2 ) {
166164 let socketDelegates = URLSession . liveQuery. delegates
167165 XCTAssertNotNil ( socketDelegates [ defaultClient. task] )
168- defaultClient. close ( )
169166 expectation1. fulfill ( )
170167 }
171168 wait ( for: [ expectation1] , timeout: 20.0 )
@@ -189,7 +186,6 @@ class ParseLiveQueryTests: XCTestCase {
189186 XCTAssertNotNil ( URLSession . liveQuery. authenticationDelegate)
190187 client. authenticationDelegate = nil
191188 XCTAssertNil ( URLSession . liveQuery. authenticationDelegate)
192- client. close ( )
193189 }
194190
195191 func testStandardMessageEncoding( ) throws {
@@ -324,7 +320,6 @@ class ParseLiveQueryTests: XCTestCase {
324320 client. isConnected = true
325321 XCTAssertEqual ( client. isConnecting, false )
326322 XCTAssertEqual ( client. isConnected, false )
327- client. close ( )
328323 }
329324
330325 func testConnectedState( ) throws {
@@ -356,7 +351,6 @@ class ParseLiveQueryTests: XCTestCase {
356351 XCTAssertEqual ( client. isConnecting, false )
357352 XCTAssertEqual ( client. clientId, " yolo " )
358353 XCTAssertEqual ( client. attempts, ParseLiveQueryConstants . maxConnectionAttempts + 1 )
359- client. close ( )
360354 }
361355
362356 func testDisconnectedState( ) throws {
@@ -378,7 +372,6 @@ class ParseLiveQueryTests: XCTestCase {
378372 XCTAssertEqual ( client. isConnected, false )
379373 XCTAssertEqual ( client. isConnecting, false )
380374 XCTAssertNil ( client. clientId)
381- client. close ( )
382375 }
383376
384377 func testSocketDisconnectedState( ) throws {
@@ -399,7 +392,6 @@ class ParseLiveQueryTests: XCTestCase {
399392 XCTAssertEqual ( client. isConnected, false )
400393 XCTAssertEqual ( client. isConnecting, false )
401394 XCTAssertNil ( client. clientId)
402- client. close ( )
403395 }
404396
405397 func testUserClosedConnectionState( ) throws {
@@ -424,7 +416,6 @@ class ParseLiveQueryTests: XCTestCase {
424416 XCTAssertEqual ( client. isConnecting, false )
425417 XCTAssertNil ( client. clientId)
426418 XCTAssertEqual ( client. isDisconnectedByUser, true )
427- client. close ( )
428419 }
429420
430421 func testOpenSocket( ) throws {
@@ -437,7 +428,6 @@ class ParseLiveQueryTests: XCTestCase {
437428 client. open ( isUserWantsToConnect: true ) { error in
438429 XCTAssertNotNil ( error) //Should always fail since WS isn't intercepted.
439430 expectation1. fulfill ( )
440- client. close ( )
441431 }
442432 wait ( for: [ expectation1] , timeout: 20.0 )
443433 }
@@ -457,7 +447,6 @@ class ParseLiveQueryTests: XCTestCase {
457447 XCTAssertEqual ( delegate. code, . goingAway)
458448 XCTAssertNil ( delegate. reason)
459449 XCTAssertTrue ( client. task. state == . completed)
460- client. close ( )
461450 expectation1. fulfill ( )
462451 }
463452 wait ( for: [ expectation1] , timeout: 20.0 )
@@ -526,11 +515,21 @@ class ParseLiveQueryTests: XCTestCase {
526515
527516 func testCloseAll( ) throws {
528517 let client = try ParseLiveQuery ( )
518+ guard let originalTask = client. task else {
519+ XCTFail ( " Should not be nil " )
520+ return
521+ }
529522 XCTAssertTrue ( client. task. state == . running)
523+ client. isSocketEstablished = true
524+ client. isConnected = true
530525 client. closeAll ( )
531526 let expectation1 = XCTestExpectation ( description: " Close all " )
532527 client. synchronizationQueue. asyncAfter ( deadline: . now( ) + 2 ) {
533528 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] )
534533 expectation1. fulfill ( )
535534 }
536535 wait ( for: [ expectation1] , timeout: 20.0 )
@@ -552,7 +551,6 @@ class ParseLiveQueryTests: XCTestCase {
552551 }
553552 XCTAssertEqual ( parseError. code, ParseError . Code. unknownError)
554553 XCTAssertTrue ( parseError. message. contains ( " socket status " ) )
555- client. close ( )
556554 expectation1. fulfill ( )
557555 }
558556 wait ( for: [ expectation1] , timeout: 20.0 )
@@ -572,7 +570,6 @@ class ParseLiveQueryTests: XCTestCase {
572570 client. sendPing { error in
573571 XCTAssertEqual ( client. isSocketEstablished, true )
574572 XCTAssertNotNil ( error) // Should have error because testcases don't intercept websocket
575- client. close ( )
576573 expectation1. fulfill ( )
577574 }
578575 wait ( for: [ expectation1] , timeout: 20.0 )
@@ -589,7 +586,6 @@ class ParseLiveQueryTests: XCTestCase {
589586 XCTAssertGreaterThan ( time, - 1 )
590587 client. attempts += index
591588 }
592- client. close ( )
593589 }
594590
595591 func testRandomIdGenerator( ) throws {
@@ -601,7 +597,6 @@ class ParseLiveQueryTests: XCTestCase {
601597 let idGenerated = client. requestIdGenerator ( )
602598 XCTAssertEqual ( idGenerated. value, index)
603599 }
604- client. close ( )
605600 }
606601
607602 func testSubscribeNotConnected( ) throws {
@@ -621,7 +616,6 @@ class ParseLiveQueryTests: XCTestCase {
621616 XCTAssertEqual ( client. pendingSubscriptions. count, 1 )
622617 XCTAssertNoThrow ( try client. removePendingSubscription ( query) )
623618 XCTAssertEqual ( client. pendingSubscriptions. count, 0 )
624- client. close ( )
625619 }
626620
627621 func pretendToBeConnected( ) throws {
@@ -675,7 +669,6 @@ class ParseLiveQueryTests: XCTestCase {
675669 XCTAssertEqual ( query, unsubscribed)
676670 XCTAssertNil ( subscription. subscribed)
677671 XCTAssertNil ( subscription. event)
678- client. close ( )
679672 expectation2. fulfill ( )
680673 }
681674
@@ -742,7 +735,6 @@ class ParseLiveQueryTests: XCTestCase {
742735 XCTAssertTrue ( client. pendingSubscriptions. isEmpty)
743736 XCTAssertTrue ( client. subscriptions. isEmpty)
744737 XCTAssertFalse ( client. isSocketEstablished)
745- client. close ( )
746738 expectation2. fulfill ( )
747739 }
748740 XCTAssertNotNil ( try ? query. unsubscribe ( ) )
@@ -810,7 +802,6 @@ class ParseLiveQueryTests: XCTestCase {
810802 } else {
811803 XCTAssertNotNil ( ParseLiveQuery . client? . task)
812804 XCTAssertFalse ( originalTask == ParseLiveQuery . client? . task)
813- client. close ( )
814805 expectation2. fulfill ( )
815806 return
816807 }
@@ -883,7 +874,6 @@ class ParseLiveQueryTests: XCTestCase {
883874 let encoded = try ParseCoding . jsonEncoder ( ) . encode ( response)
884875 client. received ( encoded)
885876 XCTAssertEqual ( client. url, url)
886- client. close ( )
887877 }
888878
889879 func testServerErrorResponse( ) throws {
@@ -908,7 +898,6 @@ class ParseLiveQueryTests: XCTestCase {
908898 XCTAssertNotNil ( delegate. error)
909899 XCTAssertEqual ( delegate. error? . code, ParseError . Code. internalServer)
910900 XCTAssertTrue ( delegate. error? . message. contains ( " message " ) != nil )
911- client. close ( )
912901 expectation1. fulfill ( )
913902 }
914903 wait ( for: [ expectation1] , timeout: 20.0 )
@@ -939,11 +928,10 @@ class ParseLiveQueryTests: XCTestCase {
939928 expectation1. fulfill ( )
940929 }
941930 let expectation2 = XCTestExpectation ( description: " Client closed " )
942- client. synchronizationQueue. async {
931+ client. synchronizationQueue. asyncAfter ( deadline : . now ( ) + 2 ) {
943932 XCTAssertTrue ( client. isDisconnectedByUser)
944933 XCTAssertFalse ( client. isConnected)
945934 XCTAssertFalse ( client. isConnecting)
946- client. close ( )
947935 expectation2. fulfill ( )
948936 }
949937 wait ( for: [ expectation1, expectation2] , timeout: 20.0 )
@@ -980,7 +968,6 @@ class ParseLiveQueryTests: XCTestCase {
980968 default :
981969 XCTFail ( " Should have receeived event " )
982970 }
983- client. close ( )
984971 expectation1. fulfill ( )
985972 }
986973
@@ -1033,7 +1020,6 @@ class ParseLiveQueryTests: XCTestCase {
10331020 default :
10341021 XCTFail ( " Should have receeived event " )
10351022 }
1036- client. close ( )
10371023 expectation1. fulfill ( )
10381024 }
10391025
@@ -1086,7 +1072,6 @@ class ParseLiveQueryTests: XCTestCase {
10861072 default :
10871073 XCTFail ( " Should have receeived event " )
10881074 }
1089- client. close ( )
10901075 expectation1. fulfill ( )
10911076 }
10921077
@@ -1139,7 +1124,6 @@ class ParseLiveQueryTests: XCTestCase {
11391124 default :
11401125 XCTFail ( " Should have receeived event " )
11411126 }
1142- client. close ( )
11431127 expectation1. fulfill ( )
11441128 }
11451129
@@ -1192,7 +1176,6 @@ class ParseLiveQueryTests: XCTestCase {
11921176 default :
11931177 XCTFail ( " Should have receeived event " )
11941178 }
1195- client. close ( )
11961179 expectation1. fulfill ( )
11971180 }
11981181
@@ -1257,7 +1240,6 @@ class ParseLiveQueryTests: XCTestCase {
12571240 XCTAssertFalse ( subscribed. isNew)
12581241 XCTAssertEqual ( client. subscriptions. count, 1 )
12591242 XCTAssertEqual ( client. pendingSubscriptions. count, 0 )
1260- client. close ( )
12611243 expectation2. fulfill ( )
12621244 return
12631245 }
@@ -1356,7 +1338,6 @@ class ParseLiveQueryTests: XCTestCase {
13561338 XCTAssertNil ( subscription. unsubscribed)
13571339 XCTAssertEqual ( client. subscriptions. count, 1 )
13581340 XCTAssertEqual ( client. pendingSubscriptions. count, 0 )
1359- client. close ( )
13601341 expectation2. fulfill ( )
13611342 return
13621343 }
@@ -1414,7 +1395,6 @@ class ParseLiveQueryTests: XCTestCase {
14141395 default :
14151396 XCTFail ( " Should have receeived event " )
14161397 }
1417- client. close ( )
14181398 expectation1. fulfill ( )
14191399 }
14201400
@@ -1458,7 +1438,6 @@ class ParseLiveQueryTests: XCTestCase {
14581438 default :
14591439 XCTFail ( " Should have receeived event " )
14601440 }
1461- client. close ( )
14621441 expectation1. fulfill ( )
14631442 }
14641443
@@ -1502,7 +1481,6 @@ class ParseLiveQueryTests: XCTestCase {
15021481 default :
15031482 XCTFail ( " Should have receeived event " )
15041483 }
1505- client. close ( )
15061484 expectation1. fulfill ( )
15071485 }
15081486
@@ -1546,7 +1524,6 @@ class ParseLiveQueryTests: XCTestCase {
15461524 default :
15471525 XCTFail ( " Should have receeived event " )
15481526 }
1549- client. close ( )
15501527 expectation1. fulfill ( )
15511528 }
15521529
@@ -1590,7 +1567,6 @@ class ParseLiveQueryTests: XCTestCase {
15901567 default :
15911568 XCTFail ( " Should have receeived event " )
15921569 }
1593- client. close ( )
15941570 expectation1. fulfill ( )
15951571 }
15961572
@@ -1635,7 +1611,6 @@ class ParseLiveQueryTests: XCTestCase {
16351611 XCTAssertFalse ( isNew)
16361612 XCTAssertEqual ( client. subscriptions. count, 1 )
16371613 XCTAssertEqual ( client. pendingSubscriptions. count, 0 )
1638- client. close ( )
16391614 expectation2. fulfill ( )
16401615 return
16411616 }
@@ -1706,7 +1681,6 @@ class ParseLiveQueryTests: XCTestCase {
17061681 XCTAssertTrue ( isNew)
17071682 XCTAssertEqual ( client. subscriptions. count, 1 )
17081683 XCTAssertEqual ( client. pendingSubscriptions. count, 0 )
1709- client. close ( )
17101684 expectation2. fulfill ( )
17111685 return
17121686 }
0 commit comments