public void OnlyReceivesPublishedEventWhenRebusTransactionScopeIsCompleted()
        {
            var network         = new InMemNetwork();
            var subscriberStore = new InMemorySubscriberStore();

            network.CreateQueue("subscriber");
            subscriberStore.AddSubscriber(typeof(TestEvent).GetSimpleAssemblyQualifiedName(), "subscriber");

            var bus = Configure.With(new BuiltinHandlerActivator())
                      .Subscriptions(config => config.StoreInMemory(subscriberStore))
                      .Transport(configurer => configurer.UseInMemoryTransport(network, "Test"))
                      .Start();

            using (var scope = new RebusTransactionScope())
            {
                bus.Advanced.SyncBus.Publish(new TestEvent("completed"));
                scope.Complete();
            }

            using (new RebusTransactionScope())
            {
                bus.Advanced.SyncBus.Publish(new TestEvent("not completed"));
                // this scope is intentionally not completed!
            }

            var messages = network.GetMessages("subscriber").ToList();

            Assert.That(messages.Count, Is.EqualTo(1));

            var transportMessage = messages.First();

            Assert.That(transportMessage.Headers.GetValue(Headers.Type), Is.EqualTo(typeof(TestEvent).GetSimpleAssemblyQualifiedName()));

            var json      = Encoding.UTF8.GetString(transportMessage.Body);
            var testEvent = JsonConvert.DeserializeObject <TestEvent>(json);

            Assert.That(testEvent.Label, Is.EqualTo("completed"));
        }
        public async Task Given_deadLetter_is_configured_when_receiving_invalid_message_it_should_be_moved_to_error_queue()
        {
            string      messageId       = null;
            TestMessage receivedMessage = null;
            var         sync            = new ManualResetEvent(false);

            var inMemNetwork = new InMemNetwork();
            var activator    = new BuiltinHandlerActivator();

            activator
            .Handle <TestMessage>(message =>
            {
                // This should not happen, but signal completion.
                receivedMessage = message;
                sync.Set();
                return(Task.CompletedTask);
            });

            using (IBus bus = CreateBus(activator,
                                        o =>
            {
                o.ValidateIncomingMessages(_validatorFactoryMock.Object,
                                           v => v.DeadLetter <TestMessage>()
                                           );
                o.OnPipelineCompletion <TestMessage>(failed =>
                {
                    messageId = MessageContext.Current.Message.GetMessageId();
                    sync.Set();
                });
            },
                                        inMemNetwork))
            {
                // Act
                var testMessage = new TestMessage
                {
                    ShouldPassValidation = false
                };

                await bus.Send(testMessage);

                // Assert
                sync.WaitOne(Debugger.IsAttached ? -1 : 5000);

                receivedMessage.Should().BeNull();

                var errorQueueMessages        = inMemNetwork.GetMessages(ErrorQueueName).ToList();
                InMemTransportMessage message = errorQueueMessages.Should()
                                                .HaveCount(1)
                                                .And.Subject.Single();
                message.Headers.Should()
                .ContainKey(Headers.ErrorDetails)
                .WhichValue.Should()
                .Match($"*{nameof(TestMessage.ShouldPassValidation)}: The specified condition was not met*");
                message.Headers.Should()
                .ContainKey(Headers.Type)
                .WhichValue.Should()
                .StartWith(typeof(TestMessage).FullName);
            }

            _loggerFactory.LogEvents
            .Select(le => le.Exception)
            .Should()
            .NotContain(ex => ex is MessageCouldNotBeDispatchedToAnyHandlersException);
            _loggerFactory.LogEvents
            .Select(le => le.ToString())
            .Should()
            .ContainMatch($"Debug: Message \"{messageId}\" failed to validate.*The specified condition was not met for 'Should Pass Validation'.*");
            _loggerFactory.LogEvents
            .Select(le => le.Message)
            .Should()
            .ContainMatch("*is configured to be moved to error queue.")
            .And
            .ContainMatch("Moving message with ID *");
        }
Exemple #3
0
    public void GetMessages_NonExistingQueue_ReturnsEmptyList()
    {
        var messages = _inMemNetwork.GetMessages("non_existing");

        Assert.That(messages, Is.Empty);
    }