我开始在Vertx的项目中使用TDD并编写了以下测试,但似乎没有来自我创建的服务器的响应并且线程被阻止。如何向localhost服务器发送请求?
@Test
public void testExecuteRequest(TestContext ctx) {
Vertx vertx = Vertx.vertx();
vertx.createHttpServer(new HttpServerOptions().setLogActivity(true))
.requestHandler(httpServerRequest -> httpServerRequest.response().end("callback"))
.listen(7777, "localhost", handler -> {
ctx.async().countDown();
if (handler.succeeded()) {
HttpUtils.executeRequest(vertx.createHttpClient(new HttpClientOptions().setLogActivity(true)),
HttpMethod.GET,
"localhost",
7777, "",
null, null, null)
.subscribe(response -> ctx.assertEquals(200, response.statusCode()),
ctx::fail,
ctx.async()::complete);
} else {
ctx.fail(handler.cause());
}
});
ctx.async().awaitSuccess();
}
它从实用程序类测试方法:
public Observable<HttpClientResponse> executeRequest(HttpClient client, HttpMethod method, String host, Integer
port, String uri, @Nullable byte[] body, @Nullable LinkedMultiValueMap<String, String> headers, @Nullable Integer timeout) {
CompletableFuture<HttpClientResponse> responseFuture = new CompletableFuture<>();
HttpClientRequest request = client.request(method, port, host, uri, responseFuture::complete);
Optional.ofNullable(headers).ifPresent(value -> headers.forEach(request::putHeader));
Optional.ofNullable(body).ifPresent(value -> request.write(Buffer.buffer(body)));
Optional.ofNullable(timeout).ifPresent(request::setTimeout);
request.end();
return Observable.fromFuture(responseFuture);
}
但是当我开始这个测试时,我得到了以下控制台输出:
11:07:48.839 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
11:07:48.844 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
11:07:48.846 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4
11:07:48.914 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 16
11:07:48.966 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
11:07:48.966 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
11:07:48.995 [main] DEBUG io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false
11:07:48.995 [main] DEBUG io.netty.util.internal.PlatformDependent0 - Java version: 8
11:07:48.997 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
11:07:48.998 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
11:07:48.999 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
11:07:48.999 [main] DEBUG io.netty.util.internal.PlatformDependent0 - direct buffer constructor: available
11:07:49.000 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
11:07:49.000 [main] DEBUG io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable prior to Java9
11:07:49.000 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available
11:07:49.000 [main] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available
11:07:49.000 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: /tmp (java.io.tmpdir)
11:07:49.000 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
11:07:49.002 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
11:07:49.002 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: 1836580864 bytes
11:07:49.002 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1
11:07:49.002 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available
11:07:49.011 [main] DEBUG io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
11:07:49.062 [main] DEBUG io.netty.resolver.dns.DefaultDnsServerAddressStreamProvider - Default DNS servers: [/127.0.0.53:53] (sun.net.dns.ResolverConfiguration)
11:07:49.071 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
11:07:49.071 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false
11:07:49.073 [main] DEBUG io.netty.util.NetUtil - Loopback interface: lo (lo, 0:0:0:0:0:0:0:1%lo)
11:07:49.074 [main] DEBUG io.netty.util.NetUtil - /proc/sys/net/core/somaxconn: 128
11:07:49.366 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 16
11:07:49.366 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 16
11:07:49.366 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192
11:07:49.366 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 11
11:07:49.366 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 16777216
11:07:49.366 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.tinyCacheSize: 512
11:07:49.366 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256
11:07:49.366 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64
11:07:49.366 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
11:07:49.366 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192
11:07:49.366 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: true
11:07:49.372 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
11:07:49.372 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
11:07:49.459 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 9561 (auto-detected)
11:07:49.462 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: 0c:d2:92:ff:fe:6c:35:f9 (auto-detected)
11:07:49.487 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
11:07:49.487 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
11:07:49.487 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
11:07:49.686 [vert.x-eventloop-thread-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.bytebuf.checkAccessible: true
11:07:49.688 [vert.x-eventloop-thread-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@51aecd1c
11:07:49.711 [vert.x-eventloop-thread-1] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0xd06045a0] REGISTERED
Jun 11, 2018 11:07:51 AM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-1,5,main] has been blocked for 2364 ms, time limit is 2000
Jun 11, 2018 11:07:52 AM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-1,5,main] has been blocked for 3364 ms, time limit is 2000
Jun 11, 2018 11:07:53 AM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-1,5,main] has been blocked for 4365 ms, time limit is 2000
Jun 11, 2018 11:07:54 AM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-1,5,main] has been blocked for 5365 ms, time limit is 2000
io.vertx.core.VertxException: Thread blocked
at sun.misc.Unsafe.park(Native Method)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.CompletableFuture$Signaller.block(CompletableFuture.java:1693)
at java.util.concurrent.ForkJoinPool.managedBlock(ForkJoinPool.java:3323)
at java.util.concurrent.CompletableFuture.waitingGet(CompletableFuture.java:1729)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)
at io.reactivex.internal.operators.observable.ObservableFromFuture.subscribeActual(ObservableFromFuture.java:41)
at io.reactivex.Observable.subscribe(Observable.java:12051)
at io.reactivex.Observable.subscribe(Observable.java:12037)
at io.reactivex.Observable.subscribe(Observable.java:11997)
at exchange.kanga.kangaroo.HttpUtilsTest.lambda$testExecuteRequest$2(HttpUtilsTest.java:30)
at exchange.kanga.kangaroo.HttpUtilsTest$$Lambda$13/1074593562.handle(Unknown Source)
at io.vertx.core.http.impl.HttpServerImpl.lambda$null$4(HttpServerImpl.java:382)
at io.vertx.core.http.impl.HttpServerImpl$$Lambda$24/1502031470.handle(Unknown Source)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:339)
at io.vertx.core.impl.ContextImpl$$Lambda$22/467658346.run(Unknown Source)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
如何测试我的方法是否正确执行了HTTP请求?