Example #1
0
        public void TryConnect_MoreThanThreeExceptionsToHandle_ConnectionUnsuccessful()
        {
            var connectionFactoryMock = new Mock <IConnectionFactory>();
            var connectionMock        = new Mock <IConnection>();

            connectionFactoryMock
            .Setup(c => c.CreateConnection())
            .Callback(() =>
            {
                throw new BrokerUnreachableException(new Exception());
            });

            var connection = new SharpRabbit.RabbitConnection(
                _logger,
                connectionFactoryMock.Object);

            Assert.False(connection.IsConnected);
        }
Example #2
0
        public void OnConnectionBlocked_NoMatterTheReason_TryReconnect()
        {
            var connectionFactoryMock = new Mock <IConnectionFactory>();
            var connectionMock        = new Mock <IConnection>();

            connectionFactoryMock
            .Setup(c => c.CreateConnection())
            .Returns(connectionMock.Object);

            var connection = new SharpRabbit.RabbitConnection(
                _logger,
                connectionFactoryMock.Object);

            var blockReason = It.IsAny <string>();

            connection.OnConnectionBlocked(new object(), new RabbitMQ.Client.Events.ConnectionBlockedEventArgs(blockReason));

            connectionFactoryMock.Verify(m => m.CreateConnection(), Times.Once);
        }
Example #3
0
        public void TryConnect_NoExceptionsToHandle_CallCreatesConnectionOnce()
        {
            var connectionFactoryMock = new Mock <IConnectionFactory>();
            var connectionMock        = new Mock <IConnection>();

            connectionFactoryMock
            .Setup(c => c.CreateConnection())
            .Returns(connectionMock.Object);

            var connection = new SharpRabbit.RabbitConnection(
                _logger,
                connectionFactoryMock.Object);

            var blockReason = It.IsAny <string>();

            connection.TryConnect();

            connectionFactoryMock.Verify(m => m.CreateConnection(), Times.Once);
        }
Example #4
0
        public void DisposeCalled_CallsConnectionDispose()
        {
            var connectionFactoryMock = new Mock <IConnectionFactory>();
            var connectionMock        = new Mock <IConnection>();

            connectionFactoryMock
            .Setup(c => c.CreateConnection())
            .Returns(connectionMock.Object);

            var rabbitConnection = new SharpRabbit.RabbitConnection(
                _logger,
                connectionFactoryMock.Object);

            rabbitConnection.TryConnect();

            rabbitConnection.Dispose();

            connectionMock.Verify(c => c.Dispose(), Times.Once);
        }
Example #5
0
        public void OnConnectionShutdown_InitiatorIsPeer_Reconnect()
        {
            var connectionFactoryMock = new Mock <IConnectionFactory>();
            var connectionMock        = new Mock <IConnection>();

            connectionFactoryMock
            .Setup(c => c.CreateConnection())
            .Returns(connectionMock.Object);

            var connection = new SharpRabbit.RabbitConnection(
                _logger,
                connectionFactoryMock.Object);

            var sender    = It.IsAny <object>();
            var replyText = It.IsAny <string>();
            var replyCode = It.IsAny <ushort>();

            connection.OnConnectionShutdown(sender, new ShutdownEventArgs(ShutdownInitiator.Peer, replyCode, replyText));

            connectionFactoryMock.Verify(m => m.CreateConnection(), Times.Once);
        }
Example #6
0
        public void TryConnect_LessThanFourExceptionsToHandle_CallCreatesConnectionAtLeastOnce()
        {
            var connectionFactoryMock = new Mock <IConnectionFactory>();
            var connectionMock        = new Mock <IConnection>();

            var callingTimes = 0;

            connectionFactoryMock
            .Setup(c => c.CreateConnection())
            .Callback(() =>
            {
                callingTimes++;
            })
            .Returns(() =>
            {
                if (callingTimes < 4)
                {
                    throw new BrokerUnreachableException(new Exception());
                }

                connectionMock
                .Setup(c => c.IsOpen)
                .Returns(true);

                return(connectionMock.Object);
            });

            var connection = new SharpRabbit.RabbitConnection(
                _logger,
                connectionFactoryMock.Object);

            var blockReason = It.IsAny <string>();

            connection.TryConnect();

            Assert.True(connection.IsConnected);
        }