Skip to content

Commit a4656cb

Browse files
authored
Retry docs' tests on the CI (#3618)
1 parent a9f3e4e commit a4656cb

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

modules/docs-tests/src/test/scala/sclicheck/DocTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DocTests extends munit.FunSuite {
3838
md <- inputs
3939
}
4040
test(s"$tpe ${md.toString.stripSuffix(".md")}") {
41-
checkFile(dir / md, options)
41+
TestUtil.retryOnCi()(checkFile(dir / md, options))
4242
}
4343

4444
}

modules/docs-tests/src/test/scala/sclicheck/TestUtil.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package sclicheck
22

3+
import scala.annotation.tailrec
4+
import scala.concurrent.duration.{DurationInt, FiniteDuration}
5+
36
object TestUtil {
7+
val isCI: Boolean = System.getenv("CI") != null
48

59
def withTmpDir[T](prefix: String)(f: os.Path => T): T = {
610
val tmpDir = os.temp.dir(prefix = prefix)
@@ -20,4 +24,36 @@ object TestUtil {
2024
sys.error("SCLICHECK_SCALA_CLI not set")
2125
}
2226

27+
def retry[T](
28+
maxAttempts: Int = 3,
29+
waitDuration: FiniteDuration = 5.seconds
30+
)(
31+
run: => T
32+
): T = {
33+
@tailrec
34+
def helper(count: Int): T =
35+
try run
36+
catch {
37+
case t: Throwable =>
38+
if (count >= maxAttempts) {
39+
System.err.println(s"$maxAttempts attempts failed, caught $t. Giving up.")
40+
throw new Exception(t)
41+
}
42+
else {
43+
val remainingAttempts = maxAttempts - count
44+
System.err.println(
45+
s"Caught $t, $remainingAttempts attempts remaining, trying again in $waitDuration"
46+
)
47+
Thread.sleep(waitDuration.toMillis)
48+
System.err.println(s"Trying attempt $count out of $maxAttempts...")
49+
helper(count + 1)
50+
}
51+
}
52+
53+
helper(1)
54+
}
55+
56+
def retryOnCi[T](maxAttempts: Int = 3, waitDuration: FiniteDuration = 5.seconds)(
57+
run: => T
58+
): T = retry(if (isCI) maxAttempts else 1, waitDuration)(run)
2359
}

0 commit comments

Comments
 (0)