Skip to content

Commit 59414c9

Browse files
authored
Capture stacktrace of exceptions thrown from async specifications (#522)
* Capture stacktrace of exceptions thrown from async specifications * Update spec projects target to net8.0 as netcoreapp3.1 is no longer available
1 parent f2ded63 commit 59414c9

File tree

7 files changed

+21
-13
lines changed

7 files changed

+21
-13
lines changed

src/Machine.Specifications.Core.Specs/Machine.Specifications.Core.Specs.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
4+
<TargetFrameworks>net472;net8.0</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

src/Machine.Specifications.Core.Specs/Runner/AsyncDelegateRunnerSpecs.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using Example.Exceptions;
34
using Machine.Specifications.Factories;
45
using Machine.Specifications.Runner;
56
using Machine.Specifications.Runner.Impl;
@@ -70,6 +71,11 @@ class when_running_async_specifications_with_exceptions : RunnerSpecs
7071

7172
It should_have_failures = () =>
7273
results.ShouldEachConformTo(x => !x.Passed);
74+
75+
It should_have_exception_details = () =>
76+
results.ShouldEachConformTo(r => r.Exception.TypeName == nameof(InvalidOperationException) &&
77+
r.Exception.Message == "something went wrong" &&
78+
r.Exception.StackTrace.Contains(typeof(AsyncSpecificationsWithExceptions).FullName));
7379
}
7480

7581
[Subject("Async Delegate Runner")]

src/Machine.Specifications.Core/Runner/Impl/AsyncSynchronizationContext.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.ExceptionServices;
23
using System.Threading;
34
using System.Threading.Tasks;
45

@@ -12,7 +13,7 @@ internal class AsyncSynchronizationContext : SynchronizationContext
1213

1314
private int callCount;
1415

15-
private Exception exception;
16+
private ExceptionDispatchInfo exceptionDispatchInfo;
1617

1718
public AsyncSynchronizationContext(SynchronizationContext inner)
1819
{
@@ -27,7 +28,7 @@ private void Execute(SendOrPostCallback callback, object state)
2728
}
2829
catch (Exception ex)
2930
{
30-
exception = ex;
31+
exceptionDispatchInfo = ExceptionDispatchInfo.Capture(ex);
3132
}
3233
finally
3334
{
@@ -88,15 +89,15 @@ public override void Send(SendOrPostCallback d, object state)
8889
}
8990
catch (Exception ex)
9091
{
91-
exception = ex;
92+
exceptionDispatchInfo = ExceptionDispatchInfo.Capture(ex);;
9293
}
9394
}
9495

95-
public async Task<Exception> WaitAsync()
96+
public async Task<ExceptionDispatchInfo> WaitAsync()
9697
{
9798
await events.WaitAsync();
9899

99-
return exception;
100+
return exceptionDispatchInfo;
100101
}
101102
}
102103
}

src/Machine.Specifications.Core/Runner/Impl/DelegateRunner.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,9 @@ public void Execute()
3636
{
3737
target.DynamicInvoke(args);
3838

39-
var exception = context.WaitAsync().Result;
39+
var exceptionDispatchInfo = context.WaitAsync().Result;
4040

41-
if (exception != null)
42-
{
43-
throw exception;
44-
}
41+
exceptionDispatchInfo?.Throw();
4542
}
4643
finally
4744
{

src/Machine.Specifications.Fixtures/RandomFixture.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,10 @@ public static ValueTask<int> Test()
11391139
Cleanup after = async () =>
11401140
cleanup_value = await Test();
11411141
}
1142+
}
1143+
1144+
namespace Example.Exceptions
1145+
{
11421146

11431147
public class AsyncSpecificationsWithExceptions
11441148
{

src/Machine.Specifications.Runner.Utility.Specs/Machine.Specifications.Runner.Utility.Specs.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
4+
<TargetFrameworks>net472;net8.0</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

src/Machine.Specifications.Should.Specs/Machine.Specifications.Should.Specs.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
4+
<TargetFrameworks>net472;net8.0</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

0 commit comments

Comments
 (0)