Skip to content

Commit

Permalink
Function concurrency error
Browse files Browse the repository at this point in the history
Fix concurrency " mutation of captured var 'response' in concurrently-executing code" and typos.
  • Loading branch information
eBlender committed Jan 7, 2025
1 parent 4f956fb commit 4edc0ad
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions FirebaseFunctions/Tests/Unit/FunctionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ class FunctionsTests: XCTestCase {
func testGenerateStreamContent() async {
let options = HTTPSCallableOptions(requireLimitedUseAppCheckTokens: true)
var response = [String]()
let responseQueue = DispatchQueue(label: "responseQueue")

let input: [String: Any] = ["data": "Why is the sky blue"]
do {
Expand All @@ -371,18 +372,22 @@ class FunctionsTests: XCTestCase {
options: options,
timeout: 4.0
)
// Fisrt chunk of the stream comes as NSDictionary
// First chunk of the stream comes as NSDictionary
if let stream = stream {
for try await result in stream {
if let dataChunk = result.data as? NSDictionary {
for (key, value) in dataChunk {
response.append("\(key) \(value)")
responseQueue.sync {
response.append("\(key) \(value)")
}
}
} else {
// Last chunk is a the concatened result so we have to parse it as String else will
// Last chunk is the concatenated result so we have to parse it as String else will
// fail.
if (result.data as? String) != nil {
response.append(result.data as! String)
if let dataString = result.data as? String {
responseQueue.sync {
response.append(dataString)
}
}
}
}
Expand All @@ -405,6 +410,7 @@ class FunctionsTests: XCTestCase {

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

Expand All @@ -415,18 +421,22 @@ class FunctionsTests: XCTestCase {
options: options,
timeout: 4.0
)
// Fisrt chunk of the stream comes as NSDictionary
// First chunk of the stream comes as NSDictionary
if let stream = stream {
for try await result in stream {
if let dataChunk = result.data as? NSDictionary {
for (key, value) in dataChunk {
response.append("\(key) \(value)")
responseQueue.sync {
response.append("\(key) \(value)")

Check warning on line 430 in FirebaseFunctions/Tests/Unit/FunctionsTests.swift

View workflow job for this annotation

GitHub Actions / spm-unit (macos-14, Xcode_15.4, iOS)

mutation of captured var 'response' in concurrently-executing code; this is an error in Swift 6

Check warning on line 430 in FirebaseFunctions/Tests/Unit/FunctionsTests.swift

View workflow job for this annotation

GitHub Actions / spm-unit (macos-14, Xcode_15.4, iOS)

mutation of captured var 'response' in concurrently-executing code; this is an error in Swift 6
}
}
// Last chunk is a the concatened result so we have to parse it as String else will
// fail.
} else {
if (result.data as? String) != nil {
response.append(result.data as! String)
// Last chunk is the concatenated result so we have to parse it as String else will
// fail.
if let dataString = result.data as? String {
responseQueue.sync {
response.append(dataString)

Check warning on line 438 in FirebaseFunctions/Tests/Unit/FunctionsTests.swift

View workflow job for this annotation

GitHub Actions / spm-unit (macos-14, Xcode_15.4, iOS)

mutation of captured var 'response' in concurrently-executing code; this is an error in Swift 6

Check warning on line 438 in FirebaseFunctions/Tests/Unit/FunctionsTests.swift

View workflow job for this annotation

GitHub Actions / spm-unit (macos-14, Xcode_15.4, iOS)

mutation of captured var 'response' in concurrently-executing code; this is an error in Swift 6
}
}
}
}
Expand Down

0 comments on commit 4edc0ad

Please sign in to comment.