Example #1
0
        public void WhenCommandPublishingThrows_ThenPublishesPendingCommandOnNextFind()
        {
            var bus      = new Mock <ICommandBus>();
            var command1 = new Envelope <ICommand>(new TestCommand());
            var command2 = new Envelope <ICommand>(new TestCommand());
            var id       = Guid.NewGuid();

            bus.Setup(x => x.Send(command2)).Throws <TimeoutException>();

            using (var context = new SqlProcessManagerDataContext <OrmTestProcessManager>(() => new TestProcessManagerDbContext(dbName), bus.Object, new JsonTextSerializer()))
            {
                var aggregate = new OrmTestProcessManager(id);
                aggregate.AddEnvelope(command1, command2);

                Assert.Throws <TimeoutException>(() => context.Save(aggregate));
            }

            bus.Verify(x => x.Send(command1));
            bus.Verify(x => x.Send(command2));


            // Clear bus for next run.
            bus = new Mock <ICommandBus>();
            using (var context = new SqlProcessManagerDataContext <OrmTestProcessManager>(() => new TestProcessManagerDbContext(dbName), bus.Object, new JsonTextSerializer()))
            {
                var aggregate = context.Find(id);

                Assert.NotNull(aggregate);
                bus.Verify(x => x.Send(It.Is <Envelope <ICommand> >(c => c.Body.Id == command1.Body.Id)), Times.Never());
                bus.Verify(x => x.Send(It.Is <Envelope <ICommand> >(c => c.Body.Id == command2.Body.Id)));
            }
        }
Example #2
0
        public given_context_that_stalls_on_save_and_on_find_when_publishing_()
        {
            this.bus1     = new Mock <ICommandBus>();
            this.command1 = new TestCommand();
            this.command2 = new TestCommand();
            this.command3 = new TestCommand();
            var id = Guid.NewGuid();

            this.exceptions = new List <Exception>();

            this.saveFinished            = new ManualResetEvent(false);
            this.sendContinueResetEvent1 = new AutoResetEvent(false);
            this.sendStartedResetEvent1  = new AutoResetEvent(false);
            this.bus1.Setup(x => x.Send(It.Is <Envelope <ICommand> >(c => c.Body.Id == command2.Id)))
            .Callback(() => { sendStartedResetEvent1.Set(); sendContinueResetEvent1.WaitOne(); });

            Task.Factory.StartNew(() =>
            {
                using (var context = new SqlProcessManagerDataContext <OrmTestProcessManager>(() => new TestProcessManagerDbContext(dbName), bus1.Object, new JsonTextSerializer()))
                {
                    var aggregate = new OrmTestProcessManager(id);
                    aggregate.AddEnvelope(new Envelope <ICommand>(command1), new Envelope <ICommand>(command2), new Envelope <ICommand>(command3));

                    context.Save(aggregate);
                }
            }).ContinueWith(t => exceptions.Add(t.Exception.InnerException), TaskContinuationOptions.OnlyOnFaulted)
            .ContinueWith(t => saveFinished.Set());

            Assert.True(sendStartedResetEvent1.WaitOne(3000));

            this.bus2 = new Mock <ICommandBus>();
            this.sendContinueResetEvent2 = new AutoResetEvent(false);
            this.sendStartedResetEvent2  = new AutoResetEvent(false);
            bus2.Setup(x => x.Send(It.Is <Envelope <ICommand> >(c => c.Body.Id == command2.Id)))
            .Callback(() => { sendStartedResetEvent2.Set(); sendContinueResetEvent2.WaitOne(); });

            this.findAndSaveFinished = new ManualResetEvent(false);

            Task.Factory.StartNew(() =>
            {
                using (var context = new SqlProcessManagerDataContext <OrmTestProcessManager>(() => new TestProcessManagerDbContext(dbName), bus2.Object, new JsonTextSerializer()))
                {
                    var entity = context.Find(id);
                    context.Save(entity);
                }
            }).ContinueWith(t => exceptions.Add(t.Exception.InnerException), TaskContinuationOptions.OnlyOnFaulted)
            .ContinueWith(t => findAndSaveFinished.Set());
        }
Example #3
0
        public void WhenCommandPublishingThrowsPartiallyOnSave_ThenPublishesPendingCommandOnNextFind()
        {
            var bus      = new Mock <ICommandBus>();
            var command1 = new Envelope <ICommand>(new TestCommand());
            var command2 = new Envelope <ICommand>(new TestCommand());
            var command3 = new Envelope <ICommand>(new TestCommand());
            var id       = Guid.NewGuid();

            bus.Setup(x => x.Send(command2)).Throws <TimeoutException>();

            using (var context = new SqlProcessManagerDataContext <OrmTestProcessManager>(() => new TestProcessManagerDbContext(dbName), bus.Object, new JsonTextSerializer()))
            {
                var aggregate = new OrmTestProcessManager(id);
                aggregate.AddEnvelope(command1, command2, command3);

                Assert.Throws <TimeoutException>(() => context.Save(aggregate));
            }

            bus.Verify(x => x.Send(command1));
            bus.Verify(x => x.Send(command2));
            bus.Verify(x => x.Send(command3), Times.Never());


            // Setup bus for failure only on the third deserialized command now.
            // The command2 will pass now as it's a different deserialized instance.
            bus.Setup(x => x.Send(It.Is <Envelope <ICommand> >(c => c.Body.Id == command3.Body.Id))).Throws <TimeoutException>();

            using (var context = new SqlProcessManagerDataContext <OrmTestProcessManager>(() => new TestProcessManagerDbContext(dbName), bus.Object, new JsonTextSerializer()))
            {
                Assert.Throws <TimeoutException>(() => context.Find(id));

                bus.Verify(x => x.Send(It.Is <Envelope <ICommand> >(c => c.Body.Id == command2.Body.Id)));
                bus.Verify(x => x.Send(It.Is <Envelope <ICommand> >(c => c.Body.Id == command3.Body.Id)));
            }

            // Clear bus now.
            bus = new Mock <ICommandBus>();
            using (var context = new SqlProcessManagerDataContext <OrmTestProcessManager>(() => new TestProcessManagerDbContext(dbName), bus.Object, new JsonTextSerializer()))
            {
                var aggregate = context.Find(id);

                Assert.NotNull(aggregate);

                bus.Verify(x => x.Send(It.Is <Envelope <ICommand> >(c => c.Body.Id == command2.Body.Id)), Times.Never());
                bus.Verify(x => x.Send(It.Is <Envelope <ICommand> >(c => c.Body.Id == command3.Body.Id)));
            }
        }
Example #4
0
        public void WhenCommandPublishingFails_ThenThrows()
        {
            var bus      = new Mock <ICommandBus>();
            var command1 = new Envelope <ICommand>(new TestCommand());
            var command2 = new Envelope <ICommand>(new TestCommand());
            var command3 = new Envelope <ICommand>(new TestCommand());
            var id       = Guid.NewGuid();

            bus.Setup(x => x.Send(command2)).Throws <TimeoutException>();

            using (var context = new SqlProcessManagerDataContext <OrmTestProcessManager>(() => new TestProcessManagerDbContext(dbName), bus.Object, new JsonTextSerializer()))
            {
                var aggregate = new OrmTestProcessManager(id);
                aggregate.AddEnvelope(command1, command2, command3);

                Assert.Throws <TimeoutException>(() => context.Save(aggregate));
            }
        }