use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class StreamReceiverTest method testReadBytesFromBodyInputStreamWithinTransactedSession.
@Test
public void testReadBytesFromBodyInputStreamWithinTransactedSession() throws Exception {
final byte[] body = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final byte[] payload = createEncodedMessage(new Data(body));
final byte[] txnId = new byte[] { 0, 1, 2, 3 };
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
peer.expectFlow();
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withMessageFormat(0).withPayload(payload).queue();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
final Client container = Client.create();
final Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
final StreamReceiver receiver = connection.openStreamReceiver("test-queue");
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectCoordinatorAttach().respond();
peer.remoteFlow().withLinkCredit(2).queue();
peer.expectDeclare().accept(txnId);
peer.expectDisposition().withSettled(true).withState().transactional().withTxnId(txnId).withAccepted();
peer.expectDischarge().withFail(false).withTxnId(txnId).accept();
receiver.session().beginTransaction();
final StreamDelivery delivery = receiver.receive();
assertNotNull(delivery);
assertTrue(delivery.completed());
assertFalse(delivery.aborted());
StreamReceiverMessage message = delivery.message();
assertNotNull(message);
InputStream bodyStream = message.body();
assertNotNull(bodyStream);
assertNull(message.header());
assertNull(message.annotations());
assertNull(message.properties());
assertNull(delivery.annotations());
final byte[] receivedBody = new byte[body.length];
for (int i = 0; i < body.length; ++i) {
receivedBody[i] = (byte) bodyStream.read();
}
assertArrayEquals(body, receivedBody);
assertEquals(-1, bodyStream.read());
receiver.session().commitTransaction();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
receiver.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class StreamSenderTest method testSendCustomMessageWithMultipleAmqpValueSections.
@Test
public void testSendCustomMessageWithMultipleAmqpValueSections() throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
// Hidden session for stream sender
peer.expectBegin().respond();
peer.expectAttach().ofSender().respond();
peer.remoteFlow().withLinkCredit(10).queue();
// Open a receiver to ensure sender link has processed
peer.expectAttach().respond();
// the inbound flow frame we sent previously before send.
peer.expectFlow();
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();
Session session = connection.openSession().openFuture().get();
StreamSenderOptions options = new StreamSenderOptions();
options.deliveryMode(DeliveryMode.AT_MOST_ONCE);
options.writeBufferSize(Integer.MAX_VALUE);
StreamSender sender = connection.openStreamSender("test-qos", options);
// Create a custom message format send context and ensure that no early buffer writes take place
StreamSenderMessage message = sender.beginMessage();
assertEquals(sender, message.sender());
assertNull(message.tracker());
assertEquals(Header.DEFAULT_PRIORITY, message.priority());
assertEquals(Header.DEFAULT_DELIVERY_COUNT, message.deliveryCount());
assertEquals(Header.DEFAULT_FIRST_ACQUIRER, message.firstAcquirer());
assertEquals(Header.DEFAULT_TIME_TO_LIVE, message.timeToLive());
assertEquals(Header.DEFAULT_DURABILITY, message.durable());
message.messageFormat(17);
// Gates send on remote flow having been sent and received
session.openReceiver("dummy").openFuture().get();
HeaderMatcher headerMatcher = new HeaderMatcher(true);
headerMatcher.withDurable(true);
headerMatcher.withPriority((byte) 1);
headerMatcher.withTtl(65535);
headerMatcher.withFirstAcquirer(true);
headerMatcher.withDeliveryCount(2);
// Note: This is a specification violation but could be used by other message formats
// and we don't attempt to enforce at the Send Context what users write
EncodedAmqpValueMatcher bodyMatcher1 = new EncodedAmqpValueMatcher("one", true);
EncodedAmqpValueMatcher bodyMatcher2 = new EncodedAmqpValueMatcher("two", true);
EncodedAmqpValueMatcher bodyMatcher3 = new EncodedAmqpValueMatcher("three", false);
TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
payloadMatcher.setHeadersMatcher(headerMatcher);
payloadMatcher.addMessageContentMatcher(bodyMatcher1);
payloadMatcher.addMessageContentMatcher(bodyMatcher2);
payloadMatcher.addMessageContentMatcher(bodyMatcher3);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectTransfer().withMore(false).withMessageFormat(17).withPayload(payloadMatcher).accept();
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
// Populate all Header values
Header header = new Header();
header.setDurable(true);
header.setPriority((byte) 1);
header.setTimeToLive(65535);
header.setFirstAcquirer(true);
header.setDeliveryCount(2);
message.header(header);
message.addBodySection(new AmqpValue<>("one"));
message.addBodySection(new AmqpValue<>("two"));
message.addBodySection(new AmqpValue<>("three"));
message.complete();
assertNotNull(message.tracker());
assertEquals(17, message.messageFormat());
Wait.assertTrue(() -> message.tracker().settlementFuture().isDone());
assertTrue(message.tracker().settlementFuture().get().settled());
assertThrows(ClientIllegalStateException.class, () -> message.addBodySection(new AmqpValue<>("three")));
assertThrows(ClientIllegalStateException.class, () -> message.body());
assertThrows(ClientIllegalStateException.class, () -> message.rawOutputStream());
assertThrows(ClientIllegalStateException.class, () -> message.abort());
sender.closeAsync().get(10, TimeUnit.SECONDS);
connection.closeAsync().get(10, TimeUnit.SECONDS);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Connection 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 com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class StreamSenderTest method doTestNonBodySectionWrittenWhenNoWritesToStream.
private void doTestNonBodySectionWrittenWhenNoWritesToStream(boolean flushBeforeClose) 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();
// Populate all Header values
Header header = new Header();
header.setDurable(true);
header.setPriority((byte) 1);
header.setTimeToLive(65535);
header.setFirstAcquirer(true);
header.setDeliveryCount(2);
message.header(header);
OutputStreamOptions options = new OutputStreamOptions();
OutputStream stream = message.body(options);
HeaderMatcher headerMatcher = new HeaderMatcher(true);
headerMatcher.withDurable(true);
headerMatcher.withPriority((byte) 1);
headerMatcher.withTtl(65535);
headerMatcher.withFirstAcquirer(true);
headerMatcher.withDeliveryCount(2);
TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
payloadMatcher.setHeadersMatcher(headerMatcher);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
if (flushBeforeClose) {
peer.expectTransfer().withMore(true).withPayload(payloadMatcher);
peer.expectTransfer().withMore(false).withNullPayload().respond().withSettled(true).withState().accepted();
} else {
peer.expectTransfer().withMore(false).withPayload(payloadMatcher).respond().withSettled(true).withState().accepted();
}
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
// the delivery.
if (flushBeforeClose) {
stream.flush();
}
stream.close();
message.tracker().awaitSettlement(10, TimeUnit.SECONDS);
sender.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Connection 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);
}
}
Aggregations