Ejemplo n.º 1
0
        public void WaitForExitPollsAllowsExecutableToContinueAfterTimeoutIfCpuOrIOActivity()
        {
            // Arrange
            var idleTimeout = TimeSpan.MinValue;
            var tracer      = new Mock <ITracer>(MockBehavior.Strict);
            var idleManager = new IdleManager(idleTimeout, tracer.Object);
            var process     = new Mock <IProcess>(MockBehavior.Strict);

            // Setup
            int num = 10;

            process.SetupGet(f => f.Name)
            .Returns("Test-Process");
            process.Setup(f => f.WaitForExit(It.IsAny <TimeSpan>()))
            .Returns(() => -- num == 0);
            process.Setup(f => f.GetTotalProcessorTime(It.IsAny <ITracer>()))
            .Returns(() =>
            {
                Thread.Sleep(10);
                idleManager.UpdateActivity();
                return(TimeSpan.FromSeconds(5));
            })
            .Verifiable();
            process.Setup(f => f.WaitUntilEOF())
            .Verifiable();
            tracer.Setup(t => t.Trace(It.IsAny <string>(), It.IsAny <IDictionary <string, string> >()))
            .Verifiable();

            // Act
            idleManager.WaitForExit(process.Object);

            // Assert
            process.Verify();
            Assert.Equal(0, num);
        }
Ejemplo n.º 2
0
        public CommandResult ExecuteCommand(string command, string workingDirectory)
        {
            var idleManager = new IdleManager(_settings.GetCommandIdleTimeout(), _tracer);
            var result      = new CommandResult();

            int exitCode      = 0;
            var outputBuilder = new StringBuilder();
            var errorBuilder  = new StringBuilder();

            Action <CommandEvent> handler = args =>
            {
                idleManager.UpdateActivity();
                switch (args.EventType)
                {
                case CommandEventType.Output:
                    outputBuilder.AppendLine(args.Data);
                    break;

                case CommandEventType.Error:
                    errorBuilder.AppendLine(args.Data);
                    break;

                case CommandEventType.Complete:
                    exitCode = args.ExitCode;
                    break;

                default:
                    break;
                }
            };

            try
            {
                // Code reuse is good
                CommandEvent += handler;

                ExecuteCommandAsync(command, workingDirectory);
            }
            finally
            {
                CommandEvent -= handler;
            }

            idleManager.WaitForExit(_executingProcess);

            result.Output   = outputBuilder.ToString();
            result.Error    = errorBuilder.ToString();
            result.ExitCode = exitCode;

            return(result);
        }
Ejemplo n.º 3
0
        public void WaitForExitPollsAllowsExecutableToContinueAfterTimeoutIfIOActivity()
        {
            // Arrange
            var idleTimeout = TimeSpan.MinValue;
            var tracer      = new Mock <ITracer>(MockBehavior.Strict);
            var idleManager = new IdleManager(idleTimeout, tracer.Object);
            var process     = new Mock <IProcess>(MockBehavior.Strict);

            // Setup
            int num = 10;

            process.SetupGet(f => f.Name)
            .Returns("Test-Process");
            process.Setup(f => f.WaitForExit(It.IsAny <TimeSpan>()))
            .Returns(() =>
            {
                if (--num == 0)
                {
                    return(true);
                }
                else
                {
                    Thread.Sleep(10);
                    idleManager.UpdateActivity();
                    return(false);
                }
            });
            process.Setup(f => f.WaitUntilEOF())
            .Verifiable();

            // Act
            idleManager.WaitForExit(process.Object);

            // Assert
            process.Verify();
            Assert.Equal(0, num);
        }