public async Task CleanupTestResultsForSpecificationProviders_GivenCurrentTestResults_ThenCallsDelete() { //Arrange TestScenarioResult testScenarioResult = CreateTestScenarioResult(); ITestResultsRepository testResultsRepository = CreateTestResultsRepository(); testResultsRepository .GetCurrentTestResults(Arg.Any <IEnumerable <string> >(), Arg.Is <string>(testScenarioResult.Specification.Id)) .Returns(new TestScenarioResult[] { testScenarioResult }); TestResultsService service = CreateTestResultsService(testResultsRepository: testResultsRepository); SpecificationProviders specificationProviders = new SpecificationProviders { SpecificationId = testScenarioResult.Specification.Id, Providers = new string[] { testScenarioResult.Provider.Id } }; Dictionary <string, string> properties = new Dictionary <string, string> { { "specificationId", testScenarioResult.Specification.Id }, { "sfa-correlationId", Guid.NewGuid().ToString() } }; Message message = new Message { Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(specificationProviders)) }; message.UserProperties["specificationId"] = testScenarioResult.Specification.Id; //Act await service.CleanupTestResultsForSpecificationProviders(message); //Assert await testResultsRepository .Received(1) .DeleteCurrentTestScenarioTestResults(Arg.Is <IEnumerable <TestScenarioResult> >(x => x.First().Provider.Id == testScenarioResult.Provider.Id)); }
public async Task CleanupTestResultsForSpecificationProviders(Message message) { string specificationId = message.UserProperties["specificationId"].ToString(); Models.Results.SpecificationProviders specificationProviders = message.GetPayloadAsInstanceOf <Models.Results.SpecificationProviders>(); IEnumerable <TestScenarioResult> testScenarioResults = await _testResultsPolicy .ExecuteAsync(() => _testResultsRepository.GetCurrentTestResults(specificationProviders.Providers, specificationId) ); if (testScenarioResults.Any()) { _logger.Information($"Removing {specificationProviders.Providers.Count()} from test results for specification {specificationId}"); await _testResultsPolicy.ExecuteAsync(() => _testResultsRepository.DeleteCurrentTestScenarioTestResults(testScenarioResults)); SearchResults <TestScenarioResultIndex> indexItems = await _testResultsSearchPolicy .ExecuteAsync(() => _searchRepository.Search(string.Empty, new SearchParameters { Top = testScenarioResults.Count(), SearchMode = SearchMode.Any, Filter = $"specificationId eq '{specificationId}' and (" + string.Join(" or ", testScenarioResults.Select(m => $"providerId eq '{m.Provider.Id}'")) + ")", QueryType = QueryType.Full } ) ); await _testResultsSearchPolicy.ExecuteAsync(() => _searchRepository.Remove(indexItems?.Results.Select(m => m.Result))); } }
public async Task RunTests(Message message) { Stopwatch runTestsStopWatch = Stopwatch.StartNew(); string specificationId = message.UserProperties["specificationId"].ToString(); if (string.IsNullOrWhiteSpace(specificationId)) { _logger.Error("Null or empty specification id provided"); return; } BuildProject buildProject = await _builProjectsRepositoryPolicy.ExecuteAsync(() => _buildProjectRepository.GetBuildProjectBySpecificationId(specificationId)); if (buildProject == null) { _logger.Error("A null build project was provided to UpdateAllocations"); throw new ArgumentNullException(nameof(buildProject)); } string cacheKey = message.UserProperties["providerResultsCacheKey"].ToString(); if (string.IsNullOrWhiteSpace(cacheKey)) { _logger.Error("Null or empty cache key provided"); return; } Stopwatch providerResultsQueryStopwatch = Stopwatch.StartNew(); IEnumerable <ProviderResult> providerResults = await _cacheProviderPolicy.ExecuteAsync(() => _cacheProvider.GetAsync <List <ProviderResult> >($"{CacheKeys.ProviderResultBatch}{cacheKey}")); providerResultsQueryStopwatch.Stop(); if (providerResults.IsNullOrEmpty()) { _logger.Error($"No provider results found in cache for key: {cacheKey}"); return; } await _cacheProviderPolicy.ExecuteAsync(() => _cacheProvider.RemoveAsync <List <ProviderResult> >($"{CacheKeys.ProviderResultBatch}{cacheKey}")); Stopwatch testScenariosStopwatch = Stopwatch.StartNew(); IEnumerable <TestScenario> testScenarios = await _scenariosRepositoryPolicy.ExecuteAsync(() => _scenariosRepository.GetTestScenariosBySpecificationId(specificationId)); testScenariosStopwatch.Stop(); if (testScenarios.IsNullOrEmpty()) { _logger.Warning($"No test scenarios found for specification id: {specificationId}"); return; } Stopwatch specificationLookupStopwatch = Stopwatch.StartNew(); SpecificationSummary specification = await _specificationRepositoryPolicy.ExecuteAsync(() => _specificationRepository.GetSpecificationSummaryById(specificationId)); specificationLookupStopwatch.Stop(); if (specification == null) { _logger.Error($"No specification found for specification id: {specificationId}"); return; } IEnumerable <string> providerIds = providerResults.Select(m => m.Provider.Id); Stopwatch providerSourceDatasetsStopwatch = Stopwatch.StartNew(); IEnumerable <ProviderSourceDataset> sourceDatasets = await _providerSourceDatasetsRepositoryPolicy.ExecuteAsync(() => _providerSourceDatasetsRepository.GetProviderSourceDatasetsByProviderIdsAndSpecificationId(providerIds, specificationId)); providerSourceDatasetsStopwatch.Stop(); if (sourceDatasets.IsNullOrEmpty()) { _logger.Error($"No source datasets found for specification id: {specificationId}"); return; } byte[] assembly = await _calculationsRepository.GetAssemblyBySpecificationId(specificationId); if (assembly.IsNullOrEmpty()) { _logger.Error($"No assemblyfor specification id: {specificationId}"); return; } buildProject.Build.Assembly = assembly; Stopwatch existingTestResultsStopwatch = Stopwatch.StartNew(); IEnumerable <TestScenarioResult> testScenarioResults = await _testResultsRepositoryPolicy.ExecuteAsync(() => _testResultsRepository.GetCurrentTestResults(providerIds, specificationId)); existingTestResultsStopwatch.Stop(); Stopwatch runTestsStopwatch = Stopwatch.StartNew(); IEnumerable <TestScenarioResult> results = await _testEngine.RunTests(testScenarios, providerResults, sourceDatasets, testScenarioResults.ToList(), specification, buildProject); runTestsStopwatch.Stop(); Stopwatch saveResultsStopwatch = new Stopwatch(); if (results.Any()) { saveResultsStopwatch.Start(); HttpStatusCode status = await _testResultsService.SaveTestProviderResults(results, providerResults); saveResultsStopwatch.Stop(); if (!status.IsSuccess()) { _logger.Error($"Failed to save test results with status code: {status.ToString()}"); } } runTestsStopWatch.Stop(); IDictionary <string, double> metrics = new Dictionary <string, double>() { { "tests-run-totalMs", runTestsStopWatch.ElapsedMilliseconds }, { "tests-run-testScenarioQueryMs", testScenariosStopwatch.ElapsedMilliseconds }, { "tests-run-numberOfTestScenarios", testScenarios.Count() }, { "tests-run-providersResultsQueryMs", providerResultsQueryStopwatch.ElapsedMilliseconds }, { "tests-run-totalProvidersProcessed", providerIds.Count() }, { "tests-run-specificationQueryMs", specificationLookupStopwatch.ElapsedMilliseconds }, { "tests-run-providerSourceDatasetsQueryMs", providerSourceDatasetsStopwatch.ElapsedMilliseconds }, { "tests-run-existingTestsQueryMs", existingTestResultsStopwatch.ElapsedMilliseconds }, { "tests-run-existingTestScenarioResultsTotal", testScenarioResults.Count() }, { "tests-run-runTestsMs", runTestsStopwatch.ElapsedMilliseconds }, }; if (results.Any()) { metrics.Add("tests-run-saveTestResultsMs", saveResultsStopwatch.ElapsedMilliseconds); metrics.Add("tests-run-numberOfSavedResults", results.Count()); } _telemetry.TrackEvent("RunTests", new Dictionary <string, string>() { { "specificationId", specificationId }, { "buildProjectId", buildProject.Id }, { "cacheKey", cacheKey }, }, metrics ); }