use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class StreamReceiverTest method testReadBytesFromBodyInputStreamWithSplitSingleByteTransfers.
private void testReadBytesFromBodyInputStreamWithSplitSingleByteTransfers(int option) 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();
for (int i = 0; i < payload.length; ++i) {
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(true).withMessageFormat(0).withPayload(new byte[] { payload[i] }).afterDelay(3).queue();
}
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withMessageFormat(0).afterDelay(5).queue();
peer.expectDisposition().withFirst(0).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();
final InputStream bodyStream = message.body();
final byte[] receivedBody = new byte[body.length];
if (option == 1) {
for (int i = 0; i < body.length; ++i) {
receivedBody[i] = (byte) bodyStream.read();
}
assertArrayEquals(body, receivedBody);
} else if (option == 2) {
assertEquals(body.length, bodyStream.read(receivedBody));
assertArrayEquals(body, receivedBody);
} else if (option == 3) {
assertEquals(body.length, bodyStream.skip(body.length));
} else {
fail("Unknown test option");
}
bodyStream.close();
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
receiver.close();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class StreamReceiverTest method testStreamDeliveryHandlesInvalidMessageAnnotationsEncoding.
@Test
public void testStreamDeliveryHandlesInvalidMessageAnnotationsEncoding() throws Exception {
final byte[] payload = createInvalidMessageAnnotationsEncoding();
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 StreamReceiverOptions options = new StreamReceiverOptions().autoAccept(false);
final StreamReceiver receiver = connection.openStreamReceiver("test-queue", options);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectDisposition().withState().rejected("decode-error", "failed reading message header");
final StreamDelivery delivery = receiver.receive();
final StreamReceiverMessage message = delivery.message();
assertThrows(ClientException.class, () -> message.annotations());
assertThrows(ClientException.class, () -> message.body());
delivery.reject("decode-error", "failed reading message header");
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
receiver.close();
connection.close();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Connection 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 com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.
the class ReconnectStreamSenderTest method testStreamMessageFlushFailsAfterConnectionDropped.
@Test
void testStreamMessageFlushFailsAfterConnectionDropped() 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.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.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();
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);
firstPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
firstPeer.expectTransfer().withPayload(payloadMatcher1).withMore(true);
firstPeer.expectTransfer().withPayload(payloadMatcher2).withMore(true);
firstPeer.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();
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();
// 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();
finalPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Connection 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