Skip to content

Commit 768fcfb

Browse files
committed
Add compiler contract to runRetrying
1 parent d9cf848 commit 768fcfb

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

kotlin-retry/src/commonMain/kotlin/com/github/michaelbull/retry/Retry.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import kotlin.coroutines.cancellation.CancellationException
1313

1414
/**
1515
* Calls the specified function [block] and returns its result if invocation was successful, catching any [Throwable]
16-
* exception that was thrown from the [block] function executing and retrying the invocation according to
16+
* exception that was thrown from the [block] function execution and retrying the invocation according to
1717
* [instructions][RetryInstruction] from the [policy].
1818
*/
1919
public suspend inline fun <T> retry(policy: RetryPolicy<Throwable>, block: () -> T): T {

kotlin-retry/src/commonMain/kotlin/com/github/michaelbull/retry/RunRetrying.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ package com.github.michaelbull.retry
22

33
import com.github.michaelbull.retry.instruction.RetryInstruction
44
import com.github.michaelbull.retry.policy.RetryPolicy
5+
import kotlin.contracts.InvocationKind
6+
import kotlin.contracts.contract
57

68
/**
79
* Calls the specified function [block] and returns its result if invocation was successful, catching any [Throwable]
8-
* exception that was thrown from the [block] function executing and retrying the invocation according to
10+
* exception that was thrown from the [block] function execution and retrying the invocation according to
911
* [instructions][RetryInstruction] from the [policy].
1012
*/
1113
public suspend inline fun <T> runRetrying(policy: RetryPolicy<Throwable>, block: () -> T): T {
14+
contract {
15+
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
16+
}
17+
1218
return retry(policy, block)
1319
}

kotlin-retry/src/commonTest/kotlin/com/github/michaelbull/retry/RetryTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ class RetryTest {
151151

152152
if (attempts == 15) {
153153
cancel()
154-
} else {
155-
throw AttemptsException(attempts)
156154
}
155+
156+
throw AttemptsException(attempts)
157157
}
158158
}
159159

@@ -170,7 +170,7 @@ class RetryTest {
170170
}
171171

172172
@Test
173-
fun cancelRetryFromWithinChildJob() = runTest {
173+
fun cancelRetryWithinChildJob() = runTest {
174174
val every20ms = constantDelay<Throwable>(20)
175175
var attempts = 0
176176

0 commit comments

Comments
 (0)