Search in sources :

Example 91 with Context

use of io.vertx.core.Context in project vert.x by eclipse.

the class AsynchronousCounter method decrementAndGet.

@Override
public void decrementAndGet(Handler<AsyncResult<Long>> resultHandler) {
    Objects.requireNonNull(resultHandler, "resultHandler");
    Context context = vertx.getOrCreateContext();
    context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.decrementAndGet())));
}
Also used : Context(io.vertx.core.Context)

Example 92 with Context

use of io.vertx.core.Context in project vert.x by eclipse.

the class VertxHttp2NetSocket method sendFile.

@Override
public NetSocket sendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
    synchronized (conn) {
        Context resultCtx = resultHandler != null ? vertx.getOrCreateContext() : null;
        File file = vertx.resolveFile(filename);
        if (!file.exists()) {
            if (resultHandler != null) {
                resultCtx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(new FileNotFoundException())));
            } else {
            // log.error("File not found: " + filename);
            }
            return this;
        }
        RandomAccessFile raf;
        try {
            raf = new RandomAccessFile(file, "r");
        } catch (IOException e) {
            if (resultHandler != null) {
                resultCtx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(e)));
            } else {
            //log.error("Failed to send file", e);
            }
            return this;
        }
        long contentLength = Math.min(length, file.length() - offset);
        FileStreamChannel fileChannel = new FileStreamChannel(ar -> {
            if (resultHandler != null) {
                resultCtx.runOnContext(v -> {
                    resultHandler.handle(Future.succeededFuture());
                });
            }
        }, this, offset, contentLength);
        drainHandler(fileChannel.drainHandler);
        handlerContext.channel().eventLoop().register(fileChannel);
        fileChannel.pipeline().fireUserEventTriggered(raf);
    }
    return this;
}
Also used : Context(io.vertx.core.Context) RandomAccessFile(java.io.RandomAccessFile) MultiMap(io.vertx.core.MultiMap) IOException(java.io.IOException) X509Certificate(javax.security.cert.X509Certificate) Context(io.vertx.core.Context) Future(io.vertx.core.Future) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Unpooled(io.netty.buffer.Unpooled) Nullable(io.vertx.codegen.annotations.Nullable) Buffer(io.vertx.core.buffer.Buffer) Charset(java.nio.charset.Charset) Http2Stream(io.netty.handler.codec.http2.Http2Stream) CharsetUtil(io.netty.util.CharsetUtil) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) StreamResetException(io.vertx.core.http.StreamResetException) NetSocket(io.vertx.core.net.NetSocket) SocketAddress(io.vertx.core.net.SocketAddress) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) RandomAccessFile(java.io.RandomAccessFile) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 93 with Context

use of io.vertx.core.Context in project vertx-proton by vert-x3.

the class ProtonServerImplTest method testAuthenticatorCreatedPerConnection.

@Test(timeout = 20000)
public void testAuthenticatorCreatedPerConnection(TestContext context) {
    Async connectedAsync = context.async();
    Async connectedAsync2 = context.async();
    AtomicInteger port = new AtomicInteger(-1);
    final TestPlainAuthenticatorFactory authenticatorFactory = new TestPlainAuthenticatorFactory();
    ProtonServer.create(vertx).saslAuthenticatorFactory(authenticatorFactory).connectHandler(protonConnection -> {
        // Verify the expected auth detail was recorded in the connection attachments, just using a String here.
        String authValue = protonConnection.attachments().get(AUTH_KEY, String.class);
        context.assertEquals(AUTH_VALUE, authValue);
    }).listen(server -> {
        port.set(server.result().actualPort());
        ProtonClient.create(vertx).connect("localhost", port.intValue(), GOOD_USER, PASSWD, protonConnectionAsyncResult -> {
            context.assertTrue(protonConnectionAsyncResult.succeeded());
            protonConnectionAsyncResult.result().disconnect();
            connectedAsync.complete();
        });
    });
    connectedAsync.awaitSuccess();
    context.assertEquals(1, authenticatorFactory.getCreateCount(), "unexpected authenticator count");
    ProtonClient.create(vertx).connect("localhost", port.intValue(), GOOD_USER, PASSWD, protonConnectionAsyncResult -> {
        context.assertTrue(protonConnectionAsyncResult.succeeded());
        protonConnectionAsyncResult.result().disconnect();
        connectedAsync2.complete();
    });
    connectedAsync2.awaitSuccess();
    context.assertEquals(2, authenticatorFactory.getCreateCount(), "unexpected authenticator count");
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Sasl(org.apache.qpid.proton.engine.Sasl) SaslOutcome(org.apache.qpid.proton.engine.Sasl.SaslOutcome) Context(io.vertx.core.Context) ProtonServer(io.vertx.proton.ProtonServer) StandardCharsets(java.nio.charset.StandardCharsets) Transport(org.apache.qpid.proton.engine.Transport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) Before(org.junit.Before) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 94 with Context

use of io.vertx.core.Context in project vertx-proton by vert-x3.

the class ProtonServerImplTest method testCustomAuthenticatorFailsAuthentication.

@Test(timeout = 20000)
public void testCustomAuthenticatorFailsAuthentication(TestContext context) {
    Async connectedAsync = context.async();
    ProtonServer.create(vertx).saslAuthenticatorFactory(new TestPlainAuthenticatorFactory()).connectHandler(protonConnection -> {
        context.fail("Handler should not be called for connection that failed authentication");
    }).listen(server -> ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), BAD_USER, PASSWD, protonConnectionAsyncResult -> {
        context.assertFalse(protonConnectionAsyncResult.succeeded());
        connectedAsync.complete();
    }));
    connectedAsync.awaitSuccess();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Sasl(org.apache.qpid.proton.engine.Sasl) SaslOutcome(org.apache.qpid.proton.engine.Sasl.SaslOutcome) Context(io.vertx.core.Context) ProtonServer(io.vertx.proton.ProtonServer) StandardCharsets(java.nio.charset.StandardCharsets) Transport(org.apache.qpid.proton.engine.Transport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) Before(org.junit.Before) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 95 with Context

use of io.vertx.core.Context in project vertx-proton by vert-x3.

the class ProtonServerImplTest method doTestAsyncServerAuthenticatorTestImpl.

private void doTestAsyncServerAuthenticatorTestImpl(TestContext context, boolean passAuthentication) {
    Async connectAsync = context.async();
    AtomicBoolean connectedServer = new AtomicBoolean();
    final long delay = 750;
    TestAsyncAuthenticator testAsyncAuthenticator = new TestAsyncAuthenticator(delay, passAuthentication);
    TestAsyncAuthenticatorFactory authenticatorFactory = new TestAsyncAuthenticatorFactory(testAsyncAuthenticator);
    ProtonServer.create(vertx).saslAuthenticatorFactory(authenticatorFactory).connectHandler(protonConnection -> {
        connectedServer.set(true);
    }).listen(server -> {
        final long startTime = System.currentTimeMillis();
        ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), GOOD_USER, PASSWD, conResult -> {
            // Verify the process took expected time from auth delay.
            long actual = System.currentTimeMillis() - startTime;
            context.assertTrue(actual >= delay, "Connect completed before expected time delay elapsed! " + actual);
            if (passAuthentication) {
                context.assertTrue(conResult.succeeded(), "Expected connect to succeed");
                conResult.result().disconnect();
            } else {
                context.assertFalse(conResult.succeeded(), "Expected connect to fail");
            }
            connectAsync.complete();
        });
    });
    connectAsync.awaitSuccess();
    if (passAuthentication) {
        context.assertTrue(connectedServer.get(), "Server handler should have been called");
    } else {
        context.assertFalse(connectedServer.get(), "Server handler should not have been called");
    }
    context.assertEquals(1, authenticatorFactory.getCreateCount(), "unexpected authenticator creation count");
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Sasl(org.apache.qpid.proton.engine.Sasl) SaslOutcome(org.apache.qpid.proton.engine.Sasl.SaslOutcome) Context(io.vertx.core.Context) ProtonServer(io.vertx.proton.ProtonServer) StandardCharsets(java.nio.charset.StandardCharsets) Transport(org.apache.qpid.proton.engine.Transport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) Before(org.junit.Before) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Async(io.vertx.ext.unit.Async)

Aggregations

Context (io.vertx.core.Context)125 Test (org.junit.Test)99 Handler (io.vertx.core.Handler)75 Vertx (io.vertx.core.Vertx)75 Before (org.junit.Before)58 Buffer (io.vertx.core.buffer.Buffer)57 TestContext (io.vertx.ext.unit.TestContext)57 Future (io.vertx.core.Future)53 Async (io.vertx.ext.unit.Async)53 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)53 RunWith (org.junit.runner.RunWith)53 JsonObject (io.vertx.core.json.JsonObject)49 HttpURLConnection (java.net.HttpURLConnection)48 StandardCharsets (java.nio.charset.StandardCharsets)48 Assert.assertThat (org.junit.Assert.assertThat)48 Mockito (org.mockito.Mockito)48 Rule (org.junit.Rule)39 CoreMatchers.is (org.hamcrest.CoreMatchers.is)38 ArgumentCaptor (org.mockito.ArgumentCaptor)38 Timeout (org.junit.rules.Timeout)36