Ejemplo n.º 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());
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 3
0
        public async Task TestWithDMLCAndConnectionListener()
        {
            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 }
            };

            var connectionFactory = new SimpleRoutingConnectionFactory();

            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 TestDirectMessageListenerContainer(connectionFactory, connectionMakerKey2);

            container.SetQueueNames("foo");
            container.LookupKeyQualifier = "xxx";
            container.ShutdownTimeout    = 10;
            container.Initialize();
            await container.Start();

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

            await container.Stop();

            Assert.Equal("xxx[foo]", connectionMakerKey1.Value);
            Assert.Equal("xxx[foo]", connectionMakerKey2.Value);
        }
Ejemplo n.º 4
0
 public TestDirectMessageListenerContainer(SimpleRoutingConnectionFactory connectionFactory, AtomicReference <object> connectionMakerKey2)
     : base(null, connectionFactory, null, null)
 {
     this.connectionMakerKey2 = connectionMakerKey2;
     simpleFactory            = connectionFactory;
 }
Ejemplo n.º 5
0
        public async Task TestAbstractRoutingConnectionFactoryWithListenerContainer()
        {
            var connectionFactory1       = new Mock <IConnectionFactory>();
            var connectionFactory2       = new Mock <IConnectionFactory>();
            var defaultConnectionFactory = new Mock <IConnectionFactory>();
            var connection1      = new Mock <IConnection>();
            var connection2      = new Mock <IConnection>();
            var defautConnection = new Mock <IConnection>();
            var channel1         = new Mock <IModel>();
            var channel2         = new Mock <IModel>();
            var defaultChannel   = new Mock <IModel>();

            connectionFactory1.SetupSequence((f) => f.CreateConnection())
            .Returns(connection1.Object);

            connectionFactory2.SetupSequence((f) => f.CreateConnection())
            .Returns(connection1.Object)
            .Returns(connection2.Object);

            defaultConnectionFactory.SetupSequence((f) => f.CreateConnection())
            .Returns(defautConnection.Object);

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

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

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

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

            var factories = new Dictionary <object, IConnectionFactory>
            {
                { "[baz]", connectionFactory1.Object },
                { "[foo,bar]", connectionFactory2.Object }
            };

            var connectionFactory = new SimpleRoutingConnectionFactory
            {
                LenientFallback = true,
                DefaultTargetConnectionFactory = defaultConnectionFactory.Object
            };

            connectionFactory.SetTargetConnectionFactories(factories);

            var container = new DirectMessageListenerContainer(null, connectionFactory);

            container.SetQueueNames("foo", "bar");
            container.Initialize();
            await container.Start();

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

            connectionFactory1.Verify((f) => f.CreateConnection(), Times.Never);
            connectionFactory2.Verify((f) => f.CreateConnection(), Times.Exactly(2));
            defaultConnectionFactory.Verify((f) => f.CreateConnection(), Times.Once); // Checks connection

            connectionFactory1.Invocations.Clear();
            connectionFactory2.Invocations.Clear();
            defaultConnectionFactory.Invocations.Clear();

            container.SetQueueNames("baz");
            connectionFactory1.Verify((f) => f.CreateConnection());
            connectionFactory2.Verify((f) => f.CreateConnection(), Times.Never);
            defaultConnectionFactory.Verify((f) => f.CreateConnection(), Times.Never);

            connectionFactory1.Invocations.Clear();
            connectionFactory2.Invocations.Clear();
            defaultConnectionFactory.Invocations.Clear();

            container.SetQueueNames("qux");
            connectionFactory1.Verify((f) => f.CreateConnection(), Times.Never);
            connectionFactory2.Verify((f) => f.CreateConnection(), Times.Never);
            defaultConnectionFactory.Verify((f) => f.CreateConnection());

            await container.Stop();
        }