use of org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher in project qpid-protonj2 by apache.
the class StreamSenderTest method testAutoFlushDuringWriteThatExceedConfiguredBufferLimit.
@Test
void testAutoFlushDuringWriteThatExceedConfiguredBufferLimit() throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().ofSender().respond();
peer.remoteFlow().withLinkCredit(1).queue();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
Client container = Client.create();
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
StreamSender sender = connection.openStreamSender("test-queue", new StreamSenderOptions().writeBufferSize(256));
StreamSenderMessage tracker = sender.beginMessage();
final byte[] payload = new byte[1024];
Arrays.fill(payload, 0, 256, (byte) 1);
Arrays.fill(payload, 256, 512, (byte) 2);
Arrays.fill(payload, 512, 768, (byte) 3);
Arrays.fill(payload, 768, 1024, (byte) 4);
final byte[] payload1 = new byte[256];
Arrays.fill(payload1, (byte) 1);
final byte[] payload2 = new byte[256];
Arrays.fill(payload2, (byte) 2);
final byte[] payload3 = new byte[256];
Arrays.fill(payload3, (byte) 3);
final byte[] payload4 = new byte[256];
Arrays.fill(payload4, (byte) 4);
// Populate all Header values
Header header = new Header();
header.setDurable(true);
header.setPriority((byte) 1);
header.setTimeToLive(65535);
header.setFirstAcquirer(true);
header.setDeliveryCount(2);
tracker.header(header);
OutputStreamOptions options = new OutputStreamOptions();
OutputStream stream = tracker.body(options);
HeaderMatcher headerMatcher = new HeaderMatcher(true);
headerMatcher.withDurable(true);
headerMatcher.withPriority((byte) 1);
headerMatcher.withTtl(65535);
headerMatcher.withFirstAcquirer(true);
headerMatcher.withDeliveryCount(2);
EncodedDataMatcher dataMatcher1 = new EncodedDataMatcher(payload1);
TransferPayloadCompositeMatcher payloadMatcher1 = new TransferPayloadCompositeMatcher();
payloadMatcher1.setHeadersMatcher(headerMatcher);
payloadMatcher1.setMessageContentMatcher(dataMatcher1);
EncodedDataMatcher dataMatcher2 = new EncodedDataMatcher(payload2);
TransferPayloadCompositeMatcher payloadMatcher2 = new TransferPayloadCompositeMatcher();
payloadMatcher2.setMessageContentMatcher(dataMatcher2);
EncodedDataMatcher dataMatcher3 = new EncodedDataMatcher(payload3);
TransferPayloadCompositeMatcher payloadMatcher3 = new TransferPayloadCompositeMatcher();
payloadMatcher3.setMessageContentMatcher(dataMatcher3);
EncodedDataMatcher dataMatcher4 = new EncodedDataMatcher(payload4);
TransferPayloadCompositeMatcher payloadMatcher4 = new TransferPayloadCompositeMatcher();
payloadMatcher4.setMessageContentMatcher(dataMatcher4);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectTransfer().withPayload(payloadMatcher1).withMore(true);
peer.expectTransfer().withPayload(payloadMatcher2).withMore(true);
peer.expectTransfer().withPayload(payloadMatcher3).withMore(true);
peer.expectTransfer().withPayload(payloadMatcher4).withMore(true);
// Stream won't output until some body bytes are written.
stream.write(payload);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectTransfer().withNullPayload().withMore(false).accept();
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
stream.close();
sender.close();
connection.close();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher in project qpid-protonj2 by apache.
the class StreamSenderTest method testStreamSenderWritesFooterAfterStreamClosed.
@Test
public void testStreamSenderWritesFooterAfterStreamClosed() throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().ofSender().respond();
peer.remoteFlow().withLinkCredit(10).queue();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Sender test started, peer listening on: {}", remoteURI);
Client container = Client.create();
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort()).openFuture().get();
StreamSender sender = connection.openStreamSender("test-queue");
StreamSenderMessage message = sender.beginMessage();
final byte[] payload = new byte[] { 0, 1, 2, 3, 4, 5 };
// First frame should include only the bits up to the body
HeaderMatcher headerMatcher = new HeaderMatcher(true);
headerMatcher.withDurable(true);
headerMatcher.withPriority((byte) 1);
headerMatcher.withTtl(65535);
headerMatcher.withFirstAcquirer(true);
headerMatcher.withDeliveryCount(2);
ApplicationPropertiesMatcher apMatcher = new ApplicationPropertiesMatcher(true);
apMatcher.withEntry("ap1", Matchers.equalTo(1));
apMatcher.withEntry("ap2", Matchers.equalTo(2));
apMatcher.withEntry("ap3", Matchers.equalTo(3));
FooterMatcher footerMatcher = new FooterMatcher(false);
footerMatcher.withEntry("f1", Matchers.equalTo(1));
footerMatcher.withEntry("f2", Matchers.equalTo(2));
footerMatcher.withEntry("f3", Matchers.equalTo(3));
EncodedDataMatcher bodyMatcher = new EncodedDataMatcher(payload, true);
TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
payloadMatcher.setHeadersMatcher(headerMatcher);
payloadMatcher.setApplicationPropertiesMatcher(apMatcher);
payloadMatcher.setMessageContentMatcher(bodyMatcher);
payloadMatcher.setFootersMatcher(footerMatcher);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectTransfer().withPayload(payloadMatcher).withMore(false).accept();
// Populate all Header values
message.durable(true);
message.priority((byte) 1);
message.timeToLive(65535);
message.firstAcquirer(true);
message.deliveryCount(2);
// Populate message application properties
message.property("ap1", 1);
message.property("ap2", 2);
message.property("ap3", 3);
// Populate message footers
assertFalse(message.hasFooters());
assertFalse(message.hasFooter("f1"));
message.footer("f1", 1);
message.footer("f2", 2);
message.footer("f3", 3);
assertTrue(message.hasFooter("f1"));
assertTrue(message.hasFooters());
OutputStreamOptions bodyOptions = new OutputStreamOptions().completeSendOnClose(true);
OutputStream stream = message.body(bodyOptions);
assertThrows(ClientUnsupportedOperationException.class, () -> message.encode(Collections.emptyMap()));
stream.write(payload);
stream.close();
assertThrows(ClientIllegalStateException.class, () -> message.footer(new Footer(Collections.emptyMap())));
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
assertNotNull(message.tracker());
Wait.assertTrue(() -> message.tracker().settlementFuture().isDone());
assertTrue(message.tracker().settlementFuture().get().settled());
sender.closeAsync().get(10, TimeUnit.SECONDS);
connection.closeAsync().get(10, TimeUnit.SECONDS);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher in project qpid-protonj2 by apache.
the class StreamSenderTest method testStreamMessageWaitingOnCreditWritesWhileCompleteSendWaitsInQueue.
@Test
void testStreamMessageWaitingOnCreditWritesWhileCompleteSendWaitsInQueue() throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().ofSender().respond();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
Client container = Client.create();
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
StreamSender sender = connection.openStreamSender("test-queue");
StreamSenderMessage tracker = sender.beginMessage();
OutputStream stream = tracker.body();
final byte[] payload1 = new byte[256];
Arrays.fill(payload1, (byte) 1);
final byte[] payload2 = new byte[256];
Arrays.fill(payload2, (byte) 2);
final byte[] payload3 = new byte[256];
Arrays.fill(payload3, (byte) 3);
EncodedDataMatcher dataMatcher1 = new EncodedDataMatcher(payload1);
TransferPayloadCompositeMatcher payloadMatcher1 = new TransferPayloadCompositeMatcher();
payloadMatcher1.setMessageContentMatcher(dataMatcher1);
EncodedDataMatcher dataMatcher2 = new EncodedDataMatcher(payload2);
TransferPayloadCompositeMatcher payloadMatcher2 = new TransferPayloadCompositeMatcher();
payloadMatcher2.setMessageContentMatcher(dataMatcher2);
EncodedDataMatcher dataMatcher3 = new EncodedDataMatcher(payload3);
TransferPayloadCompositeMatcher payloadMatcher3 = new TransferPayloadCompositeMatcher();
payloadMatcher3.setMessageContentMatcher(dataMatcher3);
final AtomicBoolean sendFailed = new AtomicBoolean();
final CountDownLatch streamSend1Complete = new CountDownLatch(1);
// Stream won't output until some body bytes are written.
ForkJoinPool.commonPool().execute(() -> {
try {
stream.write(payload1);
stream.flush();
} catch (IOException e) {
LOG.info("send failed with error: ", e);
sendFailed.set(true);
} finally {
streamSend1Complete.countDown();
}
});
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectTransfer().withPayload(payloadMatcher1).withMore(true);
// Now trigger the next send by granting credit for payload 1
peer.remoteFlow().withIncomingWindow(1).withNextIncomingId(1).withLinkCredit(10).now();
assertTrue(streamSend1Complete.await(5, TimeUnit.SECONDS), "Stream sender completed first send");
assertFalse(sendFailed.get());
final CountDownLatch sendStarted = new CountDownLatch(1);
final CountDownLatch sendCompleted = new CountDownLatch(1);
ForkJoinPool.commonPool().execute(() -> {
try {
LOG.info("Test send 1 is preparing to fire:");
sendStarted.countDown();
sender.send(Message.create(payload3));
} catch (Exception e) {
LOG.info("Test send 1 failed with error: ", e);
sendFailed.set(true);
} finally {
sendCompleted.countDown();
}
});
assertTrue(sendStarted.await(10, TimeUnit.SECONDS));
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectTransfer().withPayload(payloadMatcher2).withMore(true);
// Queue a flow that will allow send by granting credit for payload 3 via sender.send
peer.remoteFlow().withIncomingWindow(1).withNextIncomingId(3).withLinkCredit(10).queue();
// Now trigger the next send by granting credit for payload 2
peer.remoteFlow().withIncomingWindow(1).withNextIncomingId(2).withLinkCredit(10).now();
stream.write(payload2);
stream.flush();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectTransfer().withNullPayload().withMore(false).accept();
peer.remoteFlow().withIncomingWindow(1).withNextIncomingId(4).withLinkCredit(10).queue();
peer.expectTransfer().withPayload(payloadMatcher3).withMore(false);
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
stream.close();
assertTrue(sendCompleted.await(100, TimeUnit.SECONDS));
assertFalse(sendFailed.get());
sender.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher in project qpid-protonj2 by apache.
the class StreamSenderTest method testFlushAfterSecondWriteDoesNotEncodeAMQPHeaderFromConfiguration.
@Test
void testFlushAfterSecondWriteDoesNotEncodeAMQPHeaderFromConfiguration() throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().ofSender().respond();
peer.remoteFlow().withLinkCredit(1).queue();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
Client container = Client.create();
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
StreamSender sender = connection.openStreamSender("test-queue");
StreamSenderMessage tracker = sender.beginMessage();
// Populate all Header values
Header header = new Header();
header.setDurable(true);
header.setPriority((byte) 1);
header.setTimeToLive(65535);
header.setFirstAcquirer(true);
header.setDeliveryCount(2);
tracker.header(header);
OutputStreamOptions options = new OutputStreamOptions();
OutputStream stream = tracker.body(options);
HeaderMatcher headerMatcher = new HeaderMatcher(true);
headerMatcher.withDurable(true);
headerMatcher.withPriority((byte) 1);
headerMatcher.withTtl(65535);
headerMatcher.withFirstAcquirer(true);
headerMatcher.withDeliveryCount(2);
EncodedDataMatcher dataMatcher1 = new EncodedDataMatcher(new byte[] { 0, 1, 2, 3 });
TransferPayloadCompositeMatcher payloadMatcher1 = new TransferPayloadCompositeMatcher();
payloadMatcher1.setHeadersMatcher(headerMatcher);
payloadMatcher1.setMessageContentMatcher(dataMatcher1);
// Second flush expectation
EncodedDataMatcher dataMatcher2 = new EncodedDataMatcher(new byte[] { 4, 5, 6, 7 });
TransferPayloadCompositeMatcher payloadMatcher2 = new TransferPayloadCompositeMatcher();
payloadMatcher2.setMessageContentMatcher(dataMatcher2);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectTransfer().withPayload(payloadMatcher1).withMore(true);
peer.expectTransfer().withPayload(payloadMatcher2).withMore(true);
peer.expectTransfer().withNullPayload().withMore(false).accept();
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
// Stream won't output until some body bytes are written.
stream.write(new byte[] { 0, 1, 2, 3 });
stream.flush();
// Next write should only be a single Data section
stream.write(new byte[] { 4, 5, 6, 7 });
stream.flush();
// Final Transfer that completes the Delivery
stream.close();
sender.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.test.driver.matchers.types.EncodedDataMatcher in project qpid-protonj2 by apache.
the class StreamSenderTest method testStreamMessageFlushFailsAfterConnectionDropped.
@Test
void testStreamMessageFlushFailsAfterConnectionDropped() throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().ofSender().respond();
peer.remoteFlow().withLinkCredit(1).queue();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
Client container = Client.create();
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
StreamSender sender = connection.openStreamSender("test-queue");
StreamSenderMessage message = sender.beginMessage();
OutputStream stream = message.body();
EncodedDataMatcher dataMatcher1 = new EncodedDataMatcher(new byte[] { 0, 1, 2, 3 });
TransferPayloadCompositeMatcher payloadMatcher1 = new TransferPayloadCompositeMatcher();
payloadMatcher1.setMessageContentMatcher(dataMatcher1);
EncodedDataMatcher dataMatcher2 = new EncodedDataMatcher(new byte[] { 4, 5, 6, 7 });
TransferPayloadCompositeMatcher payloadMatcher2 = new TransferPayloadCompositeMatcher();
payloadMatcher2.setMessageContentMatcher(dataMatcher2);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectTransfer().withPayload(payloadMatcher1).withMore(true);
peer.expectTransfer().withPayload(payloadMatcher2).withMore(true);
peer.dropAfterLastHandler();
// Write two then after connection drops the message should fail on future writes
stream.write(new byte[] { 0, 1, 2, 3 });
stream.flush();
stream.write(new byte[] { 4, 5, 6, 7 });
stream.flush();
peer.waitForScriptToComplete();
// Next write should fail as connection should have dropped.
stream.write(new byte[] { 8, 9, 10, 11 });
try {
stream.flush();
fail("Should not be able to flush after connection drop");
} catch (IOException ioe) {
assertTrue(ioe.getCause() instanceof ClientException);
}
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
Aggregations