Skip to content

Commit 6050dbe

Browse files
committed
Now test for cancellation detection upon entering a gate that is open.
It's important to be consistent on this irrespective of the state of the gate (it's also helpful to users as they don't have to separately check for cancellation, if they're entering the gate anyway).
1 parent cc7b1b7 commit 6050dbe

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

Tests/GateTests/GateTests.swift

+22-19
Original file line numberDiff line numberDiff line change
@@ -131,31 +131,34 @@ final class GateTests: XCTestCase {
131131
}
132132
}
133133

134-
func testClosedGateEnteredByAlreadyCancelledTask() throws {
135-
let gate = Gate(initiallyOpen: false)
134+
func testGateEnteredByAlreadyCancelledTask() throws {
135+
for initialState in [true, false] {
136+
let gate = Gate(initiallyOpen: initialState)
136137

137-
let passedThroughExpectation = XCTestExpectation(description: "Task should not have passed through the gate.")
138-
passedThroughExpectation.isInverted = true
138+
let passedThroughExpectation = XCTestExpectation(description: "Task should not have passed through the gate.")
139+
passedThroughExpectation.isInverted = true
139140

140-
let cancelledExpectation = XCTestExpectation(description: "Task should have been cancelled.")
141+
let cancelledExpectation = XCTestExpectation(description: "Task should have been cancelled.")
141142

142-
Task {
143-
withUnsafeCurrentTask {
144-
$0!.cancel()
145-
}
143+
Task {
144+
withUnsafeCurrentTask {
145+
$0!.cancel()
146+
}
146147

147-
do {
148-
try await gate.enter()
149-
} catch is CancellationError {
150-
cancelledExpectation.fulfill()
151-
return
152-
}
148+
do {
149+
try await gate.enter()
150+
} catch is CancellationError {
151+
cancelledExpectation.fulfill()
152+
return
153+
}
153154

154-
passedThroughExpectation.fulfill()
155-
}
155+
passedThroughExpectation.fulfill()
156+
}
156157

157-
wait(for: [cancelledExpectation, passedThroughExpectation], timeout: 1.0)
158+
wait(for: [cancelledExpectation, passedThroughExpectation], timeout: 1.0)
158159

159-
gate.open() // Just to ensure that opening the gate afterwards doesn't somehow crash.
160+
gate.open() // Just to ensure that opening or closing the gate afterwards doesn't somehow crash.
161+
gate.close()
162+
}
160163
}
161164
}

0 commit comments

Comments
 (0)