Skip to content

Instantly share code, notes, and snippets.

@thewisenerd
Last active November 28, 2022 23:52
Show Gist options
  • Save thewisenerd/fea5a8dd7042c7e2010de5e0ff7e18ab to your computer and use it in GitHub Desktop.
Save thewisenerd/fea5a8dd7042c7e2010de5e0ff7e18ab to your computer and use it in GitHub Desktop.
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()
}
}
@thewisenerd
Copy link
Author

thewisenerd commented Nov 28, 2022

block: whenComplete: class java.util.concurrent.TimeoutException; cause: null
block: exceptionally: class java.util.concurrent.TimeoutException; cause: null
block: chain.pre: whenComplete: class java.util.concurrent.CompletionException; cause: class java.util.concurrent.TimeoutException
block: chain.pre: exceptionally: class java.util.concurrent.CompletionException; cause: class java.util.concurrent.TimeoutException
block: chain.thenApply: whenComplete: class java.util.concurrent.CompletionException; cause: class java.util.concurrent.TimeoutException
block: chain.thenApply: exceptionally: class java.util.concurrent.CompletionException; cause: class java.util.concurrent.TimeoutException
block: chain.thenApplyException: whenComplete: class java.util.concurrent.CompletionException; cause: class java.util.concurrent.TimeoutException
block: chain.thenApplyException: exceptionally: class java.util.concurrent.CompletionException; cause: class java.util.concurrent.TimeoutException
block: safe: whenComplete: null; cause: null
block: safe: 
block: safe.chain.thenApplyException: whenComplete: class java.util.concurrent.CompletionException; cause: class java.io.IOException
block: safe.chain.thenApplyException: exceptionally: class java.util.concurrent.CompletionException; cause: class java.io.IOException

@thewisenerd
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment