public void TestSendingMessageSetsNMSTimestamp() { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); List <Amqp.Message> receivedMessages = new List <Amqp.Message>(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(receivedMessages); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); IConnection connection = EstablishConnection(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue destination = session.GetQueue("myQueue"); IMessageProducer producer = session.CreateProducer(destination); DateTime timeStamp = DateTime.UtcNow; string text = "myMessage"; ITextMessage message = session.CreateTextMessage(text); producer.Send(message); Assert.IsTrue(receivedMessages.Any()); Assert.That(receivedMessages.First().Properties.CreationTime, Is.EqualTo(timeStamp).Within(TimeSpan.FromMilliseconds(100))); producer.Close(); session.Close(); connection.Close(); } }
public void TestSendingMessageSetsNMSExpirationRelatedAbsoluteExpiryAndTtlFields() { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); List <Amqp.Message> receivedMessages = new List <Amqp.Message>(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(receivedMessages); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); IConnection connection = EstablishConnection(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue destination = session.GetQueue("myQueue"); IMessageProducer producer = session.CreateProducer(destination); TimeSpan ttl = TimeSpan.FromMilliseconds(100_000); DateTime expiration = DateTime.UtcNow + ttl; string text = "myMessage"; ITextMessage message = session.CreateTextMessage(text); producer.Send(message, NMSConstants.defaultDeliveryMode, NMSConstants.defaultPriority, ttl); Assert.IsTrue(receivedMessages.Any()); Assert.That(receivedMessages.First().Properties.AbsoluteExpiryTime, Is.EqualTo(expiration).Within(TimeSpan.FromMilliseconds(100))); producer.Close(); session.Close(); connection.Close(); } }
public void TestRemotelyCloseProducer() { ManualResetEvent producentClosed = new ManualResetEvent(false); Mock <INmsConnectionListener> mockConnectionListener = new Mock <INmsConnectionListener>(); mockConnectionListener .Setup(listener => listener.OnProducerClosed(It.IsAny <NmsMessageProducer>(), It.IsAny <Exception>())) .Callback(() => { producentClosed.Set(); }); using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); NmsConnection connection = (NmsConnection)EstablishConnection(); connection.AddConnectionListener(mockConnectionListener.Object); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue destination = session.GetQueue("myQueue"); IMessageProducer producer = session.CreateProducer(destination); testLinkProcessor.Producer.Link.Close(); Assert.True(producentClosed.WaitOne(TimeSpan.FromMilliseconds(1000))); Assert.That(() => producer.DisableMessageID, Throws.Exception.InstanceOf <IllegalStateException>()); producer.Close(); session.Close(); connection.Close(); } }
public void TestSendingMessageSetsNMSDestination() { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); List <Amqp.Message> receivedMessages = new List <Amqp.Message>(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(receivedMessages); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); IConnection connection = EstablishConnection(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue destination = session.GetQueue("myQueue"); IMessageProducer producer = session.CreateProducer(destination); string text = "myMessage"; ITextMessage message = session.CreateTextMessage(text); Assert.IsNull(message.NMSDestination); producer.Send(message); Assert.AreEqual(destination, message.NMSDestination); Assert.IsTrue(receivedMessages.Any()); Assert.That(receivedMessages.First().Properties.To, Is.EqualTo(destination.QueueName)); producer.Close(); session.Close(); connection.Close(); } }
public void TestProducerOverridesMessageDeliveryMode() { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); IConnection connection = EstablishConnection(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue destination = session.GetQueue("myQueue"); IMessageProducer producer = session.CreateProducer(destination); string text = "myMessage"; ITextMessage textMessage = session.CreateTextMessage(text); textMessage.NMSDeliveryMode = MsgDeliveryMode.NonPersistent; producer.Send(textMessage); Assert.AreEqual(MsgDeliveryMode.Persistent, textMessage.NMSDeliveryMode); producer.Close(); session.Close(); connection.Close(); } }
public void TestRemotelyCloseConsumer() { Mock <INmsConnectionListener> mockConnectionListener = new Mock <INmsConnectionListener>(); bool exceptionFired = false; using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { TestLinkProcessor testLinkProcessor = new TestLinkProcessor(); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); testAmqpPeer.Open(); NmsConnection connection = (NmsConnection)EstablishConnection(); connection.Start(); connection.AddConnectionListener(mockConnectionListener.Object); connection.ExceptionListener += exception => { exceptionFired = true; }; ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue queue = session.GetQueue("myQueue"); // Create a consumer, then remotely end it afterwards. IMessageConsumer consumer = session.CreateConsumer(queue); testLinkProcessor.CloseConsumer(); Assert.That(() => exceptionFired, Is.False.After(200), "Exception listener shouldn't fire with no MessageListener"); // Verify the consumer gets marked closed mockConnectionListener.Verify(listener => listener.OnConsumerClosed(It.Is <IMessageConsumer>(x => x == consumer), It.IsAny <Exception>()), Times.Once, "consumer never closed."); session.Close(); connection.Close(); } }
public void TestSendWorksAfterConnectionStopped() { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); List <Amqp.Message> receivedMessages = new List <Amqp.Message>(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(receivedMessages); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); IConnection connection = EstablishConnection(); connection.Start(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue destination = session.GetQueue("myQueue"); IMessageProducer producer = session.CreateProducer(destination); string text = "myMessage"; ITextMessage message = session.CreateTextMessage(text); Assert.IsNull(message.NMSMessageId); connection.Stop(); producer.Send(message); Assert.IsTrue(receivedMessages.Any()); producer.Close(); session.Close(); connection.Close(); } }
public void TestCreateDurableConsumer() { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); NmsConnection connection = (NmsConnection)EstablishConnection(); connection.Start(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); string topicName = "myTopic"; string subscriptionName = "mySubscription"; ITopic topic = session.GetTopic(topicName); IMessageConsumer durableConsumer = session.CreateDurableConsumer(topic, subscriptionName, null, false); Assert.NotNull(durableConsumer); // Expect That Durable Subscriber Attach Assert.That(() => testLinkProcessor.Consumer, Is.Not.Null.After(200)); Assert.AreEqual(subscriptionName, testLinkProcessor.Consumer.Link.Name);; Source source = (Source)testLinkProcessor.Consumer.Attach.Source; Assert.AreEqual((uint)TerminusDurability.UNSETTLED_STATE, source.Durable); Assert.AreEqual(new Symbol("never"), source.ExpiryPolicy); Assert.AreEqual(topicName, source.Address); Assert.IsFalse(source.Dynamic); connection.Close(); } }
public void TestNonDefaultPriorityProducesMessagesWithPriorityFieldAndSetsNMSPriority() { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); List <Amqp.Message> receivedMessages = new List <Amqp.Message>(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(receivedMessages); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); IConnection connection = EstablishConnection(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue destination = session.GetQueue("myQueue"); IMessageProducer producer = session.CreateProducer(destination); string text = "myMessage"; ITextMessage message = session.CreateTextMessage(text); Assert.AreEqual(MsgPriority.BelowNormal, message.NMSPriority); producer.Send(message, MsgDeliveryMode.Persistent, MsgPriority.Highest, NMSConstants.defaultTimeToLive); Assert.AreEqual(MsgPriority.Highest, message.NMSPriority); Assert.IsTrue(receivedMessages.Any()); Assert.AreEqual(9, receivedMessages.First().Header.Priority); producer.Close(); session.Close(); connection.Close(); } }
public void TestSentTextMessageCanBeModified() { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); IConnection connection = EstablishConnection(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue destination = session.GetQueue("myQueue"); IMessageProducer producer = session.CreateProducer(destination); string text = "myMessage"; ITextMessage textMessage = session.CreateTextMessage(text); producer.Send(textMessage); Assert.AreEqual(text, textMessage.Text); textMessage.Text = text + text; Assert.AreEqual(text + text, textMessage.Text); producer.Close(); session.Close(); connection.Close(); } }
private void DoRemotelyCloseConsumerWithMessageListenerFiresNMSExceptionListenerTestImpl(bool closeWithError) { using (TestAmqpPeer testPeer = new TestAmqpPeer(Address1, User, Password)) { ManualResetEvent consumerClosed = new ManualResetEvent(false); ManualResetEvent exceptionListenerFired = new ManualResetEvent(false); TestLinkProcessor linkProcessor = new TestLinkProcessor(); testPeer.RegisterLinkProcessor(linkProcessor); testPeer.Open(); NmsConnection connection = EstablishConnection("failover.maxReconnectAttempts=1", testPeer); connection.ExceptionListener += exception => { exceptionListenerFired.Set(); }; Mock <INmsConnectionListener> connectionListener = new Mock <INmsConnectionListener>(); connectionListener .Setup(listener => listener.OnConsumerClosed(It.IsAny <IMessageConsumer>(), It.IsAny <Exception>())) .Callback(() => { consumerClosed.Set(); }); connection.AddConnectionListener(connectionListener.Object); connection.Start(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue queue = session.GetQueue("myQueue"); // Create a consumer, then remotely end it afterwards. IMessageConsumer consumer = session.CreateConsumer(queue); consumer.Listener += message => { }; if (closeWithError) { linkProcessor.CloseConsumerWithError(); } else { linkProcessor.CloseConsumer(); } Assert.True(consumerClosed.WaitOne(TimeSpan.FromMilliseconds(2000)), "Consumer closed callback didn't trigger"); Assert.True(exceptionListenerFired.WaitOne(TimeSpan.FromMilliseconds(2000)), "JMS Exception listener should have fired with a MessageListener"); // Try closing it explicitly, should effectively no-op in client. // The test peer will throw during close if it sends anything. consumer.Close(); connection.Close(); } }
public void TestCloseConsumer() { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { TestLinkProcessor testLinkProcessor = new TestLinkProcessor(); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); testAmqpPeer.Open(); IConnection connection = EstablishConnection(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue queue = session.GetQueue("myQueue"); IMessageConsumer consumer = session.CreateConsumer(queue); Assert.NotNull(testLinkProcessor.Consumer); consumer.Close(); Assert.That(() => testLinkProcessor.Consumer, Is.Null.After(500)); session.Close(); connection.Close(); } }
private void DoSendingMessageWithDisableMessageIdHintTestImpl(bool existingId) { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); List <Amqp.Message> receivedMessages = new List <Amqp.Message>(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(receivedMessages); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); IConnection connection = EstablishConnection(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue destination = session.GetQueue("myQueue"); IMessageProducer producer = session.CreateProducer(destination); string text = "myMessage"; ITextMessage message = session.CreateTextMessage(text); Assert.IsNull(message.NMSMessageId); if (existingId) { string existingMessageId = "ID:this-should-be-overwritten-in-send"; message.NMSMessageId = existingMessageId; Assert.AreEqual(existingMessageId, message.NMSMessageId); } producer.DisableMessageID = true; producer.Send(message); Assert.IsNull(message.NMSMessageId); Assert.IsTrue(receivedMessages.Any()); Assert.AreEqual(message.NMSMessageId, receivedMessages.First().Properties.MessageId); producer.Close(); session.Close(); connection.Close(); } }
public void TestStartMaxReconnectAttemptsTriggeredWhenRemotesAreRejecting() { using (TestAmqpPeer firstPeer = new TestAmqpPeer(Address1, User, Password)) using (TestAmqpPeer secondPeer = new TestAmqpPeer(Address2, User, Password)) using (TestAmqpPeer thirdPeer = new TestAmqpPeer(Address3, User, Password)) using (TestAmqpPeer fourthPeer = new TestAmqpPeer(Address4, User, Password)) { TestLinkProcessor linkProcessor = new TestLinkProcessor(); fourthPeer.RegisterLinkProcessor(linkProcessor); fourthPeer.Open(); ManualResetEvent failedConnection = new ManualResetEvent(false); NmsConnection connection = EstablishConnection( "failover.startupMaxReconnectAttempts=3&failover.reconnectDelay=15&failover.useReconnectBackOff=false", firstPeer, secondPeer, thirdPeer, fourthPeer); Mock <INmsConnectionListener> connectionListener = new Mock <INmsConnectionListener>(); connectionListener .Setup(listener => listener.OnConnectionFailure(It.IsAny <NMSException>())) .Callback(() => { failedConnection.Set(); }); connection.AddConnectionListener(connectionListener.Object); try { connection.Start(); Assert.Fail("Should not be able to connect"); } catch (Exception) { } Assert.True(failedConnection.WaitOne(TimeSpan.FromSeconds(5))); // Verify that no connection made to the last peer Assert.IsNull(linkProcessor.Consumer); } }
public void TestMessageListenerClosesItsConsumer() { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); testAmqpPeer.SendMessage("myQueue", "test"); NmsConnection connection = (NmsConnection)EstablishConnection(); connection.Start(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue destination = session.GetQueue("myQueue"); IMessageConsumer consumer = session.CreateConsumer(destination); ManualResetEvent latch = new ManualResetEvent(false); Exception exception = null; consumer.Listener += message => { try { consumer.Close(); latch.Set(); } catch (Exception e) { exception = e; } }; Assert.True(latch.WaitOne(TimeSpan.FromMilliseconds(1000))); Assert.That(() => testLinkProcessor.Consumer, Is.Null.After(1000, 50)); consumer.Close(); session.Close(); connection.Close(); } }
public void TestRemotelyCloseConsumerWithMessageListenerFiresExceptionListener() { Mock <INmsConnectionListener> mockConnectionListener = new Mock <INmsConnectionListener>(); bool exceptionFired = false; using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { var testLinkProcessor = new TestLinkProcessor(); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); testAmqpPeer.Open(); NmsConnection connection = (NmsConnection)EstablishConnection(); connection.Start(); connection.AddConnectionListener(mockConnectionListener.Object); connection.ExceptionListener += exception => { exceptionFired = true; }; ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue queue = session.GetQueue("myQueue"); // Create a consumer, then remotely end it afterwards. IMessageConsumer consumer = session.CreateConsumer(queue); consumer.Listener += message => { }; testLinkProcessor.CloseConsumerWithError(); Assert.That(() => exceptionFired, Is.True.After(200)); // Verify the consumer gets marked closed mockConnectionListener.Verify(listener => listener.OnConsumerClosed(It.Is <IMessageConsumer>(x => x == consumer), It.IsAny <Exception>()), Times.Once, "consumer never closed."); // Try closing it explicitly, should effectively no-op in client. // The test peer will throw during close if it sends anything. consumer.Close(); session.Close(); connection.Close(); } }
private void DoSendingMessageNonPersistentTestImpl(bool anonymousProducer, bool setPriority, bool setOnProducer) { using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); IConnection connection = EstablishConnection(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IQueue queue = session.GetQueue("myQueue"); IMessageProducer producer = null; if (anonymousProducer) { producer = session.CreateProducer(); } else { producer = session.CreateProducer(queue); } string text = "myMessage"; ITextMessage message = session.CreateTextMessage(text); if (setPriority) { message.NMSPriority = MsgPriority.High; } if (setOnProducer) { producer.DeliveryMode = MsgDeliveryMode.NonPersistent; if (setPriority) { producer.Priority = MsgPriority.High; } if (anonymousProducer) { producer.Send(queue, message); } else { producer.Send(message); } } else { if (anonymousProducer) { producer.Send(queue, message, MsgDeliveryMode.NonPersistent, setPriority ? MsgPriority.High : NMSConstants.defaultPriority, NMSConstants.defaultTimeToLive); } else { producer.Send(message, MsgDeliveryMode.NonPersistent, setPriority ? MsgPriority.High : NMSConstants.defaultPriority, NMSConstants.defaultTimeToLive); } } Assert.AreEqual(MsgDeliveryMode.NonPersistent, message.NMSDeliveryMode); producer.Close(); session.Close(); connection.Close(); } }
public void TestRecoverOrderingWithAsyncConsumer() { int recoverCount = 5; int messageCount = 8; using (TestAmqpPeer testAmqpPeer = new TestAmqpPeer(Address, User, Password)) { testAmqpPeer.Open(); TestLinkProcessor testLinkProcessor = new TestLinkProcessor(); testAmqpPeer.RegisterLinkProcessor(testLinkProcessor); for (int i = 0; i < messageCount; i++) { testAmqpPeer.SendMessage("myQueue", i.ToString()); } ManualResetEvent latch = new ManualResetEvent(false); IConnection connection = EstablishConnection(); connection.Start(); ISession session = connection.CreateSession(AcknowledgementMode.ClientAcknowledge); IQueue destination = session.GetQueue("myQueue"); IMessageConsumer consumer = session.CreateConsumer(destination); bool complete = false; int messageSeen = 0; int expectedIndex = 0; consumer.Listener += message => { if (complete) { return; } int actualIndex = int.Parse((message as ITextMessage).Text); Assert.AreEqual(expectedIndex, actualIndex); // don't ack the message until we receive it X times if (messageSeen < recoverCount) { session.Recover(); messageSeen++; } else { messageSeen = 0; expectedIndex++; message.Acknowledge(); if (expectedIndex == messageCount) { complete = true; latch.Set(); } } }; consumer.Close(); session.Close(); connection.Close(); } }