use of io.vertx.core.impl.CloseFuture in project vert.x by eclipse.
the class VertxTest method testFinalizeHttpClient.
@Test
public void testFinalizeHttpClient() throws Exception {
VertxInternal vertx = (VertxInternal) Vertx.vertx();
try {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<NetSocket> socketRef = new AtomicReference<>();
vertx.createNetServer().connectHandler(socketRef::set).listen(8080, "localhost").onComplete(onSuccess(server -> latch.countDown()));
awaitLatch(latch);
AtomicBoolean closed = new AtomicBoolean();
// No keep alive so the connection is not held in the pool ????
CloseFuture closeFuture = new CloseFuture();
closeFuture.future().onComplete(ar -> closed.set(true));
HttpClient client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false), closeFuture);
vertx.addCloseHook(closeFuture);
client.request(HttpMethod.GET, 8080, "localhost", "/").compose(HttpClientRequest::send).onComplete(onFailure(err -> {
}));
WeakReference<HttpClient> ref = new WeakReference<>(client);
closeFuture = null;
client = null;
assertWaitUntil(() -> socketRef.get() != null);
for (int i = 0; i < 10; i++) {
Thread.sleep(10);
RUNNER.runSystemGC();
assertFalse(closed.get());
assertNotNull(ref.get());
}
socketRef.get().close();
long now = System.currentTimeMillis();
while (true) {
assertTrue(System.currentTimeMillis() - now < 20_000);
RUNNER.runSystemGC();
if (ref.get() == null) {
assertTrue(closed.get());
break;
}
}
} finally {
vertx.close(ar -> {
testComplete();
});
}
await();
}
use of io.vertx.core.impl.CloseFuture in project vert.x by eclipse.
the class VertxTest method testCloseVertxShouldWaitConcurrentCloseHook.
@Test
public void testCloseVertxShouldWaitConcurrentCloseHook() throws Exception {
VertxInternal vertx = (VertxInternal) Vertx.vertx();
AtomicReference<Promise<Void>> ref = new AtomicReference<>();
CloseFuture fut = new CloseFuture();
fut.add(ref::set);
vertx.addCloseHook(fut);
Promise<Void> p = Promise.promise();
fut.close(p);
AtomicBoolean closed = new AtomicBoolean();
vertx.close(ar -> {
closed.set(true);
});
Thread.sleep(500);
assertFalse(closed.get());
ref.get().complete();
assertWaitUntil(closed::get);
}
use of io.vertx.core.impl.CloseFuture in project vert.x by eclipse.
the class VertxTest method testCloseFutureDuplicateClose.
@Test
public void testCloseFutureDuplicateClose() {
AtomicReference<Promise<Void>> ref = new AtomicReference<>();
CloseFuture fut = new CloseFuture();
fut.add(ref::set);
Promise<Void> p1 = Promise.promise();
fut.close(p1);
assertNotNull(ref.get());
Promise<Void> p2 = Promise.promise();
fut.close(p2);
assertFalse(p1.future().isComplete());
assertFalse(p2.future().isComplete());
ref.get().complete();
assertTrue(p1.future().isComplete());
assertTrue(p2.future().isComplete());
}
use of io.vertx.core.impl.CloseFuture in project vert.x by eclipse.
the class VertxTest method testFinalizeNetClient.
@Test
public void testFinalizeNetClient() throws Exception {
VertxInternal vertx = (VertxInternal) Vertx.vertx();
try {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<NetSocket> socketRef = new AtomicReference<>();
vertx.createNetServer().connectHandler(socketRef::set).listen(1234, "localhost").onComplete(onSuccess(server -> latch.countDown()));
awaitLatch(latch);
AtomicBoolean closed = new AtomicBoolean();
CloseFuture closeFuture = new CloseFuture();
NetClient client = vertx.createNetClient(new NetClientOptions(), closeFuture);
vertx.addCloseHook(closeFuture);
closeFuture.future().onComplete(ar -> closed.set(true));
closeFuture = null;
client.connect(1234, "localhost", onSuccess(so -> {
}));
WeakReference<NetClient> ref = new WeakReference<>(client);
client = null;
assertWaitUntil(() -> socketRef.get() != null);
for (int i = 0; i < 10; i++) {
Thread.sleep(10);
RUNNER.runSystemGC();
assertFalse(closed.get());
assertNotNull(ref.get());
}
socketRef.get().close();
long now = System.currentTimeMillis();
while (true) {
assertTrue(System.currentTimeMillis() - now < 20_000);
RUNNER.runSystemGC();
if (ref.get() == null) {
assertTrue(closed.get());
break;
}
}
} finally {
vertx.close(ar -> {
testComplete();
});
}
await();
}
Aggregations