use of io.vertx.core.Handler 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();
});
}
use of io.vertx.core.Handler 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);
});
});
}
use of io.vertx.core.Handler in project vertx-proton by vert-x3.
the class ProtonClientTest method remoteCloseDefaultSessionTestImpl.
private void remoteCloseDefaultSessionTestImpl(TestContext context, boolean sessionError) throws InterruptedException, ExecutionException {
server.close();
Async async = context.async();
ProtonServer protonServer = null;
try {
protonServer = createServer(serverConnection -> {
Future<ProtonSession> sessionFuture = Future.<ProtonSession>future();
// Expect a session to open, when the sender is created by the client
serverConnection.sessionOpenHandler(serverSession -> {
LOG.trace("Server session open");
serverSession.open();
sessionFuture.complete(serverSession);
});
// Expect a receiver link, then close the session after opening it.
serverConnection.receiverOpenHandler(serverReceiver -> {
LOG.trace("Server receiver open");
serverReceiver.open();
context.assertTrue(sessionFuture.succeeded(), "Session future not [yet] succeeded");
LOG.trace("Server session close");
ProtonSession s = sessionFuture.result();
if (sessionError) {
ErrorCondition error = new ErrorCondition();
error.setCondition(AmqpError.INTERNAL_ERROR);
error.setDescription("error description");
s.setCondition(error);
}
s.close();
});
serverConnection.openHandler(result -> {
LOG.trace("Server connection open");
serverConnection.open();
});
});
// ===== Client Handling =====
ProtonClient client = ProtonClient.create(vertx);
client.connect("localhost", protonServer.actualPort(), res -> {
context.assertTrue(res.succeeded());
ProtonConnection connection = res.result();
connection.openHandler(x -> {
context.assertTrue(x.succeeded(), "Connection open failed");
LOG.trace("Client connection opened");
// Create a sender to provoke creation (and subsequent
// closure of by the server) the connections default session
connection.createSender(null).open();
});
connection.closeHandler(x -> {
LOG.trace("Connection close handler called (as espected): " + x.cause());
async.complete();
});
connection.open();
});
async.awaitSuccess();
} finally {
if (protonServer != null) {
protonServer.close();
}
}
}
use of io.vertx.core.Handler in project vertx-proton by vert-x3.
the class ProtonClientTest method doDetachHandlingTestImpl.
public void doDetachHandlingTestImpl(TestContext context, boolean clientSender) throws Exception {
server.close();
final Async clientLinkOpenAsync = context.async();
final Async serverLinkOpenAsync = context.async();
final Async serverLinkDetachAsync = context.async();
final Async clientLinkDetachAsync = context.async();
final AtomicBoolean serverLinkCloseHandlerFired = new AtomicBoolean();
final AtomicBoolean clientLinkCloseHandlerFired = new AtomicBoolean();
ProtonServer protonServer = null;
try {
protonServer = createServer((serverConnection) -> {
serverConnection.openHandler(result -> {
serverConnection.open();
});
serverConnection.sessionOpenHandler(session -> session.open());
if (clientSender) {
serverConnection.receiverOpenHandler(serverReceiver -> {
LOG.trace("Server receiver opened");
serverReceiver.open();
serverLinkOpenAsync.complete();
serverReceiver.closeHandler(res -> {
serverLinkCloseHandlerFired.set(true);
});
serverReceiver.detachHandler(res -> {
context.assertTrue(res.succeeded(), "expected non-errored async result");
serverReceiver.detach();
serverLinkDetachAsync.complete();
});
});
} else {
serverConnection.senderOpenHandler(serverSender -> {
LOG.trace("Server sender opened");
serverSender.open();
serverLinkOpenAsync.complete();
serverSender.closeHandler(res -> {
serverLinkCloseHandlerFired.set(true);
});
serverSender.detachHandler(res -> {
context.assertTrue(res.succeeded(), "expected non-errored async result");
serverSender.detach();
serverLinkDetachAsync.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<?> link;
if (clientSender) {
link = connection.createSender(null);
} else {
link = connection.createReceiver("some-address");
}
link.closeHandler(clientLink -> {
clientLinkCloseHandlerFired.set(true);
});
link.detachHandler(clientLink -> {
LOG.trace("Client link detached");
clientLinkDetachAsync.complete();
});
link.openHandler(y -> {
LOG.trace("Client link opened");
clientLinkOpenAsync.complete();
link.detach();
});
link.open();
}).open();
});
serverLinkOpenAsync.awaitSuccess();
clientLinkOpenAsync.awaitSuccess();
serverLinkDetachAsync.awaitSuccess();
clientLinkDetachAsync.awaitSuccess();
} finally {
if (protonServer != null) {
protonServer.close();
}
}
context.assertFalse(serverLinkCloseHandlerFired.get(), "server link close handler should not have fired");
context.assertFalse(clientLinkCloseHandlerFired.get(), "client link close handler should not have fired");
}
use of io.vertx.core.Handler 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();
}
Aggregations