-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add side effect callbacks during retry (#106)
Co-authored-by: Adam Warski <adam@warski.org>
- Loading branch information
Showing
6 changed files
with
158 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package ox.retry | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.{EitherValues, TryValues} | ||
import ox.retry.* | ||
|
||
class OnRetryTest extends AnyFlatSpec with Matchers with EitherValues with TryValues: | ||
behavior of "RetryPolicy onRetry callback" | ||
|
||
it should "retry a succeeding function with onRetry callback" in { | ||
// given | ||
var onRetryInvocationCount = 0 | ||
|
||
var counter = 0 | ||
val successfulResult = 42 | ||
|
||
def f = | ||
counter += 1 | ||
successfulResult | ||
|
||
var returnedResult: Either[Throwable, Int] = null | ||
def onRetry(attempt: Int, result: Either[Throwable, Int]): Unit = | ||
onRetryInvocationCount += 1 | ||
returnedResult = result | ||
|
||
// when | ||
val result = retry(f)(RetryPolicy(Schedule.Immediate(3), onRetry = onRetry)) | ||
|
||
// then | ||
result shouldBe successfulResult | ||
counter shouldBe 1 | ||
|
||
onRetryInvocationCount shouldBe 1 | ||
returnedResult shouldBe Right(successfulResult) | ||
} | ||
|
||
it should "retry a failing function with onRetry callback" in { | ||
// given | ||
var onRetryInvocationCount = 0 | ||
|
||
var counter = 0 | ||
val failedResult = new RuntimeException("boom") | ||
|
||
def f = | ||
counter += 1 | ||
if true then throw failedResult | ||
|
||
var returnedResult: Either[Throwable, Unit] = null | ||
def onRetry(attempt: Int, result: Either[Throwable, Unit]): Unit = | ||
onRetryInvocationCount += 1 | ||
returnedResult = result | ||
|
||
// when | ||
val result = the[RuntimeException] thrownBy retry(f)(RetryPolicy(Schedule.Immediate(3), onRetry = onRetry)) | ||
|
||
// then | ||
result shouldBe failedResult | ||
counter shouldBe 4 | ||
|
||
onRetryInvocationCount shouldBe 4 | ||
returnedResult shouldBe Left(failedResult) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters