Last active
November 28, 2022 23:52
-
-
Save thewisenerd/fea5a8dd7042c7e2010de5e0ff7e18ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.asynchttpclient.DefaultAsyncHttpClient | |
import org.asynchttpclient.DefaultAsyncHttpClientConfig | |
import org.asynchttpclient.RequestBuilder | |
import org.asynchttpclient.Response | |
import org.junit.Ignore | |
import org.junit.Test | |
import java.io.IOException | |
@Ignore | |
class TestClientTimeout { | |
@Test | |
fun naive() { | |
val rootLogger = | |
org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME) as ch.qos.logback.classic.Logger | |
rootLogger.level = ch.qos.logback.classic.Level.WARN | |
fun noEx(block: () -> Unit) { | |
try { | |
print("block: ") | |
block() | |
} catch (e: Exception) { | |
// pass | |
} | |
} | |
val request = RequestBuilder("GET").setUrl("http://httpbin.org/delay/2").build() | |
val safeRequest = RequestBuilder("GET").setUrl("http://httpbin.org/delay/0.1").build() | |
val client = DefaultAsyncHttpClient(DefaultAsyncHttpClientConfig.Builder().apply { | |
setRequestTimeout(1000) | |
}.build()) | |
noEx { | |
client.executeRequest(request).toCompletableFuture() | |
.whenComplete { _: Response?, t: Throwable? -> | |
println("whenComplete: " + t?.javaClass + "; cause: " + t?.cause?.javaClass) | |
} | |
.get() | |
} | |
noEx { | |
client.executeRequest(request).toCompletableFuture() | |
.exceptionally { t: Throwable -> | |
println("exceptionally: " + t.javaClass + "; cause: " + t.cause?.javaClass) | |
throw t | |
} | |
.get() | |
} | |
noEx { | |
print("chain.pre: ") | |
client.executeRequest(request).toCompletableFuture() | |
.whenComplete { _: Response?, t: Throwable? -> | |
// println("whenComplete: " + t?.javaClass + "; cause: " + t?.cause?.javaClass) | |
throw t!! | |
} | |
.whenComplete { _: Response?, t: Throwable? -> | |
println("whenComplete: " + t?.javaClass + "; cause: " + t?.cause?.javaClass) | |
} | |
.get() | |
} | |
noEx { | |
print("chain.pre: ") | |
client.executeRequest(request).toCompletableFuture() | |
.exceptionally { t: Throwable -> | |
// println("exceptionally: " + t.javaClass + "; cause: " + t.cause?.javaClass) | |
throw t | |
} | |
.exceptionally { t: Throwable -> | |
println("exceptionally: " + t.javaClass + "; cause: " + t.cause?.javaClass) | |
throw t | |
} | |
.get() | |
} | |
noEx { | |
print("chain.thenApply: ") | |
client.executeRequest(request).toCompletableFuture() | |
.thenApply { r: Response -> | |
r | |
} | |
.whenComplete { _: Response?, t: Throwable? -> | |
println("whenComplete: " + t?.javaClass + "; cause: " + t?.cause?.javaClass) | |
} | |
.get() | |
} | |
noEx { | |
print("chain.thenApply: ") | |
client.executeRequest(request).toCompletableFuture() | |
.thenApply { r: Response -> | |
r | |
} | |
.exceptionally { t: Throwable -> | |
println("exceptionally: " + t.javaClass + "; cause: " + t.cause?.javaClass) | |
throw t | |
} | |
.get() | |
} | |
noEx { | |
print("chain.thenApplyException: ") | |
client.executeRequest(request).toCompletableFuture() | |
.thenApply { _: Response -> | |
throw IOException("testing") | |
} | |
.whenComplete { _: Response?, t: Throwable? -> | |
println("whenComplete: " + t?.javaClass + "; cause: " + t?.cause?.javaClass) | |
} | |
.get() | |
} | |
noEx { | |
print("chain.thenApplyException: ") | |
client.executeRequest(request).toCompletableFuture() | |
.thenApply { _: Response -> | |
throw IOException("testing") | |
} | |
.exceptionally { t: Throwable -> | |
println("exceptionally: " + t.javaClass + "; cause: " + t.cause?.javaClass) | |
throw t | |
} | |
.get() | |
} | |
noEx { | |
print("safe: ") | |
client.executeRequest(safeRequest).toCompletableFuture() | |
.whenComplete { _: Response?, t: Throwable? -> | |
println("whenComplete: " + t?.javaClass + "; cause: " + t?.cause?.javaClass) | |
} | |
.get() | |
} | |
noEx { | |
print("safe: ") | |
client.executeRequest(safeRequest).toCompletableFuture() | |
.exceptionally { t: Throwable -> | |
println("exceptionally: " + t.javaClass + "; cause: " + t.cause?.javaClass) | |
throw t | |
} | |
.get() | |
// aa | |
println("") | |
} | |
noEx { | |
print("safe.chain.thenApplyException: ") | |
client.executeRequest(safeRequest).toCompletableFuture() | |
.thenApply { _: Response -> | |
throw IOException("testing") | |
} | |
.whenComplete { _: Response?, t: Throwable? -> | |
println("whenComplete: " + t?.javaClass + "; cause: " + t?.cause?.javaClass) | |
} | |
.get() | |
} | |
noEx { // safe.chain.thenApplyException: exceptionally: class java.util.concurrent.CompletionException | |
print("safe.chain.thenApplyException: ") | |
client.executeRequest(safeRequest).toCompletableFuture() | |
.thenApply { _: Response -> | |
throw IOException("testing") | |
} | |
.exceptionally { t: Throwable -> | |
println("exceptionally: " + t.javaClass + "; cause: " + t.cause?.javaClass) | |
throw t | |
} | |
.get() | |
} | |
client.close() | |
} | |
} |
reference: https://stackoverflow.com/a/49261367
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.