Skip to content

Commit 4d3a8e4

Browse files
committed
feat: use XCTAssertNoThrow and XCTAssertThrowsError, add test for failing clocks
1 parent ce9ccef commit 4d3a8e4

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

Tests/DeadlineTests/DeadlineTests.swift

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ final class DeadlineTests: XCTestCase {
1414
}
1515

1616
await testClock.advance(by: .milliseconds(200))
17-
do {
18-
try await task.value
19-
} catch {
20-
XCTFail()
21-
}
17+
18+
let result = await task.result
19+
XCTAssertNoThrow(try result.get())
2220
}
2321

2422
func testDeadline() async {
@@ -31,13 +29,10 @@ final class DeadlineTests: XCTestCase {
3129
}
3230

3331
await testClock.advance(by: .milliseconds(200))
34-
do {
35-
try await task.value
36-
XCTFail()
37-
} catch is DeadlineExceededError {
38-
// expected
39-
} catch {
40-
XCTFail()
32+
33+
let result = await task.result
34+
XCTAssertThrowsError(try result.get()) { error in
35+
XCTAssertTrue(error is DeadlineExceededError)
4136
}
4237
}
4338

@@ -59,13 +54,9 @@ final class DeadlineTests: XCTestCase {
5954
await testClock.advance(by: .milliseconds(50))
6055
task.cancel()
6156

62-
do {
63-
try await task.value
64-
XCTFail()
65-
} catch is CustomError {
66-
// expected
67-
} catch {
68-
XCTFail()
57+
let result = await task.result
58+
XCTAssertThrowsError(try result.get()) { error in
59+
XCTAssertTrue(error is CustomError)
6960
}
7061
}
7162

@@ -86,13 +77,32 @@ final class DeadlineTests: XCTestCase {
8677

8778
task.cancel()
8879

89-
do {
90-
try await task.value
91-
XCTFail()
92-
} catch is CustomError {
93-
// expected
94-
} catch {
95-
XCTFail()
80+
let result = await task.result
81+
XCTAssertThrowsError(try result.get()) { error in
82+
XCTAssertTrue(error is CustomError)
83+
}
84+
}
85+
86+
func testFailingClock() async {
87+
88+
struct CustomError: Error { }
89+
struct CustomClock: Clock {
90+
let _internal = TestClock()
91+
var now: TestClock<Duration>.Instant { _internal.now }
92+
var minimumResolution: TestClock<Duration>.Duration { _internal.minimumResolution }
93+
func sleep(until deadline: Instant, tolerance: Duration? = nil) async throws { throw CustomError() }
94+
}
95+
96+
let customClock = CustomClock()
97+
let task = Task {
98+
try await withDeadline(until: .init(offset: .milliseconds(200)), clock: customClock) {
99+
try await customClock.sleep(until: .init(offset: .milliseconds(100)))
100+
}
101+
}
102+
103+
let result = await task.result
104+
XCTAssertThrowsError(try result.get()) { error in
105+
XCTAssertTrue(error is CustomError)
96106
}
97107
}
98108
}

0 commit comments

Comments
 (0)