Beispiel #1
0
        public async Task RunTestsForCurrentAgentAsync(string testAgentTag, int testAgentRunTimeout)
        {
            if (await IsEligibleToStartTestAgentRunAsync(testAgentTag).ConfigureAwait(false))
            {
                var newTestAgentRun = await GetFirstNewTestAgentRunForCurrentTestAgentAsync().ConfigureAwait(false);

                if (newTestAgentRun != null)
                {
                    await _testRunLogService.CreateTestRunLogAsync($"Test agent with tag {testAgentTag} starts tests execution on machine {_environmentService.MachineName}.", newTestAgentRun.TestRunId).ConfigureAwait(false);

                    await _testAgentStateSwitcher.SetTestAgentAsRunningTestsAsync(testAgentTag).ConfigureAwait(false);

                    var cancellationTokenSource = new CancellationTokenSource();
                    var cancellationTokenSourceLastAvailable = new CancellationTokenSource();
                    var testAgentUpdateAvailabilityTask      = _taskProvider.StartNewLongRunningRepeating(
                        cancellationTokenSourceLastAvailable,
                        () =>
                    {
                        UpdateTestAgentLastAvailable(newTestAgentRun.TestAgentRunId);
                    },
                        15000);

                    var executeTestAgentRunTask = _taskProvider.StartNewLongRunning(
                        (c) =>
                    {
                        ExecuteTestAgentRunAsync(newTestAgentRun, testAgentRunTimeout, cancellationTokenSource).Wait();
                    },
                        cancellationTokenSource);

                    var checkTestRunnerLastAvailableTask = _taskProvider.StartNewLongRunningRepeating(
                        cancellationTokenSource,
                        () =>
                    {
                        if (executeTestAgentRunTask.IsCompleted)
                        {
                            cancellationTokenSource.Cancel();
                            return;
                        }

                        if (executeTestAgentRunTask.IsFaulted)
                        {
                            cancellationTokenSource.Cancel();
                            return;
                        }

                        CheckTestRunnerStatus(newTestAgentRun.TestAgentRunId, cancellationTokenSource);
                    },
                        15000);

                    checkTestRunnerLastAvailableTask.Wait();
                    cancellationTokenSourceLastAvailable.Cancel();
                    testAgentUpdateAvailabilityTask.Wait();
                    try
                    {
                        // DEBUG: Turn-off if debugging
                        _consoleProvider.Clear();

                        if (_wasTestAgentRunCompleted)
                        {
                            await _testRunLogService.CreateTestRunLogAsync($"Test agent with tag {testAgentTag} finished tests execution on machine {_environmentService.MachineName}.", newTestAgentRun.TestRunId).ConfigureAwait(false);

                            await _testRunLogService.CreateTestRunLogAsync($"Test agent with tag {testAgentTag} starts waiting for new jobs on machine {_environmentService.MachineName}.", newTestAgentRun.TestRunId).ConfigureAwait(false);
                        }
                        else
                        {
                            SendTestAgentRunExceptionToRunner(newTestAgentRun, executeTestAgentRunTask);

                            // TODO: Move logic to be executed on Test Agent Run Abort- extension.
                            await _testRunLogService
                            .CreateTestRunLogAsync(
                                $"Test agent with tag {testAgentTag} starts waiting for new jobs on machine {_environmentService.MachineName}.",
                                newTestAgentRun.TestRunId).ConfigureAwait(false);

                            _consoleProvider.WriteLine($"Test agent run aborted.");
                            _consoleProvider.WriteLine($"Test agent with tag {testAgentTag} starts waiting for new jobs on machine {_environmentService.MachineName}.");
                            var cts = new CancellationTokenSource();
                            var waitForTestRunToCompleteTask = _taskProvider.StartNewLongRunningRepeating(
                                cts,
                                () =>
                            {
                                if (IsTestRunCompleted(newTestAgentRun.TestRunId).Result)
                                {
                                    cts.Cancel();
                                }
                            },
                                5000);
                            waitForTestRunToCompleteTask.Wait();
                        }
                    }
                    finally
                    {
                        await _testAgentStateSwitcher.SetTestAgentAsActiveAsync(testAgentTag).ConfigureAwait(false);
                    }
                }
            }
        }
        public async Task UpdateTestAgentStatusToActive_When_CurrentTestAgentStatusIs(TestAgentStatus status)
        {
            // Arrange
            var testAgents  = TestAgentFactory.CreateWithCurrentMachineName(status, 1);
            var testAgentId = testAgents.FirstOrDefault().TestAgentId;

            _testAgentRepositoryMock.Setup(x => x.GetAllAsync()).Returns(Task.FromResult(testAgents));

            // Act
            _testAgentStateSwitcher.SetTestAgentAsActiveAsync(testAgents.First().AgentTag);

            // Assert
            _testAgentRepositoryMock.Verify(x => x.UpdateAsync(It.IsAny <int>(), It.Is <TestAgentDto>(i => i.TestAgentId == testAgentId && i.Status == TestAgentStatus.Active)), Times.Once);
            _testAgentRepositoryMock.Verify(x => x.UpdateAsync(It.IsAny <int>(), It.IsAny <TestAgentDto>()), Times.Once);
        }