public async Task TestDirect() { var cf = new CachingConnectionFactory("localhost"); var container = new DirectMessageListenerContainer(null, cf); container.SetQueueNames(Q1, Q2); container.ConsumersPerQueue = 2; var listener = new ReplyingMessageListener(); var adapter = new MessageListenerAdapter(null, listener); container.MessageListener = adapter; container.ServiceName = "simple"; container.ConsumerTagStrategy = new TestConsumerTagStrategy(testName); await container.Start(); Assert.True(container._startedLatch.Wait(TimeSpan.FromSeconds(10))); var template = new RabbitTemplate(cf); Assert.Equal("FOO", template.ConvertSendAndReceive <string>(Q1, "foo")); Assert.Equal("BAR", template.ConvertSendAndReceive <string>(Q2, "bar")); await container.Stop(); Assert.True(await ConsumersOnQueue(Q1, 0)); Assert.True(await ConsumersOnQueue(Q2, 0)); Assert.True(await ActiveConsumerCount(container, 0)); Assert.Empty(container._consumersByQueue); await template.Stop(); cf.Destroy(); }
public void MessageListenerTest() { var mockConnectionFactory = new Mock <RC.IConnectionFactory>(); var mockConnection = new Mock <RC.IConnection>(); var onlyChannel = new Mock <RC.IModel>(); onlyChannel.Setup(m => m.IsOpen).Returns(true); var tooManyModels = new Exception(); var cachingConnectionFactory = new CachingConnectionFactory(mockConnectionFactory.Object); mockConnectionFactory.Setup(m => m.CreateConnection()).Returns(mockConnection.Object); mockConnection.Setup(m => m.IsOpen).Returns(true); Func <RC.IModel> ensureOneModel = EnsureOneModel(onlyChannel.Object, tooManyModels); mockConnection.Setup(m => m.CreateModel()).Returns(onlyChannel.Object); RC.IBasicConsumer consumer; CountdownEvent consumerLatch = new CountdownEvent(1); onlyChannel.Setup(m => m.BasicConsume(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>(), null, It.IsAny <RC.IBasicConsumer>())) .Returns((string queue, bool autoAck, string consumerTag, bool noLocal, bool exclusive, IDictionary <string, object> arguments, RC.IBasicConsumer iConsumer) => { consumer = iConsumer; consumerLatch.Signal(); return("consumerTag"); }); var commitLatch = new CountdownEvent(1); onlyChannel.Setup(m => m.TxCommit()).Callback(() => { commitLatch.Signal(); }); var rollbackEvent = new CountdownEvent(1); onlyChannel.Setup(m => m.TxRollback()).Callback(() => { rollbackEvent.Signal(); }); onlyChannel.Setup(m => m.BasicAck(It.IsAny <ulong>(), It.IsAny <bool>())); var latch = new CountdownEvent(1); var container = new DirectMessageListenerContainer(null, cachingConnectionFactory); var adapter = new MessageListenerAdapter(null, new TestListener(cachingConnectionFactory, latch)); container.SetupMessageListener(adapter); container.SetQueueNames("queue"); container.ShutdownTimeout = 100; container.TransactionManager = new DummyTxManager(); container.TransactionAttribute = new DefaultTransactionAttribute(); container.Initialize(); container.Start(); // Assert.True(consumerLatch.Wait(TimeSpan.FromSeconds(10))); }
public void Init() { SimpleService.Called = false; this.messageProperties = new MessageProperties(); this.messageProperties.ContentType = MessageProperties.CONTENT_TYPE_TEXT_PLAIN; this.adapter = new MockMessageListenerAdapter(); this.adapter.MessageConverter = new SimpleMessageConverter(); }
public void WhenNoHandlerMethodIsSuppliedTheHandlerIsAssumedToBeTheMessageListenerAdapterItself() { MessageListenerAdapter adapter = new MessageListenerAdapter(); Assert.AreSame(adapter, adapter.HandlerObject); }
public void DefaultMessageHandlerMethodNameIsTheConstantDefault() { MessageListenerAdapter adapter = new MessageListenerAdapter(); Assert.AreSame(MessageListenerAdapter.ORIGINAL_DEFAULT_LISTENER_METHOD, adapter.DefaultHandlerMethod); }
public void WhenNoHandlerMethodIsSuppliedTheHandlerIsAssumedToBeTheMessageListenerAdapterItself() { MessageListenerAdapter adapter = new MessageListenerAdapter(); Assert.AreSame(adapter, adapter.HandlerObject); }
public void DefaultMessageHandlerMethodNameIsTheConstantDefault() { MessageListenerAdapter adapter = new MessageListenerAdapter(); Assert.AreSame(MessageListenerAdapter.ORIGINAL_DEFAULT_LISTENER_METHOD, adapter.DefaultHandlerMethod); }
public ListenFromAutoDeleteQueueTest() { connectionFactory = new CachingConnectionFactory("localhost") { IsPublisherReturns = true }; // Container Admin containerAdmin = new TestAdmin(connectionFactory); // Exchange var directExchange = new DirectExchange("testContainerWithAutoDeleteQueues", true, true); listenerContainer1 = new DirectMessageListenerContainer(null, connectionFactory, "container1"); listenerContainer1.ConsumersPerQueue = 2; listenerContainer1.AddQueueNames(Q1, Q2); containerAdmin.DeclareExchange(directExchange); containerAdmin.DeclareQueue(new Config.Queue(Q1, true, false, true)); containerAdmin.DeclareQueue(new Config.Queue(Q2, true, false, true)); containerAdmin.DeclareBinding(new Binding("b1", Q1, Binding.DestinationType.QUEUE, directExchange.ExchangeName, Q1, null)); containerAdmin.DeclareBinding(new Binding("b2", Q2, Binding.DestinationType.QUEUE, directExchange.ExchangeName, Q2, null)); // Listener listener = new AppendingListener(); var adapter = new MessageListenerAdapter(null, listener); listenerContainer1.MessageListener = adapter; listenerContainer1.Start(); listenerContainer1._startedLatch.Wait(TimeSpan.FromSeconds(10)); // Conditional declarations var otherExchange = new DirectExchange(Exch2, true, true); containerAdmin.DeclareExchange(otherExchange); containerAdmin.DeclareQueue(new Config.Queue(Q3, true, false, true)); containerAdmin.DeclareBinding(new Binding("b3", Q3, Binding.DestinationType.QUEUE, otherExchange.ExchangeName, Q3, null)); listenerContainer2 = new DirectMessageListenerContainer(null, connectionFactory, "container2"); listenerContainer2.IsAutoStartup = false; listenerContainer2.ShutdownTimeout = 50; listenerContainer2.AddQueueNames(Q3); listenerContainer2.MessageListener = adapter; expiringQueue = new Config.Queue(Guid.NewGuid().ToString(), true, false, false, new Dictionary <string, object>() { { "x-expires", 200 } }); containerAdmin.DeclareQueue(expiringQueue); listenerContainer3 = new DirectMessageListenerContainer(null, connectionFactory, "container3"); listenerContainer3.IsAutoStartup = false; listenerContainer3.ShutdownTimeout = 50; listenerContainer3.AddQueueNames(expiringQueue.QueueName); listenerContainer3.MessageListener = adapter; listenerContainer4 = new DirectMessageListenerContainer(null, connectionFactory, "container4"); listenerContainer4.IsAutoStartup = false; listenerContainer4.ShutdownTimeout = 50; listenerContainer4.AddQueueNames(Q2); listenerContainer4.MessageListener = adapter; listenerContainer4.AutoDeclare = false; }