Skip to content

Commit edf83a9

Browse files
committed
Updated tests to Future and NIO
1 parent 7b8bec8 commit edf83a9

File tree

7 files changed

+490
-300
lines changed

7 files changed

+490
-300
lines changed

Tests/GraphQLTests/FieldExecutionStrategyTests/FieldExecutionStrategyTests.swift

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Dispatch
22
import GraphQL
33
import XCTest
4+
import NIO
45

56
class FieldExecutionStrategyTests: XCTestCase {
67

@@ -14,14 +15,14 @@ class FieldExecutionStrategyTests: XCTestCase {
1415
fields: [
1516
"sleep": GraphQLField(
1617
type: GraphQLString,
17-
resolve: { _, _, _, _ in
18+
resolve: { _, _, eventLoopGroup, _ in
1819
let g = DispatchGroup()
1920
g.enter()
2021
DispatchQueue.global().asyncAfter(wallDeadline: .now() + 0.1) {
2122
g.leave()
2223
}
2324
g.wait()
24-
return "z"
25+
return eventLoopGroup.next().newSucceededFuture(result: "z")
2526
}
2627
),
2728
"bang": GraphQLField(
@@ -179,41 +180,65 @@ class FieldExecutionStrategyTests: XCTestCase {
179180
}
180181

181182
func testSerialFieldExecutionStrategyWithSingleField() throws {
183+
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
184+
defer {
185+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
186+
}
187+
182188
let result = try timing(try graphql(
183189
queryStrategy: SerialFieldExecutionStrategy(),
184190
schema: schema,
185-
request: singleQuery
186-
))
191+
request: singleQuery,
192+
eventLoopGroup: eventLoopGroup
193+
).wait())
187194
XCTAssertEqual(result.value, singleExpected)
188195
//XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
189196
}
190197

191198
func testSerialFieldExecutionStrategyWithSingleFieldError() throws {
199+
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
200+
defer {
201+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
202+
}
203+
192204
let result = try timing(try graphql(
193205
queryStrategy: SerialFieldExecutionStrategy(),
194206
schema: schema,
195-
request: singleThrowsQuery
196-
))
207+
request: singleThrowsQuery,
208+
eventLoopGroup: eventLoopGroup
209+
).wait())
197210
XCTAssertEqual(result.value, singleThrowsExpected)
198211
//XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
199212
}
200213

201214
func testSerialFieldExecutionStrategyWithMultipleFields() throws {
215+
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
216+
defer {
217+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
218+
}
219+
202220
let result = try timing(try graphql(
203221
queryStrategy: SerialFieldExecutionStrategy(),
204222
schema: schema,
205-
request: multiQuery
206-
))
223+
request: multiQuery,
224+
eventLoopGroup: eventLoopGroup
225+
).wait())
207226
XCTAssertEqual(result.value, multiExpected)
208227
//XCTAssertEqualWithAccuracy(1.0, result.seconds, accuracy: 0.5)
209228
}
210229

211230
func testSerialFieldExecutionStrategyWithMultipleFieldErrors() throws {
231+
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
232+
defer {
233+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
234+
}
235+
212236
let result = try timing(try graphql(
213237
queryStrategy: SerialFieldExecutionStrategy(),
214238
schema: schema,
215-
request: multiThrowsQuery
216-
))
239+
request: multiThrowsQuery,
240+
eventLoopGroup: eventLoopGroup
241+
).wait())
217242
XCTAssertEqual(result.value["data"], multiThrowsExpectedData)
218243
let resultErrors = try result.value["errors"].asArray()
219244
XCTAssertEqual(resultErrors.count, multiThrowsExpectedErrors.count)
@@ -224,41 +249,65 @@ class FieldExecutionStrategyTests: XCTestCase {
224249
}
225250

226251
func testConcurrentDispatchFieldExecutionStrategyWithSingleField() throws {
252+
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
253+
defer {
254+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
255+
}
256+
227257
let result = try timing(try graphql(
228258
queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
229259
schema: schema,
230-
request: singleQuery
231-
))
260+
request: singleQuery,
261+
eventLoopGroup: eventLoopGroup
262+
).wait())
232263
XCTAssertEqual(result.value, singleExpected)
233264
//XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
234265
}
235266

236267
func testConcurrentDispatchFieldExecutionStrategyWithSingleFieldError() throws {
268+
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
269+
defer {
270+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
271+
}
272+
237273
let result = try timing(try graphql(
238274
queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
239275
schema: schema,
240-
request: singleThrowsQuery
241-
))
276+
request: singleThrowsQuery,
277+
eventLoopGroup: eventLoopGroup
278+
).wait())
242279
XCTAssertEqual(result.value, singleThrowsExpected)
243280
//XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
244281
}
245282

246283
func testConcurrentDispatchFieldExecutionStrategyWithMultipleFields() throws {
284+
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
285+
defer {
286+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
287+
}
288+
247289
let result = try timing(try graphql(
248290
queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
249291
schema: schema,
250-
request: multiQuery
251-
))
292+
request: multiQuery,
293+
eventLoopGroup: eventLoopGroup
294+
).wait())
252295
XCTAssertEqual(result.value, multiExpected)
253296
//XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
254297
}
255298

256299
func testConcurrentDispatchFieldExecutionStrategyWithMultipleFieldErrors() throws {
300+
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
301+
defer {
302+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
303+
}
304+
257305
let result = try timing(try graphql(
258306
queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
259307
schema: schema,
260-
request: multiThrowsQuery
261-
))
308+
request: multiThrowsQuery,
309+
eventLoopGroup: eventLoopGroup
310+
).wait())
262311
XCTAssertEqual(result.value["data"], multiThrowsExpectedData)
263312
let resultErrors = try result.value["errors"].asArray()
264313
XCTAssertEqual(resultErrors.count, multiThrowsExpectedErrors.count)

Tests/GraphQLTests/HelloWorldTests/HelloWorldTests.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import XCTest
2+
import NIO
23
@testable import GraphQL
34

45
class HelloWorldTests : XCTestCase {
@@ -8,24 +9,35 @@ class HelloWorldTests : XCTestCase {
89
fields: [
910
"hello": GraphQLField(
1011
type: GraphQLString,
11-
resolve: { _, _, _, _ in "world" }
12+
resolve: { _, _, eventLoopGroup, _ in return eventLoopGroup.next().newSucceededFuture(result: "world") }
1213
)
1314
]
1415
)
1516
)
1617

1718
func testHello() throws {
19+
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
20+
defer {
21+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
22+
}
23+
1824
let query = "{ hello }"
1925
let expected: Map = [
2026
"data": [
2127
"hello": "world"
2228
]
2329
]
24-
let result = try graphql(schema: schema, request: query)
30+
let result = try graphql(schema: schema, request: query, eventLoopGroup: eventLoopGroup).wait()
31+
2532
XCTAssertEqual(result, expected)
2633
}
2734

2835
func testBoyhowdy() throws {
36+
let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
37+
defer {
38+
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
39+
}
40+
2941
let query = "{ boyhowdy }"
3042

3143
let expectedErrors: Map = [
@@ -37,7 +49,7 @@ class HelloWorldTests : XCTestCase {
3749
]
3850
]
3951

40-
let result = try graphql(schema: schema, request: query)
52+
let result = try graphql(schema: schema, request: query, eventLoopGroup: eventLoopGroup).wait()
4153
XCTAssertEqual(result, expectedErrors)
4254
}
4355
}

0 commit comments

Comments
 (0)