Ejemplo n.º 1
0
        public void WaitForExitPollsAllowsExecutableToContinueAfterTimeoutIfCpuActivity()
        {
            // 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, cpu = 0;

            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(() => TimeSpan.FromSeconds(++cpu))
            .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 void WaitForExitPollsKillsProcessIfProcessorTimeDoesNotChangeAndNotUpdated()
        {
            // Arrange
            var      tracer      = new Mock <ITracer>(MockBehavior.Strict);
            DateTime startTime   = DateTime.UtcNow;
            TimeSpan idleTimeout = TimeSpan.FromMilliseconds(100);
            var      idleManager = new IdleManager(idleTimeout, tracer.Object);
            var      process     = new Mock <IProcess>(MockBehavior.Strict);

            // Setup
            process.SetupGet(f => f.Name)
            .Returns("Test-Process");
            process.SetupGet(f => f.Arguments)
            .Returns("");
            process.Setup(f => f.WaitForExit(It.IsAny <TimeSpan>()))
            .Returns(() => { Thread.Sleep(10); return(false); });
            process.Setup(f => f.GetTotalProcessorTime(It.IsAny <ITracer>()))
            .Returns(TimeSpan.Zero);
            process.Setup(f => f.Kill(tracer.Object))
            .Verifiable();
            tracer.Setup(t => t.Trace(It.IsAny <string>(), It.IsAny <IDictionary <string, string> >()))
            .Verifiable();

            // Act
            var ex = Assert.Throws <CommandLineException>(() => idleManager.WaitForExit(process.Object));

            // Assert
            process.Verify();

            Assert.True(DateTime.UtcNow - startTime >= idleTimeout);
            Assert.Contains("Command 'Test-Process ' aborted due to no output and CPU activity for", ex.Message);
        }
Ejemplo n.º 3
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.º 4
0
        public void WaitForExitWaitsForEOFPriorToExiting()
        {
            // Arrange
            var idleTimeout = DeploymentSettingsExtension.DefaultCommandIdleTimeout;
            var tracer      = new Mock <ITracer>(MockBehavior.Strict);
            var idleManager = new IdleManager(idleTimeout, tracer.Object);
            var process     = new Mock <IProcess>(MockBehavior.Strict);

            // Setup
            process.SetupGet(f => f.Name)
            .Returns("Test-Process");
            process.Setup(f => f.WaitForExit(It.IsAny <TimeSpan>()))
            .Returns(true)
            .Verifiable();
            process.Setup(f => f.WaitUntilEOF())
            .Verifiable();

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

            // Assert
            process.Verify();
        }
Ejemplo n.º 5
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);
        }