-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [SCALA-316] A Guide to the Retry Library * [SCALA-316] Use AtomicInteger instead of mocks in tests * [SCALA-316] Report known issue of retry library and fix tests * Update PrimeNumberRetry.scala * [SCALA-316] Move retry code to scala-libraries-3 * [SCALA-316] Fix dependencies Co-authored-by: Stefanos Georgakis <[email protected]> Co-authored-by: Grzegorz Piwowarek <[email protected]>
- Loading branch information
1 parent
0bd674e
commit a132a74
Showing
4 changed files
with
130 additions
and
1 deletion.
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
55 changes: 55 additions & 0 deletions
55
scala-libraries-3/src/main/scala/com/baeldung/scala/retry/PrimeNumberRetry.scala
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,55 @@ | ||
package com.baeldung.scala.retry | ||
|
||
import retry.{Policy, Success} | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.Future | ||
import scala.concurrent.duration._ | ||
import scala.util.Random | ||
|
||
object PrimeNumberRetry { | ||
|
||
implicit val success: Success[Int] = { | ||
val successA = retry.Success[Int](_ == 0) | ||
val successB = retry.Success[Int](isPrime) | ||
successA.or(successB) | ||
} | ||
|
||
def generateRandomNumber: Future[Int] = | ||
outerPolicy.apply(Future { | ||
val number = Random.nextInt() | ||
if (number < 0) { | ||
throw new UnsupportedOperationException( | ||
s"Got a negative number: $number" | ||
) | ||
} else if (number > 100) { | ||
throw new IllegalArgumentException( | ||
s"Expected number within the [0-100] range. Got $number" | ||
) | ||
} | ||
number | ||
}) | ||
|
||
def isPrime(number: Int): Boolean = { | ||
var prime = true | ||
for (i <- 2 until number if number % i == 0) { | ||
prime = false | ||
} | ||
prime | ||
} | ||
|
||
val policy: Policy = retry | ||
.When { | ||
case _: UnsupportedOperationException => | ||
retry.Backoff.apply(5, 5.milliseconds) | ||
case _: IllegalArgumentException => | ||
retry.Directly.apply(5) | ||
case _ => | ||
retry.JitterBackoff.apply(5, 5.milliseconds) | ||
} | ||
|
||
val outerPolicy: Policy = retry.FailFast(policy) { | ||
case _: NumberFormatException => true | ||
} | ||
} | ||
|
72 changes: 72 additions & 0 deletions
72
scala-libraries-3/src/test/scala/com/baeldung/scala/retry/PrimeNumberRetryTest.scala
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,72 @@ | ||
package com.baeldung.scala.retry | ||
|
||
import com.baeldung.scala.retry.PrimeNumberRetry.success | ||
import org.scalatest.matchers.must.Matchers | ||
import org.scalatest.wordspec.AsyncWordSpec | ||
|
||
import java.util.concurrent.atomic.AtomicInteger | ||
import scala.concurrent.Future | ||
|
||
class PrimeNumberRetryTest extends AsyncWordSpec with Matchers { | ||
"PrimeNumberRetry" should { | ||
|
||
"FailFast when an NumberFormatException is thrown" in { | ||
val counter = new AtomicInteger(0) | ||
val result = PrimeNumberRetry.outerPolicy.apply(Future { | ||
counter.incrementAndGet() | ||
throw new NumberFormatException | ||
}) | ||
result | ||
.map(_ => assert(false)) | ||
.recover { | ||
case _: NumberFormatException => | ||
assert(counter.get() == 1) | ||
} | ||
} | ||
|
||
/* | ||
* This test is affected by <a href="https://github.com/softwaremill/retry/issues/31">this issue</a> | ||
*/ | ||
"retry in any exception" in { | ||
val counter = new AtomicInteger(0) | ||
val result = PrimeNumberRetry.outerPolicy.apply(Future { | ||
counter.incrementAndGet() | ||
throw new IllegalArgumentException | ||
}) | ||
result | ||
.map(_ => assert(false)) | ||
.recover { | ||
case _: IllegalArgumentException => | ||
assert(counter.get() == 7) | ||
} | ||
} | ||
|
||
"succeed when a prime is returned" in { | ||
val counter = new AtomicInteger(0) | ||
val result = PrimeNumberRetry.outerPolicy.apply(Future { | ||
counter.incrementAndGet() | ||
7 | ||
}) | ||
result | ||
.map(_ => { | ||
assert(counter.get() == 1) | ||
}) | ||
} | ||
|
||
/* | ||
* This test is affected by <a href="https://github.com/softwaremill/retry/issues/31">this issue</a> | ||
*/ | ||
"fail when a non prime number is returned" in { | ||
val counter = new AtomicInteger(0) | ||
val result = PrimeNumberRetry.outerPolicy.apply(Future { | ||
counter.incrementAndGet() | ||
10 | ||
}) | ||
result | ||
.flatMap(number => { | ||
assert(counter.get() == 7) | ||
assert(number == 10) | ||
}) | ||
} | ||
} | ||
} |