diff options
author | Paweł Stawicki <pawelstawicki@gmail.com> | 2018-01-24 17:33:01 +0100 |
---|---|---|
committer | Paweł Stawicki <pawelstawicki@gmail.com> | 2018-01-24 17:33:01 +0100 |
commit | e60dab5c77bca2c3b7a76732e98b406a9d7a095e (patch) | |
tree | 0dd47d5af5a7c788906e8d8775f361de863534c5 /core/src/test/scala | |
parent | 2eace89cb793ae365b71f614edc157a1b682e36b (diff) | |
download | sttp-e60dab5c77bca2c3b7a76732e98b406a9d7a095e.tar.gz sttp-e60dab5c77bca2c3b7a76732e98b406a9d7a095e.tar.bz2 sttp-e60dab5c77bca2c3b7a76732e98b406a9d7a095e.zip |
Allow SttpBackendStub to accept monad with response, not to hold thread when using e.g. Futures
Diffstat (limited to 'core/src/test/scala')
-rw-r--r-- | core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala index f3d2cd4..22ceff3 100644 --- a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala +++ b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala @@ -8,6 +8,8 @@ import com.softwaremill.sttp._ import org.scalatest.concurrent.ScalaFutures import org.scalatest.{FlatSpec, Matchers} +import scala.concurrent.Future + class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { private val testingStub = SttpBackendStub(HttpURLConnectionBackend()) .whenRequestMatches(_.uri.path.startsWith(List("a", "b"))) @@ -77,7 +79,7 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { it should "handle exceptions thrown instead of a response (synchronous)" in { implicit val s = SttpBackendStub(HttpURLConnectionBackend()) .whenRequestMatches(_ => true) - .thenRespond(throw new TimeoutException()) + .thenRespondWithMonad(throw new TimeoutException()) a[TimeoutException] should be thrownBy { sttp.get(uri"http://example.org").send() @@ -87,7 +89,7 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { it should "handle exceptions thrown instead of a response (asynchronous)" in { implicit val s = SttpBackendStub(new FutureMonad()) .whenRequestMatches(_ => true) - .thenRespond(throw new TimeoutException()) + .thenRespondWithMonad(throw new TimeoutException()) val result = sttp.get(uri"http://example.org").send() result.failed.futureValue shouldBe a[TimeoutException] @@ -155,6 +157,28 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { } + it should "not hold the calling thread when passed a future monad" in { + val LongTimeMillis = 10000L + + val fm = new FutureMonad() + val f = Future { + Thread.sleep(LongTimeMillis) + Response(Right("OK"), 200, "", Nil, Nil) + } + + val before = System.currentTimeMillis() + implicit val s = SttpBackendStub(fm).whenAnyRequest + .thenRespondWithMonad(f) + + val result = sttp + .get(uri"http://example.org") + .send() + + val after = System.currentTimeMillis() + + (after - before) should be < LongTimeMillis + } + private val testingStubWithFallback = SttpBackendStub .withFallback(testingStub) .whenRequestMatches(_.uri.path.startsWith(List("c"))) |