Exemple #1
0
        public void Should_be_able_to_subscribe_with_provided_options()
        {
            // Arrange
            var newChannel  = Substitute.For <IModel>();
            var routeFinder = Substitute.For <IRouteFinder>();

            routeFinder.FindQueueName <Customer>("subscriptionName").Returns("QueueByCustomRouteFinder");
            IDurableConnection durableConnection;
            var tunnel = RabbitTunnelForTest.CreateTunnel(newChannel, out durableConnection);

            // Action
            tunnel.Subscribe(new SubscriptionOption <Customer>
            {
                SubscriptionName  = "subscriptionName",
                MessageHandler    = x => { },
                QueuePrefetchSize = 100,
                BatchSize         = 100,
                RouteFinder       = routeFinder
            });

            // Assert
            routeFinder.Received(1).FindQueueName <Customer>("subscriptionName");
            newChannel.Received().BasicQos(0, 100, false);
            newChannel.Received().BasicConsume("QueueByCustomRouteFinder", false, Arg.Is <string>(x => x.StartsWith("subscriptionName-")), Arg.Any <IBasicConsumer>());
        }
Exemple #2
0
        public static RabbitTunnel CreateTunnel(IModel channel, out IDurableConnection durableConnection, bool isChannelOpen = true)
        {
            if (channel != null)
            {
                channel.IsOpen.Returns(isChannelOpen);
            }

            var routeFinder = Substitute.For <IRouteFinder>();

            routeFinder.FindQueueName <Customer>(null).ReturnsForAnyArgs("Queue");
            durableConnection = Substitute.For <IDurableConnection>();
            //durableConnection.ConnectionFactory.Returns(Substitute.For<ConnectionFactory>());
            var conn = durableConnection;

            durableConnection.When(x => x.Connect()).Do(callInfo => // Because this is a mock objectd
            {
                //Rase connected event
                conn.Connected += Raise.Event <Action>();
                conn.IsConnected.Returns(true);
            });
            durableConnection.CreateChannel().Returns(channel);

            var tunnel = new RabbitTunnelForTest(routeFinder, durableConnection);

            tunnel.OnOpened += () => { };
            tunnel.OnClosed += () => { };
            return(tunnel);
        }
        public void Should_raise_event()
        {
            // Arrange
            var isCalled   = false;
            var newChannel = Substitute.For <IModel>();

            newChannel.IsOpen.Returns(true);
            var routeFinder = Substitute.For <IRouteFinder>();

            routeFinder.When(x => x.FindExchangeName <string>()).Do(callInfo => { throw new Exception("Test message"); });
            var durableConnection = Substitute.For <IDurableConnection>();

            //durableConnection.ConnectionFactory.Returns(Substitute.For<ConnectionFactory>());
            durableConnection.When(x => x.Connect()).Do(callInfo =>
            {
                durableConnection.Connected += Raise.Event <Action>();
                durableConnection.IsConnected.Returns(true);
            });

            durableConnection.CreateChannel().Returns(newChannel);
            var tunnel = new RabbitTunnelForTest(routeFinder, durableConnection);

            tunnel.ConsumerDisconnected += s => { isCalled = true; };

            // Action
            tunnel.PublicRaiseConsumerDisconnectedEvent(new Subscription(newChannel));
            Assert.IsTrue(isCalled);
        }
        public static RabbitTunnel CreateTunnel(IModel channel, out IDurableConnection durableConnection, bool isChannelOpen = true)
        {
            if (channel != null)
            {
                channel.IsOpen.Returns(isChannelOpen);
            }

            var routeFinder = Substitute.For<IRouteFinder>();
            routeFinder.FindQueueName<Customer>(null).ReturnsForAnyArgs("Queue");
            durableConnection = Substitute.For<IDurableConnection>();
            //durableConnection.ConnectionFactory.Returns(Substitute.For<ConnectionFactory>());
            var conn = durableConnection;
            durableConnection.When(x => x.Connect()).Do(callInfo => // Because this is a mock objectd
            {
                //Rase connected event
                conn.Connected += Raise.Event<Action>();
                conn.IsConnected.Returns(true);
            });
            durableConnection.CreateChannel().Returns(channel);

            var tunnel = new RabbitTunnelForTest(routeFinder, durableConnection);
            tunnel.OnOpened += () => { };
            tunnel.OnClosed += () => { };
            return tunnel;
        }
        public void Should_log_exception_if_cannot_reconnect()
        {
            // Arrange
            var waitHandler = new AutoResetEvent(false);
            var id = Guid.NewGuid();
            var channel = Substitute.For<IModel>();
            channel.IsOpen.Returns(true);
            var routeFinder = Substitute.For<IRouteFinder>();
            var durableConnection = Substitute.For<IDurableConnection>();
            durableConnection.ConnectionFactory.Returns(Substitute.For<ConnectionFactory>());
            durableConnection.When(x => x.Connect()).Do(callInfo =>
            {
                durableConnection.Connected += Raise.Event<Action>();
                durableConnection.IsConnected.Returns(true);
            });

            var watcher = Substitute.For<IRabbitWatcher>();

            durableConnection.CreateChannel().Returns(channel);
            var tunnel = new RabbitTunnelForTest(Substitute.For<IConsumerManager>(), watcher, routeFinder, durableConnection, Substitute.For<ISerializer>(), Substitute.For<ICorrelationIdGenerator>(), true);
            Action action = () => { waitHandler.Set(); throw new Exception("Cannot reconnect"); };
            tunnel.SubscribeActions[id] = action;
            tunnel.CreatedChannels.Add(channel);
            

            // Action
            tunnel.ExecuteSubscription(id);
            waitHandler.WaitOne(5000);

            // Assert
            watcher.Received(1).Error(Arg.Is<Exception>(e => e.Message == "Cannot reconnect"));
            
        }
        public void Should_reconnect_if_user_ack_wrong_delivery_id()
        {
            // Arrange
            var waitHandler = new AutoResetEvent(false);
            var id          = Guid.NewGuid();
            var channel     = Substitute.For <IModel>();

            channel.IsOpen.Returns(true);
            var routeFinder       = Substitute.For <IRouteFinder>();
            var durableConnection = Substitute.For <IDurableConnection>();

            //durableConnection.ConnectionFactory.Returns(Substitute.For<ConnectionFactory>());
            durableConnection.When(x => x.Connect()).Do(callInfo =>
            {
                durableConnection.Connected += Raise.Event <Action>();
                durableConnection.IsConnected.Returns(true);
            });

            durableConnection.CreateChannel().Returns(channel);
            var    tunnel = new RabbitTunnelForTest(routeFinder, durableConnection);
            Action action = () => waitHandler.Set();

            tunnel.SubscribeActions[id] = action;
            tunnel.CreatedChannels.Add(channel);


            // Action
            tunnel.PublicTryReconnect(channel, id, new ShutdownEventArgs(ShutdownInitiator.Peer, 406, "PRECONDITION_FAILED - unknown delivery tag 10000"));
            waitHandler.WaitOne(5000);

            // Assert
            Assert.IsTrue(tunnel.CreatedChannels.Count == 0);
        }
        public void Should_reconnect_if_user_ack_wrong_delivery_id()
        {
            // Arrange
            var waitHandler = new AutoResetEvent(false);
            var id = Guid.NewGuid();
            var channel = Substitute.For<IModel>();
            channel.IsOpen.Returns(true);
            var routeFinder = Substitute.For<IRouteFinder>();
            var durableConnection = Substitute.For<IDurableConnection>();
            durableConnection.ConnectionFactory.Returns(Substitute.For<ConnectionFactory>());
            durableConnection.When(x => x.Connect()).Do(callInfo =>
            {
                durableConnection.Connected += Raise.Event<Action>();
                durableConnection.IsConnected.Returns(true);
            });

            durableConnection.CreateChannel().Returns(channel);
            var tunnel = new RabbitTunnelForTest(routeFinder, durableConnection);
            Action action = () => waitHandler.Set();
            tunnel.SubscribeActions[id] = action;
            tunnel.CreatedChannels.Add(channel);
            

            // Action
            tunnel.PublicTryReconnect(channel, id, new ShutdownEventArgs(ShutdownInitiator.Peer, 406, "PRECONDITION_FAILED - unknown delivery tag 10000"));
            waitHandler.WaitOne(5000);

            // Assert
            Assert.IsTrue(tunnel.CreatedChannels.Count == 0);
            
        }
Exemple #8
0
        public void Should_log_exception_if_cannot_reconnect()
        {
            // Arrange
            var waitHandler = new AutoResetEvent(false);
            var id          = Guid.NewGuid();
            var channel     = Substitute.For <IModel>();

            channel.IsOpen.Returns(true);
            var routeFinder       = Substitute.For <IRouteFinder>();
            var durableConnection = Substitute.For <IDurableConnection>();

            durableConnection.ConnectionFactory.Returns(Substitute.For <ConnectionFactory>());
            durableConnection.When(x => x.Connect()).Do(callInfo =>
            {
                durableConnection.Connected += Raise.Event <Action>();
                durableConnection.IsConnected.Returns(true);
            });

            var watcher = Substitute.For <IRabbitWatcher>();

            durableConnection.CreateChannel().Returns(channel);
            var    tunnel = new RabbitTunnelForTest(Substitute.For <IConsumerManager>(), watcher, routeFinder, durableConnection, Substitute.For <ISerializer>(), Substitute.For <ICorrelationIdGenerator>(), true);
            Action action = () => { waitHandler.Set(); throw new Exception("Cannot reconnect"); };

            tunnel.SubscribeActions[id] = action;
            tunnel.CreatedChannels.Add(channel);


            // Action
            tunnel.ExecuteSubscription(id);
            waitHandler.WaitOne(5000);

            // Assert
            watcher.Received(1).Error(Arg.Is <Exception>(e => e.Message == "Cannot reconnect"));
        }
Exemple #9
0
        public void Should_throw_exception_if_dedicated_publish_channel_is_not_connected()
        {
            // Arrange
            IDurableConnection durableConnection;
            var tunnel = RabbitTunnelForTest.CreateTunnel(Substitute.For <IModel>(), out durableConnection, false);

            // Action
            tunnel.Publish("Muahaha");
        }
Exemple #10
0
        public void Should_throw_exception_if_dedicated_publish_channel_is_not_created_properly()
        {
            // Arrange
            IDurableConnection durableConnection;
            var tunnel = RabbitTunnelForTest.CreateTunnel(null, out durableConnection);

            // Action
            tunnel.Publish("Muahaha");
        }
Exemple #11
0
        public void Should_not_throw_any_other_exceptions()
        {
            // Arrange
            IDurableConnection durableConnection;
            var tunnel = (RabbitTunnelForTest)RabbitTunnelForTest.CreateTunnel(null, out durableConnection);

            // Action
            tunnel.TrySubscribeForTest(() => { throw new Exception("Test Exception"); });
        }
Exemple #12
0
        public void Should_not_throw_OperationInterruptedException()
        {
            // Arrange
            IDurableConnection durableConnection;
            var tunnel = (RabbitTunnelForTest)RabbitTunnelForTest.CreateTunnel(null, out durableConnection);

            // Action
            tunnel.TrySubscribeForTest(() => { throw new OperationInterruptedException(new ShutdownEventArgs(ShutdownInitiator.Peer, 3, "")); });
        }
Exemple #13
0
        public void Should_create_subscriptions_to_queues()
        {
            // Arrange
            var newChannel = Substitute.For <IModel>();
            IDurableConnection durableConnection;
            var tunnel = RabbitTunnelForTest.CreateTunnel(newChannel, out durableConnection);

            // Action
            tunnel.Subscribe <Customer>("subscriptionName", x => { });

            // Assert
            newChannel.Received().BasicQos(0, (ushort)Global.PreFetchSize, false);
            newChannel.Received().BasicConsume("Queue", false, Arg.Is <string>(x => x.StartsWith("subscriptionName-")), Arg.Any <IBasicConsumer>());
        }
Exemple #14
0
        public void Should_return_subscription()
        {
            // Arrange
            var newChannel = Substitute.For <IModel>();
            IDurableConnection durableConnection;
            var tunnel = RabbitTunnelForTest.CreateTunnel(newChannel, out durableConnection);

            // Action
            var subs = tunnel.Subscribe <Customer>("subscriptionName", (x, y) => { });

            // Assert
            newChannel.Received().BasicQos(0, (ushort)Global.PreFetchSize, false);
            newChannel.Received().BasicConsume("Queue", false, Arg.Is <string>(x => x.StartsWith("subscriptionName-")), Arg.Any <IBasicConsumer>());
            Assert.IsInstanceOfType(typeof(Subscription), subs);
        }
Exemple #15
0
        public void Should_resubscribe_to_queues_after_reconnect_successfully()
        {
            // Arrange
            var newChannel = Substitute.For <IModel>();
            IDurableConnection durableConnection;
            var tunnel = RabbitTunnelForTest.CreateTunnel(newChannel, out durableConnection);

            tunnel.Subscribe <Customer>("subscriptionName", x => { });
            tunnel.Subscribe <Customer>("subscriptionName2", x => { });

            // Action
            durableConnection.Disconnected += Raise.Event <Action>();
            durableConnection.Connected    += Raise.Event <Action>();

            // Assert
            newChannel.Received(4).BasicConsume("Queue", false, Arg.Is <string>(x => x.StartsWith("subscriptionName")), Arg.Any <IBasicConsumer>());
        }
Exemple #16
0
        public void Should_notify_all_registered_observers()
        {
            var newChannel = Substitute.For <IModel>();
            IDurableConnection durableConnection;
            var tunnel = RabbitTunnelForTest.CreateTunnel(newChannel, out durableConnection);

            var observer   = Substitute.For <IObserver <ISerializer> >();
            var serializer = Substitute.For <ISerializer>();

            tunnel.AddSerializerObserver(observer);

            // Action
            tunnel.SetSerializer(serializer);

            // Assert
            observer.Received(1).OnNext(serializer);
        }
Exemple #17
0
        public void Should_do_nothing_if_disposed()
        {
            // Arrange
            var newChannel = Substitute.For <IModel>();
            IDurableConnection durableConnection;
            var tunnel = RabbitTunnelForTest.CreateTunnel(newChannel, out durableConnection);

            tunnel.Subscribe <Customer>("subscriptionName", x => { });
            tunnel.Dispose();

            // Action
            durableConnection.Disconnected += Raise.Event <Action>();

            // Assert
            newChannel.Received(2).Abort();   //1 for publishing channel, 1 for the above subscription
            newChannel.Received(2).Dispose(); //1 for publishing channel, 1 for the above subscription
        }
Exemple #18
0
        public void Should_close_dedicatedPublishChannel()
        {
            // Arrange
            var newChannel = Substitute.For <IModel>();
            IDurableConnection durableConnection;
            var tunnel = (RabbitTunnelForTest)RabbitTunnelForTest.CreateTunnel(newChannel, out durableConnection);

            tunnel.Publish(new Customer());

            // Action
            durableConnection.Disconnected += Raise.Event <Action>();
            newChannel.BasicAcks           += Raise.EventWith <BasicAckEventArgs>();
            newChannel.BasicNacks          += Raise.EventWith <BasicNackEventArgs>();
            newChannel.BasicReturn         += Raise.EventWith <BasicReturnEventArgs>();

            // Assert
            Assert.IsNull(tunnel.OnBrokerReceivedMessageIsCall);
            Assert.IsNull(tunnel.OnBrokerRejectedMessageIsCall);
            Assert.IsNull(tunnel.OnMessageIsUnroutedIsCall);
        }
Exemple #19
0
        public void Should_not_throw_any_other_Exception()
        {
            // Arrange
            var countDownEvent = new CountdownEvent(2);
            var newChannel     = Substitute.For <IModel>();

            newChannel.When(x => x.Abort()).Do(callInfo =>
            {
                countDownEvent.Signal();
                throw new Exception();
            });
            IDurableConnection durableConnection;
            var tunnel = RabbitTunnelForTest.CreateTunnel(newChannel, out durableConnection);

            tunnel.Subscribe <Customer>("subscriptionName", x => { });

            // Action
            tunnel.Dispose();
            Assert.IsTrue(countDownEvent.Wait(1000));

            // Assert
            durableConnection.Received(1).Dispose();
        }
Exemple #20
0
        public void Should_dispose_everything()
        {
            // Arrange
            var countDownEvent = new CountdownEvent(4);
            var newChannel     = Substitute.For <IModel>();

            newChannel.When(x => x.Dispose()).Do(callInfo => countDownEvent.Signal());
            newChannel.When(x => x.Abort()).Do(callInfo => countDownEvent.Signal());

            IDurableConnection durableConnection;
            var tunnel = RabbitTunnelForTest.CreateTunnel(newChannel, out durableConnection);

            tunnel.Subscribe <Customer>("subscriptionName", x => { });

            // Action
            tunnel.Dispose();
            Assert.IsTrue(countDownEvent.Wait(1000));

            // Assert
            // One for the dedicated publish channel and the other for the above subcribe channel
            newChannel.Received(2).Dispose();
            newChannel.Received(2).Abort();
            durableConnection.Received(1).Dispose();
        }
        public void Should_raise_event()
        {
            // Arrange
            var isCalled = false;
            var newChannel = Substitute.For<IModel>();
            newChannel.IsOpen.Returns(true);
            var routeFinder = Substitute.For<IRouteFinder>();
            routeFinder.When(x => x.FindExchangeName<string>()).Do(callInfo => { throw new Exception("Test message"); });
            var durableConnection = Substitute.For<IDurableConnection>();
            //durableConnection.ConnectionFactory.Returns(Substitute.For<ConnectionFactory>());
            durableConnection.When(x => x.Connect()).Do(callInfo =>
            {
                durableConnection.Connected += Raise.Event<Action>();
                durableConnection.IsConnected.Returns(true);
            });

            durableConnection.CreateChannel().Returns(newChannel);
            var tunnel = new RabbitTunnelForTest(routeFinder, durableConnection);
            tunnel.ConsumerDisconnected += s => { isCalled = true; };

            // Action
            tunnel.PublicRaiseConsumerDisconnectedEvent(new Subscription(newChannel));
            Assert.IsTrue(isCalled);
        }