コード例 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StoredCommand" /> class.
 /// </summary>
 /// <param name="msg">The MSG.</param>
 /// <exception cref="System.ArgumentNullException">msg</exception>
 public StoredCommand(DispatchCommand msg)
 {
     if (msg == null) throw new ArgumentNullException("msg");
     Command = msg.Command;
     Id = Command.CommandId;
     Attempts = msg.Attempts;
 }
コード例 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandAborted" /> class.
 /// </summary>
 /// <param name="message">The message that was aborted.</param>
 /// <param name="exception">The exception which aborted the dispatching.</param>
 /// <exception cref="System.ArgumentNullException">The fields are required</exception>
 public CommandAborted(DispatchCommand message, Exception exception)
 {
     if (message == null) throw new ArgumentNullException("message");
     if (exception == null) throw new ArgumentNullException("exception");
     Message = message;
     Exception = exception;
 }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandCompleted" /> class.
 /// </summary>
 /// <param name="message">Message for the command that was completed.</param>
 /// <exception cref="System.ArgumentNullException">message</exception>
 public CommandCompleted(DispatchCommand message)
 {
     if (message == null)
     {
         throw new ArgumentNullException("message");
     }
     Message = message;
 }
コード例 #4
0
        /// <summary>
        /// Add a new command
        /// </summary>
        /// <param name="command">Store the command in the DB. You can use the <see cref="ICommand.CommandId"/> as an identity.</param>
        public void Add(DispatchCommand command)
        {
            if (command == null) throw new ArgumentNullException("command");

            lock (_lock)
            {
                _queue.Add(new MyStruct {Command = command});
            }
        }
コード例 #5
0
 /// <summary>
 /// Re add a command which we've tried to invoke but failed.
 /// </summary>
 /// <param name="command">Command to add</param>
 public void Update(DispatchCommand command)
 {
     lock (_lock)
     {
         var first = _queue.FirstOrDefault(x => x.Command.Command.CommandId == command.Command.CommandId);
         if (first != null)
             first.StartedAt = DateTime.MinValue;
     }
 }
コード例 #6
0
 /// <summary>
 /// Enqueue a command
 /// </summary>
 /// <param name="command">Get the command which was </param>
 public void Add(DispatchCommand command)
 {
     using (var session = _documentStore.OpenSession())
     {
         Diagnostics(command.Command.CommandId, session, "Add");
         var cmd = new StoredCommand(command);
         cmd.ProcessedAt = DateTime.Now;
         session.Store(cmd);
         session.SaveChanges();
     }
 }
コード例 #7
0
        public void Enqueue()
        {
            var dispatcher = new AsyncHandler(1);
            var context = new DownContext(null, null);
            var state = new DispatchCommand(new FakeCommand());

            dispatcher.HandleDownstream(context, state);

            Assert.True(context.WaitDown(TimeSpan.FromSeconds(1)));
            Assert.Same(state, context.Message);
        }
コード例 #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandAborted" /> class.
 /// </summary>
 /// <param name="message">The message that was aborted.</param>
 /// <param name="exception">The exception which aborted the dispatching.</param>
 /// <exception cref="System.ArgumentNullException">The fields are required</exception>
 public CommandAborted(DispatchCommand message, Exception exception)
 {
     if (message == null)
     {
         throw new ArgumentNullException("message");
     }
     if (exception == null)
     {
         throw new ArgumentNullException("exception");
     }
     Message   = message;
     Exception = exception;
 }
コード例 #9
0
        public void DispatchTwo_SingleWorker()
        {
            var dispatcher = new AsyncHandler(1);
            var state1 = new DispatchCommand(new FakeCommand());
            var state2 = new DispatchCommand(new FakeCommand());
            var context = new DownContext(null, null);

            dispatcher.HandleDownstream(context, state1);
            dispatcher.HandleDownstream(context, state2);

            Assert.True(context.WaitDown(TimeSpan.FromSeconds(1)));
            Assert.IsType<DispatchCommand>(context.Message);
        }
コード例 #10
0
        /// <summary>
        /// Re add a command which we've tried to invoke but failed.
        /// </summary>
        /// <param name="command">Command to add</param>
        public void Update(DispatchCommand command)
        {
            using (var session = _documentStore.OpenSession())
            {
                var cmd = session.Load<StoredCommand>(command.Command.CommandId);
                if (cmd == null)
                    throw new InvalidOperationException("Failed to find command with id: " + command.Command.CommandId);

                cmd.Command = command.Command;
                cmd.Attempts = command.Attempts;
                cmd.ProcessedAt = null;
                session.Store(command);
                session.SaveChanges();
            }
        }
コード例 #11
0
        public void Dispatch()
        {
            var root = Substitute.For<IRootContainer>();
            var child = Substitute.For<IScopedContainer>();
            var handler = Substitute.For<IHandleCommand<FakeCommand>>();
            root.CreateScope().Returns(child);
            child.Resolve<IHandleCommand<FakeCommand>>().Returns(handler);
            var context = Substitute.For<IDownstreamContext>();
            var dispatcher = new IocDispatcher(root);
            var msg = new DispatchCommand(new FakeCommand());

            dispatcher.HandleDownstream(context, msg);

            handler.Received().Invoke((FakeCommand) msg.Command);
            context.DidNotReceive().SendUpstream(Arg.Any<CommandFailed>());
        }
コード例 #12
0
        public void CommandFailed_SendUpstream()
        {
            var root = Substitute.For<IRootContainer>();
            var child = Substitute.For<IScopedContainer>();
            var handler = new BlockingHandler<FakeCommand>(x => { throw new Exception(); });
            root.CreateScope().Returns(child);
            child.Resolve<IHandleCommand<FakeCommand>>().Returns(handler);
            var context = Substitute.For<IDownstreamContext>();
            var dispatcher = new IocDispatcher(root);
            var msg = new DispatchCommand(new FakeCommand());

            dispatcher.HandleDownstream(context, msg);

            context.Received().SendUpstream(Arg.Any<CommandFailed>());
            Assert.Equal(1, msg.Attempts);
        }
コード例 #13
0
        public void OnlyOneWorker_TriggerThroughQueue()
        {
            var dispatcher = new AsyncHandler(1);
            var state1 = new DispatchCommand(new FakeCommand());
            var state2 = new DispatchCommand(new FakeCommand());
            var evt = new ManualResetEvent(false);
            var context = new DownContext(x => evt.WaitOne(), null);

            dispatcher.HandleDownstream(context, state1);
            Assert.True(context.WaitDown(TimeSpan.FromMilliseconds(50)));
            context.ResetDown();

            dispatcher.HandleDownstream(context, state2);
            Assert.False(context.WaitDown(TimeSpan.FromMilliseconds(50)));
            evt.Set();

            Assert.True(context.WaitDown(TimeSpan.FromMilliseconds(50)));
        }
コード例 #14
0
        public void ShutDown_NoMoreDispatching()
        {
            var dispatcher = new AsyncHandler(2);
            var command = new FakeCommand();
            var state = new DispatchCommand(command);
            var state2 = new DispatchCommand(new FakeCommand());
            var context = new DownContext(null, null);

            // dispatch first and check that it's passed by properly
            dispatcher.HandleDownstream(context, state);
            Assert.True(context.WaitDown(TimeSpan.FromMilliseconds(50)));
            context.ResetDown();

            dispatcher.Close();

            dispatcher.HandleDownstream(context, state2);
            Assert.False(context.WaitDown(TimeSpan.FromMilliseconds(50)));
        }
コード例 #15
0
        public void ExceptionHandler()
        {
            var sync = new ManualResetEvent(false);
            var expected = new Exception("Work not made");
            var dispatcher = new AsyncHandler(2);
            object msg = null;
            var context = new DownContext(y => { throw expected; }, x =>
                {
                    msg = x;
                    sync.Set();
                });

            var state1 = new DispatchCommand(new FakeCommand());

            dispatcher.HandleDownstream(context, state1);

            Assert.True(sync.WaitOne(TimeSpan.FromMilliseconds(100)));
            Assert.NotNull(msg);
            Assert.IsType<PipelineFailure>(msg);
            Assert.Same(expected, ((PipelineFailure) msg).Exception);
        }
コード例 #16
0
 /// <summary>
 /// Enqueue a command
 /// </summary>
 /// <param name="command">Get the command which was </param>
 public void Add(DispatchCommand command)
 {
     if (command == null) throw new ArgumentNullException("command");
     _queue.Enqueue(command);
 }
コード例 #17
0
        public void TwoWorkers_DispatchTwoThreads()
        {
            var dispatcher = new AsyncHandler(2);
            var state1 = new DispatchCommand(new FakeCommand());
            var state2 = new DispatchCommand(new FakeCommand());
            var evt = new ManualResetEvent(false);
            var context = new DownContext(null, x => evt.WaitOne());

            dispatcher.HandleDownstream(context, state1);
            Assert.True(context.WaitDown(TimeSpan.FromMilliseconds(100)));
            context.ResetDown();

            dispatcher.HandleDownstream(context, state2);
            Assert.True(context.WaitDown(TimeSpan.FromMilliseconds(100)));
        }
コード例 #18
0
        private void EnqueueCommand(DispatchCommand dispatchCmd)
        {
            _commands.Enqueue(dispatchCmd);

            if (_closing)
                return;

            StartWorker();
        }
コード例 #19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandCompleted" /> class.
 /// </summary>
 /// <param name="message">Message for the command that was completed.</param>
 /// <exception cref="System.ArgumentNullException">message</exception>
 public CommandCompleted(DispatchCommand message)
 {
     if (message == null) throw new ArgumentNullException("message");
     Message = message;
 }
コード例 #20
0
 /// <summary>
 /// Re add a command which we've tried to invoke but failed.
 /// </summary>
 /// <param name="command">Command to add</param>
 public void Update(DispatchCommand command)
 {
 }