Search in sources :

Example 36 with AtomicInteger

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

the class HttpTest method testFormUploadFile.

private void testFormUploadFile(String contentStr, boolean streamToDisk) throws Exception {
    Buffer content = Buffer.buffer(contentStr);
    AtomicInteger attributeCount = new AtomicInteger();
    server.requestHandler(req -> {
        if (req.method() == HttpMethod.POST) {
            assertEquals(req.path(), "/form");
            req.response().setChunked(true);
            req.setExpectMultipart(true);
            assertTrue(req.isExpectMultipart());
            req.setExpectMultipart(true);
            assertTrue(req.isExpectMultipart());
            req.uploadHandler(upload -> {
                Buffer tot = Buffer.buffer();
                assertEquals("file", upload.name());
                assertEquals("tmp-0.txt", upload.filename());
                assertEquals("image/gif", upload.contentType());
                String uploadedFileName;
                if (!streamToDisk) {
                    upload.handler(buffer -> tot.appendBuffer(buffer));
                    uploadedFileName = null;
                } else {
                    uploadedFileName = new File(testDir, UUID.randomUUID().toString()).getPath();
                    upload.streamToFileSystem(uploadedFileName);
                }
                upload.endHandler(v -> {
                    if (streamToDisk) {
                        Buffer uploaded = vertx.fileSystem().readFileBlocking(uploadedFileName);
                        assertEquals(content, uploaded);
                    } else {
                        assertEquals(content, tot);
                    }
                    assertTrue(upload.isSizeAvailable());
                    assertEquals(content.length(), upload.size());
                });
            });
            req.endHandler(v -> {
                MultiMap attrs = req.formAttributes();
                attributeCount.set(attrs.size());
                req.response().end();
            });
        }
    });
    server.listen(onSuccess(s -> {
        HttpClientRequest req = client.request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/form", resp -> {
            assertEquals(200, resp.statusCode());
            resp.bodyHandler(body -> {
                assertEquals(0, body.length());
            });
            assertEquals(0, attributeCount.get());
            testComplete();
        });
        String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
        Buffer buffer = Buffer.buffer();
        String body = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"tmp-0.txt\"\r\n" + "Content-Type: image/gif\r\n" + "\r\n" + contentStr + "\r\n" + "--" + boundary + "--\r\n";
        buffer.appendString(body);
        req.headers().set("content-length", String.valueOf(buffer.length()));
        req.headers().set("content-type", "multipart/form-data; boundary=" + boundary);
        req.write(buffer).end();
    }));
    await();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) TimeoutException(java.util.concurrent.TimeoutException) Context(io.vertx.core.Context) InetAddress(java.net.InetAddress) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Nullable(io.vertx.codegen.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) WorkerContext(io.vertx.core.impl.WorkerContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) IntStream(java.util.stream.IntStream) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) BufferedWriter(java.io.BufferedWriter) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TemporaryFolder(org.junit.rules.TemporaryFolder) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) MultiMap(io.vertx.core.MultiMap) HttpClientRequest(io.vertx.core.http.HttpClientRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File)

Example 37 with AtomicInteger

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

the class HttpTest method testConnectionErrorsGetReportedToRequest.

@Test
public void testConnectionErrorsGetReportedToRequest() throws InterruptedException {
    AtomicInteger req1Exceptions = new AtomicInteger();
    AtomicInteger req2Exceptions = new AtomicInteger();
    AtomicInteger req3Exceptions = new AtomicInteger();
    CountDownLatch latch = new CountDownLatch(3);
    // This one should cause an error in the Client Exception handler, because it has no exception handler set specifically.
    HttpClientRequest req1 = client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, "someurl1", resp -> {
        fail("Should never get a response on a bad port, if you see this message than you are running an http server on port 9998");
    });
    req1.exceptionHandler(t -> {
        assertEquals("More than one call to req1 exception handler was not expected", 1, req1Exceptions.incrementAndGet());
        latch.countDown();
    });
    HttpClientRequest req2 = client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, "someurl2", resp -> {
        fail("Should never get a response on a bad port, if you see this message than you are running an http server on port 9998");
    });
    req2.exceptionHandler(t -> {
        assertEquals("More than one call to req2 exception handler was not expected", 1, req2Exceptions.incrementAndGet());
        latch.countDown();
    });
    HttpClientRequest req3 = client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, "someurl2", resp -> {
        fail("Should never get a response on a bad port, if you see this message than you are running an http server on port 9998");
    });
    req3.exceptionHandler(t -> {
        assertEquals("More than one call to req2 exception handler was not expected", 1, req3Exceptions.incrementAndGet());
        latch.countDown();
    });
    req1.end();
    req2.end();
    req3.end();
    awaitLatch(latch);
    testComplete();
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 38 with AtomicInteger

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

the class HttpTest method testClearHandlersOnEnd.

@Test
public void testClearHandlersOnEnd() {
    String path = "/some/path";
    server = vertx.createHttpServer(createBaseServerOptions());
    server.requestHandler(req -> req.response().setStatusCode(200).end());
    server.listen(ar -> {
        assertTrue(ar.succeeded());
        HttpClientRequest req = client.request(HttpMethod.GET, HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path);
        AtomicInteger count = new AtomicInteger();
        req.handler(resp -> {
            resp.endHandler(v -> {
                try {
                    resp.endHandler(null);
                    resp.exceptionHandler(null);
                    resp.handler(null);
                } catch (Exception e) {
                    fail("Was expecting to set to null the handlers when the response is completed");
                    return;
                }
                if (count.incrementAndGet() == 2) {
                    testComplete();
                }
            });
        });
        req.endHandler(done -> {
            try {
                req.handler(null);
                req.exceptionHandler(null);
                req.endHandler(null);
            } catch (Exception e) {
                e.printStackTrace();
                fail("Was expecting to set to null the handlers when the response is completed");
                return;
            }
            if (count.incrementAndGet() == 2) {
                testComplete();
            }
        });
        req.end();
    });
    await();
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VertxException(io.vertx.core.VertxException) TimeoutException(java.util.concurrent.TimeoutException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) Test(org.junit.Test)

Example 39 with AtomicInteger

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

the class HttpTest method testResponseEndHandlers1.

@Test
public void testResponseEndHandlers1() {
    waitFor(2);
    AtomicInteger cnt = new AtomicInteger();
    server.requestHandler(req -> {
        req.response().headersEndHandler(v -> {
            req.response().putHeader("extraheader", "wibble");
            assertEquals(0, cnt.getAndIncrement());
        });
        req.response().bodyEndHandler(v -> {
            assertEquals(0, req.response().bytesWritten());
            assertEquals(1, cnt.getAndIncrement());
            complete();
        });
        req.response().end();
    }).listen(onSuccess(server -> {
        client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", res -> {
            assertEquals(200, res.statusCode());
            assertEquals("wibble", res.headers().get("extraheader"));
            complete();
        }).end();
    }));
    await();
}
Also used : VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) TimeoutException(java.util.concurrent.TimeoutException) Context(io.vertx.core.Context) InetAddress(java.net.InetAddress) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Nullable(io.vertx.codegen.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) WorkerContext(io.vertx.core.impl.WorkerContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) IntStream(java.util.stream.IntStream) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) BufferedWriter(java.io.BufferedWriter) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TemporaryFolder(org.junit.rules.TemporaryFolder) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 40 with AtomicInteger

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

the class HttpTest method testFollowRedirectHost.

@Test
public void testFollowRedirectHost() throws Exception {
    String scheme = createBaseClientOptions().isSsl() ? "https" : "http";
    waitFor(2);
    AtomicInteger redirections = new AtomicInteger();
    server.requestHandler(req -> {
        switch(redirections.getAndIncrement()) {
            case 0:
                req.response().setStatusCode(301).putHeader(HttpHeaders.LOCATION, scheme + "://localhost:8080/whatever").end();
                break;
            case 1:
                assertEquals(scheme + "://the_host/whatever", req.absoluteURI());
                req.response().end();
                complete();
                break;
        }
    });
    startServer();
    client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
        assertEquals(scheme + "://the_host/whatever", resp.request().absoluteURI());
        complete();
    }).setFollowRedirects(true).setHost("the_host").end();
    await();
}
Also used : VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) TimeoutException(java.util.concurrent.TimeoutException) Context(io.vertx.core.Context) InetAddress(java.net.InetAddress) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Nullable(io.vertx.codegen.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) WorkerContext(io.vertx.core.impl.WorkerContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) IntStream(java.util.stream.IntStream) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) BufferedWriter(java.io.BufferedWriter) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TemporaryFolder(org.junit.rules.TemporaryFolder) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) 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