public void Should_be_notified_about_a_new_established_connection()
        {
            // Arrange
            var haConnectionEstablished = false;
            var retryPolicty            = Substitute.For <IRetryPolicy>();
            var haConnection            = new HaConnection(retryPolicty,
                                                           Substitute.For <IRabbitWatcher>(),
                                                           new List <ManagedConnectionFactory> {
                new ManagedConnectionFactory()
            });

            haConnection.ConnectionFactories.ClearAll();
            var f1 = CreateManagedConnectionFactory(5671);
            var f2 = CreateManagedConnectionFactory(5672);
            var f3 = CreateManagedConnectionFactory(5673);

            haConnection.ConnectionFactories.Add(f1);
            haConnection.ConnectionFactories.Add(f2);
            haConnection.ConnectionFactories.Add(f3);

            var durableConnection = new DurableConnection(Substitute.For <IRetryPolicy>(),
                                                          Substitute.For <IRabbitWatcher>(),
                                                          CreateManagedConnectionFactory(5671));

            haConnection.Connected += () => haConnectionEstablished = true;


            // Action
            durableConnection.Connect();


            // Assert
            Assert.IsTrue(haConnectionEstablished);
        }
Exemple #2
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);
        }
Exemple #3
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);
        }
Exemple #4
0
        public void Should_catch_BrokerUnreachableException()
        {
            // Arrange
            var retryPolicy = Substitute.For <IRetryPolicy>();

            var connectionFactory = CreateMockConnectionFactory <ManagedConnectionFactory>("/vHost3");

            connectionFactory.When(x => x.CreateConnection())
            .Do(callInfo => { throw new BrokerUnreachableException(Substitute.For <Exception>()); });
            var durableConnection = new DurableConnection(retryPolicy, Substitute.For <IRabbitWatcher>(), connectionFactory);

            // Action
            durableConnection.Connect();

            // Assert
            retryPolicy.Received(1).WaitForNextRetry(Arg.Any <Action>());
        }
Exemple #5
0
        static void RawConnection()
        {
            var connector = new ExchangeApi.WebSocket.Connector(ExchangeApi.Coinbase.Instance.Prod.WebSocket);

            using (var connection = new DurableConnection <ArraySegment <byte>, ArraySegment <byte> >(connector, new Scheduler()))
            {
                connection.OnConnection += (IReader <ArraySegment <byte> > reader, IWriter <ArraySegment <byte> > writer) =>
                {
                    writer.Send(Encode("{ \"type\": \"subscribe\", \"product_id\": \"BTC-USD\" }"));
                };
                connection.OnMessage += (TimestampedMsg <ArraySegment <byte> > msg) =>
                {
                    _log.Info("OnMessage(IsLast={0}): {1} byte(s)", !connection.Scheduler.HasReady(), msg.Value.Count);
                };
                connection.Connect();
                Thread.Sleep(5000);
            }
            Thread.Sleep(2000);
        }
Exemple #6
0
        public void Should_fire_connected_event_when_connect_successfully()
        {
            // Arrange
            var are = new AutoResetEvent(false);
            var connectionFactory = CreateMockConnectionFactory <ManagedConnectionFactory>("/vHost1");
            var durableConnection = new DurableConnection(Substitute.For <IRetryPolicy>(),
                                                          Substitute.For <IRabbitWatcher>(),
                                                          connectionFactory);

            durableConnection.Connected += () => are.Set();

            // Action
            durableConnection.Connect();
            Assert.IsTrue(are.WaitOne(1000));

            // 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_called_if_App_is_closed_by_user()
        {
            // Arrange
            var         retryPolicy = Substitute.For <IRetryPolicy>();
            var         watcher     = Substitute.For <IRabbitWatcher>();
            IConnection rmqConnection;
            var         connectionFactory = CreateMockConnectionFactory <ManagedConnectionFactory>("/", 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_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>("/", 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>());
        }
        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>());
        }
Exemple #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);
        }
Exemple #11
0
 // See comments in DurableSubscriber.
 public void Connect()
 {
     _connection.Connect();
 }