-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug/502 crash thread safety fix (#95)
### Motivation Fixes apple/swift-openapi-generator#502 - Ensure thread safety of `HTTPBody.collect(upTo)`. - `makeAsyncIterator()`: Instead of crashing, return AsyncSequence which throws `TooManyIterationsError` thereby honoring the contract for `IterationBehavior.single` (HTTPBody, MultipartBody) ### Modifications - HTTPBody, MultipartBody: `makeAsyncIterator()`: removed `try!`, catch error and create a sequence which throws the error on iteration. - This removed the need for `try checkIfCanCreateIterator()` in `HTTPBody.collect(upTo)`. **Note**: This creates a small change in behavior: There may be a `TooManyBytesError` thrown before the check for `iterationBehavior`. This approach uses the simplest code, IMO. If we want to keep that `iterationBehavior` is checked first and only after that for the length, then the code needs to be more complex. - Removed `try checkIfCanCreateIterator()` in both classes (only used in `HTTPBody`). ### Result - No intentional crash in `makeAsyncIterator()` anymore. - Tests supplied as example in apple/swift-openapi-generator#502 succeed. ### Test Plan - Added check in `Test_Body.testIterationBehavior_single()` to ensure that using `makeAsyncIterator()` directly yields the expected error. - Added tests to check iteration behavior of `MultipartBody`. --------- Co-authored-by: Lars Peters <[email protected]>
- Loading branch information
1 parent
7f86e4a
commit 95307ba
Showing
4 changed files
with
71 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Tests/OpenAPIRuntimeTests/Interface/Test_MultipartBody.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
import XCTest | ||
@_spi(Generated) @testable import OpenAPIRuntime | ||
import Foundation | ||
|
||
final class Test_MultipartBody: XCTestCase { | ||
|
||
func testIterationBehavior_single() async throws { | ||
let sourceSequence = (0..<Int.random(in: 2..<10)).map { _ in UUID().uuidString } | ||
let body = MultipartBody(sourceSequence, iterationBehavior: .single) | ||
|
||
XCTAssertFalse(body.testing_iteratorCreated) | ||
|
||
let iterated = try await body.reduce("") { $0 + $1 } | ||
XCTAssertEqual(iterated, sourceSequence.joined()) | ||
|
||
XCTAssertTrue(body.testing_iteratorCreated) | ||
|
||
do { | ||
for try await _ in body {} | ||
XCTFail("Expected an error to be thrown") | ||
} catch {} | ||
} | ||
|
||
func testIterationBehavior_multiple() async throws { | ||
let sourceSequence = (0..<Int.random(in: 2..<10)).map { _ in UUID().uuidString } | ||
let body = MultipartBody(sourceSequence, iterationBehavior: .multiple) | ||
|
||
XCTAssertFalse(body.testing_iteratorCreated) | ||
for _ in 0..<2 { | ||
let iterated = try await body.reduce("") { $0 + $1 } | ||
XCTAssertEqual(iterated, sourceSequence.joined()) | ||
XCTAssertTrue(body.testing_iteratorCreated) | ||
} | ||
} | ||
|
||
} |