Exemplo n.º 1
0
        public static void IdCannotBeReused()
        {
            var database = new AgentStore();

            database.AddAgent(DummyAgent.Id, DummyProcess);
            Assert.That(() => database.AddAgent(DummyAgent.Id, DummyProcess), Throws.ArgumentException.With.Property("ParamName").EqualTo("agentId"));

            database.Register(DummyAgent);
            Assert.That(() => database.AddAgent(DummyAgent.Id, DummyProcess), Throws.ArgumentException.With.Property("ParamName").EqualTo("agentId"));

            database.MarkProcessTerminated(DummyProcess);
            Assert.That(() => database.AddAgent(DummyAgent.Id, DummyProcess), Throws.ArgumentException.With.Property("ParamName").EqualTo("agentId"));
        }
Exemplo n.º 2
0
        public static void AgentIsNotReadyWhenStarted()
        {
            var database = new AgentStore();

            database.AddAgent(DummyAgent.Id, DummyProcess);
            Assert.That(database.IsReady(DummyAgent.Id, out _), Is.False);
        }
Exemplo n.º 3
0
        public static void ConcurrentOperationsDoNotCorruptState()
        {
            var database = new AgentStore();

            RunActionConcurrently(() =>
            {
                for (var i = 0; i < 1000; i++)
                {
                    var id      = Guid.NewGuid();
                    var process = new Process();

                    Assert.That(database.IsAgentProcessActive(id, out _), Is.False);
                    Assert.That(database.IsReady(id, out _), Is.False);

                    database.AddAgent(id, process);
                    Assert.That(database.IsAgentProcessActive(id, out _), Is.True);
                    Assert.That(database.IsReady(id, out _), Is.False);

                    database.Register(new DummyTestAgent(id));
                    Assert.That(database.IsAgentProcessActive(id, out _), Is.True);
                    Assert.That(database.IsReady(id, out _), Is.True);

                    database.MarkProcessTerminated(process);
                    Assert.That(database.IsAgentProcessActive(id, out _), Is.False);
                    Assert.That(database.IsReady(id, out _), Is.False);
                }
            }, threadCount: Environment.ProcessorCount);
        }
Exemplo n.º 4
0
        public static void AgentMustNotRegisterTwice()
        {
            var database = new AgentStore();

            database.AddAgent(DummyAgent.Id, DummyProcess);
            database.Register(DummyAgent);
            Assert.That(() => database.Register(DummyAgent), Throws.ArgumentException.With.Property("ParamName").EqualTo("agent"));
        }
Exemplo n.º 5
0
        public static void AgentIsRunningWhenStarted()
        {
            var database = new AgentStore();

            database.AddAgent(DummyAgent.Id, DummyProcess);
            Assert.That(database.IsAgentProcessActive(DummyAgent.Id, out var process), Is.True);
            Assert.That(process, Is.SameAs(DummyProcess));
        }
Exemplo n.º 6
0
        public static void AgentIsNotReadyWhenTerminated()
        {
            var database = new AgentStore();

            database.AddAgent(DummyAgent.Id, DummyProcess);
            database.Register(DummyAgent);
            database.MarkProcessTerminated(DummyProcess);
            Assert.That(database.IsReady(DummyAgent.Id, out _), Is.False);
        }
Exemplo n.º 7
0
        public static void AgentIsReadyWhenRegistered()
        {
            var database = new AgentStore();

            database.AddAgent(DummyAgent.Id, DummyProcess);
            database.Register(DummyAgent);
            Assert.That(database.IsReady(DummyAgent.Id, out var registeredAgent), Is.True);
            Assert.That(registeredAgent, Is.SameAs(DummyAgent));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Return an agent, which best matches the criteria defined
        /// in a TestPackage.
        /// </summary>
        /// <param name="package">The test package to be run</param>
        /// <returns>An ITestAgent</returns>
        /// <exception cref="ArgumentException">If no agent is available.</exception>
        public ITestAgent GetAgent(TestPackage package)
        {
            // Target Runtime must be specified by this point
            string runtimeSetting = package.GetSetting(EnginePackageSettings.TargetRuntimeFramework, "");

            Guard.OperationValid(runtimeSetting.Length > 0, "LaunchAgentProcess called with no runtime specified");

            var    targetRuntime = RuntimeFramework.Parse(runtimeSetting);
            var    agentId       = Guid.NewGuid();
            string agencyUrl     = targetRuntime.FrameworkName.Identifier == ".NETFramework" ? RemotingUrl : TcpEndPoint;
            var    agentProcess  = CreateAgentProcess(agentId, agencyUrl, package);

            agentProcess.Exited += (sender, e) => OnAgentExit((Process)sender);

            Console.WriteLine(agentProcess.StartInfo.FileName);
            agentProcess.Start();
            log.Debug("Launched Agent process {0} - see testcentric-agent_{0}.log", agentProcess.Id);
            log.Debug("Command line: \"{0}\" {1}", agentProcess.StartInfo.FileName, agentProcess.StartInfo.Arguments);

            _agentStore.AddAgent(agentId, agentProcess);

            log.Debug($"Waiting for agent {agentId:B} to register");

            const int pollTime = 200;

            // Increase the timeout to give time to attach a debugger
            bool debug = package.GetSetting(EnginePackageSettings.DebugAgent, false) ||
                         package.GetSetting(EnginePackageSettings.PauseBeforeRun, false);

            int waitTime = debug ? DEBUG_TIMEOUT : NORMAL_TIMEOUT;

            // Wait for agent registration based on the agent actually getting processor time to avoid falling over
            // under process starvation.
            while (waitTime > agentProcess.TotalProcessorTime.TotalMilliseconds && !agentProcess.HasExited)
            {
                Thread.Sleep(pollTime);

                if (_agentStore.IsAvailable(agentId, out var agent))
                {
                    log.Debug($"Returning new agent {agentId:B}");

                    switch (targetRuntime.Runtime.FrameworkIdentifier)
                    {
                    case FrameworkIdentifiers.NetFramework:
                        return(new TestAgentRemotingProxy(agent, agentId));

                    case FrameworkIdentifiers.NetCoreApp:
                        return(agent);

                    default:
                        throw new InvalidOperationException($"Invalid runtime: {targetRuntime.Runtime.FrameworkIdentifier}");
                    }
                }
            }

            return(null);
        }
Exemplo n.º 9
0
        public static void AgentIsNotRunningWhenTerminated()
        {
            var database = new AgentStore();

            database.AddAgent(DummyAgent.Id, DummyProcess);
            database.Register(DummyAgent);
            database.MarkTerminated(DummyAgent.Id);
            Assert.That(database.IsAgentProcessActive(DummyAgent.Id, out _), Is.False);
        }
Exemplo n.º 10
0
        private ITestAgent CreateRemoteAgent(TestPackage package, RuntimeFramework targetRuntime)
        {
            var agentId = Guid.NewGuid();
            var process = new AgentProcess(this, package, agentId);

            process.Exited += (sender, e) => OnAgentExit((Process)sender, agentId);

            process.Start();
            log.Debug("Launched Agent process {0} - see testcentric-agent_{0}.log", process.Id);
            log.Debug("Command line: \"{0}\" {1}", process.StartInfo.FileName, process.StartInfo.Arguments);

            _agentStore.AddAgent(agentId, process);

            log.Debug($"Waiting for agent {agentId:B} to register");

            const int pollTime = 200;

            // Increase the timeout to give time to attach a debugger
            bool debug = package.GetSetting(EnginePackageSettings.DebugAgent, false) ||
                         package.GetSetting(EnginePackageSettings.PauseBeforeRun, false);

            int waitTime = debug ? DEBUG_TIMEOUT : NORMAL_TIMEOUT;

            // Wait for agent registration based on the agent actually getting processor time to avoid falling over
            // under process starvation.
            while (waitTime > process.TotalProcessorTime.TotalMilliseconds && !process.HasExited)
            {
                Thread.Sleep(pollTime);

                if (_agentStore.IsReady(agentId, out var agent))
                {
                    log.Debug($"Returning new agent {agentId:B}");

                    switch (targetRuntime.Runtime.FrameworkIdentifier)
                    {
                    case FrameworkIdentifiers.NetFramework:
                        return(new TestAgentRemotingProxy(agent, agentId));

                    case FrameworkIdentifiers.NetCoreApp:
                        return(agent);

                    default:
                        throw new InvalidOperationException($"Invalid runtime: {targetRuntime.Runtime.FrameworkIdentifier}");
                    }
                }
            }

            return(null);
        }