Skip to content

Commit 4edc0ad

Browse files
committed
Function concurrency error
Fix concurrency " mutation of captured var 'response' in concurrently-executing code" and typos.
1 parent 4f956fb commit 4edc0ad

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

FirebaseFunctions/Tests/Unit/FunctionsTests.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ class FunctionsTests: XCTestCase {
362362
func testGenerateStreamContent() async {
363363
let options = HTTPSCallableOptions(requireLimitedUseAppCheckTokens: true)
364364
var response = [String]()
365+
let responseQueue = DispatchQueue(label: "responseQueue")
365366

366367
let input: [String: Any] = ["data": "Why is the sky blue"]
367368
do {
@@ -371,18 +372,22 @@ class FunctionsTests: XCTestCase {
371372
options: options,
372373
timeout: 4.0
373374
)
374-
// Fisrt chunk of the stream comes as NSDictionary
375+
// First chunk of the stream comes as NSDictionary
375376
if let stream = stream {
376377
for try await result in stream {
377378
if let dataChunk = result.data as? NSDictionary {
378379
for (key, value) in dataChunk {
379-
response.append("\(key) \(value)")
380+
responseQueue.sync {
381+
response.append("\(key) \(value)")
382+
}
380383
}
381384
} else {
382-
// Last chunk is a the concatened result so we have to parse it as String else will
385+
// Last chunk is the concatenated result so we have to parse it as String else will
383386
// fail.
384-
if (result.data as? String) != nil {
385-
response.append(result.data as! String)
387+
if let dataString = result.data as? String {
388+
responseQueue.sync {
389+
response.append(dataString)
390+
}
386391
}
387392
}
388393
}
@@ -405,6 +410,7 @@ class FunctionsTests: XCTestCase {
405410

406411
func testGenerateStreamContentCanceled() async {
407412
var response = [String]()
413+
let responseQueue = DispatchQueue(label: "responseQueue")
408414
let options = HTTPSCallableOptions(requireLimitedUseAppCheckTokens: true)
409415
let input: [String: Any] = ["data": "Why is the sky blue"]
410416

@@ -415,18 +421,22 @@ class FunctionsTests: XCTestCase {
415421
options: options,
416422
timeout: 4.0
417423
)
418-
// Fisrt chunk of the stream comes as NSDictionary
424+
// First chunk of the stream comes as NSDictionary
419425
if let stream = stream {
420426
for try await result in stream {
421427
if let dataChunk = result.data as? NSDictionary {
422428
for (key, value) in dataChunk {
423-
response.append("\(key) \(value)")
429+
responseQueue.sync {
430+
response.append("\(key) \(value)")
431+
}
424432
}
425-
// Last chunk is a the concatened result so we have to parse it as String else will
426-
// fail.
427433
} else {
428-
if (result.data as? String) != nil {
429-
response.append(result.data as! String)
434+
// Last chunk is the concatenated result so we have to parse it as String else will
435+
// fail.
436+
if let dataString = result.data as? String {
437+
responseQueue.sync {
438+
response.append(dataString)
439+
}
430440
}
431441
}
432442
}

0 commit comments

Comments
 (0)