public UnitOfWorkBehaviorTestsFixture()
        {
            UnitsOfWork = new List <Mock <IUnitOfWork> >
            {
                new Mock <IUnitOfWork>(),
                new Mock <IUnitOfWork>(),
                new Mock <IUnitOfWork>()
            };

            UnitOfWorkBehavior = new UnitOfWorkBehavior();
            Builder            = new FakeBuilder();
            Context            = new TestableIncomingLogicalMessageContext {
                Builder = Builder
            };
            NextTask = new Mock <Func <Task> >();

            Builder.Register(UnitsOfWork.Select(u => u.Object).Concat(new[] { new NServiceBus.Features.ClientOutbox.Pipeline.UnitOfWork(null, null, null, null) }).ToArray());

            NextTask.Setup(n => n()).Returns(Task.CompletedTask).Callback(() =>
            {
                if (CommittedUnitsOfWork > 0)
                {
                    throw new Exception("Next invoked too late");
                }
            });

            UnitsOfWork.ForEach(u => u.Setup(u2 => u2.CommitAsync(It.IsAny <Func <Task> >())).Returns <Func <Task> >(t => { CommittedUnitsOfWork++; return(t()); }));
        }
Esempio n. 2
0
        public void Throws_friendly_exception_when_IFindSagas_FindBy_returns_null()
        {
            var availableTypes = new List <Type>
            {
                typeof(ReturnsNullFinder)
            };

            var messageType = typeof(StartSagaMessage);

            var messageConventions = new Conventions
            {
                IsCommandTypeAction = t => t == messageType
            };

            var sagaMetadata = SagaMetadata.Create(typeof(TestSaga), availableTypes, messageConventions);

            SagaFinderDefinition finderDefinition;

            if (!sagaMetadata.TryGetFinder(messageType.FullName, out finderDefinition))
            {
                throw new Exception("Finder not found");
            }

            var builder = new FakeBuilder();

            builder.Register(() => new ReturnsNullFinder());

            var customerFinderAdapter = new CustomFinderAdapter <TestSaga.SagaData, StartSagaMessage>();

            Assert.That(async() => await customerFinderAdapter.Find(builder, finderDefinition, new InMemorySynchronizedStorageSession(), new ContextBag(), new StartSagaMessage()),
                        Throws.Exception.With.Message.EqualTo("Return a Task or mark the method as async."));
        }
Esempio n. 3
0
        public IncomingPhysicalMessageBehaviorTestsFixture()
        {
            Behavior          = new IncomingPhysicalMessageBehavior();
            Builder           = new FakeBuilder();
            UnitOfWorkContext = new Mock <IUnitOfWorkContext>();

            Context = new TestableIncomingPhysicalMessageContext
            {
                Builder = Builder
            };

            UnitOfWorkContext.Setup(c => c.Set <DbConnection>(null)).Callback <DbConnection>(t =>
            {
                if (NextTaskInvoked)
                {
                    throw new Exception("Set called too late");
                }
            });

            UnitOfWorkContext.Setup(c => c.Set <DbTransaction>(null)).Callback <DbTransaction>(t =>
            {
                if (NextTaskInvoked)
                {
                    throw new Exception("Set called too late");
                }
            });

            Builder.Register(UnitOfWorkContext.Object);
        }
        public InvokeHandlerBehaviorTestsFixture()
        {
            Behavior                   = new UnitOfWorkContextBehavior();
            Builder                    = new FakeBuilder();
            UnitOfWorkContext          = new Mock <IUnitOfWorkContext>();
            SynchronizedStorageSession = new Mock <SynchronizedStorageSession>();
            NextTask                   = new Mock <Func <Task> >();

            Context = new TestableInvokeHandlerContext
            {
                Builder = Builder,
                SynchronizedStorageSession = SynchronizedStorageSession.Object
            };

            UnitOfWorkContext.Setup(c => c.Set(It.IsAny <SynchronizedStorageSession>())).Callback <SynchronizedStorageSession>(s =>
            {
                if (NextTaskInvoked)
                {
                    throw new Exception("Set called too late");
                }
            });

            NextTask.Setup(n => n()).Returns(Task.CompletedTask).Callback(() => NextTaskInvoked = true);

            Builder.Register(UnitOfWorkContext.Object);
        }
Esempio n. 5
0
        public void Should_throw_friendly_exception_if_IManageUnitsOfWork_End_returns_null()
        {
            var builder = new FakeBuilder();

            builder.Register <IManageUnitsOfWork>(() => new UnitOfWorkThatReturnsNullForEnd());
            Assert.That(async() => await InvokeBehavior(builder),
                        Throws.Exception.With.Message.EqualTo("Return a Task or mark the method as async."));
        }
Esempio n. 6
0
        public void Should_append_end_exception_to_rethrow()
        {
            var builder = new FakeBuilder();

            var unitOfWork = new UnitOfWorkThatThrowsFromEnd();

            builder.Register <IManageUnitsOfWork>(() => unitOfWork);

            //since it is a single exception then it will not be an AggregateException
            Assert.That(async() => await InvokeBehavior(builder), Throws.InvalidOperationException.And.SameAs(unitOfWork.ExceptionThrownFromEnd));
        }
        public void Should_append_end_exception_to_rethrow()
        {
            var builder = new FakeBuilder();

            var unitOfWork = new UnitOfWorkThatThrowsFromEnd();

            builder.Register<IManageUnitsOfWork>(() => unitOfWork);

            //since it is a single exception then it will not be an AggregateException
            Assert.That(async () => await InvokeBehavior(builder), Throws.InvalidOperationException.And.SameAs(unitOfWork.ExceptionThrownFromEnd));
        }
Esempio n. 8
0
        public void Should_call_all_end_even_if_one_or_more_of_them_throws()
        {
            var builder = new FakeBuilder();

            var unitOfWorkThatThrows = new UnitOfWorkThatThrowsFromEnd();
            var unitOfWork           = new UnitOfWork();

            builder.Register <IManageUnitsOfWork>(unitOfWorkThatThrows, unitOfWork);

            Assert.That(async() => await InvokeBehavior(builder), Throws.InvalidOperationException);
            Assert.True(unitOfWork.EndCalled);
        }
        public async Task Should_not_call_Begin_or_End_when_hasUnitsOfWork_is_false()
        {
            var builder = new FakeBuilder();

            var unitOfWork = new UnitOfWork();

            builder.Register<IManageUnitsOfWork>(unitOfWork);

            await InvokeBehavior(builder, hasUnitsOfWork: false);

            Assert.IsFalse(unitOfWork.BeginCalled);
            Assert.IsFalse(unitOfWork.EndCalled);
        }
Esempio n. 10
0
        public async Task Should_not_call_Begin_or_End_when_hasUnitsOfWork_is_false()
        {
            var builder = new FakeBuilder();

            var unitOfWork = new UnitOfWork();

            builder.Register <IManageUnitsOfWork>(unitOfWork);

            await InvokeBehavior(builder, hasUnitsOfWork : false);

            Assert.IsFalse(unitOfWork.BeginCalled);
            Assert.IsFalse(unitOfWork.EndCalled);
        }
        public UnitOfWorkBehaviorTestsFixture()
        {
            Behavior          = new UnitOfWorkBehavior();
            Builder           = new FakeBuilder();
            Db                = new Mock <IDb>();
            UnitOfWorkContext = new Mock <IUnitOfWorkContext>();

            Context = new TestableIncomingLogicalMessageContext
            {
                Builder = Builder
            };

            Events = new List <Event>
            {
                new FooEvent(),
                new BarEvent()
            };

            UnitOfWorkContext.Setup(c => c.GetEvents()).Returns(() =>
            {
                if (!NextTaskInvoked)
                {
                    throw new Exception("GetEvents called too early");
                }

                return(Events);
            });

            Db.Setup(d => d.SaveChangesAsync()).Returns(Task.CompletedTask).Callback(() =>
            {
                if (!NextTaskInvoked)
                {
                    throw new Exception("SaveChanges called too early");
                }
            });

            Builder.Register(UnitOfWorkContext.Object);
            Builder.Register(Db.Object);
        }
        public void Should_pass_exceptions_to_the_uow_end()
        {
            var builder = new FakeBuilder();

            var unitOfWork = new UnitOfWork();

            builder.Register<IManageUnitsOfWork>(() => unitOfWork);

            var ex = new Exception("Handler failed");
            //since it is a single exception then it will not be an AggregateException
            Assert.That(async () => await InvokeBehavior(builder, ex), Throws.InstanceOf<Exception>().And.SameAs(ex));
            Assert.AreSame(ex, unitOfWork.ExceptionPassedToEnd);
        }
Esempio n. 13
0
        public void Should_pass_exception_to_cleanup()
        {
            var builder = new FakeBuilder();

            var unitOfWork  = new CaptureExceptionPassedToEndUnitOfWork();
            var throwingUoW = new UnitOfWorkThatThrowsFromEnd();

            builder.Register <IManageUnitsOfWork>(unitOfWork, throwingUoW);

            //since it is a single exception then it will not be an AggregateException
            Assert.That(async() => await InvokeBehavior(builder), Throws.InstanceOf <InvalidOperationException>().And.SameAs(throwingUoW.ExceptionThrownFromEnd));
            Assert.AreSame(throwingUoW.ExceptionThrownFromEnd, unitOfWork.Exception);
        }
Esempio n. 14
0
        public void Should_not_invoke_end_if_begin_was_not_invoked()
        {
            var builder = new FakeBuilder();

            var unitOfWorkThatThrowsFromBegin = new UnitOfWorkThatThrowsFromBegin();
            var unitOfWork = new UnitOfWork();

            builder.Register <IManageUnitsOfWork>(unitOfWorkThatThrowsFromBegin, unitOfWork);

            //since it is a single exception then it will not be an AggregateException
            Assert.That(async() => await InvokeBehavior(builder), Throws.InvalidOperationException);
            Assert.False(unitOfWork.EndCalled);
        }
Esempio n. 15
0
        public void Should_pass_exceptions_to_the_uow_end()
        {
            var builder = new FakeBuilder();

            var unitOfWork = new UnitOfWork();

            builder.Register <IManageUnitsOfWork>(() => unitOfWork);

            var ex = new Exception("Handler failed");

            //since it is a single exception then it will not be an AggregateException
            Assert.That(async() => await InvokeBehavior(builder, ex), Throws.InstanceOf <Exception>().And.SameAs(ex));
            Assert.AreSame(ex, unitOfWork.ExceptionPassedToEnd);
        }
        public void Should_not_invoke_end_if_begin_was_not_invoked()
        {
            var builder = new FakeBuilder();

            var unitOfWorkThatThrowsFromBegin = new UnitOfWorkThatThrowsFromBegin();
            var unitOfWork = new UnitOfWork();

            builder.Register<IManageUnitsOfWork>(unitOfWorkThatThrowsFromBegin, unitOfWork);

            //since it is a single exception then it will not be an AggregateException
            Assert.That(async () => await InvokeBehavior(builder), Throws.InvalidOperationException);
            Assert.False(unitOfWork.EndCalled);

        }
Esempio n. 17
0
        public void Should_invoke_ends_on_all_begins_that_was_called_even_when_begin_throws()
        {
            var builder = new FakeBuilder();

            var normalUnitOfWork            = new UnitOfWork();
            var unitOfWorkThatThrows        = new UnitOfWorkThatThrowsFromBegin();
            var unitOfWorkThatIsNeverCalled = new UnitOfWork();

            builder.Register <IManageUnitsOfWork>(normalUnitOfWork, unitOfWorkThatThrows, unitOfWorkThatIsNeverCalled);

            Assert.That(async() => await InvokeBehavior(builder), Throws.InvalidOperationException);

            Assert.True(normalUnitOfWork.EndCalled);
            Assert.True(unitOfWorkThatThrows.EndCalled);
            Assert.False(unitOfWorkThatIsNeverCalled.EndCalled);
        }
Esempio n. 18
0
        public void When_first_throw_second_is_cleaned_up()
        {
            var builder = new FakeBuilder();

            var unitOfWorkThatThrowsFromEnd = new UnitOfWorkThatThrowsFromEnd();
            var unitOfWork = new UnitOfWork();

            builder.Register <IManageUnitsOfWork>(unitOfWorkThatThrowsFromEnd, unitOfWork);

            //since it is a single exception then it will not be an AggregateException
            Assert.That(async() => await InvokeBehavior(builder), Throws.InvalidOperationException);
            Assert.IsTrue(unitOfWorkThatThrowsFromEnd.BeginCalled);
            Assert.IsTrue(unitOfWorkThatThrowsFromEnd.EndCalled);
            Assert.IsTrue(unitOfWork.BeginCalled);
            Assert.IsTrue(unitOfWork.EndCalled);
        }
        public void When_first_throw_second_is_cleaned_up()
        {
            var builder = new FakeBuilder();

            var unitOfWorkThatThrowsFromEnd = new UnitOfWorkThatThrowsFromEnd();
            var unitOfWork = new UnitOfWork();

            builder.Register<IManageUnitsOfWork>(unitOfWorkThatThrowsFromEnd, unitOfWork);

            //since it is a single exception then it will not be an AggregateException
            Assert.That(async () => await InvokeBehavior(builder), Throws.InvalidOperationException);
            Assert.IsTrue(unitOfWorkThatThrowsFromEnd.BeginCalled);
            Assert.IsTrue(unitOfWorkThatThrowsFromEnd.EndCalled);
            Assert.IsTrue(unitOfWork.BeginCalled);
            Assert.IsTrue(unitOfWork.EndCalled);
        }
Esempio n. 20
0
        public async Task Should_invoke_ends_in_reverse_order_of_the_begins()
        {
            var builder = new FakeBuilder();

            var order            = new List <string>();
            var firstUnitOfWork  = new OrderAwareUnitOfWork("first", order);
            var secondUnitOfWork = new OrderAwareUnitOfWork("second", order);


            builder.Register <IManageUnitsOfWork>(firstUnitOfWork, secondUnitOfWork);

            await InvokeBehavior(builder);

            Assert.AreEqual("first", order[0]);
            Assert.AreEqual("second", order[1]);
            Assert.AreEqual("second", order[2]);
            Assert.AreEqual("first", order[3]);
        }
Esempio n. 21
0
        public async Task Verify_order()
        {
            var builder = new FakeBuilder();

            var unitOfWork1 = new CountingUnitOfWork();
            var unitOfWork2 = new CountingUnitOfWork();
            var unitOfWork3 = new CountingUnitOfWork();

            builder.Register <IManageUnitsOfWork>(unitOfWork1, unitOfWork2, unitOfWork3);

            await InvokeBehavior(builder);

            Assert.AreEqual(1, unitOfWork1.BeginCallIndex);
            Assert.AreEqual(2, unitOfWork2.BeginCallIndex);
            Assert.AreEqual(3, unitOfWork3.BeginCallIndex);
            Assert.AreEqual(3, unitOfWork1.EndCallIndex);
            Assert.AreEqual(2, unitOfWork2.EndCallIndex);
            Assert.AreEqual(1, unitOfWork3.EndCallIndex);
        }
        public InvokeHandlerBehaviorTestsFixture()
        {
            Behavior          = new InvokeHandlerBehavior();
            Builder           = new FakeBuilder();
            UnitOfWorkContext = new Mock <IUnitOfWorkContext>();
            StorageSession    = new Mock <SynchronizedStorageSession>();
            SqlStorageSession = StorageSession.As <ISqlStorageSession>();
            Connection        = new Mock <DbConnection>();
            Transaction       = new Mock <DbTransaction>();

            Context = new TestableInvokeHandlerContext
            {
                Builder = Builder,
                SynchronizedStorageSession = StorageSession.Object
            };

            UnitOfWorkContext.Setup(c => c.Set <DbConnection>(null)).Callback <DbConnection>(t =>
            {
                if (NextTaskInvoked)
                {
                    throw new Exception("Set called too late");
                }
            });

            UnitOfWorkContext.Setup(c => c.Set <DbTransaction>(null)).Callback <DbTransaction>(t =>
            {
                if (NextTaskInvoked)
                {
                    throw new Exception("Set called too late");
                }
            });

            SqlStorageSession.Setup(s => s.Connection).Returns(Connection.Object);
            SqlStorageSession.Setup(s => s.Transaction).Returns(Transaction.Object);
            Builder.Register(UnitOfWorkContext.Object);
        }
        public void Should_call_all_end_even_if_one_or_more_of_them_throws()
        {
            var builder = new FakeBuilder();

            var unitOfWorkThatThrows = new UnitOfWorkThatThrowsFromEnd();
            var unitOfWork = new UnitOfWork();

            builder.Register<IManageUnitsOfWork>(unitOfWorkThatThrows, unitOfWork);

            Assert.That(async () => await InvokeBehavior(builder), Throws.InvalidOperationException);
            Assert.True(unitOfWork.EndCalled);
        }
        public void Should_throw_friendly_exception_if_IManageUnitsOfWork_End_returns_null()
        {
            var builder = new FakeBuilder();

            builder.Register<IManageUnitsOfWork>(() => new UnitOfWorkThatReturnsNullForEnd());
            Assert.That(async () => await InvokeBehavior(builder),
                Throws.Exception.With.Message.EqualTo("Return a Task or mark the method as async."));
        }
        public async Task Should_invoke_ends_in_reverse_order_of_the_begins()
        {
            var builder = new FakeBuilder();

            var order = new List<string>();
            var firstUnitOfWork = new OrderAwareUnitOfWork("first", order);
            var secondUnitOfWork = new OrderAwareUnitOfWork("second", order);


            builder.Register<IManageUnitsOfWork>(firstUnitOfWork, secondUnitOfWork);

            await InvokeBehavior(builder);

            Assert.AreEqual("first", order[0]);
            Assert.AreEqual("second", order[1]);
            Assert.AreEqual("second", order[2]);
            Assert.AreEqual("first", order[3]);
        }
        public async Task Verify_order()
        {
            var builder = new FakeBuilder();

            var unitOfWork1 = new CountingUnitOfWork();
            var unitOfWork2 = new CountingUnitOfWork();
            var unitOfWork3 = new CountingUnitOfWork();

            builder.Register<IManageUnitsOfWork>(unitOfWork1, unitOfWork2, unitOfWork3);

            await InvokeBehavior(builder);

            Assert.AreEqual(1, unitOfWork1.BeginCallIndex);
            Assert.AreEqual(2, unitOfWork2.BeginCallIndex);
            Assert.AreEqual(3, unitOfWork3.BeginCallIndex);
            Assert.AreEqual(3, unitOfWork1.EndCallIndex);
            Assert.AreEqual(2, unitOfWork2.EndCallIndex);
            Assert.AreEqual(1, unitOfWork3.EndCallIndex);
        }
        public void Should_invoke_ends_on_all_begins_that_was_called_even_when_begin_throws()
        {
            var builder = new FakeBuilder();

            var normalUnitOfWork = new UnitOfWork();
            var unitOfWorkThatThrows = new UnitOfWorkThatThrowsFromBegin();
            var unitOfWorkThatIsNeverCalled = new UnitOfWork();

            builder.Register<IManageUnitsOfWork>(normalUnitOfWork, unitOfWorkThatThrows, unitOfWorkThatIsNeverCalled);

            Assert.That(async () => await InvokeBehavior(builder), Throws.InvalidOperationException);

            Assert.True(normalUnitOfWork.EndCalled);
            Assert.True(unitOfWorkThatThrows.EndCalled);
            Assert.False(unitOfWorkThatIsNeverCalled.EndCalled);
        }
        public void Should_pass_exception_to_cleanup()
        {
            var builder = new FakeBuilder();

            var unitOfWork = new CaptureExceptionPassedToEndUnitOfWork();
            var throwingUoW = new UnitOfWorkThatThrowsFromEnd();

            builder.Register<IManageUnitsOfWork>(unitOfWork, throwingUoW);

            //since it is a single exception then it will not be an AggregateException
            Assert.That(async () => await InvokeBehavior(builder), Throws.InstanceOf<InvalidOperationException>().And.SameAs(throwingUoW.ExceptionThrownFromEnd));
            Assert.AreSame(throwingUoW.ExceptionThrownFromEnd, unitOfWork.Exception);
        }