Search in sources :

Example 61 with TestContext

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

the class ProtonClientTest method testImmediateInitialCreditWithPrefetchDisabled.

@Test(timeout = 20000)
public void testImmediateInitialCreditWithPrefetchDisabled(TestContext context) {
    Async async = context.async();
    connect(context, connection -> {
        connection.open();
        AtomicInteger counter = new AtomicInteger(0);
        AtomicBoolean creditGranted = new AtomicBoolean();
        ProtonReceiver receiver = connection.createReceiver(MockServer.Addresses.five_messages.toString());
        receiver.handler((d, m) -> {
            int count = counter.incrementAndGet();
            switch(count) {
                // Fall-through
                case 1:
                // Fall-through
                case 2:
                case 3:
                    {
                        validateMessage(context, count, String.valueOf(count), m);
                        break;
                    }
                case 4:
                    {
                        validateMessage(context, count, String.valueOf(count), m);
                        // We only issued 4 credits, so we should not get
                        // more messages until more credit is flowed, use
                        // the callback for this msg to do that
                        vertx.setTimer(1000, x -> {
                            LOG.trace("Flowing more credit");
                            creditGranted.set(true);
                            receiver.flow(1);
                        });
                        // Check that we haven't processed any more messages before then
                        vertx.setTimer(500, x -> {
                            LOG.trace("Checking msg 5 not received yet");
                            context.assertEquals(4, counter.get());
                        });
                        break;
                    }
                case 5:
                    {
                        validateMessage(context, count, String.valueOf(count), m);
                        context.assertTrue(creditGranted.get(), "Additional credit not yet granted, so we" + " should not have received message 5 yet!");
                        // Got the last message, lets finish the test.
                        LOG.trace("Got msg 5, completing async");
                        async.complete();
                        connection.disconnect();
                        break;
                    }
            }
        }).setPrefetch(// Turn off prefetch and related automatic credit handling
        0).flow(// Explicitly grant initial credit of 4. Handler will grant more later.
        4).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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 62 with TestContext

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

the class ProtonClientTest method testDelayedInitialCreditWithPrefetchDisabled.

@Test(timeout = 20000)
public void testDelayedInitialCreditWithPrefetchDisabled(TestContext context) {
    Async async = context.async();
    connect(context, connection -> {
        connection.open();
        AtomicInteger counter = new AtomicInteger(0);
        AtomicBoolean initialCreditGranted = new AtomicBoolean();
        AtomicBoolean additionalCreditGranted = new AtomicBoolean();
        final int delay = 250;
        final long startTime = System.currentTimeMillis();
        // Create receiver with prefetch disabled
        ProtonReceiver receiver = connection.createReceiver(MockServer.Addresses.two_messages.toString());
        receiver.handler((d, m) -> {
            int count = counter.incrementAndGet();
            switch(count) {
                case 1:
                    {
                        validateMessage(context, count, String.valueOf(count), m);
                        context.assertTrue(initialCreditGranted.get(), "Initial credit not yet granted, so we" + " should not have received message 1 yet!");
                        // Verify lack of initial credit results in delayed receipt of first message.
                        context.assertTrue(System.currentTimeMillis() > startTime + delay, "Message received before expected time delay elapsed!");
                        LOG.trace("Got msg 1");
                        // We only issued 1 credit, so we should not get more
                        // messages until more credit is flowed, use the
                        // callback for this msg to do that after further delay
                        vertx.setTimer(delay, x -> {
                            LOG.trace("Granting additional credit");
                            additionalCreditGranted.set(true);
                            receiver.flow(1);
                        });
                        break;
                    }
                case 2:
                    {
                        validateMessage(context, count, String.valueOf(count), m);
                        context.assertTrue(additionalCreditGranted.get(), "Additional credit not yet granted, so we" + " should not have received message " + count + " yet!");
                        context.assertTrue(System.currentTimeMillis() > startTime + (delay * 2), "Message received before expected time delay elapsed!");
                        // Got the last message, lets finish the test.
                        LOG.trace("Got msg 2, completing async");
                        async.complete();
                        connection.disconnect();
                        break;
                    }
            }
        }).setPrefetch(// Turn off automatic prefetch / credit handling
        0).open();
        // Explicitly grant an initial credit after a delay. Handler will then grant more.
        vertx.setTimer(delay, x -> {
            LOG.trace("Flowing initial credit");
            initialCreditGranted.set(true);
            receiver.flow(1);
        });
    });
}
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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 63 with TestContext

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

the class ProtonClientTest method testConnectionPropertiesSetNonDefaultWithoutProductVersion.

@Test(timeout = 20000)
public void testConnectionPropertiesSetNonDefaultWithoutProductVersion(TestContext context) throws Exception {
    Symbol clientCustomProp = Symbol.valueOf("custom-client-prop-key");
    String clientCustomPropValue = "custom-client-prop-value";
    Symbol serverCustomProp = Symbol.valueOf("custom-server-prop-key");
    String serverCustomPropValue = "custom-server-prop-value";
    LinkedHashMap<Symbol, Object> clientProps = new LinkedHashMap<Symbol, Object>();
    clientProps.put(clientCustomProp, clientCustomPropValue);
    LinkedHashMap<Symbol, Object> serverProps = new LinkedHashMap<Symbol, Object>();
    serverProps.put(serverCustomProp, serverCustomPropValue);
    final ConPropValidator serverExpectedPropsHandler = (c, props) -> {
        new ProductVersionConPropValidator(ProtonMetaDataSupportImpl.PRODUCT, ProtonMetaDataSupportImpl.VERSION).validate(c, props);
        context.assertTrue(props.containsKey(clientCustomProp), "custom client prop not present");
        context.assertEquals(clientCustomPropValue, props.get(clientCustomProp), "unexpected custom client prop value");
    };
    final ConPropValidator clientExpectedPropsHandler = (c, props) -> {
        new ProductVersionConPropValidator(ProtonMetaDataSupportImpl.PRODUCT, ProtonMetaDataSupportImpl.VERSION).validate(c, props);
        context.assertTrue(props.containsKey(serverCustomProp), "custom server prop not present");
        context.assertEquals(serverCustomPropValue, props.get(serverCustomProp), "unexpected custom server prop value");
    };
    doConnectionPropertiesTestImpl(context, true, clientProps, serverExpectedPropsHandler, serverProps, clientExpectedPropsHandler);
}
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) Symbol(org.apache.qpid.proton.amqp.Symbol) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 64 with TestContext

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

the class ProtonClientTest method doConnectionPropertiesTestImpl.

public void doConnectionPropertiesTestImpl(TestContext context, boolean setProperties, Map<Symbol, Object> clientGivenProps, ConPropValidator serverExpectedPropsHandler, Map<Symbol, Object> serverGivenProps, ConPropValidator clientExpectedPropsHandler) throws Exception {
    server.close();
    Async serverAsync = context.async();
    Async clientAsync = context.async();
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(x -> {
                if (setProperties) {
                    serverConnection.setProperties(serverGivenProps);
                }
                serverExpectedPropsHandler.validate(context, serverConnection.getRemoteProperties());
                serverConnection.open();
                serverAsync.complete();
            });
        });
        // ===== Client Handling =====
        ProtonClient client = ProtonClient.create(vertx);
        client.connect("localhost", protonServer.actualPort(), res -> {
            context.assertTrue(res.succeeded());
            ProtonConnection clientConnection = res.result();
            if (setProperties) {
                clientConnection.setProperties(clientGivenProps);
            }
            clientConnection.openHandler(x -> {
                context.assertTrue(x.succeeded());
                LOG.trace("Client connection opened");
                clientExpectedPropsHandler.validate(context, clientConnection.getRemoteProperties());
                clientAsync.complete();
            }).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) Async(io.vertx.ext.unit.Async)

Example 65 with TestContext

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

the class ProtonClientTest method doTestCapabilities.

private void doTestCapabilities(TestContext context, Symbol[] clientDesired, Symbol[] clientOffered, Symbol[] serverDesired, Symbol[] serverOffered, boolean setServerOffered) throws Exception {
    server.close();
    Async serverAsync = context.async();
    Async clientAsync = context.async();
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(x -> {
                serverConnection.setDesiredCapabilities(serverDesired);
                // if anon relay then don't overwrite with null
                if (setServerOffered) {
                    serverConnection.setOfferedCapabilities(serverOffered);
                }
                context.assertTrue(Arrays.equals(clientOffered == null ? NO_CAPABILITIES : clientOffered, serverConnection.getRemoteOfferedCapabilities()));
                context.assertTrue(Arrays.equals(clientDesired == null ? NO_CAPABILITIES : clientDesired, serverConnection.getRemoteDesiredCapabilities()));
                serverConnection.open();
                serverAsync.complete();
            });
        });
        // ===== Client Handling =====
        ProtonClient client = ProtonClient.create(vertx);
        client.connect("localhost", protonServer.actualPort(), res -> {
            context.assertTrue(res.succeeded());
            ProtonConnection clientConnection = res.result();
            clientConnection.setOfferedCapabilities(clientOffered);
            clientConnection.setDesiredCapabilities(clientDesired);
            clientConnection.openHandler(x -> {
                context.assertTrue(x.succeeded());
                LOG.trace("Client connection opened");
                context.assertTrue(Arrays.equals(serverDesired == null ? NO_CAPABILITIES : serverDesired, clientConnection.getRemoteDesiredCapabilities()));
                final Symbol[] expectedServerOffered = setServerOffered ? NO_CAPABILITIES : ANON_RELAY_ONLY;
                context.assertTrue(Arrays.equals(serverOffered == null ? expectedServerOffered : serverOffered, clientConnection.getRemoteOfferedCapabilities()));
                clientAsync.complete();
            }).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) Async(io.vertx.ext.unit.Async)

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