GivenConnectionWhenPublishShouldDeclareExchangeAndQueueBindRoutingKeyPublishMessageCloseAndDisposeChannel()
        {
            const string notification = "test";
            var          exchange     = Exchange.Create("test", ExchangeType.Direct);
            var          queue        = Queue.Create("test.requested");
            var          routingKey   = RoutingKey.Create("test.route");
            var          body         = Encoding.UTF8.GetBytes("test");

            _busSerializerMock.Setup(x => x.Serialize(It.IsAny <object>()))
            .ReturnsAsync(body)
            .Verifiable();

            await _busConnection.Publish(exchange, queue, routingKey, notification);

            _connectionFactoryMock.Verify(x => x.CreateConnection(), Times.Once());
            _connectionMock.Verify(x => x.CreateModel(), Times.Once());
            _channelMock.Verify(x => x.ExchangeDeclare(
                                    exchange.Name.Value,
                                    exchange.Type.Value,
                                    exchange.Durability.IsDurable,
                                    exchange.IsAutoDelete,
                                    It.IsAny <IDictionary <string, object> >()), Times.Once());
            _channelMock.Verify(x => x.QueueDeclare(
                                    queue.Name.Value,
                                    queue.Durability.IsDurable,
                                    false,
                                    queue.IsAutoDelete,
                                    It.IsAny <IDictionary <string, object> >()));
            _channelMock.Verify(x => x.QueueBind(
                                    queue.Name.Value,
                                    exchange.Name.Value,
                                    routingKey.Value,
                                    It.IsAny <IDictionary <string, object> >()));
            _channelMock.Verify(
                x => x.BasicPublish(
                    exchange.Name.Value,
                    routingKey.Value,
                    false,
                    null,
                    body), Times.Once());
            _channelMock.Verify(x => x.Close(), Times.Once());
            _channelMock.Verify(x => x.Dispose(), Times.Once());
        }
Exemplo n.º 2
0
        public void GivenConnectionWhenPublishFailsShouldExecuteFailedCallback()
        {
            const string notification = "test";
            var          exchange     = Exchange.Create("test", ExchangeType.Direct);
            var          queue        = Queue.Create("test.requested");
            var          routingKey   = RoutingKey.Create("test.route");
            var          body         = Encoding.UTF8.GetBytes("test");

            var headersMock = new Mock <IDictionary <string, object> >();

            _busSerializerMock.Setup(x => x.SerializeAsync(It.IsAny <object>()))
            .ReturnsAsync(body)
            .Verifiable();
            _basicPropertiesMock.Setup(x => x.Headers)
            .Returns(headersMock.Object)
            .Verifiable();

            var autoResetEvent = new AutoResetEvent(false);

            _publishBatchMock.Setup(x => x.Publish())
            .Throws(new Exception())
            .Verifiable();

            Exception        callbackException = null;
            List <BatchItem> callbackItems     = null;

            _busConnection.PublishFailed += (items, ex) =>
            {
                callbackItems     = items.ToList();
                callbackException = ex;
                autoResetEvent.Set();
            };
            _busConnection.Publish(exchange, queue, routingKey, notification);

            autoResetEvent.WaitOne(TimeSpan.FromSeconds(5));

            _publishBatchMock.VerifyAll();

            callbackItems.Should().NotBeNull();
            callbackItems.Should().NotBeEmpty();
            callbackException.Should().NotBeNull();
        }