Search in sources :

Example 1 with Coordinator

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);
    }
}
Also used : ClientTransactionDeclarationException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionDeclarationException) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientTransactionNotActiveException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionNotActiveException) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 2 with Coordinator

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);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientTransactionNotActiveException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionNotActiveException) Connection(org.apache.qpid.protonj2.client.Connection) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 3 with Coordinator

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;
}
Also used : Target(org.apache.qpid.protonj2.test.driver.codec.messaging.Target) AttachInjectAction(org.apache.qpid.protonj2.test.driver.actions.AttachInjectAction) Coordinator(org.apache.qpid.protonj2.test.driver.codec.transactions.Coordinator) Source(org.apache.qpid.protonj2.test.driver.codec.messaging.Source)

Example 4 with Coordinator

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());
}
Also used : LinkTracker(org.apache.qpid.protonj2.test.driver.LinkTracker)

Example 5 with Coordinator

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
    }
}
Also used : Role(org.apache.qpid.protonj2.test.driver.codec.transport.Role) Target(org.apache.qpid.protonj2.test.driver.codec.messaging.Target) SessionTracker(org.apache.qpid.protonj2.test.driver.SessionTracker) Coordinator(org.apache.qpid.protonj2.test.driver.codec.transactions.Coordinator) LinkTracker(org.apache.qpid.protonj2.test.driver.LinkTracker) UnsignedShort(org.apache.qpid.protonj2.test.driver.codec.primitives.UnsignedShort)

Aggregations

Coordinator (org.apache.qpid.protonj2.types.transactions.Coordinator)35 Test (org.junit.jupiter.api.Test)26 Source (org.apache.qpid.protonj2.types.messaging.Source)22 Connection (org.apache.qpid.protonj2.engine.Connection)21 Engine (org.apache.qpid.protonj2.engine.Engine)21 Session (org.apache.qpid.protonj2.engine.Session)21 ProtonTestConnector (org.apache.qpid.protonj2.test.driver.ProtonTestConnector)21 TransactionController (org.apache.qpid.protonj2.engine.TransactionController)19 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 ProtonBuffer (org.apache.qpid.protonj2.buffer.ProtonBuffer)7 InputStream (java.io.InputStream)5 ProtonBufferInputStream (org.apache.qpid.protonj2.buffer.ProtonBufferInputStream)5 ErrorCondition (org.apache.qpid.protonj2.types.transport.ErrorCondition)5 Sender (org.apache.qpid.protonj2.engine.Sender)4 Transaction (org.apache.qpid.protonj2.engine.Transaction)4 Symbol (org.apache.qpid.protonj2.types.Symbol)4 Target (org.apache.qpid.protonj2.types.messaging.Target)4 URI (java.net.URI)3 Client (org.apache.qpid.protonj2.client.Client)3