@@ -78,7 +78,7 @@ final class RetryTests: XCTestCase {
7878 }
7979
8080 func testAllAttemptsFail_failureAfterRetries( ) async throws {
81- try await assertThrowsErrorFake {
81+ try await assertThrows ( ErrorFake . self ) {
8282 try await retry ( with: testingConfiguration) {
8383 throw ErrorFake ( )
8484 }
@@ -90,7 +90,7 @@ final class RetryTests: XCTestCase {
9090 func testFailure_shouldRetryReturnsFalse_failureWithoutRetry( ) async throws {
9191 precondition ( Self . maxAttempts > 1 )
9292
93- try await assertThrowsErrorFake {
93+ try await assertThrows ( ErrorFake . self ) {
9494 try await retry ( with: testingConfiguration. withShouldRetry ( { _ in false } ) ) {
9595 throw ErrorFake ( )
9696 }
@@ -102,7 +102,7 @@ final class RetryTests: XCTestCase {
102102 func testFailure_isNotRetryableError_failureWithoutRetry( ) async throws {
103103 precondition ( Self . maxAttempts > 1 )
104104
105- try await assertThrowsErrorFake {
105+ try await assertThrows ( ErrorFake . self ) {
106106 try await retry ( with: testingConfiguration) {
107107 throw NotRetryable ( ErrorFake ( ) )
108108 }
@@ -130,7 +130,7 @@ final class RetryTests: XCTestCase {
130130 }
131131
132132 func testAllAttemptsFail_latestErrorIsRetryableError_throwsOriginalError( ) async throws {
133- try await assertThrowsErrorFake {
133+ try await assertThrows ( ErrorFake . self ) {
134134 try await retry ( with: testingConfiguration) {
135135 throw Retryable ( NotRetryable ( ErrorFake ( ) ) )
136136 }
@@ -139,8 +139,8 @@ final class RetryTests: XCTestCase {
139139 assertRetried ( times: Self . maxAttempts - 1 )
140140 }
141141
142- func testFailure_errorIsNotRetryableError_throwsOriginalError ( ) async throws {
143- try await assertThrowsErrorFake {
142+ func testFailure_isNotRetryableError_throwsOriginalError ( ) async throws {
143+ try await assertThrows ( ErrorFake . self ) {
144144 try await retry ( with: testingConfiguration) {
145145 throw NotRetryable ( Retryable ( ErrorFake ( ) ) )
146146 }
@@ -149,12 +149,82 @@ final class RetryTests: XCTestCase {
149149 assertRetried ( times: 0 )
150150 }
151151
152+ func testFailure_isCancellationError_failureWithoutRetry( ) async throws {
153+ precondition ( Self . maxAttempts > 1 )
154+
155+ try await assertThrows ( CancellationError . self) {
156+ try await retry ( with: testingConfiguration) {
157+ throw CancellationError ( )
158+ }
159+ }
160+
161+ assertRetried ( times: 0 )
162+ }
163+
164+ func testFailure_isCancellationErrorWrappedInRetryableError_failureWithoutRetry( ) async throws {
165+ precondition ( Self . maxAttempts > 1 )
166+
167+ try await assertThrows ( CancellationError . self) {
168+ try await retry ( with: testingConfiguration) {
169+ throw Retryable ( CancellationError ( ) )
170+ }
171+ }
172+
173+ assertRetried ( times: 0 )
174+ }
175+
176+ func testFailure_isCancellationErrorWrappedInNotRetryableError_failureWithoutRetry( ) async throws {
177+ precondition ( Self . maxAttempts > 1 )
178+
179+ try await assertThrows ( CancellationError . self) {
180+ try await retry ( with: testingConfiguration) {
181+ throw NotRetryable ( CancellationError ( ) )
182+ }
183+ }
184+
185+ assertRetried ( times: 0 )
186+ }
187+
188+ func testCancelledDuringSleep_immediateFailure( ) async throws {
189+ precondition ( Self . maxAttempts > 1 )
190+
191+ clockFake. isSleepEnabled = true
192+ let configuration = testingConfiguration. withBackoff ( . constant( . seconds( 60 ) ) )
193+
194+ let retryTask = Task {
195+ try await retry ( with: configuration) {
196+ throw ErrorFake ( )
197+ }
198+ }
199+
200+ // Wait until the retry task is sleeping after the first attempt.
201+ while clockFake. allSleepDurations. isEmpty {
202+ try await Task . sleep ( for: . milliseconds( 1 ) )
203+ }
204+
205+ retryTask. cancel ( )
206+
207+ let realClock = ContinuousClock ( )
208+ let start = realClock. now
209+
210+ try await assertThrows ( CancellationError . self) {
211+ try await retryTask. value
212+ }
213+
214+ let end = realClock. now
215+ let duration = end - start
216+ XCTAssertLessThan ( duration, . seconds( 1 ) )
217+ }
218+
152219 // MARK: - Assertions
153220
154- private func assertThrowsErrorFake( operation: ( ) async throws -> Void ) async throws {
221+ private func assertThrows< T: Error > (
222+ _ errorType: T . Type ,
223+ operation: ( ) async throws -> Void
224+ ) async throws {
155225 do {
156226 try await operation ( )
157- } catch is ErrorFake {
227+ } catch is T {
158228 // Expected.
159229 }
160230 }
0 commit comments