Connect() public method

Try to connect to rabbitmq server, retry if it cann't connect to the broker.
public Connect ( ) : void
return void
Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public void Should_catch_BrokerUnreachableException()
        {
            // Arrange
            var retryPolicy = Substitute.For<IRetryPolicy>();

            var connectionFactory = CreateMockConnectionFactory<ManagedConnectionFactory>("/");
            connectionFactory.When(x => x.CreateConnection())
                             .Do(callInfo => { throw new BrokerUnreachableException(Substitute.For<IDictionary<AmqpTcpEndpoint, int>>(), Substitute.For<IDictionary<AmqpTcpEndpoint, Exception>>(), Substitute.For<Exception>()); });
            var durableConnection = new DurableConnection(retryPolicy, Substitute.For<IRabbitWatcher>(), connectionFactory);
            // Action
            durableConnection.Connect();

            // Assert
            retryPolicy.Received(1).WaitForNextRetry(Arg.Any<Action>());
        }
Exemplo n.º 3
0
        public void Should_catch_ConnectFailureException()
        {
            // Arrange
            var retryPolicy = Substitute.For<IRetryPolicy>();

            var connectionFactory = CreateMockConnectionFactory<ManagedConnectionFactory>("/");
            connectionFactory.When(x => x.CreateConnection())
                             .Do(callInfo => { throw new ConnectFailureException("Network error", Substitute.For<Exception>()); });
            var durableConnection = new DurableConnection(retryPolicy, Substitute.For<IRabbitWatcher>(), connectionFactory);
            // Action
            durableConnection.Connect();

            // Assert
            retryPolicy.Received(1).WaitForNextRetry(Arg.Any<Action>());
        }
Exemplo n.º 4
0
        public void Should_fire_connected_event_when_connect_successfully()
        {
            // Arrange
            var are = new AutoResetEvent(false);
            var connectionFactory = CreateMockConnectionFactory<ManagedConnectionFactory>("/");
            var durableConnection = new DurableConnection(Substitute.For<IRetryPolicy>(),
                                                          Substitute.For<IRabbitWatcher>(),
                                                          connectionFactory);
            durableConnection.Connected += () => are.Set();

            // Action
            durableConnection.Connect();
            are.WaitOne();

            // Assert
            connectionFactory.Received(1).CreateConnection();
            Assert.AreEqual(1, ManagedConnectionFactory.SharedConnections.Count);
            Assert.IsTrue(connectionFactory.Endpoint.Equals(ManagedConnectionFactory.SharedConnections.First().Value.Endpoint));
        }
        public void Should_be_call_if_App_is_closed_by_user()
        {
            // Arrange
            var retryPolicy = Substitute.For<IRetryPolicy>();
            var watcher = Substitute.For<IRabbitWatcher>();
            IConnection rmqConnection;
            var connectionFactory = CreateMockConnectionFactory("/", out rmqConnection);
            
            
            var durableConnection = new DurableConnection(retryPolicy, watcher, connectionFactory);
            durableConnection.Disconnected += () => { };
            durableConnection.Connect();

            // Action
            rmqConnection.ConnectionShutdown += Raise.Event<ConnectionShutdownEventHandler>(rmqConnection, new ShutdownEventArgs(ShutdownInitiator.Application, 0, "Connection disposed by application"));
            
            //Assert
            retryPolicy.DidNotReceive().WaitForNextRetry(Arg.Any<Action>());
        }
        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_clear_existing_connection_from_shared_connection_list_then_retry_if_connection_is_dropped_by_peer()
        {
            // Arrange
            var retryPolicy = Substitute.For<IRetryPolicy>();
            var watcher = Substitute.For<IRabbitWatcher>();
            IConnection rmqConnection;
            var connectionFactory = CreateMockConnectionFactory("/", out rmqConnection);


            var durableConnection = new DurableConnection(retryPolicy, watcher, connectionFactory);
            durableConnection.Disconnected += () => { };
            durableConnection.Connect();
            Assert.AreEqual(1, DurableConnection.SharedConnections.Count);

            // Action
            rmqConnection.ConnectionShutdown += Raise.Event<ConnectionShutdownEventHandler>(rmqConnection, new ShutdownEventArgs(ShutdownInitiator.Application, 0, "Connection dropped for unknow reason ;)"));

            //Assert
            Assert.AreEqual(0, DurableConnection.SharedConnections.Count);
            retryPolicy.Received().WaitForNextRetry(Arg.Any<Action>());
        }
        public void Should_try_reconnect_by_retryPolicy_if_Connection_Shutdown_event_was_fired()
        {
            // Arrange
            var retryPolicy = Substitute.For<IRetryPolicy>();
            var watcher = Substitute.For<IRabbitWatcher>();
            IConnection rmqConnection;
            var connectionFactory = CreateMockConnectionFactory<ManagedConnectionFactory>("/vHost21", out rmqConnection);


            var durableConnection = new DurableConnection(retryPolicy, watcher, connectionFactory);
            durableConnection.Disconnected += () => { };
            durableConnection.Connect();
            Assert.AreEqual(1, ManagedConnectionFactory.SharedConnections.Count);

            // Action
            rmqConnection.ConnectionShutdown += Raise.Event<ConnectionShutdownEventHandler>(rmqConnection, new ShutdownEventArgs(ShutdownInitiator.Application, 0, "Connection dropped for unknow reason ;)"));

            //Assert
            Assert.AreEqual(0, ManagedConnectionFactory.SharedConnections.Count);
            retryPolicy.Received().WaitForNextRetry(Arg.Any<Action>());
        }
Exemplo n.º 9
0
        public void Should_only_be_notified_about_a_new_established_connection_that_has_the_same_endpoint_and_virtual_host()
        {
            // Arrange
            var connection1Connected = false;
            var connection2Connected = false;
            var connectionFactory1 = CreateMockConnectionFactory<ManagedConnectionFactory>("/vHost10");
            var durableConnection1 = new DurableConnection(Substitute.For<IRetryPolicy>(),
                                                          Substitute.For<IRabbitWatcher>(),
                                                          connectionFactory1);
            durableConnection1.Connected += () => connection1Connected = true;

            var connectionFactory2 = CreateMockConnectionFactory<ManagedConnectionFactory>("/vHost11");
            var durableConnection2 = new DurableConnection(Substitute.For<IRetryPolicy>(),
                                                          Substitute.For<IRabbitWatcher>(),
                                                          connectionFactory2);
            durableConnection2.Connected += () => connection2Connected = true;

            // Action
            durableConnection1.Connect();


            // Assert
            connectionFactory1.Received(1).CreateConnection();
            Assert.IsTrue(connection1Connected);
            Assert.IsFalse(connection2Connected);
        }
Exemplo n.º 10
0
        public void Should_be_notified_about_a_new_established_connection()
        {
            // Arrange
            var connection2Connected = false;
            var connectionFactory = CreateMockConnectionFactory<ManagedConnectionFactory>("/vHost9");
            var durableConnection1 = new DurableConnection(Substitute.For<IRetryPolicy>(),
                                                          Substitute.For<IRabbitWatcher>(),
                                                          connectionFactory);

            var durableConnection2 = new DurableConnection(Substitute.For<IRetryPolicy>(),
                                                          Substitute.For<IRabbitWatcher>(),
                                                          connectionFactory);


            durableConnection2.Connected += () => connection2Connected = true;

            // Action
            durableConnection1.Connect();


            // Assert
            connectionFactory.Received(1).CreateConnection();
            Assert.IsTrue(connection2Connected);
        }