private void AppendDuplicatedResouceErrorToBuffer(IEnumerable <SqlBulkCopyDataWrapper> mergedResources, List <string> importErrorBuffer) { foreach (SqlBulkCopyDataWrapper resourceWrapper in mergedResources) { importErrorBuffer.Add(_importErrorSerializer.Serialize(resourceWrapper.Index, string.Format(Resources.FailedToImportForDuplicatedResource, resourceWrapper.Resource.ResourceId, resourceWrapper.Index))); } }
public async Task GivenResourceLoader_WhenLoadResourcesWithDifferentResourceType_ThenResourcesWithDifferentTypeShouldBeSkipped() { string errorMessage = "Resource type not match."; using MemoryStream stream = new MemoryStream(); using StreamWriter writer = new StreamWriter(stream); await writer.WriteLineAsync("test"); await writer.FlushAsync(); stream.Position = 0; IIntegrationDataStoreClient integrationDataStoreClient = Substitute.For <IIntegrationDataStoreClient>(); integrationDataStoreClient.DownloadResource(Arg.Any <Uri>(), Arg.Any <long>(), Arg.Any <CancellationToken>()).ReturnsForAnyArgs(stream); integrationDataStoreClient.TryAcquireLeaseAsync(Arg.Any <Uri>(), Arg.Any <string>(), Arg.Any <CancellationToken>()).ReturnsForAnyArgs(string.Empty); IImportResourceParser importResourceParser = Substitute.For <IImportResourceParser>(); importResourceParser.Parse(Arg.Any <long>(), Arg.Any <long>(), Arg.Any <string>()) .Returns(callInfo => { ImportResource importResource = new ImportResource(null); return(importResource); }); IImportErrorSerializer serializer = Substitute.For <IImportErrorSerializer>(); serializer.Serialize(Arg.Any <long>(), Arg.Any <Exception>()) .Returns(callInfo => { Exception ex = (Exception)callInfo[1]; return(ex.Message); }); Func <long, long> idGenerator = (i) => i; ImportResourceLoader loader = new ImportResourceLoader(integrationDataStoreClient, importResourceParser, serializer, NullLogger <ImportResourceLoader> .Instance); (Channel <ImportResource> outputChannel, Task importTask) = loader.LoadResources("http://dummy", 0, "DummyType", idGenerator, CancellationToken.None); int errorCount = 0; await foreach (ImportResource resource in outputChannel.Reader.ReadAllAsync()) { Assert.Equal(errorMessage, resource.ImportError); ++errorCount; } await importTask; Assert.Equal(1, errorCount); }
public async Task GivenResourceLoader_WhenCancelLoadTask_ThenDataLoadTaskShouldBeCanceled() { string errorMessage = "error"; using MemoryStream stream = new MemoryStream(); using StreamWriter writer = new StreamWriter(stream); await writer.WriteLineAsync("test"); await writer.WriteLineAsync("test"); await writer.WriteLineAsync("test"); await writer.WriteLineAsync("test"); await writer.FlushAsync(); stream.Position = 0; AutoResetEvent resetEvent1 = new AutoResetEvent(false); ManualResetEvent resetEvent2 = new ManualResetEvent(false); IIntegrationDataStoreClient integrationDataStoreClient = Substitute.For <IIntegrationDataStoreClient>(); integrationDataStoreClient.DownloadResource(Arg.Any <Uri>(), Arg.Any <long>(), Arg.Any <CancellationToken>()).ReturnsForAnyArgs(stream); integrationDataStoreClient.TryAcquireLeaseAsync(Arg.Any <Uri>(), Arg.Any <string>(), Arg.Any <CancellationToken>()).ReturnsForAnyArgs(string.Empty); IImportResourceParser importResourceParser = Substitute.For <IImportResourceParser>(); importResourceParser.Parse(Arg.Any <long>(), Arg.Any <long>(), Arg.Any <string>()) .Returns(callInfo => { resetEvent1.Set(); resetEvent2.WaitOne(); throw new InvalidCastException(errorMessage); }); IImportErrorSerializer serializer = Substitute.For <IImportErrorSerializer>(); serializer.Serialize(Arg.Any <long>(), Arg.Any <Exception>()) .Returns(callInfo => { Exception ex = (Exception)callInfo[1]; return(ex.Message); }); Func <long, long> idGenerator = (i) => i; ImportResourceLoader loader = new ImportResourceLoader(integrationDataStoreClient, importResourceParser, serializer, NullLogger <ImportResourceLoader> .Instance); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); (Channel <ImportResource> outputChannel, Task importTask) = loader.LoadResources("http://dummy", 0, null, idGenerator, cancellationTokenSource.Token); resetEvent1.WaitOne(); cancellationTokenSource.Cancel(); resetEvent2.Set(); await foreach (ImportResource resource in outputChannel.Reader.ReadAllAsync()) { // do nothing. } try { await importTask; throw new InvalidOperationException(); } catch (TaskCanceledException) { // Expected error } catch (OperationCanceledException) { // Expected error } }