-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathFieldExecutionStrategyTests.swift
361 lines (325 loc) · 12.3 KB
/
FieldExecutionStrategyTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import Dispatch
@testable import GraphQL
import NIO
import XCTest
class FieldExecutionStrategyTests: XCTestCase {
enum StrategyError: Error {
case exampleError(msg: String)
}
let schema = try! GraphQLSchema(
query: GraphQLObjectType(
name: "RootQueryType",
fields: [
"sleep": GraphQLField(
type: GraphQLString,
resolve: { _, _, _, eventLoopGroup, _ in
eventLoopGroup.next().makeSucceededVoidFuture().map {
Thread.sleep(forTimeInterval: 0.1)
return "z"
}
}
),
"bang": GraphQLField(
type: GraphQLString,
resolve: { (_, _, _, _, info: GraphQLResolveInfo) in
let group = DispatchGroup()
group.enter()
DispatchQueue.global().asyncAfter(wallDeadline: .now() + 0.1) {
group.leave()
}
group.wait()
throw StrategyError.exampleError(
msg: "\(info.fieldName): \(info.path.elements.last!)"
)
}
),
"futureBang": GraphQLField(
type: GraphQLString,
resolve: { (_, _, _, eventLoopGroup, info: GraphQLResolveInfo) in
let g = DispatchGroup()
g.enter()
DispatchQueue.global().asyncAfter(wallDeadline: .now() + 0.1) {
g.leave()
}
g.wait()
return eventLoopGroup.next().makeFailedFuture(StrategyError.exampleError(
msg: "\(info.fieldName): \(info.path.elements.last!)"
))
}
),
]
)
)
let singleQuery = "{ sleep }"
let singleExpected = GraphQLResult(
data: [
"sleep": "z",
]
)
let multiQuery =
"{ a: sleep b: sleep c: sleep d: sleep e: sleep f: sleep g: sleep h: sleep i: sleep j: sleep }"
let multiExpected = GraphQLResult(
data: [
"a": "z",
"b": "z",
"c": "z",
"d": "z",
"e": "z",
"f": "z",
"g": "z",
"h": "z",
"i": "z",
"j": "z",
]
)
let singleThrowsQuery = "{ bang }"
let singleThrowsExpected = GraphQLResult(
data: [
"bang": nil,
],
errors: [
GraphQLError(
message: "exampleError(msg: \"bang: bang\")",
locations: [SourceLocation(line: 1, column: 3)],
path: ["bang"]
),
]
)
let singleFailedFutureQuery = "{ futureBang }"
let singleFailedFutureExpected = GraphQLResult(
data: [
"futureBang": nil,
],
errors: [
GraphQLError(
message: "exampleError(msg: \"futureBang: futureBang\")",
locations: [SourceLocation(line: 1, column: 3)],
path: ["futureBang"]
),
]
)
let multiThrowsQuery =
"{ a: bang b: bang c: bang d: bang e: bang f: bang g: bang h: bang i: bang j: futureBang }"
let multiThrowsExpectedData: Map = [
"a": nil,
"b": nil,
"c": nil,
"d": nil,
"e": nil,
"f": nil,
"g": nil,
"h": nil,
"i": nil,
"j": nil,
]
let multiThrowsExpectedErrors: [GraphQLError] = [
GraphQLError(
message: "exampleError(msg: \"bang: a\")",
locations: [SourceLocation(line: 1, column: 3)],
path: ["a"]
),
GraphQLError(
message: "exampleError(msg: \"bang: b\")",
locations: [SourceLocation(line: 1, column: 11)],
path: ["b"]
),
GraphQLError(
message: "exampleError(msg: \"bang: c\")",
locations: [SourceLocation(line: 1, column: 19)],
path: ["c"]
),
GraphQLError(
message: "exampleError(msg: \"bang: d\")",
locations: [SourceLocation(line: 1, column: 27)],
path: ["d"]
),
GraphQLError(
message: "exampleError(msg: \"bang: e\")",
locations: [SourceLocation(line: 1, column: 35)],
path: ["e"]
),
GraphQLError(
message: "exampleError(msg: \"bang: f\")",
locations: [SourceLocation(line: 1, column: 43)],
path: ["f"]
),
GraphQLError(
message: "exampleError(msg: \"bang: g\")",
locations: [SourceLocation(line: 1, column: 51)],
path: ["g"]
),
GraphQLError(
message: "exampleError(msg: \"bang: h\")",
locations: [SourceLocation(line: 1, column: 59)],
path: ["h"]
),
GraphQLError(
message: "exampleError(msg: \"bang: i\")",
locations: [SourceLocation(line: 1, column: 67)],
path: ["i"]
),
GraphQLError(
message: "exampleError(msg: \"futureBang: j\")",
locations: [SourceLocation(line: 1, column: 75)],
path: ["j"]
),
]
func timing<T>(_ block: @autoclosure () throws -> T) throws -> (value: T, seconds: Double) {
let start = DispatchTime.now()
let value = try block()
let nanoseconds = DispatchTime.now().uptimeNanoseconds - start.uptimeNanoseconds
let seconds = Double(nanoseconds) / 1_000_000_000
return (
value: value,
seconds: seconds
)
}
private var eventLoopGroup: EventLoopGroup!
override func setUp() {
eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
}
override func tearDown() {
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
}
func testSerialFieldExecutionStrategyWithSingleField() throws {
let result = try timing(graphql(
queryStrategy: SerialFieldExecutionStrategy(),
schema: schema,
request: singleQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value, singleExpected)
// XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
}
func testSerialFieldExecutionStrategyWithSingleFieldError() throws {
let result = try timing(graphql(
queryStrategy: SerialFieldExecutionStrategy(),
schema: schema,
request: singleThrowsQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value, singleThrowsExpected)
// XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
}
func testSerialFieldExecutionStrategyWithSingleFieldFailedFuture() throws {
let result = try timing(graphql(
queryStrategy: SerialFieldExecutionStrategy(),
schema: schema,
request: singleFailedFutureQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value, singleFailedFutureExpected)
// XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
}
func testSerialFieldExecutionStrategyWithMultipleFields() throws {
let result = try timing(graphql(
queryStrategy: SerialFieldExecutionStrategy(),
schema: schema,
request: multiQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value, multiExpected)
// XCTAssertEqualWithAccuracy(1.0, result.seconds, accuracy: 0.5)
}
func testSerialFieldExecutionStrategyWithMultipleFieldErrors() throws {
let result = try timing(graphql(
queryStrategy: SerialFieldExecutionStrategy(),
schema: schema,
request: multiThrowsQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value.data, multiThrowsExpectedData)
let resultErrors = result.value.errors
XCTAssertEqual(resultErrors.count, multiThrowsExpectedErrors.count)
for m in multiThrowsExpectedErrors {
XCTAssertTrue(resultErrors.contains(m), "Expecting result errors to contain \(m)")
}
// XCTAssertEqualWithAccuracy(1.0, result.seconds, accuracy: 0.5)
}
func testConcurrentDispatchFieldExecutionStrategyWithSingleField() throws {
let result = try timing(graphql(
queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
schema: schema,
request: singleQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value, singleExpected)
// XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
}
func testConcurrentDispatchFieldExecutionStrategyWithSingleFieldError() throws {
let result = try timing(graphql(
queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
schema: schema,
request: singleThrowsQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value, singleThrowsExpected)
// XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
}
func testConcurrentDispatchFieldExecutionStrategyWithMultipleFields() throws {
let result = try timing(graphql(
queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
schema: schema,
request: multiQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value, multiExpected)
// XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
}
func testConcurrentDispatchFieldExecutionStrategyWithMultipleFieldErrors() throws {
let result = try timing(graphql(
queryStrategy: ConcurrentDispatchFieldExecutionStrategy(),
schema: schema,
request: multiThrowsQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value.data, multiThrowsExpectedData)
let resultErrors = result.value.errors
XCTAssertEqual(resultErrors.count, multiThrowsExpectedErrors.count)
for m in multiThrowsExpectedErrors {
XCTAssertTrue(resultErrors.contains(m), "Expecting result errors to contain \(m)")
}
// XCTAssertEqualWithAccuracy(0.1, result.seconds, accuracy: 0.25)
}
func testConcurrentFieldExecutionStrategyWithSingleField() throws {
let result = try timing(graphql(
queryStrategy: ConcurrentFieldExecutionStrategy(),
schema: schema,
request: singleQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value, singleExpected)
}
func testConcurrentFieldExecutionStrategyWithSingleFieldError() throws {
let result = try timing(graphql(
queryStrategy: ConcurrentFieldExecutionStrategy(),
schema: schema,
request: singleThrowsQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value, singleThrowsExpected)
}
func testConcurrentFieldExecutionStrategyWithMultipleFields() throws {
let result = try timing(graphql(
queryStrategy: ConcurrentFieldExecutionStrategy(),
schema: schema,
request: multiQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value, multiExpected)
}
func testConcurrentFieldExecutionStrategyWithMultipleFieldErrors() throws {
let result = try timing(graphql(
queryStrategy: ConcurrentFieldExecutionStrategy(),
schema: schema,
request: multiThrowsQuery,
eventLoopGroup: eventLoopGroup
).wait())
XCTAssertEqual(result.value.data, multiThrowsExpectedData)
let resultErrors = result.value.errors
XCTAssertEqual(resultErrors.count, multiThrowsExpectedErrors.count)
for m in multiThrowsExpectedErrors {
XCTAssertTrue(resultErrors.contains(m), "Expecting result errors to contain \(m)")
}
}
}