Search in sources :

Example 11 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class Http2ClientTest method testClientRequestWriteability.

@Test
public void testClientRequestWriteability() throws Exception {
    Buffer content = Buffer.buffer();
    Buffer expected = Buffer.buffer();
    String chunk = TestUtils.randomAlphaString(100);
    CompletableFuture<Void> done = new CompletableFuture<>();
    AtomicBoolean paused = new AtomicBoolean();
    AtomicInteger numPause = new AtomicInteger();
    server.requestHandler(req -> {
        Context ctx = vertx.getOrCreateContext();
        done.thenAccept(v1 -> {
            paused.set(false);
            ctx.runOnContext(v2 -> {
                req.resume();
            });
        });
        numPause.incrementAndGet();
        req.pause();
        paused.set(true);
        req.handler(content::appendBuffer);
        req.endHandler(v -> {
            assertEquals(expected, content);
            req.response().end();
        });
    });
    startServer();
    HttpClientRequest req = client.post(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
        testComplete();
    }).setChunked(true).exceptionHandler(err -> {
        fail();
    });
    AtomicInteger sent = new AtomicInteger();
    AtomicInteger count = new AtomicInteger();
    AtomicInteger drained = new AtomicInteger();
    vertx.setPeriodic(1, timerID -> {
        Context ctx = vertx.getOrCreateContext();
        if (req.writeQueueFull()) {
            assertTrue(paused.get());
            assertEquals(1, numPause.get());
            req.drainHandler(v -> {
                assertOnIOContext(ctx);
                assertEquals(0, drained.getAndIncrement());
                assertEquals(1, numPause.get());
                assertFalse(paused.get());
                req.end();
            });
            vertx.cancelTimer(timerID);
            done.complete(null);
        } else {
            count.incrementAndGet();
            expected.appendString(chunk);
            req.write(chunk);
            sent.addAndGet(chunk.length());
        }
    });
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) HttpClientRequest(io.vertx.core.http.HttpClientRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 12 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class Http2ClientTest method testServerCloseNetSocket.

@Test
public void testServerCloseNetSocket() throws Exception {
    waitFor(2);
    AtomicInteger status = new AtomicInteger();
    server.requestHandler(req -> {
        NetSocket socket = req.netSocket();
        socket.handler(buff -> {
            switch(status.getAndIncrement()) {
                case 0:
                    assertEquals(Buffer.buffer("some-data"), buff);
                    socket.end(buff);
                    break;
                case 1:
                    assertEquals(Buffer.buffer("last-data"), buff);
                    break;
                default:
                    fail();
                    break;
            }
        });
        socket.endHandler(v -> {
            assertEquals(2, status.getAndIncrement());
        });
        socket.closeHandler(v -> {
            assertEquals(3, status.getAndIncrement());
            complete();
        });
    });
    startServer();
    client.request(HttpMethod.CONNECT, DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
        assertEquals(200, resp.statusCode());
        NetSocket socket = resp.netSocket();
        AtomicInteger count = new AtomicInteger();
        socket.handler(buff -> {
            switch(count.getAndIncrement()) {
                case 0:
                    assertEquals("some-data", buff.toString());
                    break;
                default:
                    fail();
                    break;
            }
        });
        socket.endHandler(v -> {
            assertEquals(1, count.getAndIncrement());
            socket.end(Buffer.buffer("last-data"));
        });
        socket.closeHandler(v -> {
            assertEquals(2, count.getAndIncrement());
            complete();
        });
        socket.write(Buffer.buffer("some-data"));
    }).setHost("whatever.com").sendHead();
    await();
}
Also used : NetSocket(io.vertx.core.net.NetSocket) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 13 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class Http2ClientTest method testConnectionShutdownInConnectionHandler.

@Test
public void testConnectionShutdownInConnectionHandler() throws Exception {
    AtomicInteger serverStatus = new AtomicInteger();
    server.connectionHandler(conn -> {
        if (serverStatus.getAndIncrement() == 0) {
            conn.goAwayHandler(ga -> {
                assertEquals(0, ga.getErrorCode());
                assertEquals(1, serverStatus.getAndIncrement());
            });
            conn.shutdownHandler(v -> {
                assertEquals(2, serverStatus.getAndIncrement());
            });
            conn.closeHandler(v -> {
                assertEquals(3, serverStatus.getAndIncrement());
            });
        }
    });
    server.requestHandler(req -> {
        assertEquals(5, serverStatus.getAndIncrement());
        req.response().end();
    });
    startServer();
    AtomicInteger clientStatus = new AtomicInteger();
    HttpClientRequest req1 = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath");
    req1.connectionHandler(conn -> {
        Context ctx = Vertx.currentContext();
        conn.shutdownHandler(v -> {
            assertOnIOContext(ctx);
            clientStatus.compareAndSet(1, 2);
        });
        if (clientStatus.getAndIncrement() == 0) {
            conn.shutdown();
        }
    });
    req1.exceptionHandler(err -> {
        fail();
    });
    req1.handler(resp -> {
        assertEquals(2, clientStatus.getAndIncrement());
        resp.endHandler(v -> {
            testComplete();
        });
    });
    req1.end();
    await();
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientRequest(io.vertx.core.http.HttpClientRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 14 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class Http2ClientTest method testMaxConcurrency.

private void testMaxConcurrency(int poolSize, int maxConcurrency) throws Exception {
    int rounds = 1 + poolSize;
    int maxRequests = poolSize * maxConcurrency;
    int totalRequests = maxRequests + maxConcurrency;
    Set<HttpConnection> serverConns = new HashSet<>();
    server.connectionHandler(conn -> {
        serverConns.add(conn);
        assertTrue(serverConns.size() <= poolSize);
    });
    ArrayList<HttpServerRequest> requests = new ArrayList<>();
    server.requestHandler(req -> {
        if (requests.size() < maxRequests) {
            requests.add(req);
            if (requests.size() == maxRequests) {
                vertx.setTimer(300, v -> {
                    assertEquals(maxRequests, requests.size());
                    requests.forEach(r -> r.response().end());
                });
            }
        } else {
            req.response().end();
        }
    });
    startServer();
    client.close();
    client = vertx.createHttpClient(new HttpClientOptions(clientOptions).setHttp2MaxPoolSize(poolSize).setHttp2MultiplexingLimit(maxConcurrency));
    AtomicInteger respCount = new AtomicInteger();
    Set<HttpConnection> clientConnections = Collections.synchronizedSet(new HashSet<>());
    for (int j = 0; j < rounds; j++) {
        for (int i = 0; i < maxConcurrency; i++) {
            client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
                resp.endHandler(v -> {
                    if (respCount.incrementAndGet() == totalRequests) {
                        testComplete();
                    }
                });
            }).connectionHandler(clientConnections::add).end();
        }
        if (j < poolSize) {
            int threshold = j + 1;
            waitUntil(() -> clientConnections.size() == threshold);
        }
    }
    await();
}
Also used : HttpConnection(io.vertx.core.http.HttpConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerRequest(io.vertx.core.http.HttpServerRequest) ArrayList(java.util.ArrayList) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HashSet(java.util.HashSet)

Example 15 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class Http2ClientTest method testNetSocketConnect.

@Test
public void testNetSocketConnect() throws Exception {
    waitFor(2);
    server.requestHandler(req -> {
        NetSocket socket = req.netSocket();
        AtomicInteger status = new AtomicInteger();
        socket.handler(buff -> {
            switch(status.getAndIncrement()) {
                case 0:
                    assertEquals(Buffer.buffer("some-data"), buff);
                    socket.write(buff);
                    break;
                case 1:
                    assertEquals(Buffer.buffer("last-data"), buff);
                    break;
                default:
                    fail();
                    break;
            }
        });
        socket.endHandler(v -> {
            assertEquals(2, status.getAndIncrement());
            socket.write(Buffer.buffer("last-data"));
        });
        socket.closeHandler(v -> {
            assertEquals(3, status.getAndIncrement());
            complete();
        });
    });
    startServer();
    client.request(HttpMethod.CONNECT, DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
        assertEquals(200, resp.statusCode());
        NetSocket socket = resp.netSocket();
        StringBuilder received = new StringBuilder();
        AtomicInteger count = new AtomicInteger();
        socket.handler(buff -> {
            if (buff.length() > 0) {
                received.append(buff.toString());
                if (received.toString().equals("some-data")) {
                    received.setLength(0);
                    socket.end(Buffer.buffer("last-data"));
                } else if (received.toString().equals("last-data")) {
                    assertEquals(0, count.getAndIncrement());
                }
            }
        });
        socket.endHandler(v -> {
            assertEquals(1, count.getAndIncrement());
        });
        socket.closeHandler(v -> {
            assertEquals(2, count.getAndIncrement());
            complete();
        });
        socket.write(Buffer.buffer("some-data"));
    }).setHost("whatever.com").sendHead();
    await();
}
Also used : NetSocket(io.vertx.core.net.NetSocket) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8428 Test (org.junit.Test)3991 CountDownLatch (java.util.concurrent.CountDownLatch)1141 ArrayList (java.util.ArrayList)1107 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)906 List (java.util.List)808 IOException (java.io.IOException)751 AtomicReference (java.util.concurrent.atomic.AtomicReference)636 HashMap (java.util.HashMap)570 Map (java.util.Map)519 Test (org.testng.annotations.Test)428 TimeUnit (java.util.concurrent.TimeUnit)377 AtomicLong (java.util.concurrent.atomic.AtomicLong)376 Test (org.junit.jupiter.api.Test)376 HashSet (java.util.HashSet)353 File (java.io.File)350 ExecutorService (java.util.concurrent.ExecutorService)344 Arrays (java.util.Arrays)336 Collections (java.util.Collections)312 Set (java.util.Set)312