Skip to content

Commit

Permalink
[fix] Retry flaky test (#1874)
Browse files Browse the repository at this point in the history
  • Loading branch information
kciesielski authored Jun 30, 2023
1 parent a1bffc9 commit eaadfb1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
if: matrix.target-platform == 'JVM'
run: sbt -v compileDocs
- name: Test
run: sbt -v "testScoped ${{ matrix.scala-version }} ${{ matrix.target-platform }}"
run: sbt -v "testScoped ${{ matrix.scala-version }} ${{ matrix.target-platform }}"
- name: Prepare release notes
uses: release-drafter/release-drafter@v5
with:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sttp.client4.testing.streaming

import org.scalatest.Assertion
import org.scalatest.BeforeAndAfterAll
import org.scalatest.freespec.AsyncFreeSpec
import org.scalatest.matchers.should.Matchers
Expand All @@ -10,8 +11,9 @@ import sttp.client4.testing.HttpTest.endpoint
import sttp.client4.testing.streaming.StreamingTest._
import sttp.client4.testing.{ConvertToFuture, ToFutureWrapper}
import sttp.model.sse.ServerSentEvent
import sttp.monad.MonadError
import sttp.monad.{FutureMonad, MonadError}
import sttp.monad.syntax._
import scala.concurrent.Future

abstract class StreamingTest[F[_], S]
extends AsyncFreeSpec
Expand Down Expand Up @@ -189,6 +191,17 @@ abstract class StreamingTest[F[_], S]
val numChunks = 100
val url = uri"https://httpbin.org/stream/$numChunks"

implicit val monadError: MonadError[Future] = new FutureMonad

def retryImmediatelyOnError[A](action: => Future[A], retriesLeft: Int): Future[A] =
action.handleError { case error =>
new Exception(s"Error in ${getClass.getSimpleName}, retries left = $retriesLeft", error).printStackTrace
if (retriesLeft > 1)
retryImmediatelyOnError(action, retriesLeft - 1)
else
action
}

// TODO: for some reason these explicit types are needed in Dotty
val r0: StreamRequest[streams.BinaryStream, S] = basicRequest
// of course, you should never rely on the internet being available
Expand All @@ -197,7 +210,7 @@ abstract class StreamingTest[F[_], S]
.get(url)
.response(asStreamAlwaysUnsafe(streams))

r0
def runTest: Future[Assertion] = r0
.send(backend)
.toFuture()
.flatMap { response =>
Expand All @@ -208,6 +221,7 @@ abstract class StreamingTest[F[_], S]

urlRegex.findAllIn(responseBody).length shouldBe numChunks
}
retryImmediatelyOnError(runTest, retriesLeft = 5)
}

"lift errors due to mapping with impure functions into the response monad" in {
Expand Down

0 comments on commit eaadfb1

Please sign in to comment.