public void Should_execute_cleanup_steps_on_sync_exception_in_delegate_and_complete_with_exception() { CleanupGuard guard = new CleanupGuard(); List<int> steps = new List<int>(); Func<int, Task> doStepAsync = delegate(int i) { steps.Add(i); return Task.FromResult(false); }; guard.Register(() => doStepAsync(1)); guard.Register(() => doStepAsync(2)); InvalidTimeZoneException expectedException = new InvalidTimeZoneException("Expected."); Func<CleanupGuard, Task> doAsync = delegate(CleanupGuard g) { throw expectedException; }; Task task = guard.RunAsync(doAsync); Assert.Equal(TaskStatus.Faulted, task.Status); Assert.NotNull(task.Exception); AggregateException ae = Assert.IsType<AggregateException>(task.Exception).Flatten(); Assert.Equal(1, ae.InnerExceptions.Count); Assert.Same(expectedException, ae.InnerExceptions[0]); Assert.Equal(new int[] { 2, 1 }, steps.ToArray()); }
private static TimeZoneInfoResult TryGetTimeZoneFromLocalMachineCore(string id, out TimeZoneInfo?value, out Exception?e) { value = id == LocalId?GetLocalTimeZoneCore() : GetTimeZone(id, id); if (value == null) { e = new InvalidTimeZoneException(SR.Format(SR.InvalidTimeZone_InvalidFileData, id, AndroidTzDataInstance.GetTimeZoneDirectory() + TimeZoneFileName)); return(TimeZoneInfoResult.TimeZoneNotFoundException); } e = null; return(TimeZoneInfoResult.Success); }
public void read_aggregate_exception() { var ex1 = new DivideByZeroException("Only Chuck Norris can do that"); var ex2 = new RankException("You're last!"); var ex3 = new InvalidTimeZoneException("You are in the wrong place!"); var ex = new AggregateException(ex1, ex2, ex3); var record = new JobExecutionRecord(); record.ReadException(ex); record.ExceptionText.ShouldNotBe(ex.ToString()); record.ExceptionText.ShouldContain(ex1.ToString()); record.ExceptionText.ShouldContain(ex2.ToString()); record.ExceptionText.ShouldContain(ex3.ToString()); record.ExceptionText.ShouldContain(JobExecutionRecord.ExceptionSeparator); }
private static TimeZoneInfoResult TryGetTimeZoneFromLocalMachineCore(string id, out TimeZoneInfo?value, out Exception?e) { value = null; e = null; string timeZoneDirectory = GetTimeZoneDirectory(); string timeZoneFilePath = Path.Combine(timeZoneDirectory, id); byte[] rawData; try { rawData = File.ReadAllBytes(timeZoneFilePath); } catch (UnauthorizedAccessException ex) { e = ex; return(TimeZoneInfoResult.SecurityException); } catch (FileNotFoundException ex) { e = ex; return(TimeZoneInfoResult.TimeZoneNotFoundException); } catch (DirectoryNotFoundException ex) { e = ex; return(TimeZoneInfoResult.TimeZoneNotFoundException); } catch (IOException ex) { e = new InvalidTimeZoneException(SR.Format(SR.InvalidTimeZone_InvalidFileData, id, timeZoneFilePath), ex); return(TimeZoneInfoResult.InvalidTimeZoneException); } value = GetTimeZoneFromTzData(rawData, id); if (value == null) { e = new InvalidTimeZoneException(SR.Format(SR.InvalidTimeZone_InvalidFileData, id, timeZoneFilePath)); return(TimeZoneInfoResult.InvalidTimeZoneException); } return(TimeZoneInfoResult.Success); }
public void Open_with_events_traces_start_and_end_error() { ConnectionStub<IMyProxy> inner = new ConnectionStub<IMyProxy>(true); ClientEventSource eventSource = ClientEventSource.Instance; Guid id = new Guid(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5); ConnectionWithEvents<IMyProxy> outer = new ConnectionWithEvents<IMyProxy>(inner, eventSource, id); using (ClientEventListener listener = new ClientEventListener(eventSource, EventLevel.Informational, ClientEventSource.Keywords.Connection)) { Task task = outer.OpenAsync(); Assert.False(task.IsCompleted); listener.VerifyEvent(ClientEventId.ConnectionOpening, EventLevel.Informational, ClientEventSource.Keywords.Connection, EventOpcode.Start, id); listener.Events.Clear(); InvalidTimeZoneException expectedException = new InvalidTimeZoneException("Expected."); inner.OpenCall.SetException(expectedException); Assert.True(task.IsFaulted); AggregateException ae = Assert.IsType<AggregateException>(task.Exception); Assert.Equal(1, ae.InnerExceptions.Count); Assert.Same(expectedException, ae.InnerException); listener.VerifyEvent(ClientEventId.ConnectionError, EventLevel.Informational, ClientEventSource.Keywords.Connection, EventOpcode.Stop, id, "System.InvalidTimeZoneException", "Expected."); listener.Events.Clear(); } }
public void Execute_before_retry_throws_async_exception_caught_and_set_in_context() { RetryLoop loop = new RetryLoop(r => Task.FromResult(false)); loop.Succeeded = r => false; loop.ShouldRetry = r => r.Iteration < 1; InvalidTimeZoneException exception = new InvalidTimeZoneException("Expected."); TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(); tcs.SetException(exception); loop.BeforeRetry = r => tcs.Task; Task<RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); RetryContext context = task.Result; Assert.Equal(2, context.Iteration); Assert.False(context.Succeeded); Assert.NotNull(context.Exception); Assert.Equal(1, context.Exception.InnerExceptions.Count); Assert.Same(exception, context.Exception.InnerExceptions[0]); }
public void Execute_func_throws_sync_exception_caught_and_set_in_context() { InvalidTimeZoneException exception = new InvalidTimeZoneException("Expected."); Func<RetryContext, Task> func = delegate(RetryContext r) { throw exception; }; RetryLoop loop = new RetryLoop(func); Task<RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); RetryContext context = task.Result; Assert.Equal(1, context.Iteration); Assert.False(context.Succeeded); Assert.NotNull(context.Exception); Assert.Equal(1, context.Exception.InnerExceptions.Count); Assert.Same(exception, context.Exception.InnerExceptions[0]); }
public void Should_execute_all_cleanup_steps_despite_async_exceptions_in_run_and_cleanup_and_complete_with_all_exceptions() { CleanupGuard guard = new CleanupGuard(); List<int> steps = new List<int>(); Func<int, Task, Task> doStepAsync = delegate(int i, Task t) { steps.Add(i); return t; }; TaskCompletionSource<bool> cleanupTcs = new TaskCompletionSource<bool>(); guard.Register(() => doStepAsync(1, Task.FromResult(false))); guard.Register(() => doStepAsync(2, cleanupTcs.Task)); TaskCompletionSource<bool> runTcs = new TaskCompletionSource<bool>(); Task task = guard.RunAsync(g => runTcs.Task); Assert.False(task.IsCompleted); InvalidProgramException expectedRunException = new InvalidProgramException("Expected (run)."); runTcs.SetException(expectedRunException); InvalidTimeZoneException expectedCleanupException = new InvalidTimeZoneException("Expected (cleanup)."); cleanupTcs.SetException(expectedCleanupException); Assert.Equal(TaskStatus.Faulted, task.Status); Assert.NotNull(task.Exception); AggregateException ae = Assert.IsType<AggregateException>(task.Exception).Flatten(); Assert.Equal(2, ae.InnerExceptions.Count); Assert.Same(expectedRunException, ae.InnerExceptions[0]); Assert.Same(expectedCleanupException, ae.InnerExceptions[1]); Assert.Equal(new int[] { 2, 1 }, steps.ToArray()); }
public void GetOrAdd_CachesCompilationExceptions() { // Arrange var fileProvider = new TestFileProvider(); fileProvider.AddFile(ViewPath, "some content"); var cache = new CompilerCache(fileProvider); var exception = new InvalidTimeZoneException(); // Act and Assert - 1 var actual = Assert.Throws<InvalidTimeZoneException>(() => cache.GetOrAdd(ViewPath, _ => { throw exception; })); Assert.Same(exception, actual); // Act and Assert - 2 actual = Assert.Throws<InvalidTimeZoneException>(() => cache.GetOrAdd(ViewPath, ThrowsIfCalled)); Assert.Same(exception, actual); }