Skip to content

Commit

Permalink
Tests for timeouts to/from ledger in JSON API failure tests (digital-…
Browse files Browse the repository at this point in the history
…asset#7791)

* Tests for timeouts to/from ledger in JSON API failure tests

changelog_begin
changelog_end

* Update ledger-service/http-json-testing/src/main/scala/com/daml/http/HttpServiceTestFixture.scala

Co-authored-by: Stephen Compall <stephen.compall@daml.com>

* Update ledger-service/http-json-testing/src/main/scala/com/daml/http/HttpServiceTestFixture.scala

Co-authored-by: Stephen Compall <stephen.compall@daml.com>

* Apply suggestions from code review

Co-authored-by: Stephen Compall <stephen.compall@daml.com>
  • Loading branch information
cocreature and S11001001 authored Oct 26, 2020
1 parent 667e29b commit 339704d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ object HttpServiceTestFixture extends LazyLogging with Assertions with Inside {
}
}

def postJsonStringRequest(uri: Uri, jsonString: String, headers: List[HttpHeader])(
def postJsonStringRequestEncoded(uri: Uri, jsonString: String, headers: List[HttpHeader])(
implicit as: ActorSystem,
ec: ExecutionContext,
mat: Materializer): Future[(StatusCode, JsValue)] = {
mat: Materializer): Future[(StatusCode, String)] = {
logger.info(s"postJson: ${uri.toString} json: ${jsonString: String}")
Http()
.singleRequest(
Expand All @@ -305,10 +305,18 @@ object HttpServiceTestFixture extends LazyLogging with Assertions with Inside {
)
.flatMap { resp =>
val bodyF: Future[String] = getResponseDataBytes(resp, debug = true)
bodyF.map(body => (resp.status, body.parseJson))
bodyF.map(body => (resp.status, body))
}
}

def postJsonStringRequest(uri: Uri, jsonString: String, headers: List[HttpHeader])(
implicit as: ActorSystem,
ec: ExecutionContext,
mat: Materializer): Future[(StatusCode, JsValue)] =
postJsonStringRequestEncoded(uri, jsonString, headers).map {
case (status, body) => (status, body.parseJson)
}

def postJsonRequest(uri: Uri, json: JsValue, headers: List[HttpHeader])(
implicit as: ActorSystem,
ec: ExecutionContext,
Expand Down
42 changes: 42 additions & 0 deletions ledger-service/http-json/src/failure/scala/http/FailureTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import scala.concurrent.{Future, Promise}
import scala.util.{Failure, Success}
import com.daml.http.domain.Offset
import com.daml.http.json.{JsonError, SprayJson}
import com.daml.http.util.FutureUtil
import com.daml.ledger.api.testing.utils.SuiteResourceManagementAroundAll
import com.daml.timer.RetryStrategy
import eu.rekawek.toxiproxy.model.ToxicDirection
import org.scalatest._
import org.scalatest.concurrent.Eventually
import scalaz.\/
Expand Down Expand Up @@ -72,6 +74,46 @@ final class FailureTests
} yield succeed
}

"Command submission timeouts" in withHttpService { (uri, encoder, _, client) =>
import encoder.implicits._
import json.JsonProtocol._
for {
p <- allocateParty(client, "Alice")
(status, _) <- postCreateCommand(
accountCreateCommand(p, "23"),
encoder,
uri,
headersWithParties(List(p.unwrap)))
_ = status shouldBe StatusCodes.OK
// Client -> Server connection
_ = proxy.toxics().timeout("timeout", ToxicDirection.UPSTREAM, 0)
body <- FutureUtil.toFuture(SprayJson.encode1(accountCreateCommand(p, "24"))): Future[JsValue]
(status, output) <- postJsonStringRequestEncoded(
uri.withPath(Uri.Path("/v1/create")),
body.compactPrint,
headersWithParties(List(p.unwrap)))
_ = status shouldBe StatusCodes.ServiceUnavailable
_ = println(output.toString)
_ = output shouldBe "The server was not able to produce a timely response to your request.\r\nPlease try again in a short while!"
_ = proxy.toxics().get("timeout").remove()
(status, _) <- postCreateCommand(
accountCreateCommand(p, "25"),
encoder,
uri,
headersWithParties(List(p.unwrap)))
_ = status shouldBe StatusCodes.OK
// Server -> Client connection
_ = proxy.toxics().timeout("timeout", ToxicDirection.DOWNSTREAM, 0)
(status, output) <- postJsonStringRequestEncoded(
uri.withPath(Uri.Path("/v1/create")),
body.compactPrint,
headersWithParties(List(p.unwrap)))
_ = status shouldBe StatusCodes.ServiceUnavailable
_ = println(output.toString)
_ = output shouldBe "The server was not able to produce a timely response to your request.\r\nPlease try again in a short while!"
} yield succeed
}

"/v1/query GET succeeds after reconnect" in withHttpService[Assertion] {
(uri, encoder, _, client) =>
for {
Expand Down

0 comments on commit 339704d

Please sign in to comment.