public async Task DataflowTest_CompletedEntireDataflow()
        {
            var exportCountdown  = new CountdownEvent(1);
            var convertCountdown = new CountdownEvent(1);

            var service = new TestExportService(_logger.Object, _configuration, _serviceScopeFactory.Object, _storageInfoProvider.Object);

            service.ExportDataBlockCalled += (sender, args) =>
            {
                exportCountdown.Signal();
            };
            service.ConvertDataBlockCalled += (sender, args) =>
            {
                convertCountdown.Signal();
            };

            var tasks = GenerateTaskResponse(1);

            _resultsService.Setup(p => p.GetPendingJobs(It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <int>())).Returns(Task.FromResult(tasks));
            _resultsService.Setup(p => p.ReportSuccess(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(true));
            _resultsService.Setup(p => p.ReportFailure(It.IsAny <Guid>(), It.IsAny <bool>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(true));
            _payloadsApi.Setup(p => p.Download(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new PayloadFile
            {
                Name = tasks.First().Uris.First(),
                Data = InstanceGenerator.GenerateDicomData()
            }));
            _storageInfoProvider.Setup(p => p.HasSpaceAvailableForExport).Returns(true);
            _storageInfoProvider.Setup(p => p.AvailableFreeSpace).Returns(100);

            await service.StartAsync(_cancellationTokenSource.Token);

            Assert.True(convertCountdown.Wait(3000));
            Assert.True(exportCountdown.Wait(3000));

            _resultsService.Verify(p => p.GetPendingJobs(TestExportService.AgentName, It.IsAny <CancellationToken>(), 10), Times.AtLeastOnce());
            _resultsService.Verify(p => p.ReportSuccess(It.IsAny <Guid>(), It.IsAny <CancellationToken>()), Times.Once());
            _payloadsApi.Verify(p => p.Download(tasks.First().PayloadId, tasks.First().Uris.First()), Times.Once());

            _logger.VerifyLogging($"Failed to download file {tasks.First().Uris.First()}.", LogLevel.Warning, Times.Never());
            _logger.VerifyLogging($"Failure rate exceeded threshold and will not be exported.", LogLevel.Error, Times.Never());
            _logger.VerifyLogging($"Task marked as successful.", LogLevel.Error, Times.Never());

            await StopAndVerify(service);

            _logger.VerifyLogging($"Export Service completed timer routine.", LogLevel.Trace, Times.AtLeastOnce());
            _storageInfoProvider.Verify(p => p.HasSpaceAvailableForExport, Times.AtLeastOnce());
            _storageInfoProvider.Verify(p => p.AvailableFreeSpace, Times.Never());
        }
        private HttpResponseMessage GenerateMultipartResponse()
        {
            var data    = InstanceGenerator.GenerateDicomData();
            var content = new MultipartContent("related");

            content.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("type", $"\"application/dicom\""));
            var byteContent = new StreamContent(new MemoryStream(data));

            byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/dicom");
            content.Add(byteContent);
            return(new HttpResponseMessage()
            {
                Content = content
            });
        }
Exemple #3
0
        public async Task ExportDataBlockCallback_ReturnsNullIfInferenceRequestCannotBeFound()
        {
            var service = new DicomWebExportService(
                _loggerFactory.Object,
                _httpClientFactory.Object,
                _serviceScopeFactory.Object,
                _logger.Object,
                _configuration,
                _storageInfoProvider.Object);

            var tasks = ExportServiceBaseTest.GenerateTaskResponse(1);

            _resultsService.Setup(p => p.GetPendingJobs(It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <int>())).Returns(Task.FromResult(tasks));
            _resultsService.Setup(p => p.ReportSuccess(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(true));
            _resultsService.Setup(p => p.ReportFailure(It.IsAny <Guid>(), It.IsAny <bool>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(true));
            _payloadsApi.Setup(p => p.Download(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new PayloadFile
            {
                Name = tasks.First().Uris.First(),
                Data = InstanceGenerator.GenerateDicomData()
            }));
            _inferenceRequestStore.Setup(p => p.Get(It.IsAny <string>(), It.IsAny <string>())).Returns((InferenceRequest)null);

            var dataflowCompleted = new ManualResetEvent(false);

            service.ReportActionStarted += (sender, args) =>
            {
                dataflowCompleted.Set();
            };

            await service.StartAsync(_cancellationTokenSource.Token);

            dataflowCompleted.WaitOne(5000);

            _resultsService.Verify(
                p => p.GetPendingJobs(
                    _configuration.Value.Dicom.Scu.ExportSettings.Agent,
                    It.IsAny <CancellationToken>(),
                    10), Times.Once());
            _payloadsApi.Verify(p => p.Download(tasks.First().PayloadId, tasks.First().Uris.First()), Times.AtLeastOnce());
            _logger.VerifyLogging($"The specified job cannot be found in the inference request store and will not be exported.", LogLevel.Error, Times.AtLeastOnce());
            _logger.VerifyLogging($"Task {tasks.First().TaskId} marked as failure and will not be retried.", LogLevel.Warning, Times.AtLeastOnce());

            await StopAndVerify(service);
        }
Exemple #4
0
        public async Task CompletesDataflow(HttpStatusCode httpStatusCode)
        {
            var service = new DicomWebExportService(
                _loggerFactory.Object,
                _httpClientFactory.Object,
                _serviceScopeFactory.Object,
                _logger.Object,
                _configuration,
                _storageInfoProvider.Object);

            var url = "http://my-dicom-web.site";
            var inferenceRequest = new InferenceRequest();

            inferenceRequest.OutputResources.Add(new RequestOutputDataResource
            {
                Interface         = InputInterfaceType.DicomWeb,
                ConnectionDetails = new DicomWebConnectionDetails
                {
                    AuthId   = "token",
                    AuthType = ConnectionAuthType.Bearer,
                    Uri      = url
                }
            });

            var tasks = ExportServiceBaseTest.GenerateTaskResponse(1);

            _resultsService.Setup(p => p.GetPendingJobs(It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <int>())).Returns(Task.FromResult(tasks));
            _resultsService.Setup(p => p.ReportSuccess(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(true));
            _resultsService.Setup(p => p.ReportFailure(It.IsAny <Guid>(), It.IsAny <bool>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(true));
            _payloadsApi.Setup(p => p.Download(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new PayloadFile
            {
                Name = tasks.First().Uris.First(),
                Data = InstanceGenerator.GenerateDicomData()
            }));
            _inferenceRequestStore.Setup(p => p.Get(It.IsAny <string>(), It.IsAny <string>())).Returns(inferenceRequest);

            var response = new HttpResponseMessage(httpStatusCode);

            response.Content = new StringContent("result");

            _handlerMock = new Mock <HttpMessageHandler>();
            _handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(response);

            _httpClientFactory.Setup(p => p.CreateClient(It.IsAny <string>()))
            .Returns(new HttpClient(_handlerMock.Object));

            var dataflowCompleted = new ManualResetEvent(false);

            service.ReportActionStarted += (sender, args) =>
            {
                dataflowCompleted.Set();
            };

            await service.StartAsync(_cancellationTokenSource.Token);

            dataflowCompleted.WaitOne(5000);

            _resultsService.Verify(
                p => p.GetPendingJobs(
                    _configuration.Value.Dicom.Scu.ExportSettings.Agent,
                    It.IsAny <CancellationToken>(),
                    10), Times.Once());
            _payloadsApi.Verify(p => p.Download(tasks.First().PayloadId, tasks.First().Uris.First()), Times.AtLeastOnce());

            _logger.VerifyLogging($"Exporting data to {inferenceRequest.OutputResources.First().ConnectionDetails.Uri}.", LogLevel.Debug, Times.AtLeastOnce());

            if (httpStatusCode == HttpStatusCode.OK)
            {
                _logger.VerifyLogging($"Task marked as successful.", LogLevel.Information, Times.AtLeastOnce());
            }
            else
            {
                _logger.VerifyLogging($"Failed to export data to DICOMweb destination.", LogLevel.Error, Times.AtLeastOnce());
                _logger.VerifyLoggingMessageBeginsWith("Task marked as failed with failure rate=", LogLevel.Warning, Times.AtLeastOnce());
            }

            _handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Post &&
                                               req.RequestUri.ToString().StartsWith($"{url}/studies/")),
                ItExpr.IsAny <CancellationToken>());

            await StopAndVerify(service);
        }