示例#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_unsubscribe_to_ConnectionEstablished_event()
        {
            var connectionFactory = new ConnectionFactory
            {
                HostName = "localhost",
                UserName = "******",
                Password = "******"
            };
            var connection = new DurableConnection(Substitute.For <IRetryPolicy>(), Substitute.For <IRabbitWatcher>(), connectionFactory);

            var fired = false;

            connection.Connected += () => { fired = true; };
            ManagedConnectionFactory.ConnectionEstablished += (a, b) => { }; //NOTE: To make it not null
            connection.Dispose();

            var model = Substitute.For <IModel>();

            model.IsOpen.Returns(true);
            var c = Substitute.For <IConnection>();

            c.CreateModel().Returns(model);
            c.IsOpen.Returns(true);
            c.Endpoint.Returns(connectionFactory.Endpoint);

            ManagedConnectionFactory.SharedConnections[connectionFactory.Endpoint + connectionFactory.VirtualHost] = c;
            ManagedConnectionFactory.ConnectionEstablished.Invoke(new AmqpTcpEndpoint("localhost"), "/");

            Assert.IsFalse(fired);
        }
示例#3
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"));
        }
示例#4
0
 // Asynchronous. Events may fire even after Dispose() returns.
 public void Dispose()
 {
     _connection.Dispose();
 }