public void TestAutoAcknowledgeMessagesAsync() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { int msgCount = 6; IConnection connection = EstablishConnection(testPeer); connection.Start(); testPeer.ExpectBegin(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue queue = session.GetQueue("myQueue"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithNullContent(), count: msgCount); IMessageConsumer consumer = session.CreateConsumer(queue); for (int i = 0; i < msgCount; i++) { testPeer.ExpectDispositionThatIsAcceptedAndSettled(); } consumer.Listener += (message) => { }; testPeer.WaitForAllMatchersToComplete(3000); testPeer.ExpectClose(); connection.Close(); testPeer.WaitForAllMatchersToComplete(3000); } }
public void TestReceiveMessageWithReceiveZeroTimeout() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = EstablishConnection(testPeer); connection.Start(); testPeer.ExpectBegin(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue queue = session.GetQueue("myQueue"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: new Amqp.Message() { BodySection = new AmqpValue() { Value = null } }, count: 1); testPeer.ExpectDispositionThatIsAcceptedAndSettled(); IMessageConsumer consumer = session.CreateConsumer(queue); IMessage message = consumer.Receive(); Assert.NotNull(message, "A message should have been received"); testPeer.ExpectClose(); connection.Close(); testPeer.WaitForAllMatchersToComplete(10000); } }
public void TestCloseDurableSubscriberWithUnackedAndUnconsumedPrefetchedMessages() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = EstablishConnection(testPeer); connection.Start(); testPeer.ExpectBegin(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); string topicName = "myTopic"; string subscriptionName = "mySubscription"; ITopic topic = session.GetTopic(topicName); int messageCount = 5; // Create a consumer and fill the prefetch with some messages, // which we will consume some of but ack none of. testPeer.ExpectDurableSubscriberAttach(topicName, subscriptionName); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithContent(), count: messageCount); IMessageConsumer durableConsumer = session.CreateDurableConsumer(topic, subscriptionName, null, false); int consumeCount = 2; IMessage receivedMessage = null; for (int i = 1; i <= consumeCount; i++) { receivedMessage = durableConsumer.Receive(); Assert.NotNull(receivedMessage); Assert.IsInstanceOf <NmsTextMessage>(receivedMessage); } // Expect the messages that were not delivered to be released. for (int i = 1; i <= consumeCount; i++) { testPeer.ExpectDispositionThatIsAcceptedAndSettled(); } receivedMessage.Acknowledge(); testPeer.ExpectDetach(expectClosed: false, sendResponse: true, replyClosed: false); for (int i = consumeCount + 1; i <= messageCount; i++) { testPeer.ExpectDispositionThatIsReleasedAndSettled(); } testPeer.ExpectEnd(); durableConsumer.Close(); session.Close(); testPeer.ExpectClose(); connection.Close(); testPeer.WaitForAllMatchersToComplete(3000); } }
public async Task TestMessageListenerClosesItsConsumer() { var latch = new ManualResetEvent(false); var exceptionListenerFired = new ManualResetEvent(false); using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); connection.ExceptionListener += _ => exceptionListenerFired.Set(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); IQueue destination = await session.GetQueueAsync("myQueue"); await connection.StartAsync(); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithContent(), 1); IMessageConsumer consumer = await session.CreateConsumerAsync(destination); testPeer.ExpectLinkFlow(drain: true, sendDrainFlowResponse: true, creditMatcher: credit => Assert.AreEqual(99, credit)); // Not sure if expected credit is right testPeer.ExpectDispositionThatIsAcceptedAndSettled(); testPeer.ExpectDetach(expectClosed: true, sendResponse: true, replyClosed: true); Exception exception = null; consumer.Listener += message => { try { consumer.Close(); latch.Set(); } catch (Exception e) { exception = e; } }; Assert.True(latch.WaitOne(TimeSpan.FromMilliseconds(1000)), "Process not completed within given timeout"); Assert.IsNull(exception, "No error expected during close"); testPeer.WaitForAllMatchersToComplete(2000); testPeer.ExpectClose(); await connection.CloseAsync(); Assert.False(exceptionListenerFired.WaitOne(20), "Exception listener shouldn't have fired"); testPeer.WaitForAllMatchersToComplete(2000); } }
public async Task TestMessageListenerCallsSessionCloseThrowsIllegalStateException() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); IQueue destination = await session.GetQueueAsync("myQueue"); await connection.StartAsync(); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithContent(), 1); IMessageConsumer consumer = await session.CreateConsumerAsync(destination); testPeer.ExpectDispositionThatIsAcceptedAndSettled(); ManualResetEvent latch = new ManualResetEvent(false); Exception exception = null; consumer.Listener += message => { try { session.Close(); } catch (Exception e) { exception = e; } latch.Set(); }; Assert.True(latch.WaitOne(3000), "Messages not received within given timeout."); Assert.IsNotNull(exception); Assert.IsInstanceOf <IllegalStateException>(exception); testPeer.WaitForAllMatchersToComplete(2000); testPeer.ExpectDetach(expectClosed: true, sendResponse: true, replyClosed: true); await consumer.CloseAsync(); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(2000); } }
public void TestMessageListenerCallsSessionCloseThrowsIllegalStateException() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { var context = EstablishNMSContext(testPeer); context.Start(); testPeer.ExpectBegin(); IQueue destination = context.GetQueue("myQueue"); context.Start(); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithContent(), 1); var consumer = context.CreateConsumer(destination); testPeer.ExpectDispositionThatIsAcceptedAndSettled(); ManualResetEvent latch = new ManualResetEvent(false); Exception exception = null; consumer.Listener += message => { try { context.Close(); } catch (Exception e) { exception = e; } latch.Set(); }; Assert.True(latch.WaitOne(3000), "Messages not received within given timeout."); Assert.IsNotNull(exception); Assert.IsInstanceOf <IllegalStateException>(exception); testPeer.WaitForAllMatchersToComplete(2000); testPeer.ExpectDetach(expectClosed: true, sendResponse: true, replyClosed: true); consumer.Close(); testPeer.ExpectEnd(); // testPeer.ExpectClose(); context.Close(); testPeer.WaitForAllMatchersToComplete(2000); } }
public async Task TestClientAcknowledgeMessagesAsync() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { int msgCount = 3; IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.ClientAcknowledge); IQueue queue = await session.GetQueueAsync("myQueue"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithNullContent(), count: msgCount); IMessageConsumer consumer = await session.CreateConsumerAsync(queue); CountdownEvent latch = new CountdownEvent(3); IMessage lastReceivedMessage = null; consumer.Listener += message => { lastReceivedMessage = message; latch.Signal(); }; Assert.True(latch.Wait(2000)); for (int i = 0; i < msgCount; i++) { testPeer.ExpectDispositionThatIsAcceptedAndSettled(); } await lastReceivedMessage.AcknowledgeAsync(); testPeer.WaitForAllMatchersToComplete(2000); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(2000); } }
public async Task TestSessionCloseWaitsForAsyncDeliveryToComplete() { ManualResetEvent latch = new ManualResetEvent(false); using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); IQueue destination = await session.GetQueueAsync("myQueue"); await connection.StartAsync(); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithContent(), count: 1); IMessageConsumer consumer = await session.CreateConsumerAsync(destination); testPeer.ExpectDispositionThatIsAcceptedAndSettled(); consumer.Listener += _ => { latch.Set(); Task.Delay(TimeSpan.FromMilliseconds(100)).GetAwaiter().GetResult(); }; Assert.True(latch.WaitOne(TimeSpan.FromMilliseconds(3000)), "Messages not received within given timeout."); testPeer.ExpectEnd(); await session.CloseAsync(); testPeer.WaitForAllMatchersToComplete(2000); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(2000); } }
public async Task TestSetMessageListenerAfterStartAndSend() { int messageCount = 4; CountdownEvent latch = new CountdownEvent(messageCount); using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); IQueue destination = await session.GetQueueAsync("myQueue"); await connection.StartAsync(); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithContent(), messageCount); IMessageConsumer consumer = await session.CreateConsumerAsync(destination); for (int i = 0; i < messageCount; i++) { testPeer.ExpectDispositionThatIsAcceptedAndSettled(); } consumer.Listener += message => latch.Signal(); Assert.True(latch.Wait(4000), "Messages not received within given timeout. Count remaining: " + latch.CurrentCount); testPeer.WaitForAllMatchersToComplete(2000); testPeer.ExpectDetach(expectClosed: true, sendResponse: true, replyClosed: true); await consumer.CloseAsync(); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(2000); } }
public async Task TestCreateProducerInOnMessage() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); IQueue destination = await session.GetQueueAsync("myQueue"); IQueue outbound = await session.GetQueueAsync("ForwardDest"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithContent(), 1); testPeer.ExpectSenderAttach(); testPeer.ExpectTransfer(messageMatcher: Assert.NotNull); testPeer.ExpectDetach(expectClosed: true, sendResponse: true, replyClosed: true); testPeer.ExpectDispositionThatIsAcceptedAndSettled(); IMessageConsumer consumer = await session.CreateConsumerAsync(destination); consumer.Listener += message => { IMessageProducer producer = session.CreateProducer(outbound); producer.Send(message); producer.Close(); }; testPeer.WaitForAllMatchersToComplete(10_000); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(2000); } }
public void TestSetMessageListenerAfterStartAndSend() { int messageCount = 4; CountdownEvent latch = new CountdownEvent(messageCount); using (TestAmqpPeer testPeer = new TestAmqpPeer()) { var context = EstablishNMSContext(testPeer); context.Start(); testPeer.ExpectBegin(); IQueue destination = context.GetQueue("myQueue"); context.Start(); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithContent(), messageCount); var consumer = context.CreateConsumer(destination); for (int i = 0; i < messageCount; i++) { testPeer.ExpectDispositionThatIsAcceptedAndSettled(); } consumer.Listener += message => latch.Signal(); Assert.True(latch.Wait(4000), "Messages not received within given timeout. Count remaining: " + latch.CurrentCount); testPeer.WaitForAllMatchersToComplete(2000); testPeer.ExpectDetach(expectClosed: true, sendResponse: true, replyClosed: true); consumer.Close(); testPeer.ExpectEnd(); testPeer.ExpectClose(); context.Close(); testPeer.WaitForAllMatchersToComplete(2000); } }
public void TestConsumerCloseWaitsForAsyncDeliveryToComplete() { ManualResetEvent latch = new ManualResetEvent(false); using (TestAmqpPeer testPeer = new TestAmqpPeer()) { var context = EstablishNMSContext(testPeer); context.Start(); testPeer.ExpectBegin(); IQueue destination = context.GetQueue("myQueue"); context.Start(); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithContent(), count: 1); var consumer = context.CreateConsumer(destination); testPeer.ExpectDispositionThatIsAcceptedAndSettled(); consumer.Listener += _ => { latch.Set(); Task.Delay(TimeSpan.FromMilliseconds(100)).GetAwaiter().GetResult(); }; Assert.True(latch.WaitOne(TimeSpan.FromMilliseconds(3000)), "Messages not received within given timeout."); testPeer.ExpectDetach(expectClosed: true, sendResponse: true, replyClosed: true); consumer.Close(); testPeer.WaitForAllMatchersToComplete(2000); testPeer.ExpectEnd(); testPeer.ExpectClose(); context.Close(); testPeer.WaitForAllMatchersToComplete(2000); } }
public void TestClientAcknowledgeMessages() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { int msgCount = 3; IConnection connection = EstablishConnection(testPeer); connection.Start(); testPeer.ExpectBegin(); ISession session = connection.CreateSession(AcknowledgementMode.ClientAcknowledge); IQueue queue = session.GetQueue("myQueue"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithNullContent(), count: msgCount); IMessageConsumer consumer = session.CreateConsumer(queue); IMessage lastReceivedMessage = null; for (int i = 0; i < msgCount; i++) { lastReceivedMessage = consumer.Receive(); Assert.NotNull(lastReceivedMessage, "Message " + i + " was not received"); } for (int i = 0; i < msgCount; i++) { testPeer.ExpectDispositionThatIsAcceptedAndSettled(); } lastReceivedMessage.Acknowledge(); testPeer.WaitForAllMatchersToComplete(2000); testPeer.ExpectClose(); connection.Close(); testPeer.WaitForAllMatchersToComplete(2000); } }
public void TestCreateProducerInOnMessage() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { var context = EstablishNMSContext(testPeer); context.Start(); testPeer.ExpectBegin(); IQueue destination = context.GetQueue("myQueue"); IQueue outbound = context.GetQueue("ForwardDest"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithContent(), 1); testPeer.ExpectSenderAttach(); testPeer.ExpectTransfer(messageMatcher: Assert.NotNull); testPeer.ExpectDetach(expectClosed: true, sendResponse: true, replyClosed: true); testPeer.ExpectDispositionThatIsAcceptedAndSettled(); var consumer = context.CreateConsumer(destination); consumer.Listener += message => { var producer = context.CreateProducer(); producer.Send(outbound, message); producer.Close(); }; testPeer.WaitForAllMatchersToComplete(10_000); testPeer.ExpectEnd(); testPeer.ExpectClose(); context.Close(); testPeer.WaitForAllMatchersToComplete(2000); } }
public async Task TestAutoAcknowledgeMessages() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { int msgCount = 6; IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); IQueue queue = await session.GetQueueAsync("myQueue"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithNullContent(), count: msgCount); IMessageConsumer consumer = await session.CreateConsumerAsync(queue); for (int i = 0; i < msgCount; i++) { testPeer.ExpectDispositionThatIsAcceptedAndSettled(); } for (int i = 0; i < msgCount; i++) { Assert.NotNull(await consumer.ReceiveAsync(TimeSpan.FromMilliseconds(3000)), $"Message {i} not received within given timeout."); } testPeer.WaitForAllMatchersToComplete(3000); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(3000); } }
public async Task TestReceiveMessageWithReceiveZeroTimeout() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { var context = await EstablishNMSContextAsync(testPeer); await context.StartAsync(); testPeer.ExpectBegin(); IQueue queue = await context.GetQueueAsync("myQueue"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: new Amqp.Message() { BodySection = new AmqpValue() { Value = null } }, count: 1); testPeer.ExpectDispositionThatIsAcceptedAndSettled(); var consumer = await context.CreateConsumerAsync(queue); IMessage message = await consumer.ReceiveAsync(); Assert.NotNull(message, "A message should have been received"); testPeer.ExpectEnd(); testPeer.ExpectClose(); await context.CloseAsync(); testPeer.WaitForAllMatchersToComplete(10000); } }
public void TestConsumerCanReceivesMessagesWhenConnectionLostDuringAutoAck() { using (TestAmqpPeer originalPeer = new TestAmqpPeer()) using (TestAmqpPeer finalPeer = new TestAmqpPeer()) { ManualResetEvent consumerReady = new ManualResetEvent(false); ManualResetEvent originalConnected = new ManualResetEvent(false); ManualResetEvent finalConnected = new ManualResetEvent(false); // Connect to the first peer originalPeer.ExpectSaslAnonymous(); originalPeer.ExpectOpen(); originalPeer.ExpectBegin(); originalPeer.ExpectBegin(); NmsConnection connection = EstablishAnonymousConnection(originalPeer, finalPeer); Mock <INmsConnectionListener> connectionListener = new Mock <INmsConnectionListener>(); connectionListener .Setup(listener => listener.OnConnectionEstablished(It.IsAny <Uri>())) .Callback(() => { originalConnected.Set(); }); connectionListener .Setup(listener => listener.OnConnectionRestored(It.IsAny <Uri>())) .Callback(() => { finalConnected.Set(); }); connection.AddConnectionListener(connectionListener.Object); connection.Start(); Assert.True(originalConnected.WaitOne(TimeSpan.FromSeconds(5)), "Should connect to original peer"); originalPeer.ExpectReceiverAttach(); originalPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithContent(), 1); originalPeer.RunAfterLastHandler(() => consumerReady.WaitOne(TimeSpan.FromSeconds(2))); originalPeer.DropAfterLastMatcher(); // Post Failover Expectations of FinalPeer finalPeer.ExpectSaslAnonymous(); finalPeer.ExpectOpen(); finalPeer.ExpectBegin(); finalPeer.ExpectBegin(); finalPeer.ExpectReceiverAttach(); finalPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithContent(), 1); finalPeer.ExpectDispositionThatIsAcceptedAndSettled(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue queue = session.GetQueue("myQueue"); IMessageConsumer messageConsumer = session.CreateConsumer(queue); CountdownEvent msgReceivedLatch = new CountdownEvent(2); messageConsumer.Listener += message => { if (msgReceivedLatch.CurrentCount == 2) { consumerReady.Set(); finalConnected.WaitOne(2000); } msgReceivedLatch.Signal(); }; finalPeer.WaitForAllMatchersToComplete(5000); Assert.IsTrue(msgReceivedLatch.Wait(TimeSpan.FromSeconds(10)), $"Expected 2 messages, but got {2 - msgReceivedLatch.CurrentCount}"); } }