public async Task DoesNotPreventThrowStackTrace() { // Arrange Exception innerException = null; try { await Task.Run(() => throw new Exception()).ConfigureAwait(false); } catch (Exception ex) { innerException = ex; } // Act Exception demystifiedException = new Exception(innerException.Message, innerException).Demystify(); // Assert var stackTrace = demystifiedException.ToString(); stackTrace = ReplaceLineEndings.Replace(stackTrace, ""); var trace = stackTrace.Split(Environment.NewLine); Assert.Equal( new[] { "System.Exception: Exception of type 'System.Exception' was thrown. ---> System.Exception: Exception of type 'System.Exception' was thrown.", " at Task Demystify.NonThrownException.DoesNotPreventThrowStackTrace()+()=>{}", " at void System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state)", " at async Task Demystify.NonThrownException.DoesNotPreventThrowStackTrace()", " --- End of inner exception stack trace ---" }, trace); // Act try { throw demystifiedException; } catch (Exception ex) { demystifiedException = ex.Demystify(); } // Assert stackTrace = demystifiedException.ToString(); stackTrace = ReplaceLineEndings.Replace(stackTrace, ""); trace = stackTrace.Split(Environment.NewLine); Assert.Equal( new[] { "System.Exception: Exception of type 'System.Exception' was thrown. ---> System.Exception: Exception of type 'System.Exception' was thrown.", " at Task Demystify.NonThrownException.DoesNotPreventThrowStackTrace()+()=>{}", " at void System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state)", " at async Task Demystify.NonThrownException.DoesNotPreventThrowStackTrace()", " --- End of inner exception stack trace ---", " at async Task Demystify.NonThrownException.DoesNotPreventThrowStackTrace()" }, trace); }
public void DemystifiesAggregateExceptions() { Exception demystifiedException = null; try { var tasks = new List <Task> { Task.Run(async() => await Throw1()), Task.Run(async() => await Throw2()), Task.Run(async() => await Throw3()) }; Task.WaitAll(tasks.ToArray()); } catch (Exception ex) { demystifiedException = ex.Demystify(); } // Assert var stackTrace = demystifiedException.ToString(); stackTrace = ReplaceLineEndings.Replace(stackTrace, ""); var trace = string.Join("", stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) // Remove items that vary between test runners .Where(s => s != " at Task Demystify.DynamicCompilation.DoesNotPreventStackTrace()+()=>{}" && !s.Contains("System.Threading.Tasks.Task.WaitAll") ) .Skip(1) .ToArray()) // Remove Full framework back arrow .Replace("<---", ""); var expected = string.Join("", new[] { " at async Task Demystify.AggregateException.Throw1()", " at async void Demystify.AggregateException.DemystifiesAggregateExceptions()+(?)=>{}", " --- End of inner exception stack trace ---", " at void Demystify.AggregateException.DemystifiesAggregateExceptions()", "---> (Inner Exception #0) System.ArgumentException: Value does not fall within the expected range.", " at async Task Demystify.AggregateException.Throw1()", " at async void Demystify.AggregateException.DemystifiesAggregateExceptions()+(?)=>{}", "---> (Inner Exception #1) System.NullReferenceException: Object reference not set to an instance of an object.", " at async Task Demystify.AggregateException.Throw2()", " at async void Demystify.AggregateException.DemystifiesAggregateExceptions()+(?)=>{}", "---> (Inner Exception #2) System.InvalidOperationException: Operation is not valid due to the current state of the object.", " at async Task Demystify.AggregateException.Throw3()", " at async void Demystify.AggregateException.DemystifiesAggregateExceptions()+(?)=>{}" }); Assert.Equal(expected, trace); }
public async Task DoesNotPreventStackTrace() { // Arrange var expression = Expression.Throw( Expression.New( typeof(ArgumentException).GetConstructor( new Type[] { typeof(string) }), Expression.Constant("Message"))); var lambda = Expression.Lambda <Action>(expression); var action = lambda.Compile(); // Act Exception demystifiedException = null; try { await Task.Run(() => action()).ConfigureAwait(false); } catch (Exception ex) { demystifiedException = ex.Demystify(); } // Assert var stackTrace = demystifiedException.ToString(); stackTrace = ReplaceLineEndings.Replace(stackTrace, ""); var trace = stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.None) // Remove items that vary between test runners .Where(s => s != " at void System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state)" && s != " at Task Demystify.DynamicCompilation.DoesNotPreventStackTrace()+() => { }" ) .ToArray(); Assert.Equal( new[] { "System.ArgumentException: Message", " at void lambda_method(Closure)", " at async Task Demystify.DynamicCompilation.DoesNotPreventStackTrace()" }, trace); }
public async Task Current() { // Arrange EnhancedStackTrace est = null; // Act await Task.Run(() => est = EnhancedStackTrace.Current()).ConfigureAwait(false); // Assert var stackTrace = est.ToString(); stackTrace = ReplaceLineEndings.Replace(stackTrace, ""); var trace = stackTrace.Split(Environment.NewLine); Assert.Equal( new[] { " at void System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state)", " at bool System.Threading.ThreadPoolWorkQueue.Dispatch()" }, trace); }
public async Task Current() { // Arrange EnhancedStackTrace est = null; // Act await Task.Run(() => est = EnhancedStackTrace.Current()).ConfigureAwait(false); // Assert var stackTrace = est.ToString(); stackTrace = ReplaceLineEndings.Replace(stackTrace, ""); var trace = stackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.None) // Remove Full framework entries .Where(s => !s.StartsWith(" at bool System.Threading._ThreadPoolWaitCallbac") && !s.StartsWith(" at void System.Threading.Tasks.Task.System.Thre")); Assert.Equal( new[] { " at bool System.Threading.ThreadPoolWorkQueue.Dispatch()" }, trace); }