+doc,htp #19896 directives for RequestTimeout and docs

This commit is contained in:
Konrad Malawski 2016-02-26 18:38:24 +01:00
parent 27c004d274
commit 2d7d24dee6
24 changed files with 523 additions and 70 deletions

View file

@ -0,0 +1,13 @@
/*
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
*/
package docs
trait CompileOnlySpec {
/**
* Given a block of code... does NOT execute it.
* Useful when writing code samples in tests, which should only be compiled.
*/
def compileOnlySpec(body: => Unit) = ()
}

View file

@ -12,18 +12,18 @@ import akka.http.scaladsl.model._
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{ Flow, Sink }
import akka.testkit.TestActors
import docs.CompileOnlySpec
import org.scalatest.{ Matchers, WordSpec }
import scala.language.postfixOps
import scala.concurrent.{ ExecutionContext, Future }
class HttpServerExampleSpec extends WordSpec with Matchers {
class HttpServerExampleSpec extends WordSpec with Matchers
with CompileOnlySpec {
// never actually called
val log: LoggingAdapter = null
def compileOnlySpec(body: => Unit) = ()
"binding-example" in compileOnlySpec {
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer

View file

@ -6,6 +6,9 @@ package docs.http.scaladsl.server
import akka.http.scaladsl.server.Directives
import akka.http.scaladsl.testkit.ScalatestRouteTest
import docs.CompileOnlySpec
import org.scalatest.{ Matchers, WordSpec }
abstract class RoutingSpec extends WordSpec with Matchers with Directives with ScalatestRouteTest
abstract class RoutingSpec extends WordSpec with Matchers
with Directives with ScalatestRouteTest
with CompileOnlySpec

View file

@ -260,6 +260,7 @@ class BasicDirectivesExamplesSpec extends RoutingSpec {
//#1mapResponse-advanced
trait ApiRoutes {
protected def system: ActorSystem
private val log = Logging(system, "ApiRoutes")
private val NullJsonEntity = HttpEntity(ContentTypes.`application/json`, "{}")
@ -800,5 +801,4 @@ class BasicDirectivesExamplesSpec extends RoutingSpec {
//#
}
private def compileOnlySpec(block: => Unit) = pending
}
}

View file

@ -102,5 +102,4 @@ class FileAndResourceDirectivesExamplesSpec extends RoutingSpec {
}
}
private def compileOnlySpec(block: => Unit) = pending
}

View file

@ -0,0 +1,78 @@
/*
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
*/
package docs.http.scaladsl.server.directives
import akka.http.scaladsl.model.{ HttpResponse, StatusCodes }
import akka.http.scaladsl.server.RoutingSpec
import docs.CompileOnlySpec
import scala.concurrent.duration._
import scala.concurrent.{ Future, Promise }
class TimeoutDirectivesExamplesSpec extends RoutingSpec with CompileOnlySpec {
"Request Timeout" should {
"be configurable in routing layer" in compileOnlySpec {
//#withRequestTimeout-plain
val route =
path("timeout") {
withRequestTimeout(3.seconds) {
val response: Future[String] = slowFuture() // very slow
complete(response)
}
}
//#
}
"without timeout" in compileOnlySpec {
//#withoutRequestTimeout-1
val route =
path("timeout") {
withoutRequestTimeout {
val response: Future[String] = slowFuture() // very slow
complete(response)
}
}
//#
}
"allow mapping the response while setting the timeout" in compileOnlySpec {
//#withRequestTimeout-with-handler
val timeoutResponse = HttpResponse(StatusCodes.EnhanceYourCalm,
entity = "Unable to serve response within time limit, please enchance your calm.")
val route =
path("timeout") {
// updates timeout and handler at
withRequestTimeout(1.milli, request => timeoutResponse) {
val response: Future[String] = slowFuture() // very slow
complete(response)
}
}
//#
}
"allow mapping the response" in compileOnlySpec {
pending // compile only spec since requires actuall Http server to be run
//#withRequestTimeoutResponse
val timeoutResponse = HttpResponse(StatusCodes.EnhanceYourCalm,
entity = "Unable to serve response within time limit, please enchance your calm.")
val route =
path("timeout") {
withRequestTimeout(1.milli) {
withRequestTimeoutResponse(request => timeoutResponse) {
val response: Future[String] = slowFuture() // very slow
complete(response)
}
}
}
//#
}
}
def slowFuture(): Future[String] = Promise[String].future
}