Ejemplo n.º 1
0
        public async Task GivenAnExportJobToResume_WhenExecuted_ThenItShouldExportAllRecordsAsExpected()
        {
            // We are using the SearchService to throw an exception in order to simulate the export job task
            // "crashing" while in the middle of the process.
            _exportJobConfiguration.NumberOfPagesPerCommit = 2;

            int numberOfCalls           = 0;
            int numberOfSuccessfulPages = 2;

            _searchService.SearchAsync(
                Arg.Any <string>(),
                Arg.Any <IReadOnlyList <Tuple <string, string> > >(),
                _cancellationToken)
            .Returns(x =>
            {
                int count = numberOfCalls;

                if (count == numberOfSuccessfulPages)
                {
                    throw new Exception();
                }

                numberOfCalls++;
                return(CreateSearchResult(
                           new[]
                {
                    new ResourceWrapper(
                        count.ToString(CultureInfo.InvariantCulture),
                        "1",
                        "Patient",
                        new RawResource("data", Core.Models.FhirResourceFormat.Json),
                        null,
                        DateTimeOffset.MinValue,
                        false,
                        null,
                        null,
                        null),
                },
                           continuationToken: "ct"));
            });

            await _exportJobTask.ExecuteAsync(_exportJobRecord, _weakETag, _cancellationToken);

            string exportedIds = _inMemoryDestinationClient.GetExportedData(new Uri(PatientFileName, UriKind.Relative));

            Assert.Equal("01", exportedIds);
            Assert.NotNull(_exportJobRecord.Progress);

            // We create a new export job task here to simulate the worker picking up the "old" export job record
            // and resuming the export process. The export destination client contains data that has
            // been committed up until the "crash".
            _inMemoryDestinationClient = new InMemoryExportDestinationClient();
            _exportDestinationClientFactory.Create("in-memory").Returns(_inMemoryDestinationClient);
            var secondExportJobTask = new ExportJobTask(
                () => _fhirOperationDataStore.CreateMockScope(),
                _secretStore,
                Options.Create(_exportJobConfiguration),
                () => _searchService.CreateMockScope(),
                _resourceToByteArraySerializer,
                _exportDestinationClientFactory,
                NullLogger <ExportJobTask> .Instance);

            numberOfSuccessfulPages = 5;
            await secondExportJobTask.ExecuteAsync(_exportJobRecord, _weakETag, _cancellationToken);

            exportedIds = _inMemoryDestinationClient.GetExportedData(new Uri(PatientFileName, UriKind.Relative));
            Assert.Equal("23", exportedIds);
        }