예제 #1
0
        public async Task disposes_consumer_scope_when_consuming_single_message()
        {
            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();

            var spy = new ConsumerScopeSpy(messageResultStub);

            var registry = new MessageHandlerRegistry();

            registry.Register(messageRegistrationStub);

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

            await consumer.ConsumeSingle(CancellationToken.None);


            Assert.Equal(1, spy.Disposed);
        }
예제 #2
0
        public async Task disposes_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 spy = new ConsumerScopeSpy(messageResultStub, () =>
                {
                    loops++;

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

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

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

                await consumer.ConsumeAll(cancellationTokenSource.Token);

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