public void ThrowsAnExceptionWhenTheProducerIsNull()
        {
            resolver.Setup(o => o.Resolve(It.IsAny <ProducerConsumerContext <object> >())).Returns((IProducer <object>)null);

            var target = new DefaultProducerExecutionStrategy <object>(scopeFactory.Object, resolver.Object, synchronizationPolicy.Object, true);

            Assert.ThrowsAsync <RuntimeException>(() => target.ExecuteAsync(context => { }, CancellationToken.None));
        }
 private void AssertContext(object item, DefaultProducerExecutionStrategy <object> target, IProducerConsumerContext <object> context)
 {
     Assert.AreEqual(item, context.Item);
     Assert.AreEqual(producer.Object, context.ProductionContext.Producer);
     Assert.AreEqual(scope.Object, context.LifetimeScope);
     Assert.AreEqual(target, context.ProductionContext.ExecutionStrategy);
     Assert.AreEqual(synchronizationLock.Object, context.SynchronizationLock);
 }
        public void ThrowsAnExceptionWhenTheScopeFactoryReturnsNull()
        {
            scopeFactory.Setup(o => o.CreateScope()).Returns((IServiceScope)null).Verifiable();

            var target = new DefaultProducerExecutionStrategy <object>(scopeFactory.Object, resolver.Object, synchronizationPolicy.Object, true);

            Assert.ThrowsAsync <RuntimeException>(() => target.ExecuteAsync(context => { }, CancellationToken.None));

            scopeFactory.Verify();
        }
        public async Task ShouldNotExecuteTheCallbackWhenNull()
        {
            var called = false;

            producer.Setup(o => o.ProduceAsync(It.IsAny <CancellationToken>())).ReturnsAsync((object)null);

            var target = new DefaultProducerExecutionStrategy <object>(scopeFactory.Object, resolver.Object, synchronizationPolicy.Object, false);
            await target.ExecuteAsync(context =>
            {
                called = true;
            }, CancellationToken.None);

            Assert.IsFalse(called);
        }
        public async Task ShouldExecuteTheCallbackWhenNotNull()
        {
            var called = false;

            var item = new object();

            producer.Setup(o => o.ProduceAsync(It.IsAny <CancellationToken>())).ReturnsAsync(item);

            var target = new DefaultProducerExecutionStrategy <object>(scopeFactory.Object, resolver.Object, synchronizationPolicy.Object, true);
            await target.ExecuteAsync(context =>
            {
                AssertContext(item, target, context);
                called = true;
            }, CancellationToken.None);

            Assert.IsTrue(called);
        }
        public void ThrowsAnExceptionWhenTheCallbackIsNull()
        {
            var target = new DefaultProducerExecutionStrategy <object>(scopeFactory.Object, resolver.Object, synchronizationPolicy.Object, true);

            Assert.ThrowsAsync <ArgumentNullException>(() => target.ExecuteAsync(null, CancellationToken.None));
        }