public void WorkerSingleWorkerError_Test() { string errorMessage = "Test"; var bytes = new byte[] { 10, 20, 30, 40, 50 }; using (var sourceStream = new MemoryStream(bytes)) using (var destinationStream = new MemoryStream()) { int thrownFlag = 0; PackerEngineMock packer = new PackerEngineMock(); PackerEngineMock.PackedChangedEventHandler packedChanged = (object sender, PackedChangedEventArgs e) => { if (Interlocked.Exchange(ref thrownFlag, 1) == 0) { throw new UnauthorizedAccessException(errorMessage); } }; packer.PackedChanged += packedChanged; Assert.Equal(ExitStatus.ERROR, Packer.Run(sourceStream, destinationStream, packer, PackerMode.Pack, 1, Environment.ProcessorCount, token, logger)); packer.PackedChanged -= packedChanged; Assert.Single(logger.LoggedErrors); WorkersAggregateException aggregateException = logger.LoggedErrors[0] as WorkersAggregateException; Assert.Single(aggregateException.InnerExceptions); Assert.Equal(errorMessage, aggregateException.InnerExceptions[0].Message); } }
public void WorkerUserCancel_Test() { var bytes = new byte[] { 10, 20, 30, 40 }; using (var sourceStream = new MemoryStream(bytes)) using (var destinationStream = new MemoryStream()) { PackerEngineMock packer = new PackerEngineMock(); PackerEngineMock.PackedChangedEventHandler packedChanged = (object sender, PackedChangedEventArgs e) => { token.Cancel(); }; packer.PackedChanged += packedChanged; Assert.Equal(ExitStatus.CANCEL, Packer.Run(sourceStream, destinationStream, packer, PackerMode.Pack, 1, Environment.ProcessorCount, token, logger)); packer.PackedChanged -= packedChanged; Assert.Empty(logger.LoggedErrors); } }
public void WorkerMultipleErrors_Test() { var bytes = new byte[] { 10, 20, 30, 40, 50 }; using (var sourceStream = new MemoryStream(bytes)) using (var destinationStream = new MemoryStream()) { int thrownCounter = 0; PackerEngineMock packer = new PackerEngineMock(); PackerEngineMock.PackedChangedEventHandler packedChanged = (object sender, PackedChangedEventArgs e) => { Interlocked.Increment(ref thrownCounter); throw new UnauthorizedAccessException(); }; packer.PackedChanged += packedChanged; Assert.Equal(ExitStatus.ERROR, Packer.Run(sourceStream, destinationStream, packer, PackerMode.Pack, 1, Environment.ProcessorCount, token, logger)); packer.PackedChanged -= packedChanged; Assert.Single(logger.LoggedErrors); WorkersAggregateException aggregateException = logger.LoggedErrors[0] as WorkersAggregateException; Assert.Equal(thrownCounter, aggregateException.InnerExceptions.Count); } }