public async Task TestIncomingExpiredMessageGetsFiltered() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); IQueue queue = await session.GetQueueAsync("myQueue"); // Expected the consumer to attach and send credit, then send it an // already-expired message followed by a live message. testPeer.ExpectReceiverAttach(); string expiredMsgContent = "already-expired"; Amqp.Message message = CreateExpiredMessage(expiredMsgContent); testPeer.ExpectLinkFlowRespondWithTransfer(message: message); string liveMsgContent = "valid"; testPeer.SendTransferToLastOpenedLinkOnLastOpenedSession(message: new Amqp.Message() { BodySection = new AmqpValue() { Value = liveMsgContent } }, nextIncomingId: 2); IMessageConsumer consumer = await session.CreateConsumerAsync(queue); // Call receive, expect the first message to be filtered due to expiry, // and the second message to be given to the test app and accepted. Action <DeliveryState> modifiedMatcher = state => { var modified = state as Modified; Assert.IsNotNull(modified); Assert.IsTrue(modified.DeliveryFailed); Assert.IsTrue(modified.UndeliverableHere); }; testPeer.ExpectDisposition(settled: true, stateMatcher: modifiedMatcher, firstDeliveryId: 1, lastDeliveryId: 1); testPeer.ExpectDisposition(settled: true, stateMatcher: Assert.IsInstanceOf <Accepted>, firstDeliveryId: 2, lastDeliveryId: 2); IMessage m = await consumer.ReceiveAsync(TimeSpan.FromMilliseconds(3000)); Assert.NotNull(m, "Message should have been received"); Assert.IsInstanceOf <ITextMessage>(m); Assert.AreEqual(liveMsgContent, (m as ITextMessage).Text, "Unexpected message content"); // Verify the other message is not there. Will drain to be sure there are no messages. Assert.IsNull(await consumer.ReceiveAsync(TimeSpan.FromMilliseconds(10)), "Message should not have been received"); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(3000); } }
public async Task TestIncomingExpiredMessageGetsConsumedWhenFilterDisabled() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = await EstablishConnectionAsync(testPeer, "?nms.localMessageExpiry=false"); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); IQueue queue = await session.GetQueueAsync("myQueue"); // Expected the consumer to attach and send credit, then send it an // already-expired message followed by a live message. testPeer.ExpectReceiverAttach(); string expiredMsgContent = "already-expired"; Amqp.Message message = CreateExpiredMessage(expiredMsgContent); testPeer.ExpectLinkFlowRespondWithTransfer(message: message); string liveMsgContent = "valid"; testPeer.SendTransferToLastOpenedLinkOnLastOpenedSession(message: new Amqp.Message() { BodySection = new AmqpValue() { Value = liveMsgContent } }, nextIncomingId: 2); IMessageConsumer consumer = await session.CreateConsumerAsync(queue); // Call receive, expect the expired message as we disabled local expiry. testPeer.ExpectDisposition(settled: true, stateMatcher: Assert.IsInstanceOf <Accepted>, firstDeliveryId: 1, lastDeliveryId: 1); IMessage m = await consumer.ReceiveAsync(TimeSpan.FromMilliseconds(3000)); Assert.NotNull(m, "Message should have been received"); Assert.IsInstanceOf <ITextMessage>(m); Assert.AreEqual(expiredMsgContent, ((ITextMessage)m).Text, "Unexpected message content"); // Verify the other message is there testPeer.ExpectDisposition(settled: true, stateMatcher: Assert.IsInstanceOf <Accepted>, firstDeliveryId: 2, lastDeliveryId: 2); m = await consumer.ReceiveAsync(TimeSpan.FromMilliseconds(3000)); Assert.NotNull(m, "Message should have been received"); Assert.IsInstanceOf <ITextMessage>(m); Assert.AreEqual(liveMsgContent, ((ITextMessage)m).Text, "Unexpected message content"); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(3000); } }
public async Task TestIncomingExpiredMessageGetsConsumedWhenFilterDisabledAsync() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = await EstablishConnectionAsync(testPeer, "?nms.localMessageExpiry=false"); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); IQueue queue = await session.GetQueueAsync("myQueue"); // Expected the consumer to attach and send credit, then send it an // already-expired message followed by a live message. testPeer.ExpectReceiverAttach(); string expiredMsgContent = "already-expired"; Amqp.Message message = CreateExpiredMessage(expiredMsgContent); testPeer.ExpectLinkFlowRespondWithTransfer(message: message); string liveMsgContent = "valid"; testPeer.SendTransferToLastOpenedLinkOnLastOpenedSession(message: new Amqp.Message() { BodySection = new AmqpValue() { Value = liveMsgContent } }, nextIncomingId: 2); IMessageConsumer consumer = await session.CreateConsumerAsync(queue); // Add message listener, expect both messages as the filter is disabled testPeer.ExpectDisposition(settled: true, stateMatcher: Assert.IsInstanceOf <Accepted>, firstDeliveryId: 1, lastDeliveryId: 1); testPeer.ExpectDisposition(settled: true, stateMatcher: Assert.IsInstanceOf <Accepted>, firstDeliveryId: 2, lastDeliveryId: 2); CountdownEvent success = new CountdownEvent(2); consumer.Listener += m => { if (expiredMsgContent.Equals(((ITextMessage)m).Text) || liveMsgContent.Equals(((ITextMessage)m).Text)) { success.Signal(); } }; Assert.IsTrue(success.Wait(TimeSpan.FromSeconds(5)), "Didn't get expected messages"); testPeer.WaitForAllMatchersToComplete(3000); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(3000); } }
public void TestAcknowledgeIndividualMessagesAsync() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { int msgCount = 6; IConnection connection = EstablishConnection(testPeer); connection.Start(); testPeer.ExpectBegin(); ISession session = connection.CreateSession(AcknowledgementMode.IndividualAcknowledge); IQueue queue = session.GetQueue("myQueue"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer( message: CreateMessageWithNullContent(), count: msgCount, drain: false, nextIncomingId: 1, addMessageNumberProperty: true, sendDrainFlowResponse: false, sendSettled: false, creditMatcher: credit => Assert.Greater(credit, msgCount)); IMessageConsumer consumer = session.CreateConsumer(queue); CountdownEvent latch = new CountdownEvent(msgCount); List <ITextMessage> messages = new List <ITextMessage>(); consumer.Listener += message => { messages.Add((ITextMessage)message); latch.Signal(); }; Assert.True(latch.Wait(TimeSpan.FromMilliseconds(1000)), $"Should receive: {msgCount}, but received: {messages.Count}"); Action <DeliveryState> dispositionMatcher = state => { Assert.AreEqual(state.Descriptor.Code, MessageSupport.ACCEPTED_INSTANCE.Descriptor.Code); }; // Acknowledge the messages in a random order and verify the individual dispositions have expected delivery state. Random random = new Random(); for (int i = 0; i < msgCount; i++) { var message = messages[random.Next(msgCount - i)]; messages.Remove(message); uint deliveryNumber = (uint)message.Properties.GetInt(TestAmqpPeer.MESSAGE_NUMBER) + 1; testPeer.ExpectDisposition(settled: true, stateMatcher: dispositionMatcher, firstDeliveryId: deliveryNumber, lastDeliveryId: deliveryNumber); message.Acknowledge(); testPeer.WaitForAllMatchersToComplete(3000); } testPeer.ExpectClose(); connection.Close(); testPeer.WaitForAllMatchersToComplete(3000); } }
public void TestReceiveBody <T>(T inputValue) { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { var context = EstablishNMSContext(testPeer); testPeer.ExpectBegin(); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(CreateMessageWithValueContent(inputValue)); testPeer.ExpectDisposition(true, _ => { }); IQueue destination = context.GetQueue("myQueue"); var consumer = context.CreateConsumer(destination); T body = consumer.ReceiveBody <T>(); Assert.AreEqual(inputValue, body); Assert.AreNotSame(inputValue, body); testPeer.ExpectEnd(); testPeer.ExpectClose(); context.Close(); testPeer.WaitForAllMatchersToComplete(2000); } }
private async Task DoReceiveMessageDeliveryTime(object setDeliveryTimeAnnotation, DateTime?expectedDeliveryTime) { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { var connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); var session = await connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge); var queue = await session.GetQueueAsync("myQueue"); var message = CreateMessageWithNullContent(); if (setDeliveryTimeAnnotation != null) { message.MessageAnnotations = message.MessageAnnotations ?? new MessageAnnotations(); message.MessageAnnotations[SymbolUtil.NMS_DELIVERY_TIME] = setDeliveryTimeAnnotation; } testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message); testPeer.ExpectDisposition(true, (deliveryState) => { }); DateTime startingTimeFrom = DateTime.UtcNow; var messageConsumer = await session.CreateConsumerAsync(queue); var receivedMessage = await messageConsumer.ReceiveAsync(TimeSpan.FromMilliseconds(3000)); DateTime receivingTime = DateTime.UtcNow; testPeer.WaitForAllMatchersToComplete(3000); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(3000); Assert.IsNotNull(receivedMessage); if (expectedDeliveryTime != null) { Assert.AreEqual(receivedMessage.NMSDeliveryTime, expectedDeliveryTime.Value); } else { Assert.LessOrEqual(receivedMessage.NMSDeliveryTime, receivingTime); Assert.GreaterOrEqual(receivedMessage.NMSDeliveryTime, startingTimeFrom); } } }
public void TestReceiveBodyNoWait <T>(T inputValue) { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { var context = EstablishNMSContext(testPeer); ManualResetEvent beforeFlow = new ManualResetEvent(false); testPeer.ExpectBegin(); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(CreateMessageWithValueContent(inputValue), 1, false, false, false, false, (credit) => { beforeFlow.WaitOne(); }, 1); testPeer.ExpectDisposition(true, _ => { }); IQueue destination = context.GetQueue("myQueue"); var consumer = context.CreateConsumer(destination); T initialBody = consumer.ReceiveBodyNoWait <T>(); // Assert initially its null Assert.AreEqual(default(T), initialBody); // Release and allow link to flow beforeFlow.Set(); // Give short time to arrive Thread.Sleep(100); T body = consumer.ReceiveBodyNoWait <T>(); Assert.AreEqual(inputValue, body); Assert.AreNotSame(inputValue, body); testPeer.ExpectEnd(); testPeer.ExpectClose(); context.Close(); testPeer.WaitForAllMatchersToComplete(2000); } }
public void TestIncomingExpiredMessageGetsFilteredAsync() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = EstablishConnection(testPeer); connection.Start(); testPeer.ExpectBegin(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue queue = session.GetQueue("myQueue"); // Expected the consumer to attach and send credit, then send it an // already-expired message followed by a live message. testPeer.ExpectReceiverAttach(); string expiredMsgContent = "already-expired"; Amqp.Message message = CreateExpiredMessage(expiredMsgContent); testPeer.ExpectLinkFlowRespondWithTransfer(message: message); string liveMsgContent = "valid"; testPeer.SendTransferToLastOpenedLinkOnLastOpenedSession(message: new Amqp.Message() { BodySection = new AmqpValue() { Value = liveMsgContent } }, nextIncomingId: 2); IMessageConsumer consumer = session.CreateConsumer(queue); // Add message listener, expect the first message to be filtered due to expiry, // and the second message to be given to the test app and accepted. Action <DeliveryState> modifiedMatcher = state => { var modified = state as Modified; Assert.IsNotNull(modified); Assert.IsTrue(modified.DeliveryFailed); Assert.IsTrue(modified.UndeliverableHere); }; testPeer.ExpectDisposition(settled: true, stateMatcher: modifiedMatcher, firstDeliveryId: 1, lastDeliveryId: 1); testPeer.ExpectDisposition(settled: true, stateMatcher: Assert.IsInstanceOf <Accepted>, firstDeliveryId: 2, lastDeliveryId: 2); ManualResetEvent success = new ManualResetEvent(false); ManualResetEvent listenerFailure = new ManualResetEvent(false); consumer.Listener += m => { if (liveMsgContent.Equals(((ITextMessage)m).Text)) { success.Set(); } else { listenerFailure.Set(); } }; Assert.True(success.WaitOne(TimeSpan.FromSeconds(5)), "didn't get expected message"); Assert.False(listenerFailure.WaitOne(TimeSpan.FromMilliseconds(100)), "Received message when message should not have been received"); testPeer.WaitForAllMatchersToComplete(3000); testPeer.ExpectClose(); connection.Close(); testPeer.WaitForAllMatchersToComplete(3000); } }
public void TestRecoverOrderingWithAsyncConsumer() { ManualResetEvent latch = new ManualResetEvent(false); Exception asyncError = null; int recoverCount = 5; int messageCount = 8; int testPayloadLength = 255; string payload = Encoding.UTF8.GetString(new byte[testPayloadLength]); using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = EstablishConnection(testPeer); connection.Start(); testPeer.ExpectBegin(); ISession session = connection.CreateSession(AcknowledgementMode.ClientAcknowledge); IQueue destination = session.GetQueue("myQueue"); connection.Start(); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer( message: new Amqp.Message() { BodySection = new AmqpValue() { Value = payload } }, count: messageCount, drain: false, nextIncomingId: 1, addMessageNumberProperty: true, sendDrainFlowResponse: false, sendSettled: false, creditMatcher: credit => Assert.Greater(credit, messageCount) ); IMessageConsumer consumer = session.CreateConsumer(destination); bool complete = false; int messageSeen = 0; int expectedIndex = 0; consumer.Listener += message => { if (complete) { return; } try { int actualIndex = message.Properties.GetInt(TestAmqpPeer.MESSAGE_NUMBER); Assert.AreEqual(expectedIndex, actualIndex, "Received Message Out Of Order"); // don't ack the message until we receive it X times if (messageSeen < recoverCount) { session.Recover(); messageSeen++; } else { messageSeen = 0; expectedIndex++; // Have the peer expect the accept the disposition (1-based, hence pre-incremented). testPeer.ExpectDisposition(settled: true, stateMatcher: state => Assert.AreEqual(state.Descriptor.Code, MessageSupport.ACCEPTED_INSTANCE.Descriptor.Code )); message.Acknowledge(); if (expectedIndex == messageCount) { complete = true; latch.Set(); } } } catch (Exception e) { complete = true; asyncError = e; latch.Set(); } }; Assert.True(latch.WaitOne(TimeSpan.FromSeconds(15)), "Messages not received within given timeout."); Assert.IsNull(asyncError, "Unexpected exception"); testPeer.WaitForAllMatchersToComplete(2000); testPeer.ExpectClose(); connection.Close(); testPeer.WaitForAllMatchersToComplete(2000); } }
public async Task TestAcknowledgeIndividualMessages() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { int msgCount = 6; IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.IndividualAcknowledge); IQueue queue = await session.GetQueueAsync("myQueue"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer( message: CreateMessageWithNullContent(), count: msgCount, drain: false, nextIncomingId: 1, addMessageNumberProperty: true, sendDrainFlowResponse: false, sendSettled: false, creditMatcher: credit => Assert.Greater(credit, msgCount)); IMessageConsumer consumer = await session.CreateConsumerAsync(queue); var messages = new List <IMessage>(); for (int i = 0; i < msgCount; i++) { IMessage message = await consumer.ReceiveAsync(TimeSpan.FromMilliseconds(3000)); Assert.NotNull(message, "Message " + i + " was not received"); messages.Add(message); Assert.AreEqual(i, message.Properties.GetInt(TestAmqpPeer.MESSAGE_NUMBER), "unexpected message number property"); } Action <DeliveryState> dispositionMatcher = state => { Assert.AreEqual(state.Descriptor.Code, MessageSupport.ACCEPTED_INSTANCE.Descriptor.Code); }; // Acknowledge the messages in a random order and verify the individual dispositions have expected delivery state. Random random = new Random(); for (int i = 0; i < msgCount; i++) { var message = messages[random.Next(msgCount - i)]; messages.Remove(message); uint deliveryNumber = (uint)message.Properties.GetInt(TestAmqpPeer.MESSAGE_NUMBER) + 1; testPeer.ExpectDisposition(settled: true, stateMatcher: dispositionMatcher, firstDeliveryId: deliveryNumber, lastDeliveryId: deliveryNumber); await message.AcknowledgeAsync(); testPeer.WaitForAllMatchersToComplete(3000); } testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(3000); } }