Search in sources :

Example 6 with TestContext

use of io.vertx.ext.unit.TestContext in project vertx-proton by vert-x3.

the class ProtonClientTest method testClientIdentification.

@Test(timeout = 20000)
public void testClientIdentification(TestContext context) {
    Async async = context.async();
    connect(context, connection -> {
        connection.setContainer("foo").openHandler(x -> {
            context.assertEquals("foo", connection.getContainer());
            // Our mock server responds with a pong container id
            context.assertEquals("pong: foo", connection.getRemoteContainer());
            connection.disconnect();
            async.complete();
        }).open();
    });
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LoggerFactory(io.vertx.core.logging.LoggerFactory) LinkedHashMap(java.util.LinkedHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) Target(org.apache.qpid.proton.amqp.transport.Target) Map(java.util.Map) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) Logger(io.vertx.core.logging.Logger) ProtonServerImpl(io.vertx.proton.impl.ProtonServerImpl) ProtonMetaDataSupportImpl(io.vertx.proton.impl.ProtonMetaDataSupportImpl) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) Proton(org.apache.qpid.proton.Proton) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Source(org.apache.qpid.proton.amqp.messaging.Source) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) NetServer(io.vertx.core.net.NetServer) ProtonConnectionImpl(io.vertx.proton.impl.ProtonConnectionImpl) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 7 with TestContext

use of io.vertx.ext.unit.TestContext in project vertx-proton by vert-x3.

the class ProtonClientTest method testReceiveMultipleMessagesWithLowerPrefetch.

@Test(timeout = 20000)
public void testReceiveMultipleMessagesWithLowerPrefetch(TestContext context) {
    Async async = context.async();
    connect(context, connection -> {
        connection.open();
        AtomicInteger counter = new AtomicInteger(0);
        ProtonReceiver receiver = connection.createReceiver(MockServer.Addresses.five_messages.toString());
        // Set prefetch to 2 credit. Test verifies receiver gets multiple messages, i.e credit is being replenished.
        receiver.setPrefetch(2).handler((d, m) -> {
            int count = counter.incrementAndGet();
            validateMessage(context, count, String.valueOf(count), m);
            if (count == 5) {
                // Got the last message, lets finish the test.
                LOG.trace("Got msg 5, completing async");
                async.complete();
                connection.disconnect();
            }
        }).open();
    });
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LoggerFactory(io.vertx.core.logging.LoggerFactory) LinkedHashMap(java.util.LinkedHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) Target(org.apache.qpid.proton.amqp.transport.Target) Map(java.util.Map) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) Logger(io.vertx.core.logging.Logger) ProtonServerImpl(io.vertx.proton.impl.ProtonServerImpl) ProtonMetaDataSupportImpl(io.vertx.proton.impl.ProtonMetaDataSupportImpl) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) Proton(org.apache.qpid.proton.Proton) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Source(org.apache.qpid.proton.amqp.messaging.Source) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) NetServer(io.vertx.core.net.NetServer) ProtonConnectionImpl(io.vertx.proton.impl.ProtonConnectionImpl) Handler(io.vertx.core.Handler) Collections(java.util.Collections) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 8 with TestContext

use of io.vertx.ext.unit.TestContext in project vertx-proton by vert-x3.

the class ProtonClientTest method testMaxMessageSize.

@Test(timeout = 20000)
public void testMaxMessageSize(TestContext context) throws Exception {
    server.close();
    Async serverAsync = context.async();
    Async clientAsync = context.async();
    final UnsignedLong clientMaxMsgSize = UnsignedLong.valueOf(54321);
    final UnsignedLong serverMaxMsgSize = UnsignedLong.valueOf(12345);
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> {
                session.open();
            });
            serverConnection.senderOpenHandler(serverSender -> {
                context.assertEquals(clientMaxMsgSize, serverSender.getRemoteMaxMessageSize(), "unexpected remote max message size at server");
                context.assertNull(serverSender.getMaxMessageSize(), "Expected no value to be set");
                serverSender.setMaxMessageSize(serverMaxMsgSize);
                context.assertEquals(serverMaxMsgSize, serverSender.getMaxMessageSize(), "Expected value to now be set");
                LOG.trace("Server sender opened");
                serverSender.open();
                serverAsync.complete();
            });
        });
        // ===== Client Handling =====
        ProtonClient client = ProtonClient.create(vertx);
        client.connect("localhost", protonServer.actualPort(), res -> {
            context.assertTrue(res.succeeded());
            ProtonConnection connection = res.result();
            connection.openHandler(x -> {
                LOG.trace("Client connection opened");
                final ProtonLink<?> receiver = connection.createReceiver("some-address");
                context.assertNull(receiver.getMaxMessageSize(), "Expected no value to be set");
                receiver.setMaxMessageSize(clientMaxMsgSize);
                context.assertEquals(clientMaxMsgSize, receiver.getMaxMessageSize(), "Expected value to now be set");
                receiver.openHandler(y -> {
                    LOG.trace("Client link opened");
                    context.assertEquals(serverMaxMsgSize, receiver.getRemoteMaxMessageSize(), "unexpected remote max message size at client");
                    clientAsync.complete();
                });
                receiver.open();
            }).open();
        });
        serverAsync.awaitSuccess();
        clientAsync.awaitSuccess();
    } finally {
        if (protonServer != null) {
            protonServer.close();
        }
    }
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LoggerFactory(io.vertx.core.logging.LoggerFactory) LinkedHashMap(java.util.LinkedHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) Target(org.apache.qpid.proton.amqp.transport.Target) Map(java.util.Map) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) Logger(io.vertx.core.logging.Logger) ProtonServerImpl(io.vertx.proton.impl.ProtonServerImpl) ProtonMetaDataSupportImpl(io.vertx.proton.impl.ProtonMetaDataSupportImpl) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) Proton(org.apache.qpid.proton.Proton) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Source(org.apache.qpid.proton.amqp.messaging.Source) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) NetServer(io.vertx.core.net.NetServer) ProtonConnectionImpl(io.vertx.proton.impl.ProtonConnectionImpl) Handler(io.vertx.core.Handler) Collections(java.util.Collections) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 9 with TestContext

use of io.vertx.ext.unit.TestContext in project vertx-proton by vert-x3.

the class ProtonServerImplTest method testCustomAuthenticatorHasInitCalled.

@Test(timeout = 20000)
public void testCustomAuthenticatorHasInitCalled(TestContext context) {
    Async initCalledAsync = context.async();
    ProtonServer.create(vertx).saslAuthenticatorFactory(new ProtonSaslAuthenticatorFactory() {

        @Override
        public ProtonSaslAuthenticator create() {
            return new ProtonSaslAuthenticator() {

                @Override
                public void init(NetSocket socket, ProtonConnection protonConnection, Transport transport) {
                    initCalledAsync.complete();
                }

                @Override
                public void process(Handler<Boolean> completionHandler) {
                    completionHandler.handle(false);
                }

                @Override
                public boolean succeeded() {
                    return false;
                }
            };
        }
    }).connectHandler(protonConnection -> {
    }).listen(server -> ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), protonConnectionAsyncResult -> {
    }));
}
Also used : NetSocket(io.vertx.core.net.NetSocket) 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) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) Transport(org.apache.qpid.proton.engine.Transport) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Test(org.junit.Test)

Example 10 with TestContext

use of io.vertx.ext.unit.TestContext in project vertx-proton by vert-x3.

the class ProtonServerImplTest method testCustomAuthenticatorSuceedsAuthentication.

@Test(timeout = 20000)
public void testCustomAuthenticatorSuceedsAuthentication(TestContext context) {
    Async connectedAsync = context.async();
    Async authenticatedAsync = context.async();
    ProtonServer.create(vertx).saslAuthenticatorFactory(new TestPlainAuthenticatorFactory()).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);
        authenticatedAsync.complete();
    }).listen(server -> ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), GOOD_USER, PASSWD, protonConnectionAsyncResult -> {
        context.assertTrue(protonConnectionAsyncResult.succeeded());
        protonConnectionAsyncResult.result().disconnect();
        connectedAsync.complete();
    }));
    authenticatedAsync.awaitSuccess();
    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)

Aggregations

TestContext (io.vertx.ext.unit.TestContext)148 Test (org.junit.Test)147 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)141 RunWith (org.junit.runner.RunWith)141 Async (io.vertx.ext.unit.Async)123 Future (io.vertx.core.Future)121 Handler (io.vertx.core.Handler)112 Vertx (io.vertx.core.Vertx)103 HttpURLConnection (java.net.HttpURLConnection)100 Before (org.junit.Before)97 Timeout (org.junit.rules.Timeout)95 JsonObject (io.vertx.core.json.JsonObject)91 Rule (org.junit.Rule)83 Mockito (org.mockito.Mockito)74 Constants (org.eclipse.hono.util.Constants)68 Assert.assertThat (org.junit.Assert.assertThat)62 Context (io.vertx.core.Context)57 CoreMatchers.is (org.hamcrest.CoreMatchers.is)54 AsyncResult (io.vertx.core.AsyncResult)53 Buffer (io.vertx.core.buffer.Buffer)52