CreateChannel() public method

Create a RabbitMQ channel
public CreateChannel ( ) : IModel
return IModel
Example #1
0
        public void Should_NOT_close_any_connection()
        {
            // Arrange
            var retryPolicy = Substitute.For<IRetryPolicy>();
            var watcher = Substitute.For<IRabbitWatcher>();
            IConnection rmqConnection;
            var connectionFactory = CreateMockConnectionFactory<ManagedConnectionFactory>("/vHost19", out rmqConnection);

            var channel = Substitute.For<IModel>();
            rmqConnection.CreateModel().Returns(channel);

            var durableConnection = new DurableConnection(retryPolicy, watcher, connectionFactory);
            var count = ManagedConnectionFactory.SharedConnections.Count;
            durableConnection.Connect();
            durableConnection.CreateChannel();
            Assert.AreEqual(count + 1, ManagedConnectionFactory.SharedConnections.Count);


            // Action
            durableConnection.Dispose();

            //Assert
            rmqConnection.DidNotReceive().Close(Arg.Any<ushort>(), Arg.Any<string>());
            rmqConnection.DidNotReceive().Dispose();
            Assert.AreEqual(count + 1, ManagedConnectionFactory.SharedConnections.Count);
        }
        public void Should_throw_exception_if_cannot_connect()
        {
            // Arrange
            var retryPolicy = Substitute.For<IRetryPolicy>();
            var watcher = Substitute.For<IRabbitWatcher>();
            IConnection rmqConnection;
            var connectionFactory = CreateMockConnectionFactory("/", out rmqConnection);
            rmqConnection.IsOpen.Returns(false);

            var durableConnection = new DurableConnection(retryPolicy, watcher, connectionFactory);

            // Action
            durableConnection.CreateChannel();
        }
        public void Should_try_to_connection_first_if_not_connected()
        {
            // Arrange
            var retryPolicy = Substitute.For<IRetryPolicy>();
            var watcher = Substitute.For<IRabbitWatcher>();
            IConnection rmqConnection;
            var connectionFactory = CreateMockConnectionFactory("/", out rmqConnection);
            var channel = Substitute.For<IModel>();
            rmqConnection.CreateModel().Returns(channel);

            var durableConnection = new DurableConnection(retryPolicy, watcher, connectionFactory);

            // Action
            durableConnection.CreateChannel();

            //Assert
            connectionFactory.Received().CreateConnection();
        }
        public void Should_be_call_if_channel_is_shutdown()
        {
            // Arrange
            var retryPolicy = Substitute.For<IRetryPolicy>();
            var watcher = Substitute.For<IRabbitWatcher>();
            IConnection rmqConnection;
            var connectionFactory = CreateMockConnectionFactory("/", out rmqConnection);
            var channel = Substitute.For<IModel>();
            rmqConnection.CreateModel().Returns(channel);

            var durableConnection = new DurableConnection(retryPolicy, watcher, connectionFactory);
            durableConnection.Connect();
            durableConnection.CreateChannel();

            // Action
            channel.ModelShutdown += Raise.Event<ModelShutdownEventHandler>(channel, new ShutdownEventArgs(ShutdownInitiator.Application, 0, "Channel shutdown"));
            
            //Assert
            watcher.Received().WarnFormat("Channel shutdown: {0}", "Channel shutdown");
        }
        public void Should_close_and_dispose_all_SharedConnections()
        {
            var retryPolicy = Substitute.For<IRetryPolicy>();
            var watcher = Substitute.For<IRabbitWatcher>();
            IConnection rmqConnection;
            var connectionFactory = DurableConnectionTestHelper.CreateMockConnectionFactory("/", out rmqConnection);
            var channel = Substitute.For<IModel>();
            rmqConnection.CreateModel().Returns(channel);

            var durableConnection = new DurableConnection(retryPolicy, watcher, connectionFactory);
            durableConnection.CreateChannel();
            Assert.AreEqual(1, DurableConnection.SharedConnections.Count);


            // Action
            RabbitTunnel.Factory.CloseAllConnections();

            //Assert
            rmqConnection.Received().Close(Arg.Any<ushort>(), Arg.Any<string>());
            rmqConnection.Received().Dispose();
            Assert.AreEqual(0, DurableConnection.SharedConnections.Count);
        }
Example #6
0
        public void Should_catch_all_exceptions()
        {
            // Arrange
            var retryPolicy = Substitute.For<IRetryPolicy>();
            var watcher = Substitute.For<IRabbitWatcher>();
            IConnection rmqConnection;
            var connectionFactory = CreateMockConnectionFactory("/", out rmqConnection);
            var channel = Substitute.For<IModel>();
            rmqConnection.CreateModel().Returns(channel);
            rmqConnection.When(x => x.Dispose())
                         .Do(callInfo => { throw new Exception("Can't dispose :D");});
            var durableConnection = new DurableConnection(retryPolicy, watcher, connectionFactory);
            durableConnection.CreateChannel();
            Assert.AreEqual(1, DurableConnection.SharedConnections.Count);


            // Action
            durableConnection.Dispose();

            //Assert
            watcher.Received().Error(Arg.Is<Exception>(x => x.Message == "Can't dispose :D"));
        }