Example #1
0
        public void Can_execute_nested_Normal_command_from_Normal_parent_on_same_thread()
        {
            int nestedCommandThreadId = -1;
            int parentCommandThreadId = -1;

            var nestedCommandExecuted = false;
            var nestedCommand         = new TestCommand(commandHandler, "nested", CommandExecutionMode.Normal, () =>
            {
                nestedCommandThreadId = Thread.CurrentThread.ManagedThreadId;
                nestedCommandExecuted = true;
            });

            commandHandler.RegisterCommand(nestedCommand.Command);
            var parentCommand = new TestCommand(commandHandler, "cmd1", CommandExecutionMode.Normal, () =>
            {
                parentCommandThreadId = Thread.CurrentThread.ManagedThreadId;
                commandHandler.InvokeSyntax(",nested");
            });

            commandHandler.RegisterCommand(parentCommand.Command);

            commandHandler.InvokeSyntax(",cmd1");
            parentCommand.WaitForInitialization();
            nestedCommand.WaitForAdditionalAction();

            nestedCommandExecuted.Should().BeTrue();
            commandHandler.RunningCommands.Select(c => c.Name).Should().Contain("cmd1");
            commandHandler.RunningCommands.Select(c => c.Name).Should().NotContain("nested");
            nestedCommandThreadId.Should().NotBe(-1);
            parentCommandThreadId.Should().NotBe(-1);
            nestedCommandThreadId.Should().Be(parentCommandThreadId);

            nestedCommand.Finish();
            parentCommand.Finish();
        }
Example #2
0
        public void Can_list_Normal_commands_executing_Normal_nested_command_multipletimes()
        {
            var nestedCommand = new TestCommand(commandHandler, "nested", CommandExecutionMode.Normal, () => { });

            commandHandler.RegisterCommand(nestedCommand.Command);
            var command = new TestCommand(commandHandler, "cmd1", () => commandHandler.InvokeSyntax(",nested"));

            commandHandler.RegisterCommand(command.Command);

            for (int i = 0; i < 100; i++)
            {
                nestedCommand.Reset();
                command.Reset();

                commandHandler.InvokeSyntax(",cmd1");
                nestedCommand.WaitForAdditionalAction();

                commandHandler.RunningCommands.Select(x => x.Name).Should().Contain("cmd1");
                commandHandler.RunningCommands.Select(x => x.Name).Should().NotContain("nested");

                nestedCommand.Finish();
                command.Finish();
                command.WaitForFinished();

                commandHandler.RunningCommands.Should().BeEmpty();
            }
        }
Example #3
0
        public void Can_terminate_all_running_commands_multiple_times()
        {
            var command1 = new TestCommand(commandHandler, "cmd1", () => DoSomeCancellableAction());

            commandHandler.RegisterCommand(command1.Command);

            var command2 = new TestCommand(commandHandler, "cmd2", () => DoSomeCancellableAction());

            commandHandler.RegisterCommand(command2.Command);

            for (var i = 0; i < 10; i++)
            {
                command1.Reset();
                command2.Reset();
                commandHandler.InvokeSyntax(",cmd1");
                command1.WaitForInitialization();
                commandHandler.InvokeSyntax(",cmd2");
                command2.WaitForInitialization();

                commandHandler.RunningCommands.Should().NotBeEmpty();

                commandHandler.BeginTerminate();

                command1.Finish();
                command1.WaitForFinished().Should().BeTrue();
                command2.Finish();
                command2.WaitForFinished().Should().BeTrue();

                commandHandler.RunningCommands.Should().BeEmpty();
            }
        }
Example #4
0
        public void Exception_thrown_from_nested_command_propagates_to_parent_command()
        {
            bool nestedCommandExecuted = false;
            var  nestedCommand         = new TestCommand(commandHandler, "nested", CommandExecutionMode.Normal, () =>
            {
                nestedCommandExecuted = true;
                throw new InvalidOperationException();
            });

            commandHandler.RegisterCommand(nestedCommand.Command);

            bool exceptionThrownFromNestedCommand = false;
            var  parentCommand = new TestCommand(commandHandler, "parent", () =>
            {
                try
                {
                    commandHandler.InvokeSyntax(",nested");
                }
                catch (InvalidOperationException)
                {
                    exceptionThrownFromNestedCommand = true;
                }
            });

            commandHandler.RegisterCommand(parentCommand.Command);

            nestedCommand.Finish();
            parentCommand.Finish();
            commandHandler.InvokeSyntax(",parent");

            parentCommand.WaitForFinished();

            nestedCommandExecuted.Should().BeTrue();
            exceptionThrownFromNestedCommand.Should().BeTrue();
        }
Example #5
0
        public void Can_execute_nested_command_When_AlwaysParallel()
        {
            var nestedCommandExecuted = false;
            var nestedCommand         = new TestCommand(commandHandler, "nested", CommandExecutionMode.AlwaysParallel, () => nestedCommandExecuted = true);

            commandHandler.RegisterCommand(nestedCommand.Command);
            var command = new TestCommand(commandHandler, "cmd1", () => commandHandler.InvokeSyntax(",nested"));

            commandHandler.RegisterCommand(command.Command);

            nestedCommand.Reset();
            command.Reset();
            commandHandler.InvokeSyntax(",cmd1");
            command.WaitForInitialization();
            nestedCommand.WaitForInitialization();
            nestedCommand.WaitForAdditionalAction();

            nestedCommandExecuted.Should().BeTrue();
            commandHandler.RunningCommands.Select(c => c.Name).Should().Contain("cmd1");
            commandHandler.RunningCommands.Select(c => c.Name).Should().Contain("nested");

            nestedCommand.Finish();
            command.Finish();
            nestedCommand.WaitForFinished();
            command.WaitForFinished();
        }
Example #6
0
        public void Can_execute_Direct_command_on_caller_thread()
        {
            int commandThread = -1;
            var command       = new TestCommand(commandHandler, "cmd1", CommandExecutionMode.Direct, () => commandThread = Thread.CurrentThread.ManagedThreadId);

            commandHandler.RegisterCommand(command.Command);
            command.Finish();
            commandHandler.InvokeSyntax(",cmd1");
            command.WaitForAdditionalAction();

            commandThread.Should().Be(Thread.CurrentThread.ManagedThreadId);
        }
Example #7
0
        public void Can_terminate_only_specific_running_command()
        {
            var command = new TestCommand(commandHandler, "cmd", () => DoSomeCancellableAction());

            commandHandler.RegisterCommand(command.Command);
            commandHandler.InvokeSyntax(",cmd");
            command.WaitForInitialization();

            commandHandler.Terminate("non_existing_command");
            command.Finish();

            command.WaitForFinished(TimeSpan.FromMilliseconds(50)).Should().BeFalse();
        }
Example #8
0
        public void Can_tell_whether_a_command_is_running()
        {
            var command1 = new TestCommand("cmd1");

            commandHandler.RegisterCommand(command1.Command);
            commandHandler.InvokeSyntax(",cmd1");

            command1.WaitForInitialization();

            commandHandler.IsCommandRunning("cmd1").Should().BeTrue("cmd1 command is running");
            commandHandler.IsCommandRunning("cmd2").Should().BeFalse("cmd2 command is not running");

            command1.Finish();
        }
Example #9
0
        public void Cannot_execute_same_commands_in_parallel()
        {
            int executionCount = 0;

            var command = new TestCommand(commandHandler, "cmd1", () => executionCount++);

            commandHandler.RegisterCommand(command.Command);
            commandHandler.InvokeSyntax(",cmd1");
            commandHandler.InvokeSyntax(",cmd1");

            command.Finish();
            command.WaitForFinished();

            executionCount.Should().Be(1);
        }
Example #10
0
        public void Can_terminate_Normal_command_with_custom_CancellationTokenSource()
        {
            var command = new TestCommand(commandHandler, "cmd1", CommandExecutionMode.Normal, () => { DoSomeCancellableAction(); });

            commandHandler.RegisterCommand(command.Command);
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

            commandHandler.InvokeSyntax(",cmd1", null, cancellationTokenSource);
            cancellationTokenSource.Cancel();

            command.Finish();

            command.WaitForFinished().Should().BeTrue();
            commandHandler.RunningCommands.Should().BeEmpty();
        }
Example #11
0
        public void Can_terminate_Direct_command_with_custom_CancellationTokenSource()
        {
            var command = new TestCommand(commandHandler, "cmd1", CommandExecutionMode.Direct, () => { DoSomeCancellableAction(); });

            commandHandler.RegisterCommand(command.Command);
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

            // ReSharper disable once MethodSupportsCancellation
            Task.Run(() => commandHandler.InvokeSyntax(",cmd1", null, cancellationTokenSource));

            command.Finish();
            cancellationTokenSource.Cancel();

            command.WaitForFinished().Should().BeTrue();
            commandHandler.RunningCommands.Should().BeEmpty();
        }
Example #12
0
        public void Can_call_Terminate_for_Direct_command_without_custom_CancellationTokenSource()
        {
            var command = new TestCommand(commandHandler, "cmd1", CommandExecutionMode.Direct, () => { });

            commandHandler.RegisterCommand(command.Command);

            // ReSharper disable once MethodSupportsCancellation
            Task.Run(() => commandHandler.InvokeSyntax(",cmd1"));
            command.WaitForAdditionalAction();

            commandHandler.BeginTerminate();

            commandHandler.CommandNames.Should().Contain("cmd1", "Direct command without custom cancellation token cannot be terminated, to support special commands like ,terminate.");

            command.Finish();
        }
Example #13
0
        public void Can_remove_finished_command_from_list()
        {
            var finishedCommand = new TestCommand(commandHandler, "finished_cmd", () => { });

            commandHandler.RegisterCommand(finishedCommand.Command);
            commandHandler.InvokeSyntax(",finished_cmd");
            finishedCommand.Finish();
            finishedCommand.WaitForFinished();

            var runningCommand = new TestCommand("running_cmd");

            commandHandler.RegisterCommand(runningCommand.Command);
            commandHandler.InvokeSyntax(",running_cmd");
            runningCommand.WaitForInitialization();

            commandHandler.RunningCommands.Select(c => c.Name).Should().NotContain("finished_cmd");
            commandHandler.RunningCommands.Select(c => c.Name).Should().Contain("running_cmd");
        }
Example #14
0
        public void TerminateAll_terminates_normal_commands()
        {
            var command = new TestCommand(commandHandler, "normalcmd", CommandExecutionMode.Normal, () =>
            {
                DoSomeCancellableAction();
            });

            commandHandler.RegisterCommand(command.Command);

            commandHandler.InvokeSyntax(",normalcmd");
            command.WaitForInitialization();
            command.Finish();
            commandHandler.TerminateAll();

            command.WaitForFinished(TimeSpan.FromMilliseconds(100)).Should()
            .BeTrue("normal command should not run after non-specific terminate all");
            commandHandler.RunningCommands.Select(x => x.Name).Should().NotContain("normalcmd");
        }
Example #15
0
        public void Can_list_running_commands()
        {
            var command1 = new TestCommand("cmd1");
            var command2 = new TestCommand("cmd2");

            commandHandler.RegisterCommand(command1.Command);
            commandHandler.RegisterCommand(command2.Command);
            commandHandler.InvokeSyntax(",cmd1");
            commandHandler.InvokeSyntax(",cmd2");

            command1.WaitForInitialization();
            command2.WaitForInitialization();

            commandHandler.RunningCommands.Select(c => c.Name).Should().Contain("cmd1", "cmd2");

            command1.Finish();
            command2.Finish();
        }
Example #16
0
        public void Specific_Terminate_terminates_background_command()
        {
            var command = new TestCommand(commandHandler, "backgroundcmd", CommandExecutionMode.Background, () =>
            {
                DoSomeCancellableAction();
            });

            commandHandler.RegisterCommand(command.Command);

            commandHandler.InvokeSyntax(",backgroundcmd");
            command.WaitForInitialization();
            command.Finish();
            commandHandler.Terminate("backgroundcmd");

            command.WaitForFinished(TimeSpan.FromMilliseconds(100)).Should()
            .BeTrue("background command didn't finish");
            commandHandler.RunningCommands.Select(x => x.Name).Should().NotContain("backgroundcmd");
        }
Example #17
0
        public void Non_specific_Terminate_does_not_terminate_background_commands()
        {
            var command = new TestCommand(commandHandler, "backgroundcmd", CommandExecutionMode.Background, () =>
            {
                DoSomeCancellableAction();
            });

            commandHandler.RegisterCommand(command.Command);

            commandHandler.InvokeSyntax(",backgroundcmd");
            command.WaitForInitialization();
            command.Finish();
            commandHandler.BeginTerminate();

            command.WaitForFinished(TimeSpan.FromMilliseconds(100)).Should()
            .BeFalse("background command should still run after non-specific terminate");
            commandHandler.RunningCommands.Select(x => x.Name).Should().Contain("backgroundcmd");
        }
Example #18
0
        public void Can_terminate_parent_command_executing_Normal_nested_command()
        {
            var nestedCommand = new TestCommand(commandHandler, "nested", CommandExecutionMode.Normal, () => DoSomeCancellableAction());

            commandHandler.RegisterCommand(nestedCommand.Command);

            var parentCommand = new TestCommand(commandHandler, "parent", () => commandHandler.InvokeSyntax(",nested"));

            commandHandler.RegisterCommand(parentCommand.Command);

            commandHandler.InvokeSyntax(",parent");
            parentCommand.WaitForInitialization();

            commandHandler.Terminate("parent");

            nestedCommand.Finish();
            parentCommand.Finish();

            parentCommand.WaitForFinished().Should().BeTrue();

            commandHandler.RunningCommands.Should().BeEmpty();
        }
Example #19
0
        public void Can_execute_nested_Normal_command_from_Direct_parent()
        {
            var nestedCommandExecuted = false;
            var nestedCommand         = new TestCommand(commandHandler, "nested", CommandExecutionMode.Normal,
                                                        () =>
            {
                nestedCommandExecuted = true;
                commandHandler.RunningCommands.Select(c => c.Name).Should().Contain("parent");
                commandHandler.RunningCommands.Select(c => c.Name).Should().NotContain("nested");
            });

            commandHandler.RegisterCommand(nestedCommand.Command);
            var parentCommand = new TestCommand(commandHandler, "parent", CommandExecutionMode.Direct,
                                                () => commandHandler.InvokeSyntax(",nested"));

            commandHandler.RegisterCommand(parentCommand.Command);

            nestedCommand.Finish();
            parentCommand.Finish();
            commandHandler.InvokeSyntax(",parent");

            nestedCommandExecuted.Should().BeTrue();
        }