Пример #1
0
        public void TestSimpleRoutingConnectionFactory()
        {
            var connectionFactory1 = new Mock <IConnectionFactory>();
            var connectionFactory2 = new Mock <IConnectionFactory>();
            var factories          = new Dictionary <object, IConnectionFactory>
            {
                { "foo", connectionFactory1.Object },
                { "bar", connectionFactory2.Object }
            };

            var connectionFactory = new SimpleRoutingConnectionFactory();

            connectionFactory.SetTargetConnectionFactories(factories);
            var tasks = new List <Task>();

            for (var i = 0; i < 3; i++)
            {
                var count = i;
                var task  = Task.Run(() =>
                {
                    SimpleResourceHolder.Bind(connectionFactory, count % 2 == 0 ? "foo" : "bar");
                    connectionFactory.CreateConnection();
                    SimpleResourceHolder.Unbind(connectionFactory);
                });
                tasks.Add(task);
            }

            Assert.True(Task.WaitAll(tasks.ToArray(), 10000));
            connectionFactory1.Verify((f) => f.CreateConnection(), Times.Exactly(2));
            connectionFactory2.Verify((f) => f.CreateConnection());
        }
Пример #2
0
        public async Task TestWithDRTDMLCAndConnectionListenerExistingRFK()
        {
            var connectionFactory1 = new Mock <IConnectionFactory>();
            var connection1        = new Mock <IConnection>();
            var channel1           = new Mock <IModel>();

            var factories = new Dictionary <object, IConnectionFactory>
            {
                { "xxx[foo]", connectionFactory1.Object },
                { "xxx[amq.rabbitmq.reply-to]", connectionFactory1.Object }
            };

            var connectionFactory = new SimpleRoutingConnectionFactory();

            SimpleResourceHolder.Bind(connectionFactory, "foo");

            connection1.Setup((c) => c.IsOpen).Returns(true);
            connection1.Setup(c => c.CreateChannel(It.IsAny <bool>())).Returns(channel1.Object);

            channel1.Setup(c => c.IsOpen).Returns(true);

            var connectionMakerKey1 = new AtomicReference <object>();
            var latch = new CountdownEvent(1);

            connectionFactory1.Setup((f) => f.CreateConnection())
            .Returns(connection1.Object)
            .Callback(() =>
            {
                connectionMakerKey1.Value = connectionFactory.DetermineCurrentLookupKey();
                latch.Signal();
            });
            connectionFactory.SetTargetConnectionFactories(factories);
            var connectionMakerKey2 = new AtomicReference <object>();

            var container = new TestDirectReplyToMessageListenerContainer(connectionFactory, connectionMakerKey2)
            {
                LookupKeyQualifier = "xxx",
                ShutdownTimeout    = 10
            };

            container.Initialize();
            await container.Start();

            Assert.True(container._startedLatch.Wait(TimeSpan.FromSeconds(10))); // Container started

            var channelHolder = container.GetChannelHolder();

            Assert.True(latch.Wait(TimeSpan.FromSeconds(10)));
            container.ReleaseConsumerFor(channelHolder, true, "test");
            await container.Stop();

            Assert.Equal("xxx[amq.rabbitmq.reply-to]", connectionMakerKey1.Value);
            Assert.Equal("xxx[amq.rabbitmq.reply-to]", connectionMakerKey2.Value);
            Assert.Equal("foo", SimpleResourceHolder.Unbind(connectionFactory));
        }