Пример #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>("/", 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);
        }
Пример #2
0
        public void Should_throw_exception_if_cannot_connect()
        {
            // Arrange
            var         retryPolicy = Substitute.For <IRetryPolicy>();
            var         watcher     = Substitute.For <IRabbitWatcher>();
            IConnection rmqConnection;
            var         connectionFactory = CreateMockConnectionFactory <ManagedConnectionFactory>("/", out rmqConnection);

            rmqConnection.IsOpen.Returns(false);

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

            // Action
            durableConnection.CreateChannel();
        }
Пример #3
0
        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 <ManagedConnectionFactory>("/", 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_close_and_dispose_all_SharedConnections()
        {
            var         retryPolicy = Substitute.For <IRetryPolicy>();
            var         watcher     = Substitute.For <IRabbitWatcher>();
            IConnection rmqConnection;
            var         connectionFactory = DurableConnectionTestHelper.CreateMockConnectionFactory <ManagedConnectionFactory>("/", out rmqConnection);
            var         channel           = Substitute.For <IModel>();

            rmqConnection.CreateModel().Returns(channel);

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

            durableConnection.CreateChannel();
            Assert.AreEqual(1, ManagedConnectionFactory.SharedConnections.Count);


            // Action
            RabbitTunnel.Factory.CloseAllConnections();

            //Assert
            rmqConnection.Received().Close(Arg.Any <ushort>(), Arg.Any <string>());
            rmqConnection.Received().Dispose();
            Assert.AreEqual(0, ManagedConnectionFactory.SharedConnections.Count);
        }
Пример #5
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"));
        }