Esempio n. 1
0
        public async Task creates_consumer_scope_when_consuming_multiple_messages()
        {
            var messageResultStub = new MessageResultBuilder().WithTransportLevelMessage(new TransportLevelMessageBuilder().WithType("foo").Build()).Build();
            var handlerStub       = Dummy.Of <IMessageHandler <FooMessage> >();

            var messageRegistrationStub = new MessageRegistrationBuilder()
                                          .WithHandlerInstanceType(handlerStub.GetType())
                                          .WithMessageInstanceType(typeof(FooMessage))
                                          .WithMessageType("foo")
                                          .Build();

            using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
            {
                var loops = 0;

                var subscriberScopeStub = new ConsumerScopeDecoratorWithHooks(
                    inner: new ConsumerScopeStub(messageResultStub),
                    postHook: () =>
                {
                    loops++;

                    if (loops == 2)
                    {
                        cancellationTokenSource.Cancel();
                    }
                }
                    );

                var spy = new ConsumerScopeFactorySpy(subscriberScopeStub);

                var registry = new MessageHandlerRegistry();
                registry.Register(messageRegistrationStub);

                var consumer = new ConsumerBuilder()
                               .WithConsumerScopeFactory(spy)
                               .WithUnitOfWork(new UnitOfWorkStub(handlerStub))
                               .WithMessageHandlerRegistry(registry)
                               .Build();

                await consumer.ConsumeAll(cancellationTokenSource.Token);

                Assert.Equal(2, loops);
                Assert.Equal(1, spy.CreateConsumerScopeCalled);
            }
        }