+doc #20466 akka http java dsl example snippets timeout directives (#20812)

* #20466 added checks to the TimeoutDirectivesExampleSpec

* #20466 added missing Java directive wrapper, added Java test, added some comments to Scala spec

* #20466 test minor fixes (typos, comments, unused code)

* #20466 replaced placeholders with proper snippets in Java docs

* #20466 added proper withRequestTimeout example to JavaTestServer

* #20466 comment - clarifies the request-timeout = infinite case

* #20466 post-review changes (#20812)
This commit is contained in:
gosubpl 2016-06-22 01:55:28 +02:00 committed by Konrad Malawski
parent ee77273fac
commit 4b89fcd643
7 changed files with 273 additions and 18 deletions

View file

@ -26,11 +26,18 @@ public class JavaTestServer extends AllDirectives { // or import static Directiv
final Route index = path("", () ->
withRequestTimeout(timeout, this::mkTimeoutResponse, () -> {
silentSleep(5000); // too long, trigger failure
silentSleep(5000); // too long, but note that this will NOT activate withRequestTimeout, see below
return complete(index());
})
);
final Route requestTimeout = path("timeout", () ->
withRequestTimeout(timeout, this::mkTimeoutResponse, () -> {
// here timeout will work
return completeOKWithFutureString(neverEndingFuture(index()));
})
);
final Function<Optional<ProvidedCredentials>, Optional<String>> handleAuth = (maybeCreds) -> {
if (maybeCreds.isPresent() && maybeCreds.get().verify("pa$$word")) // some secure hash + check
return Optional.of(maybeCreds.get().identifier());
@ -58,7 +65,7 @@ public class JavaTestServer extends AllDirectives { // or import static Directiv
return get(() ->
index.orElse(secure).orElse(ping).orElse(crash).orElse(inner)
index.orElse(secure).orElse(ping).orElse(crash).orElse(inner).orElse(requestTimeout)
);
}
@ -70,10 +77,14 @@ public class JavaTestServer extends AllDirectives { // or import static Directiv
}
}
private CompletableFuture<String> neverEndingFuture(String futureContent) {
return new CompletableFuture<>().thenApply((string) -> futureContent);
}
private HttpResponse mkTimeoutResponse(HttpRequest request) {
return HttpResponse.create()
.withStatus(StatusCodes.ENHANCE_YOUR_CALM)
.withEntity("Unable to serve response within time limit, please enchance your calm.");
.withEntity("Unable to serve response within time limit, please enhance your calm.");
}
private String index() {
@ -85,6 +96,7 @@ public class JavaTestServer extends AllDirectives { // or import static Directiv
" <li><a href=\"/ping\">/ping</a></li>\n" +
" <li><a href=\"/secure\">/secure</a> Use any username and '&lt;username&gt;-password' as credentials</li>\n" +
" <li><a href=\"/crash\">/crash</a></li>\n" +
" <li><a href=\"/timeout\">/timeout</a> Demonstrates timeout </li>\n" +
" </ul>\n" +
" </body>\n" +
" </html>\n";