Skip to content

Commit c470e17

Browse files
test: streaming test uses dynamic wait
1 parent 31451f9 commit c470e17

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Tests/GraphQLTransportWSTests/GraphQLTransportWSTests.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class GraphqlTransportWSTests: XCTestCase {
1010
var serverMessenger: TestMessenger!
1111
var server: Server<TokenInitPayload, AsyncThrowingStream<GraphQLResult, Error>>!
1212
var context: TestContext!
13+
var subscribeReady: Bool! = false
1314

1415
override func setUp() {
1516
// Point the client and server at each other
@@ -30,10 +31,12 @@ class GraphqlTransportWSTests: XCTestCase {
3031
)
3132
},
3233
onSubscribe: { graphQLRequest in
33-
try await api.subscribe(
34+
let subscription = try await api.subscribe(
3435
request: graphQLRequest.query,
3536
context: context
3637
).get()
38+
self.subscribeReady = true
39+
return subscription
3740
}
3841
)
3942
self.context = context
@@ -172,8 +175,15 @@ class GraphqlTransportWSTests: XCTestCase {
172175
id: id
173176
)
174177

175-
// Short sleep to allow for server to register subscription
176-
usleep(3000)
178+
// Wait until server has registered subscription
179+
var i = 0
180+
while !self.subscribeReady, i < 50 {
181+
usleep(1000)
182+
i = i + 1
183+
}
184+
if i == 50 {
185+
XCTFail("Subscription timeout: Took longer than 50ms to set up")
186+
}
177187

178188
self.context.publisher.emit(event: "hello \(dataIndex)")
179189
}

0 commit comments

Comments
 (0)