Search in sources :

Example 1 with CloseFuture

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();
}
Also used : NetSocket(io.vertx.core.net.NetSocket) VertxInternal(io.vertx.core.impl.VertxInternal) URL(java.net.URL) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) NetClientOptions(io.vertx.core.net.NetClientOptions) TimeUnit(java.util.concurrent.TimeUnit) HttpClientRequest(io.vertx.core.http.HttpClientRequest) RepeatRule(io.vertx.test.core.RepeatRule) OptionsBuilder(org.openjdk.jmh.runner.options.OptionsBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) Repeat(io.vertx.test.core.Repeat) URLClassLoader(java.net.URLClassLoader) Rule(org.junit.Rule) AsyncTestBase(io.vertx.test.core.AsyncTestBase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpMethod(io.vertx.core.http.HttpMethod) CloseFuture(io.vertx.core.impl.CloseFuture) HttpClientOptions(io.vertx.core.http.HttpClientOptions) NetClient(io.vertx.core.net.NetClient) Runner(org.openjdk.jmh.runner.Runner) WeakReference(java.lang.ref.WeakReference) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) CloseFuture(io.vertx.core.impl.CloseFuture) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientOptions(io.vertx.core.http.HttpClientOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClient(io.vertx.core.http.HttpClient) WeakReference(java.lang.ref.WeakReference) Test(org.junit.Test)

Example 2 with CloseFuture

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CloseFuture(io.vertx.core.impl.CloseFuture) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 3 with CloseFuture

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());
}
Also used : CloseFuture(io.vertx.core.impl.CloseFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 4 with CloseFuture

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();
}
Also used : NetSocket(io.vertx.core.net.NetSocket) VertxInternal(io.vertx.core.impl.VertxInternal) URL(java.net.URL) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) NetClientOptions(io.vertx.core.net.NetClientOptions) TimeUnit(java.util.concurrent.TimeUnit) HttpClientRequest(io.vertx.core.http.HttpClientRequest) RepeatRule(io.vertx.test.core.RepeatRule) OptionsBuilder(org.openjdk.jmh.runner.options.OptionsBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) Repeat(io.vertx.test.core.Repeat) URLClassLoader(java.net.URLClassLoader) Rule(org.junit.Rule) AsyncTestBase(io.vertx.test.core.AsyncTestBase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpMethod(io.vertx.core.http.HttpMethod) CloseFuture(io.vertx.core.impl.CloseFuture) HttpClientOptions(io.vertx.core.http.HttpClientOptions) NetClient(io.vertx.core.net.NetClient) Runner(org.openjdk.jmh.runner.Runner) WeakReference(java.lang.ref.WeakReference) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) CloseFuture(io.vertx.core.impl.CloseFuture) NetClientOptions(io.vertx.core.net.NetClientOptions) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NetClient(io.vertx.core.net.NetClient) WeakReference(java.lang.ref.WeakReference) Test(org.junit.Test)

Aggregations

CloseFuture (io.vertx.core.impl.CloseFuture)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Test (org.junit.Test)4 VertxInternal (io.vertx.core.impl.VertxInternal)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 HttpClient (io.vertx.core.http.HttpClient)2 HttpClientOptions (io.vertx.core.http.HttpClientOptions)2 HttpClientRequest (io.vertx.core.http.HttpClientRequest)2 HttpMethod (io.vertx.core.http.HttpMethod)2 NetClient (io.vertx.core.net.NetClient)2 NetClientOptions (io.vertx.core.net.NetClientOptions)2 NetSocket (io.vertx.core.net.NetSocket)2 AsyncTestBase (io.vertx.test.core.AsyncTestBase)2 Repeat (io.vertx.test.core.Repeat)2 RepeatRule (io.vertx.test.core.RepeatRule)2 WeakReference (java.lang.ref.WeakReference)2 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2