@@ -65,9 +65,35 @@ class IntegrationTests: XCTestCase {
65
65
XCTAssert ( false , " Failed to unwrap the function result: \( error) " )
66
66
}
67
67
}
68
- waitForExpectations ( timeout: 1 )
68
+ waitForExpectations ( timeout: 5 )
69
69
}
70
70
71
+ #if compiler(>=5.5) && canImport(_Concurrency)
72
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
73
+ func testDataAsync( ) async throws {
74
+ let input = [
75
+ " bool " : true ,
76
+ " int " : 2 as Int32 ,
77
+ " long " : 9_876_543_210 ,
78
+ " string " : " four " ,
79
+ " array " : [ 5 as Int32 , 6 as Int32 ] ,
80
+ " null " : nil ,
81
+ ] as [ String : Any ? ]
82
+
83
+ let function = functions. httpsCallable ( " dataTest " )
84
+ XCTAssertNotNil ( function)
85
+
86
+ let result = try await function. call ( input)
87
+ let data = try XCTUnwrap ( result. data as? [ String : Any ] )
88
+ let message = try XCTUnwrap ( data [ " message " ] as? String )
89
+ let long = try XCTUnwrap ( data [ " long " ] as? Int64 )
90
+ let code = try XCTUnwrap ( data [ " code " ] as? Int32 )
91
+ XCTAssertEqual ( message, " stub response " )
92
+ XCTAssertEqual ( long, 420 )
93
+ XCTAssertEqual ( code, 42 )
94
+ }
95
+ #endif
96
+
71
97
func testScalar( ) {
72
98
let expectation = expectation ( description: #function)
73
99
let function = functions. httpsCallable ( " scalarTest " )
@@ -82,11 +108,23 @@ class IntegrationTests: XCTestCase {
82
108
XCTAssert ( false , " Failed to unwrap the function result: \( error) " )
83
109
}
84
110
}
85
- waitForExpectations ( timeout: 1 )
111
+ waitForExpectations ( timeout: 5 )
86
112
}
87
113
114
+ #if compiler(>=5.5) && canImport(_Concurrency)
115
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
116
+ func testScalarAsync( ) async throws {
117
+ let function = functions. httpsCallable ( " scalarTest " )
118
+ XCTAssertNotNil ( function)
119
+
120
+ let result = try await function. call ( 17 as Int16 )
121
+ let data = try XCTUnwrap ( result. data as? Int )
122
+ XCTAssertEqual ( data, 76 )
123
+ }
124
+ #endif
125
+
88
126
func testToken( ) {
89
- // Recreate _functions with a token.
127
+ // Recreate functions with a token.
90
128
let functions = FunctionsFake (
91
129
projectID: " functions-integration-test " ,
92
130
region: " us-central1 " ,
@@ -108,9 +146,30 @@ class IntegrationTests: XCTestCase {
108
146
XCTAssert ( false , " Failed to unwrap the function result: \( error) " )
109
147
}
110
148
}
111
- waitForExpectations ( timeout: 1 )
149
+ waitForExpectations ( timeout: 5 )
112
150
}
113
151
152
+ #if compiler(>=5.5) && canImport(_Concurrency)
153
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
154
+ func testTokenAsync( ) async throws {
155
+ // Recreate functions with a token.
156
+ let functions = FunctionsFake (
157
+ projectID: " functions-integration-test " ,
158
+ region: " us-central1 " ,
159
+ customDomain: nil ,
160
+ withToken: " token "
161
+ )
162
+ functions. useLocalhost ( )
163
+
164
+ let function = functions. httpsCallable ( " FCMTokenTest " )
165
+ XCTAssertNotNil ( function)
166
+
167
+ let result = try await function. call ( [ : ] )
168
+ let data = try XCTUnwrap ( result. data) as? [ String : Int ]
169
+ XCTAssertEqual ( data, [ : ] )
170
+ }
171
+ #endif
172
+
114
173
func testFCMToken( ) {
115
174
let expectation = expectation ( description: #function)
116
175
let function = functions. httpsCallable ( " FCMTokenTest " )
@@ -125,9 +184,21 @@ class IntegrationTests: XCTestCase {
125
184
XCTAssert ( false , " Failed to unwrap the function result: \( error) " )
126
185
}
127
186
}
128
- waitForExpectations ( timeout: 1 )
187
+ waitForExpectations ( timeout: 5 )
129
188
}
130
189
190
+ #if compiler(>=5.5) && canImport(_Concurrency)
191
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
192
+ func testFCMTokenAsync( ) async throws {
193
+ let function = functions. httpsCallable ( " FCMTokenTest " )
194
+ XCTAssertNotNil ( function)
195
+
196
+ let result = try await function. call ( [ : ] )
197
+ let data = try XCTUnwrap ( result. data) as? [ String : Int ]
198
+ XCTAssertEqual ( data, [ : ] )
199
+ }
200
+ #endif
201
+
131
202
func testNull( ) {
132
203
let expectation = expectation ( description: #function)
133
204
let function = functions. httpsCallable ( " nullTest " )
@@ -142,9 +213,51 @@ class IntegrationTests: XCTestCase {
142
213
XCTAssert ( false , " Failed to unwrap the function result: \( error) " )
143
214
}
144
215
}
145
- waitForExpectations ( timeout: 1 )
216
+ waitForExpectations ( timeout: 5 )
217
+ }
218
+
219
+ #if compiler(>=5.5) && canImport(_Concurrency)
220
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
221
+ func testNullAsync( ) async throws {
222
+ let function = functions. httpsCallable ( " nullTest " )
223
+ XCTAssertNotNil ( function)
224
+
225
+ let result = try await function. call ( nil )
226
+ let data = try XCTUnwrap ( result. data) as? NSNull
227
+ XCTAssertEqual ( data, NSNull ( ) )
228
+ }
229
+ #endif
230
+
231
+ // No parameters to call should be the same as passing nil.
232
+ func testParameterless( ) {
233
+ let expectation = expectation ( description: #function)
234
+ let function = functions. httpsCallable ( " nullTest " )
235
+ XCTAssertNotNil ( function)
236
+ function. call { result, error in
237
+ do {
238
+ XCTAssertNil ( error)
239
+ let data = try XCTUnwrap ( result? . data) as? NSNull
240
+ XCTAssertEqual ( data, NSNull ( ) )
241
+ expectation. fulfill ( )
242
+ } catch {
243
+ XCTAssert ( false , " Failed to unwrap the function result: \( error) " )
244
+ }
245
+ }
246
+ waitForExpectations ( timeout: 5 )
146
247
}
147
248
249
+ #if compiler(>=5.5) && canImport(_Concurrency)
250
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
251
+ func testParameterlessAsync( ) async throws {
252
+ let function = functions. httpsCallable ( " nullTest " )
253
+ XCTAssertNotNil ( function)
254
+
255
+ let result = try await function. call ( )
256
+ let data = try XCTUnwrap ( result. data) as? NSNull
257
+ XCTAssertEqual ( data, NSNull ( ) )
258
+ }
259
+ #endif
260
+
148
261
func testMissingResult( ) {
149
262
let expectation = expectation ( description: #function)
150
263
let function = functions. httpsCallable ( " missingResultTest " )
@@ -161,9 +274,26 @@ class IntegrationTests: XCTestCase {
161
274
}
162
275
}
163
276
XCTAssert ( true )
164
- waitForExpectations ( timeout: 1 )
277
+ waitForExpectations ( timeout: 5 )
165
278
}
166
279
280
+ #if compiler(>=5.5) && canImport(_Concurrency)
281
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
282
+ func testMissingResultAsync( ) async throws {
283
+ let function = functions. httpsCallable ( " missingResultTest " )
284
+ XCTAssertNotNil ( function)
285
+ do {
286
+ _ = try await function. call ( nil )
287
+ } catch {
288
+ let error = try XCTUnwrap ( error) as NSError
289
+ XCTAssertEqual ( FunctionsErrorCode . internal. rawValue, error. code)
290
+ XCTAssertEqual ( " Response is missing data field. " , error. localizedDescription)
291
+ return
292
+ }
293
+ XCTAssertFalse ( true , " Failed to throw error for missing result " )
294
+ }
295
+ #endif
296
+
167
297
func testUnhandledError( ) {
168
298
let expectation = expectation ( description: #function)
169
299
let function = functions. httpsCallable ( " unhandledErrorTest " )
@@ -180,9 +310,26 @@ class IntegrationTests: XCTestCase {
180
310
}
181
311
}
182
312
XCTAssert ( true )
183
- waitForExpectations ( timeout: 1 )
313
+ waitForExpectations ( timeout: 5 )
184
314
}
185
315
316
+ #if compiler(>=5.5) && canImport(_Concurrency)
317
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
318
+ func testUnhandledErrorAsync( ) async throws {
319
+ let function = functions. httpsCallable ( " unhandledErrorTest " )
320
+ XCTAssertNotNil ( function)
321
+ do {
322
+ _ = try await function. call ( [ ] )
323
+ } catch {
324
+ let error = try XCTUnwrap ( error) as NSError
325
+ XCTAssertEqual ( FunctionsErrorCode . internal. rawValue, error. code)
326
+ XCTAssertEqual ( " INTERNAL " , error. localizedDescription)
327
+ return
328
+ }
329
+ XCTAssertFalse ( true , " Failed to throw error for missing result " )
330
+ }
331
+ #endif
332
+
186
333
func testUnknownError( ) {
187
334
let expectation = expectation ( description: #function)
188
335
let function = functions. httpsCallable ( " unknownErrorTest " )
@@ -199,9 +346,26 @@ class IntegrationTests: XCTestCase {
199
346
}
200
347
}
201
348
XCTAssert ( true )
202
- waitForExpectations ( timeout: 1 )
349
+ waitForExpectations ( timeout: 5 )
203
350
}
204
351
352
+ #if compiler(>=5.5) && canImport(_Concurrency)
353
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
354
+ func testUnknownErrorAsync( ) async throws {
355
+ let function = functions. httpsCallable ( " unknownErrorTest " )
356
+ XCTAssertNotNil ( function)
357
+ do {
358
+ _ = try await function. call ( [ ] )
359
+ } catch {
360
+ let error = try XCTUnwrap ( error) as NSError
361
+ XCTAssertEqual ( FunctionsErrorCode . internal. rawValue, error. code)
362
+ XCTAssertEqual ( " INTERNAL " , error. localizedDescription)
363
+ return
364
+ }
365
+ XCTAssertFalse ( true , " Failed to throw error for missing result " )
366
+ }
367
+ #endif
368
+
205
369
func testExplicitError( ) {
206
370
let expectation = expectation ( description: #function)
207
371
let function = functions. httpsCallable ( " explicitErrorTest " )
@@ -220,9 +384,28 @@ class IntegrationTests: XCTestCase {
220
384
}
221
385
}
222
386
XCTAssert ( true )
223
- waitForExpectations ( timeout: 1 )
387
+ waitForExpectations ( timeout: 5 )
224
388
}
225
389
390
+ #if compiler(>=5.5) && canImport(_Concurrency)
391
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
392
+ func testExplicitErrorAsync( ) async throws {
393
+ let function = functions. httpsCallable ( " explicitErrorTest " )
394
+ XCTAssertNotNil ( function)
395
+ do {
396
+ _ = try await function. call ( [ ] )
397
+ } catch {
398
+ let error = try XCTUnwrap ( error) as NSError
399
+ XCTAssertEqual ( FunctionsErrorCode . outOfRange. rawValue, error. code)
400
+ XCTAssertEqual ( " explicit nope " , error. localizedDescription)
401
+ XCTAssertEqual ( [ " start " : 10 as Int32 , " end " : 20 as Int32 , " long " : 30 ] ,
402
+ error. userInfo [ FunctionsErrorDetailsKey] as! [ String : Int32 ] )
403
+ return
404
+ }
405
+ XCTAssertFalse ( true , " Failed to throw error for missing result " )
406
+ }
407
+ #endif
408
+
226
409
func testHttpError( ) {
227
410
let expectation = expectation ( description: #function)
228
411
let function = functions. httpsCallable ( " httpErrorTest " )
@@ -238,9 +421,25 @@ class IntegrationTests: XCTestCase {
238
421
}
239
422
}
240
423
XCTAssert ( true )
241
- waitForExpectations ( timeout: 1 )
424
+ waitForExpectations ( timeout: 5 )
242
425
}
243
426
427
+ #if compiler(>=5.5) && canImport(_Concurrency)
428
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
429
+ func testHttpErrorAsync( ) async throws {
430
+ let function = functions. httpsCallable ( " httpErrorTest " )
431
+ XCTAssertNotNil ( function)
432
+ do {
433
+ _ = try await function. call ( [ ] )
434
+ } catch {
435
+ let error = try XCTUnwrap ( error) as NSError
436
+ XCTAssertEqual ( FunctionsErrorCode . invalidArgument. rawValue, error. code)
437
+ return
438
+ }
439
+ XCTAssertFalse ( true , " Failed to throw error for missing result " )
440
+ }
441
+ #endif
442
+
244
443
func testTimeout( ) {
245
444
let expectation = expectation ( description: #function)
246
445
let function = functions. httpsCallable ( " timeoutTest " )
@@ -259,6 +458,25 @@ class IntegrationTests: XCTestCase {
259
458
}
260
459
}
261
460
XCTAssert ( true )
262
- waitForExpectations ( timeout: 1 )
461
+ waitForExpectations ( timeout: 5 )
263
462
}
463
+
464
+ #if compiler(>=5.5) && canImport(_Concurrency)
465
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
466
+ func testTimeoutAsync( ) async throws {
467
+ let function = functions. httpsCallable ( " timeoutTest " )
468
+ XCTAssertNotNil ( function)
469
+ function. timeoutInterval = 0.05
470
+ do {
471
+ _ = try await function. call ( [ ] )
472
+ } catch {
473
+ let error = try XCTUnwrap ( error) as NSError
474
+ XCTAssertEqual ( FunctionsErrorCode . deadlineExceeded. rawValue, error. code)
475
+ XCTAssertEqual ( " DEADLINE EXCEEDED " , error. localizedDescription)
476
+ XCTAssertNil ( error. userInfo [ FunctionsErrorDetailsKey] )
477
+ return
478
+ }
479
+ XCTAssertFalse ( true , " Failed to throw error for missing result " )
480
+ }
481
+ #endif
264
482
}
0 commit comments