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)));
            }
        }
        public void Setup()
        {
            using (var context = new TestProcessManagerDbContext(this.DbName)) {
                context.Database.Delete();
                context.Database.Create();
            }

            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 == this._command2.Id)))
            .Callback(() => {
                this._sendStartedResetEvent1.Set();
                this._sendContinueResetEvent1.WaitOne();
            });

            Task.Factory.StartNew(() => {
                using (var context = new SqlProcessManagerDataContext <OrmTestProcessManager>(
                           () => new TestProcessManagerDbContext(this.DbName), this._bus1.Object, new JsonTextSerializer())) {
                    var aggregate = new OrmTestProcessManager(id);
                    aggregate.AddEnvelope(new Envelope <ICommand>(this._command1), new Envelope <ICommand>(this._command2),
                                          new Envelope <ICommand>(this._command3));
                    context.Save(aggregate);
                }
            }).ContinueWith(t => this._exceptions.Add(t.Exception.InnerException), TaskContinuationOptions.OnlyOnFaulted)
            .ContinueWith(t => this._saveFinished.Set());

            Assert.IsTrue(this._sendStartedResetEvent1.WaitOne(3000));

            this._bus2 = new Mock <ICommandBus>();
            this._sendContinueResetEvent2 = new AutoResetEvent(false);
            this._sendStartedResetEvent2  = new AutoResetEvent(false);
            this._bus2.Setup(x => x.Send(It.Is <Envelope <ICommand> >(c => c.Body.Id == this._command2.Id)))
            .Callback(() => {
                this._sendStartedResetEvent2.Set();
                this._sendContinueResetEvent2.WaitOne();
            });

            this._findAndSaveFinished = new ManualResetEvent(false);

            Task.Factory.StartNew(() => {
                using (var context = new SqlProcessManagerDataContext <OrmTestProcessManager>(
                           () => new TestProcessManagerDbContext(this.DbName), this._bus2.Object, new JsonTextSerializer())) {
                    var entity = context.Find(id);
                    context.Save(entity);
                }
            }).ContinueWith(t => this._exceptions.Add(t.Exception.InnerException), TaskContinuationOptions.OnlyOnFaulted)
            .ContinueWith(t => this._findAndSaveFinished.Set());
        }
예제 #3
0
        public given_context_that_stalls_on_save_and_on_find_when_publishing_()
        {
            bus1     = new Mock <ICommandBus>();
            command1 = new TestCommand();
            command2 = new TestCommand();
            command3 = new TestCommand();
            var id = Guid.NewGuid();

            exceptions = new List <Exception>();

            saveFinished            = new ManualResetEvent(false);
            sendContinueResetEvent1 = new AutoResetEvent(false);
            sendStartedResetEvent1  = new AutoResetEvent(false);
            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));

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

            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());
        }
        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)));
            }
        }
예제 #5
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));
            }
        }
        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());
        }
        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)));
            }
        }
        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));
            }
        }
        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)));
            }
        }