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();
});
}
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);
});
});
}
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);
}
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();
}
}
}
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();
}
}
}
Aggregations