public void Resolve_should_return_message()
        {
            var messageType    = typeof(DummyMessage);
            var message        = DummyMessage.New();
            var encodedMessage = Newtonsoft.Json.JsonConvert.SerializeObject(message);
            var messageBytes   = Encoding.UTF8.GetBytes(encodedMessage);

            var decoder = NSubstitute.Substitute.For <ISerializer>();

            decoder.Deserialize(messageBytes, messageType).Returns(message);

            var resolver = NSubstitute.Substitute.For <ITypeResolver>();

            resolver.Resolve(messageType.FullName).Returns(messageType);

            var sut = new MessageParser(resolver, decoder);

            var consumeResult = new ConsumeResult <Guid, byte[]>()
            {
                Message = new Message <Guid, byte[]>()
                {
                    Headers = new Headers()
                    {
                        { HeaderNames.MessageType, Encoding.UTF8.GetBytes(messageType.FullName) }
                    },
                    Value = messageBytes
                }
            };
            var result = sut.Parse(consumeResult);

            result.Should().Be(message);
        }
        public async Task PublishAsync_should_publish_message()
        {
            var message = DummyMessage.New();

            var topicName = "lorem";

            var producerResult = new DeliveryResult <Guid, byte[]>()
            {
                Status = PersistenceStatus.Persisted
            };
            var producer = NSubstitute.Substitute.For <IProducer <Guid, byte[]> >();

            producer.ProduceAsync(topicName, Arg.Any <Message <Guid, byte[]> >(), Arg.Any <CancellationToken>())
            .Returns(producerResult);

            var serializer = NSubstitute.Substitute.For <ITransportSerializer>();
            var logger     = NSubstitute.Substitute.For <ILogger <KafkaPublisherExecutor> >();

            var sut = new KafkaPublisherExecutor(producer, serializer, logger);

            await sut.PublishAsync(message, topicName);

            await producer.Received(1)
            .ProduceAsync(topicName,
                          Arg.Is((Message <Guid, byte[]> km) => km.Headers.Any(h =>
                                                                               h.Key == HeaderNames.MessageType) &&
                                 km.Key == message.Id));
        }
        public async Task ReadMessagesToProcess_should_not_return_sent_messages()
        {
            var messages = new[]
            {
                DummyMessage.New(),
                DummyMessage.New(),
                DummyMessage.New(),
            };
            var sut = new InMemoryOutboxRepository();

            foreach (var message in messages)
            {
                await sut.AppendAsync(message);
            }

            var lockId = await sut.LockAsync(messages[0]);

            await sut.ReleaseAsync(messages[0], lockId);

            var results = await sut.ReadMessagesToProcess();

            results.Should().NotBeNull()
            .And.HaveCount(2)
            .And.NotContain(messages[0]);
        }
        public async Task CleanProcessedAsync_should_remove_processed_messages()
        {
            var messages = new[]
            {
                DummyMessage.New(),
                DummyMessage.New(),
                DummyMessage.New(),
            };
            var sut = new InMemoryOutboxRepository();

            foreach (var message in messages)
            {
                await sut.AppendAsync(message);
            }

            var results = await sut.ReadMessagesToProcess();

            results.Should().NotBeNull().And.Contain(messages);

            foreach (var message in messages)
            {
                await sut.LockAsync(message);
            }

            await sut.CleanProcessedAsync();

            results = await sut.ReadMessagesToProcess();

            results.Should().NotBeNull().And.BeEmpty();
        }
        public async Task MarkAsSentAsync_should_throw_when_message_lock_id_invalid()
        {
            var message = DummyMessage.New();
            var sut     = new InMemoryOutboxRepository();

            await sut.AppendAsync(message);

            await Assert.ThrowsAsync <ArgumentException>(async() => await sut.ReleaseAsync(message, Guid.Empty));
        }
        public async Task BeginProcessingAsync_should_throw_when_message_already_locked()
        {
            var message = DummyMessage.New();

            var sut = new InMemoryOutboxRepository();
            await sut.AppendAsync(message);

            await sut.LockAsync(message);

            await Assert.ThrowsAsync <LockException>(async() => await sut.LockAsync(message));
        }
Exemplo n.º 7
0
        public void Create_should_return_valid_references()
        {
            var sp      = NSubstitute.Substitute.For <IServiceProvider>();
            var sysInfo = SystemInfo.New();
            var sut     = new QueueReferenceFactory(sp, sysInfo);
            var message = DummyMessage.New();
            var result  = sut.Create(message);

            result.Should().NotBeNull();
            result.ExchangeName.Should().Be("dummymessage");
            result.QueueName.Should().Be("dummymessage.workers");
            result.DeadLetterExchangeName.Should().Be("dummymessage.dead");
            result.DeadLetterQueue.Should().Be("dummymessage.dead.workers");
        }
Exemplo n.º 8
0
        public async Task PublishAsync_should_write_message_to_proper_channel()
        {
            var message = DummyMessage.New();

            var channelFactory = NSubstitute.Substitute.For <IChannelFactory>();
            var writer         = NSubstitute.Substitute.For <ChannelWriter <DummyMessage> >();

            channelFactory.GetWriter <DummyMessage>()
            .Returns(writer);

            var sut = new InMemoryPublisher(channelFactory);

            await sut.PublishAsync(message);

            await writer.Received(1)
            .WriteAsync(message, Arg.Any <CancellationToken>());
        }
        public async Task ReadMessagesToProcess_should_return_pending_messages()
        {
            var messages = new[]
            {
                DummyMessage.New(),
                DummyMessage.New(),
                DummyMessage.New(),
            };
            var sut = new InMemoryOutboxRepository();

            foreach (var message in messages)
            {
                await sut.AppendAsync(message);
            }

            var results = await sut.ReadMessagesToProcess();

            results.Should().NotBeNull().And.Contain(messages);
        }
        public async Task PublishAsync_should_include_additional_headers_when_provided()
        {
            var message = DummyMessage.New();

            var topicName = "lorem";

            var producerResult = new DeliveryResult <Guid, byte[]>()
            {
                Status = PersistenceStatus.Persisted
            };
            var producer = NSubstitute.Substitute.For <IProducer <Guid, byte[]> >();

            producer.ProduceAsync(topicName, Arg.Any <Message <Guid, byte[]> >(), Arg.Any <CancellationToken>())
            .Returns(producerResult);

            var serializer = NSubstitute.Substitute.For <ITransportSerializer>();
            var logger     = NSubstitute.Substitute.For <ILogger <KafkaPublisherExecutor> >();

            var sut = new KafkaPublisherExecutor(producer, serializer, logger);

            var headers = new[]
            {
                new Header("lorem", Encoding.UTF8.GetBytes("ipsum")),
                new Header("dolor", Encoding.UTF8.GetBytes("amet"))
            };

            await sut.PublishAsync(message, topicName, headers);

            await producer.Received(1)
            .ProduceAsync(topicName,
                          Arg.Is((Message <Guid, byte[]> km) => km.Headers.Count == 3 &&
                                 km.Headers.Single(h => h.Key == HeaderNames.MessageType) != null &&
                                 km.Headers.Single(h => h.Key == "lorem") != null &&
                                 km.Headers.Single(h => h.Key == "dolor") != null &&
                                 km.Key == message.Id));
        }
        public async Task PublishAsync_should_throw_when_input_invalid()
        {
            var producer   = NSubstitute.Substitute.For <IProducer <Guid, byte[]> >();
            var serializer = NSubstitute.Substitute.For <ITransportSerializer>();
            var logger     = NSubstitute.Substitute.For <ILogger <KafkaPublisherExecutor> >();

            var sut = new KafkaPublisherExecutor(producer, serializer, logger);
            await Assert.ThrowsAsync <ArgumentNullException>(async() => await sut.PublishAsync(null, "lorem"));

            await Assert.ThrowsAsync <ArgumentNullException>(async() => await sut.PublishAsync(DummyMessage.New(), null));

            await Assert.ThrowsAsync <ArgumentNullException>(async() => await sut.PublishAsync(DummyMessage.New(), ""));

            await Assert.ThrowsAsync <ArgumentNullException>(async() => await sut.PublishAsync(DummyMessage.New(), "   "));
        }