Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ extension HTTPBodyOutputStreamBridge {
case .needBytes(_, let producerContinuation):
self = .closed(error)
return .cancelProducerAndCloseStream(producerContinuation)
case .closed: preconditionFailure("\(#function) called in invalid state: \(self)")
case .closed:
debug("Ignoring \(#function) event in closed state")
return .none
}
}

Expand All @@ -216,8 +218,10 @@ extension HTTPBodyOutputStreamBridge {
case .waitingForBytes(_):
self = .closed(nil)
return .closeStream
case .initial, .haveBytes, .needBytes, .closed:
preconditionFailure("\(#function) called in invalid state: \(self)")
case .closed:
debug("Ignoring \(#function) event in closed state")
return .none
case .initial, .haveBytes, .needBytes: preconditionFailure("\(#function) called in invalid state: \(self)")
}
}

Expand All @@ -232,7 +236,10 @@ extension HTTPBodyOutputStreamBridge {
case .needBytes(_, let producerContinuation):
self = .closed(nil)
return .cancelProducerAndCloseStream(producerContinuation)
case .initial, .closed: preconditionFailure("\(#function) called in invalid state: \(self)")
case .closed:
debug("Ignoring \(#function) event in closed state")
return .none
case .initial: preconditionFailure("\(#function) called in invalid state: \(self)")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,44 @@ class HTTPBodyOutputStreamBridgeTests: XCTestCase {

}

func testWroteFinalChunkIsNoOpWhenAlreadyClosed() {
var state = HTTPBodyOutputStreamBridge.State.closed(URLError(.cannotFindHost))
guard case .none = state.wroteFinalChunk() else {
XCTFail("Expected .none action from wroteFinalChunk() in .closed state")
return
}
guard case .closed = state else {
XCTFail("Expected state to remain .closed, got \(state)")
return
}
}

func testEndEncounteredIsNoOpWhenAlreadyClosed() {
var state = HTTPBodyOutputStreamBridge.State.closed(URLError(.cannotFindHost))
guard case .none = state.endEncountered() else {
XCTFail("Expected .none action from endEncountered() in .closed state")
return
}
guard case .closed = state else {
XCTFail("Expected state to remain .closed, got \(state)")
return
}
}

func testErrorOccurredIsNoOpWhenAlreadyClosed() {
let firstError = URLError(.cannotFindHost)
var state = HTTPBodyOutputStreamBridge.State.closed(firstError)
guard case .none = state.errorOccurred(URLError(.timedOut)) else {
XCTFail("Expected .none action from errorOccurred() in .closed state")
return
}
guard case .closed(let storedError) = state else {
XCTFail("Expected state to remain .closed, got \(state)")
return
}
XCTAssertEqual((storedError as? URLError)?.code, firstError.code, "First error should win")
}

// Wait for given condition to be true within wait duration.
private func waitForCondition(_ label: String, within waitDuration: TimeInterval = 1.0, condition: () -> Bool)
async throws
Expand Down
Loading