@@ -11,12 +11,30 @@ class StarWarsSubscriptionTests: XCTestCase {
1111 var client : ApolloClient !
1212 var webSocketTransport : WebSocketTransport !
1313
14+ var connectionStartedExpectation : XCTestExpectation ?
15+
1416 override func setUp( ) {
1517 super. setUp ( )
1618
19+ self . connectionStartedExpectation = self . expectation ( description: " Web socket connected " )
20+
1721 WebSocketTransport . provider = ApolloWebSocket . self
1822 webSocketTransport = WebSocketTransport ( request: URLRequest ( url: URL ( string: SERVER) !) )
23+ webSocketTransport. delegate = self
1924 client = ApolloClient ( networkTransport: webSocketTransport)
25+
26+ self . wait ( for: [ self . connectionStartedExpectation!] , timeout: 5 )
27+ }
28+
29+ private func waitForSubscriptionsToStart( for delay: TimeInterval = 0.1 , on queue: DispatchQueue = . main) {
30+ /// This method works around changes to the subscriptions package which mean that subscriptions do not start passing on data the absolute instant they are created.
31+ let waitExpectation = self . expectation ( description: " Waited! " )
32+
33+ queue. asyncAfter ( deadline: . now( ) + delay) {
34+ waitExpectation. fulfill ( )
35+ }
36+
37+ self . wait ( for: [ waitExpectation] , timeout: delay + 1 )
2038 }
2139
2240 // MARK: Subscriptions
@@ -45,6 +63,8 @@ class StarWarsSubscriptionTests: XCTestCase {
4563 }
4664 }
4765
66+ self . waitForSubscriptionsToStart ( )
67+
4868 client. perform ( mutation: CreateReviewForEpisodeMutation ( episode: . jedi, review: ReviewInput ( stars: 6 , commentary: " This is the greatest movie! " ) ) )
4969
5070 waitForExpectations ( timeout: 10 , handler: nil )
@@ -74,6 +94,8 @@ class StarWarsSubscriptionTests: XCTestCase {
7494 }
7595 }
7696
97+ self . waitForSubscriptionsToStart ( )
98+
7799 client. perform ( mutation: CreateReviewForEpisodeMutation ( episode: . empire, review: ReviewInput ( stars: 13 , commentary: " This is an even greater movie! " ) ) )
78100
79101 waitForExpectations ( timeout: 2 , handler: nil )
@@ -103,6 +125,8 @@ class StarWarsSubscriptionTests: XCTestCase {
103125 }
104126 }
105127
128+ self . waitForSubscriptionsToStart ( )
129+
106130 client. perform ( mutation: CreateReviewForEpisodeMutation ( episode: . empire, review: ReviewInput ( stars: 10 , commentary: " This is an even greater movie! " ) ) )
107131
108132 waitForExpectations ( timeout: 3 , handler: nil )
@@ -117,6 +141,8 @@ class StarWarsSubscriptionTests: XCTestCase {
117141 XCTFail ( " Received subscription after cancel " )
118142 }
119143
144+ self . waitForSubscriptionsToStart ( )
145+
120146 sub. cancel ( )
121147
122148 client. perform ( mutation: CreateReviewForEpisodeMutation ( episode: . jedi, review: ReviewInput ( stars: 10 , commentary: " This is an even greater movie! " ) ) )
@@ -147,6 +173,8 @@ class StarWarsSubscriptionTests: XCTestCase {
147173 XCTFail ( " Unexpected error: \( error) " )
148174 }
149175 }
176+
177+ self . waitForSubscriptionsToStart ( )
150178
151179 for i in 1 ... count {
152180 let review = ReviewInput ( stars: i, commentary: " The greatest movie ever! " )
@@ -223,6 +251,8 @@ class StarWarsSubscriptionTests: XCTestCase {
223251 newHopeFulfilledCount += 1
224252 }
225253
254+ self . waitForSubscriptionsToStart ( )
255+
226256 let episodes : [ Episode ] = [ . empire, . jedi, . newhope]
227257
228258 var selectedEpisodes = [ Episode] ( )
@@ -280,6 +310,8 @@ class StarWarsSubscriptionTests: XCTestCase {
280310 }
281311 }
282312
313+ self . waitForSubscriptionsToStart ( on: concurrentQueue)
314+
283315 // dispatched with a barrier flag to make sure
284316 // this is performed after subscription calls
285317 concurrentQueue. sync ( flags: . barrier) {
@@ -310,7 +342,9 @@ class StarWarsSubscriptionTests: XCTestCase {
310342 let sub2 = client. subscribe ( subscription: secondSubscription) { _ in
311343 invertedExpectation. fulfill ( )
312344 }
313-
345+
346+ self . waitForSubscriptionsToStart ( on: concurrentQueue)
347+
314348 concurrentQueue. async {
315349 sub1. cancel ( )
316350 expectation. fulfill ( )
@@ -337,6 +371,8 @@ class StarWarsSubscriptionTests: XCTestCase {
337371 invertedExpectation. fulfill ( )
338372 }
339373
374+ self . waitForSubscriptionsToStart ( on: concurrentQueue)
375+
340376 concurrentQueue. async {
341377 sub. cancel ( )
342378 }
@@ -373,3 +409,10 @@ class StarWarsSubscriptionTests: XCTestCase {
373409 waitForExpectations ( timeout: 10 , handler: nil )
374410 }
375411}
412+
413+ extension StarWarsSubscriptionTests : WebSocketTransportDelegate {
414+
415+ func webSocketTransportDidConnect( _ webSocketTransport: WebSocketTransport ) {
416+ self . connectionStartedExpectation? . fulfill ( )
417+ }
418+ }
0 commit comments