use of org.apache.qpid.protonj2.types.messaging.Section 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.types.messaging.Section in project qpid-protonj2 by apache.
the class ImperativeClientTestCase method createEncodedMessage.
protected byte[] createEncodedMessage(Section<?>... body) {
Encoder encoder = CodecFactory.getEncoder();
ProtonBuffer buffer = new ProtonByteBufferAllocator().allocate();
for (Section<?> section : body) {
encoder.writeObject(buffer, encoder.newEncoderState(), section);
}
byte[] result = new byte[buffer.getReadableBytes()];
buffer.readBytes(result);
return result;
}
use of org.apache.qpid.protonj2.types.messaging.Section in project qpid-protonj2 by apache.
the class StreamReceiverTest method testStreamReceiverMessageThrowsOnAnyMessageModificationAPI.
@Test
public void testStreamReceiverMessageThrowsOnAnyMessageModificationAPI() throws Exception {
final byte[] body = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final byte[] payload = createEncodedMessage(new Data(body));
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.expectDisposition().withFirst(0).withState().accepted().withSettled(true);
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");
final StreamDelivery delivery = receiver.receive();
final StreamReceiverMessage message = delivery.message();
assertThrows(ClientUnsupportedOperationException.class, () -> message.header(new Header()));
assertThrows(ClientUnsupportedOperationException.class, () -> message.properties(new Properties()));
assertThrows(ClientUnsupportedOperationException.class, () -> message.applicationProperties(new ApplicationProperties(null)));
assertThrows(ClientUnsupportedOperationException.class, () -> message.annotations(new MessageAnnotations(null)));
assertThrows(ClientUnsupportedOperationException.class, () -> message.footer(new Footer(null)));
assertThrows(ClientUnsupportedOperationException.class, () -> message.messageFormat(1));
assertThrows(ClientUnsupportedOperationException.class, () -> message.durable(true));
assertThrows(ClientUnsupportedOperationException.class, () -> message.priority((byte) 4));
assertThrows(ClientUnsupportedOperationException.class, () -> message.timeToLive(128));
assertThrows(ClientUnsupportedOperationException.class, () -> message.firstAcquirer(false));
assertThrows(ClientUnsupportedOperationException.class, () -> message.deliveryCount(10));
assertThrows(ClientUnsupportedOperationException.class, () -> message.messageId(10));
assertThrows(ClientUnsupportedOperationException.class, () -> message.correlationId(10));
assertThrows(ClientUnsupportedOperationException.class, () -> message.userId(new byte[] { 1 }));
assertThrows(ClientUnsupportedOperationException.class, () -> message.to("test"));
assertThrows(ClientUnsupportedOperationException.class, () -> message.subject("test"));
assertThrows(ClientUnsupportedOperationException.class, () -> message.replyTo("test"));
assertThrows(ClientUnsupportedOperationException.class, () -> message.contentType("test"));
assertThrows(ClientUnsupportedOperationException.class, () -> message.contentEncoding("test"));
assertThrows(ClientUnsupportedOperationException.class, () -> message.absoluteExpiryTime(10));
assertThrows(ClientUnsupportedOperationException.class, () -> message.creationTime(10));
assertThrows(ClientUnsupportedOperationException.class, () -> message.groupId("test"));
assertThrows(ClientUnsupportedOperationException.class, () -> message.groupSequence(10));
assertThrows(ClientUnsupportedOperationException.class, () -> message.replyToGroupId("test"));
assertThrows(ClientUnsupportedOperationException.class, () -> message.annotation("test", 1));
assertThrows(ClientUnsupportedOperationException.class, () -> message.removeAnnotation("test"));
assertThrows(ClientUnsupportedOperationException.class, () -> message.property("test", 1));
assertThrows(ClientUnsupportedOperationException.class, () -> message.removeProperty("test"));
assertThrows(ClientUnsupportedOperationException.class, () -> message.footer("test", 1));
assertThrows(ClientUnsupportedOperationException.class, () -> message.removeFooter("test"));
assertThrows(ClientUnsupportedOperationException.class, () -> message.body(InputStream.nullInputStream()));
assertThrows(ClientUnsupportedOperationException.class, () -> message.addBodySection(new AmqpValue<>("test")));
assertThrows(ClientUnsupportedOperationException.class, () -> message.bodySections(Collections.emptyList()));
assertThrows(ClientUnsupportedOperationException.class, () -> message.bodySections());
assertThrows(ClientUnsupportedOperationException.class, () -> message.clearBodySections());
assertThrows(ClientUnsupportedOperationException.class, () -> message.forEachBodySection((section) -> {
}));
assertThrows(ClientUnsupportedOperationException.class, () -> message.encode(Collections.emptyMap()));
InputStream bodyStream = message.body();
assertNotNull(bodyStream.readAllBytes());
bodyStream.close();
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
receiver.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.types.messaging.Section in project qpid-protonj2 by apache.
the class ReconnectStreamSenderTest method testStreamMessageWriteThatFlushesFailsAfterConnectionDropped.
@Test
void testStreamMessageWriteThatFlushesFailsAfterConnectionDropped() throws Exception {
try (ProtonTestServer firstPeer = new ProtonTestServer();
ProtonTestServer finalPeer = new ProtonTestServer()) {
firstPeer.expectSASLAnonymousConnect();
firstPeer.expectOpen().respond();
firstPeer.expectBegin().respond();
firstPeer.expectAttach().ofSender().respond();
firstPeer.remoteFlow().withLinkCredit(1).queue();
firstPeer.dropAfterLastHandler();
firstPeer.start();
finalPeer.expectSASLAnonymousConnect();
finalPeer.expectOpen().respond();
finalPeer.expectBegin().respond();
finalPeer.expectAttach().ofSender().respond();
finalPeer.remoteFlow().withLinkCredit(1).queue();
finalPeer.start();
final URI primaryURI = firstPeer.getServerURI();
final URI backupURI = finalPeer.getServerURI();
ConnectionOptions options = new ConnectionOptions();
options.maxFrameSize(32768);
options.idleTimeout(5, TimeUnit.SECONDS);
options.reconnectOptions().reconnectEnabled(true);
options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
Client container = Client.create();
Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
StreamSender sender = connection.openStreamSender("test-queue");
StreamSenderMessage message = sender.beginMessage();
byte[] payload = new byte[65536];
Arrays.fill(payload, (byte) 65);
OutputStreamOptions streamOptions = new OutputStreamOptions().bodyLength(payload.length);
OutputStream stream = message.body(streamOptions);
firstPeer.waitForScriptToComplete();
// Reconnection should have occurred now and we should not be able to flush data
// from
// the stream as its initial sender instance was closed on disconnect.
finalPeer.waitForScriptToComplete();
finalPeer.expectClose().respond();
try {
stream.write(payload);
fail("Should not be able to write section after connection drop");
} catch (IOException ioe) {
assertTrue(ioe.getCause() instanceof ClientException);
}
connection.closeAsync().get();
finalPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.types.messaging.Section in project qpid-protonj2 by apache.
the class ReconnectStreamSenderTest method testStreamMessageWriteThatFlushesFailsAfterConnectionDroppedAndReconnected.
@Test
public void testStreamMessageWriteThatFlushesFailsAfterConnectionDroppedAndReconnected() throws Exception {
try (ProtonTestServer firstPeer = new ProtonTestServer();
ProtonTestServer finalPeer = new ProtonTestServer()) {
EncodedDataMatcher dataMatcher = new EncodedDataMatcher(new byte[] { 0, 1, 2, 3 });
TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
payloadMatcher.setMessageContentMatcher(dataMatcher);
firstPeer.expectSASLAnonymousConnect();
firstPeer.expectOpen().respond();
firstPeer.expectBegin().respond();
firstPeer.expectAttach().ofSender().respond();
firstPeer.remoteFlow().withLinkCredit(1).queue();
firstPeer.expectTransfer().withPayload(payloadMatcher).withMore(true);
firstPeer.dropAfterLastHandler();
firstPeer.start();
finalPeer.expectSASLAnonymousConnect();
finalPeer.expectOpen().respond();
finalPeer.expectBegin().respond();
finalPeer.expectAttach().ofSender().respond();
finalPeer.start();
final URI primaryURI = firstPeer.getServerURI();
final URI backupURI = finalPeer.getServerURI();
ConnectionOptions options = new ConnectionOptions();
options.maxFrameSize(32768);
options.idleTimeout(5, TimeUnit.SECONDS);
options.reconnectOptions().reconnectEnabled(true);
options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
Client container = Client.create();
Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
StreamSenderOptions senderOptions = new StreamSenderOptions();
senderOptions.sendTimeout(1000);
StreamSender sender = connection.openStreamSender("test-queue", senderOptions);
StreamSenderMessage message = sender.beginMessage();
OutputStream stream = message.body();
stream.write(new byte[] { 0, 1, 2, 3 });
stream.flush();
firstPeer.waitForScriptToComplete();
// Reconnection should have occurred now and we should not be able to flush data
// from the stream as its initial sender instance was closed on disconnect.
finalPeer.waitForScriptToComplete();
// Ensure that idle processing happens in case send blocks so we can see the
// send timed out exception
finalPeer.remoteEmptyFrame().later(5000);
finalPeer.remoteEmptyFrame().later(10000);
finalPeer.remoteEmptyFrame().later(15000);
// Test timeout kicks in now
finalPeer.remoteEmptyFrame().later(20000);
finalPeer.expectClose().respond();
byte[] payload = new byte[1024];
Arrays.fill(payload, (byte) 65);
try {
stream.write(payload);
stream.flush();
fail("Should not be able to write section after connection drop");
} catch (IOException ioe) {
assertFalse(ioe.getCause() instanceof ClientSendTimedOutException);
assertTrue(ioe.getCause() instanceof ClientConnectionRemotelyClosedException);
}
connection.closeAsync().get();
finalPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
Aggregations