public void CancelCommand_CommandIsBlockedAndShouldFailIfBlocked_CommandFails()
        {
            var resetEvent = new ManualResetEventSlim(false);

            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Returns(true);
            var processor = new CommandProcessor(null, fakeFilterManager);
            var command = new TestCommand(CommandState.New, blockCanExecute: true, shouldFailIfBlocked:true);

            command.RegisterForStateChange(Observer.Create<CommandState>(b =>
            {
                if (b == CommandState.Failed)
                {
                    resetEvent.Set();
                }
            }));
            processor.PublishCommand(command);
            resetEvent.Wait();

            Assert.AreEqual(CommandState.Failed, command.CurrentState);
        }
        //   [TestMethod]
        ///TODO: Fix this test!
        public void PublishOrderedCommands_TwoCommandsAreSendInOrder_CommandsAreExecutedInOrder()
        {
            var resetEvent = new ManualResetEventSlim(false);
            var commandsCompleted = new bool[2];

            var filterManager = new FilterManager();
            var processor = new CommandProcessor(null, filterManager);
            var command = new TestCommand(CommandState.New, shouldCompleteAfterExecute:false) { Order = 13};

            command.RegisterForStateChange(Observer.Create<CommandState>(b =>
            {
                if (b == CommandState.Successed)
                {
                    commandsCompleted[0] = true;
                }
            }));

            var command2 = new TestCommand(CommandState.New, shouldCompleteAfterExecute: true) { Order = 2 };

            command2.RegisterForStateChange(Observer.Create<CommandState>(b =>
            {
                if (b == CommandState.Successed)
                {
                    commandsCompleted[1] = true;
                    resetEvent.Set();
                }
            }));
            processor.PublishOrderedCommands(new[] { command, command2 });

            resetEvent.Wait();

            Assert.IsTrue(commandsCompleted.All(f => f));
        }
        public void PublishOrderedCommands_OneCommandInCommands_ThisCommandIsExecuted()
        {
            var resetEvent = new ManualResetEventSlim(false);

            var filterManager = new FilterManager();
            var processor = new CommandProcessor(null, filterManager);
            var command = new TestCommand(CommandState.New);
            command.RegisterForStateChange(Observer.Create<CommandState>(b =>
            {
                if (b == CommandState.Executing)
                {
                    resetEvent.Set();
                }
            }));
            processor.PublishOrderedCommands(new[] {command});

            resetEvent.Wait();

            Assert.AreEqual(CommandState.Executing, command.CurrentState);
        }
        public void PublishCommand_CommandShouldFailWhenFiltered_CommandFailed()
        {
            var fakeFilterManager = A.Fake<IFilterManager>();
            var processor = new CommandProcessor(null, fakeFilterManager);

            var evt = new ManualResetEventSlim(false);
            var command = new TestCommand(CommandState.New, shouldFailIfFiltered: true);

            command.RegisterForStateChange(Observer.Create<CommandState>(b =>
            {
                if (b == CommandState.Failed)
                {
                    evt.Set();
                }
            }));

            processor.PublishCommand(command);

            evt.Wait();

            Assert.AreEqual(CommandState.Failed, command.CurrentState);
        }
        public void PublishCommand_CommandPassesFilter_CommandIsExecuted()
        {
            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Returns(true);

            var processor = new CommandProcessor(null, fakeFilterManager);

            var evt = new ManualResetEventSlim(false);
            var command = new TestCommand(CommandState.New);

            command.RegisterForStateChange(Observer.Create<CommandState>(b =>
            {
                if (b == CommandState.Executing)
                {
                    evt.Set();
                }
            }));

            processor.PublishCommand(command);

            evt.Wait();

            Assert.AreEqual(CommandState.Executing, command.CurrentState);
        }
        public void InputSource_NewMessageIsRecievedByComamnds_InterpretResponseIsCalled()
        {
            var interpretResponseCalled = false;
            var inputSource = new Subject<ProcessorInput>();
            var resetEvent = new ManualResetEventSlim(false);

            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Returns(true);
            var processor = new CommandProcessor(inputSource, fakeFilterManager);
            var command = new TestCommand(CommandState.New, interpretResponseAction: i =>
                                                                                     {
                                                                                         interpretResponseCalled = true;
                                                                                         resetEvent.Set();
                                                                                         return false;
                                                                                     });
            command.RegisterForStateChange(Observer.Create<CommandState>(b =>
            {
                if (b == CommandState.Executing)
                {
                    resetEvent.Set();
                }
            }));

            processor.PublishCommand(command);

            resetEvent.Wait();

            inputSource.OnNext(new ProcessorInput());

            resetEvent.Wait();

            Assert.IsTrue(interpretResponseCalled);
        }
        public void InputSource_CommandSucceedsDueToInput_CommandIsCompleted()
        {
            var commandCompleted = false;
            var inputSource = new Subject<ProcessorInput>();
            var resetEvent = new ManualResetEventSlim(false);

            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Returns(true);
            var processor = new CommandProcessor(inputSource, fakeFilterManager);
            var command = new TestCommand(CommandState.New, interpretResponseAction: i => true);

            command.RegisterForStateChange(Observer.Create<CommandState>(b =>
            {
                if (b == CommandState.Executing)
                {
                    resetEvent.Set();
                }
            }));

            processor.PublishCommand(command, Observer.Create<ICommandResponse<Unit>>(_ => { }, () =>
            {
                commandCompleted = true;
                resetEvent.Set();
            }));

            resetEvent.Wait();

            inputSource.OnNext(new ProcessorInput());

            resetEvent.Wait();

            Assert.IsTrue(commandCompleted);
        }
        public void InputSource_CommandFailsDueToInput_CommandIsFailed()
        {
            var commandFailed = false;
            var inputSource = new Subject<ProcessorInput>();
            var resetEvent = new ManualResetEventSlim(false);
            Exception thrownedException = null;
            var exceptionMessage = "Test";

            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Returns(true);
            var processor = new CommandProcessor(inputSource, fakeFilterManager);
            var command = new TestCommand(CommandState.New, interpretResponseAction: i =>
            {
                throw new Exception(exceptionMessage);
            });

            command.RegisterForStateChange(Observer.Create<CommandState>(b =>
            {
                if (b == CommandState.Executing)
                {
                    resetEvent.Set();
                }
            }));

            processor.PublishCommand(command, Observer.Create<ICommandResponse<Unit>>(_ => { }, ex =>
                                                                                                {
                                                                                                    thrownedException =
                                                                                                        ex;
                                                                                                    commandFailed = true;
                                                                                                    resetEvent.Set();

                                                                                                }));

            resetEvent.Wait();

            inputSource.OnNext(new ProcessorInput());

            resetEvent.Wait();

            Assert.IsTrue(commandFailed);
            Assert.AreEqual(thrownedException.Message, exceptionMessage);
        }
        public void RerunBlockedCommand_CommandIsBlocked_CommandIsPendingAgain()
        {
            var resetEvent = new ManualResetEventSlim(false);
            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Returns(true);
            var processor = new CommandProcessor(null, fakeFilterManager);
            var command = new TestCommand(CommandState.New, blockCanExecute: true);

            command.RegisterForStateChange(Observer.Create<IObservedChange<CommandBase, CommandState>>(b =>
            {
                if (b.Value == CommandState.Blocked)
                {
                    resetEvent.Set();
                }
            }));
            processor.PublishCommand(command);
            resetEvent.Wait();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Returns(false);
            command.RegisterForStateChange(Observer.Create<IObservedChange<CommandBase, CommandState>>(b =>
            {
                if (b.Value == CommandState.Pending)
                {
                    resetEvent.Set();
                }
            }));
            processor.RerunBlockedCommand(command);
            resetEvent.Wait();

            Assert.AreEqual(CommandState.Pending, command.CurrentState);
        }
        public void InputSource_CommandSucceedsDueToInputButIsExecutingForEver_CommandSuccededButStillExecuting()
        {
            var commandSucceed = false;
            var inputSource = new Subject<ProcessorInput>();
            var resetEvent = new ManualResetEventSlim(false);

            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Returns(true);
            var processor = new CommandProcessor(inputSource, fakeFilterManager);
            var command = new TestCommand(CommandState.New, interpretResponseAction: i => true, shouldExecuteForever: true);

            command.RegisterForStateChange(Observer.Create<IObservedChange<CommandBase, CommandState>>(b =>
            {
                if (b.Value == CommandState.Executing)
                {
                    resetEvent.Set();
                }
            }));

            processor.PublishCommand(command, Observer.Create<ICommandResponse<Unit>>(r =>
            {
                commandSucceed = true;
                resetEvent.Set();
            }));

            resetEvent.Wait();

            inputSource.OnNext(new ProcessorInput());

            resetEvent.Wait();

            Assert.IsTrue(commandSucceed && command.CurrentState == CommandState.Executing);
        }