Inheritance: System.CommandBase
        public void Process_CommandIsPendingWithNoFilters_ReturnTrue()
        {
            var command = new TestCommand(CommandState.Pending);

            FilterManager filterManager = new FilterManager();
            var result = filterManager.Process(command);
            Assert.IsTrue(result);
        }
        public void Process_CommandIsNotPending_ReturnFalse()
        {
            var command = new TestCommand(CommandState.New);

            FilterManager filterManager = new FilterManager();
            var result = filterManager.Process(command);
            Assert.IsFalse(result);
        }
        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);
        }
        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 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);
        }
        //   [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 PublishCommand_WithObserver_CommandIsChangedToPending()
        {
            var fakeFilterManager = A.Fake<IFilterManager>();
            var processor = new CommandProcessor(null, fakeFilterManager);
            var command = new TestCommand(CommandState.New);

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

            Assert.AreEqual(CommandState.Pending, command.CurrentState);
        }
        public void PublishCommand_FilterThrewExceptionAndCommandShouldFail_CommandFailed()
        {
            bool isFailedWithException = false;
            var expectedException = new Exception();
            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Throws(expectedException);

            var processor = new CommandProcessor(null, fakeFilterManager);

            var evt = new ManualResetEventSlim(false);
            var command = new TestCommand(CommandState.New, shouldFailIfFiltered: true);
            processor.PublishCommand(command, Observer.Create<ICommandResponse<Unit>>(_ => { }, ex =>
                                                                        {
                                                                            isFailedWithException = ex ==
                                                                                                    expectedException;
                                                                            evt.Set();

                                                                        }));
            evt.Wait();

            Assert.AreEqual(CommandState.Failed, command.CurrentState);
            Assert.IsTrue(isFailedWithException);
        }
        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 CommandPublishGeneric_CommandGotFullfillmentWithReturnValue_ReturnValue()
        {
            var resetEvent = new ManualResetEventSlim(false);
            var inputSource = new Subject<ProcessorInput>();

            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Returns(true);

            var processor = new CommandProcessor(inputSource, fakeFilterManager);
            var command = new TestCommand<bool>(CommandState.New, returnValue:true, interpretResponseAction: r => true, fullfillmentAction: i => resetEvent.Set());
            processor.PublishCommand(command);
            inputSource.OnNext(new ProcessorInput());

            resetEvent.Wait();

            Assert.IsTrue(command.ReturnValue);
        }
        public void CancelCommand_CommandIsPending_CommandIsCancled()
        {
            var resetEvent = new ManualResetEventSlim(false);

            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Returns(false);
            var processor = new CommandProcessor(null, fakeFilterManager);
            var command = new TestCommand(CommandState.New, shouldFailIfFiltered: false,
            // ReSharper disable RedundantArgumentName
                //We want to keep this verbose to clarity
                                         startRequestAction: (s, e) => resetEvent.Set());
            // ReSharper restore RedundantArgumentName
            processor.PublishCommand(command);
            resetEvent.Wait();

            processor.CancelCommand(command);

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

            processor.CancelCommand(command);

            Assert.AreEqual(CommandState.Canceled, command.CurrentState);
        }
        public void Process_CommandIsPendingWitWithOnePassingFilter_CommandNotFiltered()
        {
            var command = new TestCommand(CommandState.Pending);

            FilterManager filterManager = new FilterManager();
            var filter = new GenericFilter(c => true);
            filterManager.AddFilter(filter);
            var result = filterManager.Process(command);
            Assert.IsTrue(result);
        }
        public void Process_NonPassingFilterAddedDuringCommandProcessing_CommandIsFiltered()
        {
            var command = new TestCommand(CommandState.Pending);

            FilterManager filterManager = new FilterManager();
            ManualResetEventSlim evt = new ManualResetEventSlim(false);
            var filter = new GenericFilter(c =>
                {
                    evt.Wait();
                    return true;
                });

            filterManager.AddFilter(filter);

            Task<bool> result = Task.Factory.StartNew(() => filterManager.Process(command));

            filterManager.AddFilter(new GenericFilter(c => false));
            evt.Set();

            Assert.IsFalse(result.Result);
        }
        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 CommandPublish_CommandHasErrorAction_ActionCalled()
        {
            var actionCalled = false;
            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
                                          , errorAction: (i, ex) =>
                                                         {
                                                             actionCalled = true;
                                                             resetEvent.Set();
                                                         },
                                          executeAction: new Action(() =>
                                                                    {
                                                                        throw new Exception();
                                                                    }));
            processor.PublishCommand(command);

            resetEvent.Wait();

            Assert.IsTrue(actionCalled);
        }
        public void PublishCommand_CommandIsNotNew_ThrowsException()
        {
            bool wasThrown = false;
            Exception thrownException = null;

            var fakeFilterManager = A.Fake<IFilterManager>();
            var processor = new CommandProcessor(null, fakeFilterManager);

            var command = new TestCommand(CommandState.Canceled);

            try
            {
                processor.PublishCommand(command);
            }
            catch (CommandProcessorException ex)
            {
                thrownException = ex;
                wasThrown = true;
            }

            Assert.IsTrue(wasThrown);
            Assert.AreEqual("Command is not new", thrownException.Message);
        }
        public void CommandPublish_CommandHasFullfillmentAction_ActionCalled()
        {
            var actionCalled = false;
            var resetEvent = new ManualResetEventSlim(false);
            var inputSource = new Subject<ProcessorInput>();

            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: r => true, fullfillmentAction: i =>
                                                                                                                    {
                                                                                                                        actionCalled
                                                                                                                            =
                                                                                                                            true;
                                                                                                                        resetEvent
                                                                                                                            .
                                                                                                                            Set
                                                                                                                            ();
                                                                                                                    });
            processor.PublishCommand(command);
            inputSource.OnNext(new ProcessorInput());

            resetEvent.Wait();

            Assert.IsTrue(actionCalled);
        }
        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 CommandPublish_CommandShouldCompleteWhenAfterExecuting_CommandSucceded()
        {
            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, shouldCompleteAfterExecute: true);

            processor.PublishCommand(command, Observer.Create<ICommandResponse<Unit>>(_ => { }, resetEvent.Set));
            resetEvent.Wait();
            Assert.AreEqual(CommandState.Successed, command.CurrentState);
        }
        public void PublishCommand_FilterThrewExceptionAndCommandIsPending_CommandStillPending()
        {
            bool isPendingWithException = false;
            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Throws(new Exception());

            var processor = new CommandProcessor(null, fakeFilterManager);

            var evt = new ManualResetEventSlim(false);
            var command = new TestCommand(CommandState.New, shouldFailIfFiltered: false,
            // ReSharper disable RedundantArgumentName
            //We want to keep this verbose to clarity
                                          startRequestAction: (s, e) =>
            // ReSharper restore RedundantArgumentName
                                                              {
                                                                  isPendingWithException = (s == CommandState.Pending &&
                                                                                            e != null);
                                                                  evt.Set();
                                                              });

            processor.PublishCommand(command);

            evt.Wait();

            Assert.IsTrue(isPendingWithException);
        }
        public void CommandPublish_CommandThrowsExceptionWhileExecuting_CommandFailsWithThatException()
        {
            bool wasThrown = false;
            Exception thrownException = new Exception("Text");
            Exception expectedException = null;
            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, executeAction: () =>
                                                                           {
                                                                               throw thrownException;
                                                                           });

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

            Assert.IsTrue(wasThrown);
            Assert.AreEqual(expectedException, thrownException);
        }
        public void PublishCommand_NoObserver_CommandIsChangedToPending()
        {
            var fakeFilterManager = A.Fake<IFilterManager>();
            var processor = new CommandProcessor(null, fakeFilterManager);

            var command = new TestCommand(CommandState.New);

            processor.PublishCommand(command);

            Assert.AreEqual(CommandState.Pending, command.CurrentState);
        }
        public void CommandTimeouts_CommandHasElapsingPendingTimeout_CommandFails()
        {
            bool wasThrown = false;
            Exception thrownException = null;
            var resetEvent = new ManualResetEventSlim(false);

            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Invokes(a => Thread.Sleep(1000));

            var processor = new CommandProcessor(null, fakeFilterManager);

            var command = new TestCommand(CommandState.New, pendingTimeout:new TimeSpan(500));

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

            Assert.IsTrue(wasThrown);
            Assert.AreEqual("Command has exceded its Pending timeout", thrownException.Message);
        }
        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 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_CommandIsNewThusNotBlocked_ThrowException()
        {
            bool wasThrown = false;
            Exception thrownException = null;

            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);

            try
            {
                processor.RerunBlockedCommand(command);

            }
            catch (CommandProcessorException ex)
            {
                thrownException = ex;
                wasThrown = true;
            }

            Assert.IsTrue(wasThrown);
            Assert.AreEqual("Command is not blocked", thrownException.Message);
        }
        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);
        }