Skip to content

Commit 24e2813

Browse files
authored
Merge pull request #397 from eed3si9n/wip/retry
fix: Retry to retry non-IOException
2 parents 88fbe96 + e4da4c5 commit 24e2813

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

build.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ val io = (project in file("io"))
8080
commonSettings,
8181
name := "IO",
8282
libraryDependencies ++= {
83-
Vector(scalaCompiler.value % Test, scalaCheck % Test, scalatest % Test)
83+
Vector(scalaCompiler.value % Test, scalaVerify % Test, scalaCheck % Test, scalatest % Test)
8484
} ++ Vector(swovalFiles),
85+
testFrameworks += new TestFramework("verify.runner.Framework"),
8586
Test / fork := System.getProperty("sbt.test.fork", "false") == "true",
8687
Test / testForkedParallel := true,
8788
Compile / generateContrabands / sourceManaged := baseDirectory.value / "src" / "main" / "contraband-scala",
@@ -106,6 +107,7 @@ val io = (project in file("io"))
106107
"1.7.0",
107108
"1.8.0",
108109
"1.9.0",
110+
"1.10.0",
109111
) map (version => organization.value %% moduleName.value % version)
110112
}),
111113
mimaBinaryIssueFilters ++= Seq(

io/src/main/scala/sbt/internal/io/Retry.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,33 @@ private[sbt] object Retry {
2020
try System.getProperty("sbt.io.retry.limit", defaultLimit.toString).toInt
2121
catch { case NonFatal(_) => defaultLimit }
2222
}
23-
private[sbt] def apply[@specialized T](f: => T, excludedExceptions: Class[? <: IOException]*): T =
23+
private[sbt] def apply[@specialized T](f: => T, excludedExceptions: Class[? <: Throwable]*): T =
2424
apply(f, limit, excludedExceptions: _*)
2525
private[sbt] def apply[@specialized T](
2626
f: => T,
2727
limit: Int,
28-
excludedExceptions: Class[? <: IOException]*,
28+
excludedExceptions: Class[? <: Throwable]*,
2929
): T = apply(f, limit, 100, excludedExceptions: _*)
3030
private[sbt] def apply[@specialized T](
3131
f: => T,
3232
limit: Int,
3333
sleepInMillis: Long,
34-
excludedExceptions: Class[? <: IOException]*,
34+
excludedExceptions: Class[? <: Throwable]*,
3535
): T = {
3636
require(limit >= 1, "limit must be 1 or higher: was: " + limit)
37-
def filter(e: Exception): Boolean = excludedExceptions match {
37+
def filter(e: Throwable): Boolean = excludedExceptions match {
3838
case s if s.nonEmpty =>
3939
!excludedExceptions.exists(_.isAssignableFrom(e.getClass))
4040
case _ =>
4141
true
4242
}
4343
var attempt = 1
44-
var firstException: IOException = null
44+
var firstException: Throwable = null
4545
while (attempt <= limit) {
4646
try {
4747
return f
4848
} catch {
49-
case e: IOException if filter(e) =>
49+
case NonFatal(e) if filter(e) =>
5050
if (firstException == null) firstException = e
5151
// https://github.com/sbt/io/issues/295
5252
// On Windows, we're seeing java.nio.file.AccessDeniedException with sleep(0).

io/src/test/scala/sbt/internal/io/RetrySpec.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ package sbt.internal.io
1414
import java.io.IOException
1515
import java.util.concurrent.atomic.AtomicInteger
1616

17-
import org.scalatest.flatspec.AnyFlatSpec
18-
19-
final class RetrySpec extends AnyFlatSpec {
17+
object RetrySpec extends verify.BasicTestSuite {
2018
private val noExcluded: List[Class[? <: IOException]] = List[Class[? <: IOException]]()
21-
"retry" should "throw first exception after number of failures" in {
19+
test("retry should throw first exception after number of failures") {
2220
val i = new AtomicInteger()
2321
def throww(): Any = throw new IOException(i.incrementAndGet().toString)
2422
try {
@@ -31,7 +29,7 @@ final class RetrySpec extends AnyFlatSpec {
3129
}
3230
}
3331

34-
"retry" should "throw recover" in {
32+
test("retry should throw recover") {
3533
for (recoveryStep <- (1 to 14)) {
3634
val i = new AtomicInteger()
3735
val value = Retry(
@@ -46,4 +44,13 @@ final class RetrySpec extends AnyFlatSpec {
4644
assert(value == "recover")
4745
}
4846
}
47+
48+
test("retry should recover from non-IO exceptions") {
49+
val i = new AtomicInteger()
50+
def throww(): Any =
51+
if (i.incrementAndGet() == 5) 0
52+
else ???
53+
Retry(throww())
54+
()
55+
}
4956
}

project/Dependencies.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ object Dependencies {
1111
"org.scala-lang" % "scala-compiler" % v
1212
}
1313

14+
val scalaVerify = "com.eed3si9n.verify" %% "verify" % "1.0.0"
1415
val scalaCheck = "org.scalacheck" %% "scalacheck" % "1.18.1"
1516
val scalatest = "org.scalatest" %% "scalatest" % "3.2.19"
1617
val swovalFiles = "com.swoval" % "file-tree-views" % "2.1.12"

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.9.8
1+
sbt.version=1.10.7

0 commit comments

Comments
 (0)