public async Task TestSendWorksAfterConnectionStopped() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); testPeer.ExpectSenderAttach(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); IQueue destination = await session.GetQueueAsync("myQueue"); IMessageProducer producer = await session.CreateProducerAsync(destination); testPeer.ExpectTransfer(Assert.IsNotNull); await connection.StopAsync(); await producer.SendAsync(await session.CreateMessageAsync()); testPeer.ExpectDetach(expectClosed: true, sendResponse: true, replyClosed: true); testPeer.ExpectClose(); await producer.CloseAsync(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(1000); } }
public async Task TestCloseSender() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = await base.EstablishConnectionAsync(testPeer); testPeer.ExpectBegin(); testPeer.ExpectSenderAttach(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); IQueue queue = await session.GetQueueAsync("myQueue"); IMessageProducer producer = await session.CreateProducerAsync(); testPeer.ExpectDetach(expectClosed: true, sendResponse: true, replyClosed: true); testPeer.ExpectClose(); await producer.CloseAsync(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(1000); } }
public async Task TestURI(string connectionURI) { { Uri uri = URISupport.CreateCompatibleUri(NMSTestSupport.ReplaceEnvVar(connectionURI)); ConnectionFactory factory = new ConnectionFactory(uri); Assert.IsNotNull(factory); using (IConnection connection = await factory.CreateConnectionAsync("", "")) { Assert.IsNotNull(connection); using (ISession session = await connection.CreateSessionAsync()) { IDestination destination = await session.CreateTemporaryTopicAsync(); using (IMessageProducer producer = await session.CreateProducerAsync(destination)) { await producer.CloseAsync(); } using (IMessageConsumer consumer = await session.CreateConsumerAsync(destination)) { await consumer.CloseAsync(); } await session.CloseAsync(); } await connection.CloseAsync(); } } { ConnectionFactory factory = new ConnectionFactory(NMSTestSupport.ReplaceEnvVar(connectionURI)); Assert.IsNotNull(factory); using (IConnection connection = await factory.CreateConnectionAsync("", "")) { Assert.IsNotNull(connection); using (ISession session = await connection.CreateSessionAsync()) { IDestination destination = await session.CreateTemporaryTopicAsync(); using (IMessageProducer producer = await session.CreateProducerAsync(destination)) { await producer.CloseAsync(); } using (IMessageConsumer consumer = await session.CreateConsumerAsync(destination)) { await consumer.CloseAsync(); } await session.CloseAsync(); } await connection.CloseAsync(); } } }
public async Task TestRemotelyCloseProducer() { string breadCrumb = "ErrorMessageBreadCrumb"; ManualResetEvent producerClosed = new ManualResetEvent(false); Mock <INmsConnectionListener> mockConnectionListener = new Mock <INmsConnectionListener>(); mockConnectionListener .Setup(listener => listener.OnProducerClosed(It.IsAny <NmsMessageProducer>(), It.IsAny <Exception>())) .Callback(() => { producerClosed.Set(); }); using (TestAmqpPeer testPeer = new TestAmqpPeer()) { NmsConnection connection = (NmsConnection) await EstablishConnectionAsync(testPeer); connection.AddConnectionListener(mockConnectionListener.Object); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); // Create a producer, then remotely end it afterwards. testPeer.ExpectSenderAttach(); testPeer.RemotelyDetachLastOpenedLinkOnLastOpenedSession(expectDetachResponse: true, closed: true, errorType: AmqpError.RESOURCE_DELETED, breadCrumb, delayBeforeSend: 10); IQueue destination = await session.GetQueueAsync("myQueue"); IMessageProducer producer = await session.CreateProducerAsync(destination); // Verify the producer gets marked closed testPeer.WaitForAllMatchersToComplete(1000); Assert.True(producerClosed.WaitOne(TimeSpan.FromMilliseconds(1000)), "Producer closed callback didn't trigger"); Assert.That(() => producer.DisableMessageID, Throws.Exception.InstanceOf <IllegalStateException>(), "Producer never closed"); // Try closing it explicitly, should effectively no-op in client. // The test peer will throw during close if it sends anything. await producer.CloseAsync(); } }
public async Task TestReceiveIgnoreExpirationMessage( [Values(AcknowledgementMode.AutoAcknowledge, AcknowledgementMode.ClientAcknowledge, AcknowledgementMode.DupsOkAcknowledge, AcknowledgementMode.Transactional)] AcknowledgementMode ackMode, [Values(MsgDeliveryMode.NonPersistent, MsgDeliveryMode.Persistent)] MsgDeliveryMode deliveryMode, [Values(ExpirationOptions.DEFAULT, ExpirationOptions.IGNORE, ExpirationOptions.DO_NOT_IGNORE)] ExpirationOptions expirationOption) { using (IConnection connection = CreateConnection(TEST_CLIENT_ID)) { await connection.StartAsync(); using (Session session = await connection.CreateSessionAsync(ackMode) as Session) { string destinationName = DESTINATION_NAME; if (ExpirationOptions.IGNORE == expirationOption) { destinationName += "?consumer.nms.ignoreExpiration=true"; } else if (ExpirationOptions.DO_NOT_IGNORE == expirationOption) { destinationName += "?consumer.nms.ignoreExpiration=false"; } try { IDestination destination = SessionUtil.GetDestination(session, destinationName); using (IMessageConsumer consumer = await session.CreateConsumerAsync(destination)) using (IMessageProducer producer = await session.CreateProducerAsync(destination)) { producer.DeliveryMode = deliveryMode; string msgText = string.Format("ExpiredMessage: {0}", Guid.NewGuid().ToString()); ActiveMQTextMessage msg = await session.CreateTextMessageAsync(msgText) as ActiveMQTextMessage; // Give it two seconds to live. msg.NMSTimeToLive = TimeSpan.FromMilliseconds(2000); await producer.SendAsync(msg); if (AcknowledgementMode.Transactional == ackMode) { await session.CommitAsync(); } // Wait for four seconds before processing it. The broker will have sent it to our local // client dispatch queue, but we won't attempt to process the message until it has had // a chance to expire within our internal queue system. Thread.Sleep(4000); ActiveMQTextMessage rcvMsg = consumer.ReceiveNoWait() as ActiveMQTextMessage; if (ExpirationOptions.IGNORE == expirationOption) { Assert.IsNotNull(rcvMsg, "Did not receive expired message."); await rcvMsg.AcknowledgeAsync(); Assert.AreEqual(msgText, rcvMsg.Text, "Message text does not match."); Assert.IsTrue(rcvMsg.IsExpired()); if (AcknowledgementMode.Transactional == ackMode) { await session.CommitAsync(); } } else { // Should not receive a message. Assert.IsNull(rcvMsg, "Received an expired message!"); } await consumer.CloseAsync(); await producer.CloseAsync(); } } finally { try { // Ensure that Session resources on the Broker release transacted Consumers. await session.CloseAsync(); // Give the Broker some time to remove the subscriptions. Thread.Sleep(2000); SessionUtil.DeleteDestination(session, destinationName); } catch { } } } } }