use of org.apache.qpid.protonj2.test.driver.codec.transactions.Coordinator in project qpid-protonj2 by apache.
the class TransactionsTest method testExceptionOnBeginWhenCoordinatorLinkRefused.
@Test
public void testExceptionOnBeginWhenCoordinatorLinkRefused() throws Exception {
final String errorMessage = "CoordinatorLinkRefusal-breadcrumb";
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectCoordinatorAttach().reject(true, AmqpError.NOT_IMPLEMENTED.toString(), errorMessage);
peer.expectDetach();
peer.expectEnd().respond();
peer.expectClose().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());
Session session = connection.openSession().openFuture().get();
try {
session.beginTransaction();
fail("Begin should have failed after link closed.");
} catch (ClientTransactionDeclarationException expected) {
// Expect this to time out.
String message = expected.getMessage();
assertTrue(message.contains(errorMessage));
}
try {
session.commitTransaction();
fail("Commit should have failed due to no active transaction.");
} catch (ClientTransactionNotActiveException expected) {
// Expect this as the begin failed on coordinator rejected
}
try {
session.rollbackTransaction();
fail("Rollback should have failed due to no active transaction.");
} catch (ClientTransactionNotActiveException expected) {
// Expect this as the begin failed on coordinator rejected
}
session.closeAsync();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.test.driver.codec.transactions.Coordinator in project qpid-protonj2 by apache.
the class TransactionsTest method testExceptionOnBeginWhenCoordinatorLinkClosedAfterDeclare.
@Test
public void testExceptionOnBeginWhenCoordinatorLinkClosedAfterDeclare() throws Exception {
final String errorMessage = "CoordinatorLinkClosed-breadcrumb";
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectCoordinatorAttach().respond();
peer.remoteFlow().withLinkCredit(2).queue();
peer.expectDeclare();
peer.remoteDetach().withClosed(true).withErrorCondition(AmqpError.NOT_IMPLEMENTED.toString(), errorMessage).queue();
peer.expectDetach();
peer.expectEnd().respond();
peer.expectClose().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());
Session session = connection.openSession().openFuture().get();
try {
session.beginTransaction();
fail("Begin should have failed after link closed.");
} catch (ClientException expected) {
// Expect this to time out.
String message = expected.getMessage();
assertTrue(message.contains(errorMessage));
}
try {
session.commitTransaction();
fail("Commit should have failed due to no active transaction.");
} catch (ClientTransactionNotActiveException expected) {
// Expect this as the begin failed on coordinator close
}
try {
session.rollbackTransaction();
fail("Rollback should have failed due to no active transaction.");
} catch (ClientTransactionNotActiveException expected) {
// Expect this as the begin failed on coordinator close
}
session.closeAsync();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.test.driver.codec.transactions.Coordinator in project qpid-protonj2 by apache.
the class ScriptWriter method respondToLastAttach.
/**
* Creates a Attach response for the last link Attach that was received and fills in the Attach
* fields based on values from the remote. The caller can further customize the Attach that is
* emitted by using the various with methods to assign values to the fields in the Attach.
*
* @return a new {@link AttachInjectAction} that can be queued or sent immediately.
*
* @throws IllegalStateException if no Attach has yet been received from the remote.
*/
public AttachInjectAction respondToLastAttach() {
AttachInjectAction response = new AttachInjectAction(getDriver());
final SessionTracker session = getDriver().sessions().getLastRemotelyOpenedSession();
final LinkTracker link = session.getLastRemotelyOpenedLink();
if (link == null) {
throw new IllegalStateException("Cannot create response to Attach before one has been received.");
}
if (link.isLocallyAttached()) {
throw new IllegalStateException("Cannot create response to Attach since a local Attach was already sent.");
}
// Populate the response using data in the locally opened link, script can override this after return.
response.onChannel(link.getSession().getLocalChannel());
response.withName(link.getName());
response.withRole(link.getRole());
response.withSndSettleMode(link.getRemoteSenderSettleMode());
response.withRcvSettleMode(link.getRemoteReceiverSettleMode());
if (link.getRemoteSource() != null) {
response.withSource(new Source(link.getRemoteSource()));
if (Boolean.TRUE.equals(link.getRemoteSource().getDynamic())) {
response.withSource().withAddress(UUID.randomUUID().toString());
}
}
if (link.getRemoteTarget() != null) {
response.withTarget(new Target(link.getRemoteTarget()));
if (Boolean.TRUE.equals(link.getRemoteTarget().getDynamic())) {
response.withTarget().withAddress(UUID.randomUUID().toString());
}
}
if (link.getRemoteCoordinator() != null) {
response.withTarget(new Coordinator(link.getRemoteCoordinator()));
}
if (response.getPerformative().getInitialDeliveryCount() == null) {
if (link.isSender()) {
response.withInitialDeliveryCount(0);
}
}
return response;
}
use of org.apache.qpid.protonj2.test.driver.codec.transactions.Coordinator in project qpid-protonj2 by apache.
the class DetachLastCoordinatorInjectAction method beforeActionPerformed.
@Override
protected void beforeActionPerformed(AMQPTestDriver driver) {
LinkTracker tracker = driver.sessions().getLastOpenedCoordinator();
if (tracker == null) {
throw new AssertionError("Cannot send coordinator detach as scripted, no active coordinator found.");
}
onChannel(tracker.getSession().getLocalChannel().intValue());
if (!tracker.isLocallyAttached()) {
AttachInjectAction attach = new AttachInjectAction(driver);
attach.onChannel(onChannel());
attach.withName(tracker.getName());
attach.withSource(tracker.getRemoteSource());
if (tracker.getRemoteTarget() != null) {
attach.withTarget(tracker.getRemoteTarget());
} else {
attach.withTarget(tracker.getRemoteCoordinator());
}
if (tracker.isSender()) {
attach.withRole(Role.SENDER);
// the action will not override an explicitly null source.
if (getPerformative().getError() != null) {
attach.withNullSource();
}
} else {
attach.withRole(Role.RECEIVER);
// the action will not override an explicitly null target.
if (getPerformative().getError() != null) {
if (getPerformative().getError() != null) {
attach.withNullTarget();
}
}
}
attach.perform(driver);
}
getPerformative().setHandle(tracker.getHandle());
}
use of org.apache.qpid.protonj2.test.driver.codec.transactions.Coordinator in project qpid-protonj2 by apache.
the class AttachExpectation method handleAttach.
// ----- Handle the performative and configure response is told to respond
@Override
public void handleAttach(int frameSize, Attach attach, ByteBuf payload, int channel, AMQPTestDriver context) {
super.handleAttach(frameSize, attach, payload, channel, context);
final UnsignedShort remoteChannel = UnsignedShort.valueOf(channel);
final SessionTracker session = driver.sessions().getSessionFromRemoteChannel(remoteChannel);
if (session == null) {
throw new AssertionError(String.format("Received Attach on channel [%s] that has no matching Session for that remote channel. ", remoteChannel));
}
final LinkTracker link = session.handleRemoteAttach(attach);
if (response != null) {
// to say otherwise by the test.
if (response.onChannel() == BeginInjectAction.CHANNEL_UNSET) {
response.onChannel(link.getSession().getLocalChannel());
}
// Populate the fields of the response with defaults if non set by the test script
if (response.getPerformative().getHandle() == null) {
response.withHandle(session.findFreeLocalHandle());
}
if (response.getPerformative().getName() == null) {
response.withName(attach.getName());
}
if (response.getPerformative().getRole() == null) {
response.withRole(Boolean.TRUE.equals(attach.getRole()) ? Role.SENDER : Role.RECEIVER);
}
if (response.getPerformative().getSenderSettleMode() == null) {
response.withSndSettleMode(SenderSettleMode.valueOf(attach.getSenderSettleMode()));
}
if (response.getPerformative().getReceiverSettleMode() == null) {
response.withRcvSettleMode(ReceiverSettleMode.valueOf(attach.getReceiverSettleMode()));
}
if (response.getPerformative().getSource() == null && !response.isNullSourceRequired()) {
response.withSource(attach.getSource());
if (attach.getSource() != null && Boolean.TRUE.equals(attach.getSource().getDynamic())) {
attach.getSource().setAddress(UUID.randomUUID().toString());
}
}
if (rejecting) {
if (Boolean.FALSE.equals(attach.getRole())) {
// Sender attach so response should have null target
response.withNullTarget();
} else {
// Receiver attach so response should have null source
response.withNullSource();
}
}
if (response.getPerformative().getTarget() == null && !response.isNullTargetRequired()) {
if (attach.getTarget() != null) {
if (attach.getTarget() instanceof Target) {
Target target = (Target) attach.getTarget();
response.withTarget(target);
if (target != null && Boolean.TRUE.equals(target.getDynamic())) {
target.setAddress(UUID.randomUUID().toString());
}
} else {
Coordinator coordinator = (Coordinator) attach.getTarget();
response.withTarget(coordinator);
}
}
}
if (response.getPerformative().getInitialDeliveryCount() == null) {
Role role = Role.valueOf(response.getPerformative().getRole());
if (role == Role.SENDER) {
response.withInitialDeliveryCount(0);
}
}
// Other fields are left not set for now unless test script configured
}
}
Aggregations