-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for async/await specifications
- Loading branch information
1 parent
f13ec7d
commit 5ed7261
Showing
9 changed files
with
343 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Threading.Tasks; | ||
using Machine.Specifications; | ||
|
||
namespace Example.Random | ||
{ | ||
public class AsyncSpecifications | ||
{ | ||
public static bool establish_invoked; | ||
|
||
public static bool because_invoked; | ||
|
||
public static bool async_it_invoked; | ||
|
||
public static bool sync_it_invoked; | ||
|
||
public static bool cleanup_invoked; | ||
|
||
Establish context = async () => | ||
{ | ||
establish_invoked = true; | ||
await Task.Delay(10); | ||
}; | ||
|
||
Because of = async () => | ||
{ | ||
because_invoked = true; | ||
await Task.Delay(10); | ||
}; | ||
|
||
It should_invoke_sync = () => | ||
sync_it_invoked = true; | ||
|
||
It should_invoke_async = async () => | ||
{ | ||
async_it_invoked = true; | ||
await Task.Delay(10); | ||
}; | ||
|
||
Cleanup after = async () => | ||
{ | ||
cleanup_invoked = true; | ||
await Task.Delay(10); | ||
}; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Examples/Example.Random/AsyncSpecificationsWithExceptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Machine.Specifications; | ||
|
||
namespace Example.Random | ||
{ | ||
public class AsyncSpecificationsWithExceptions | ||
{ | ||
Because of = async () => | ||
{ | ||
await Task.Delay(10); | ||
|
||
throw new InvalidOperationException("something went wrong"); | ||
}; | ||
|
||
It should_invoke_sync = () => | ||
{ | ||
throw new InvalidOperationException("something went wrong"); | ||
}; | ||
|
||
It should_invoke_async = async () => | ||
{ | ||
await Task.Delay(10); | ||
|
||
throw new InvalidOperationException("something went wrong"); | ||
}; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/Machine.Specifications.Specs/Runner/AsyncDelegateRunnerSpecs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Linq; | ||
using Example.Random; | ||
using FluentAssertions; | ||
using Machine.Specifications.Factories; | ||
using Machine.Specifications.Runner; | ||
using Machine.Specifications.Runner.Impl; | ||
|
||
namespace Machine.Specifications.Specs.Runner | ||
{ | ||
[Subject("Async Delegate Runner")] | ||
public class when_running_async_specifications : RunnerSpecs | ||
{ | ||
Establish context = () => | ||
{ | ||
AsyncSpecifications.establish_invoked = false; | ||
AsyncSpecifications.because_invoked = false; | ||
AsyncSpecifications.async_it_invoked = false; | ||
AsyncSpecifications.sync_it_invoked = false; | ||
AsyncSpecifications.cleanup_invoked = false; | ||
}; | ||
|
||
Because of = () => | ||
Run<AsyncSpecifications>(); | ||
|
||
It should_call_establish = () => | ||
AsyncSpecifications.establish_invoked.Should().BeTrue(); | ||
|
||
It should_call_because = () => | ||
AsyncSpecifications.because_invoked.Should().BeTrue(); | ||
|
||
It should_call_async_spec = () => | ||
AsyncSpecifications.async_it_invoked.Should().BeTrue(); | ||
|
||
It should_call_sync_spec = () => | ||
AsyncSpecifications.sync_it_invoked.Should().BeTrue(); | ||
|
||
It should_call_cleanup = () => | ||
AsyncSpecifications.cleanup_invoked.Should().BeTrue(); | ||
} | ||
|
||
[Subject("Async Delegate Runner")] | ||
public class when_running_async_specifications_with_exceptions : RunnerSpecs | ||
{ | ||
static ContextFactory factory; | ||
|
||
static Result[] results; | ||
|
||
Establish context = () => | ||
factory = new ContextFactory(); | ||
|
||
Because of = () => | ||
{ | ||
var context = factory.CreateContextFrom(Activator.CreateInstance<AsyncSpecificationsWithExceptions>()); | ||
|
||
results = ContextRunnerFactory | ||
.GetContextRunnerFor(context) | ||
.Run(context, | ||
new RunListenerBase(), | ||
RunOptions.Default, | ||
Array.Empty<ICleanupAfterEveryContextInAssembly>(), | ||
Array.Empty<ISupplementSpecificationResults>()) | ||
.ToArray(); | ||
}; | ||
|
||
It should_run_two_specs = () => | ||
results.Length.Should().Be(2); | ||
|
||
It should_have_failures = () => | ||
results.Should().Match(x => x.All(y => !y.Passed)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Machine.Specifications/Runner/Impl/AsyncManualResetEvent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#if !NET35 | ||
using System.Threading.Tasks; | ||
|
||
namespace Machine.Specifications.Runner.Impl | ||
{ | ||
internal class AsyncManualResetEvent | ||
{ | ||
private volatile TaskCompletionSource<bool> source = new TaskCompletionSource<bool>(); | ||
|
||
public AsyncManualResetEvent() | ||
{ | ||
source.TrySetResult(true); | ||
} | ||
|
||
public void Reset() | ||
{ | ||
if (source.Task.IsCompleted) | ||
{ | ||
source = new TaskCompletionSource<bool>(); | ||
} | ||
} | ||
|
||
public void Set() | ||
{ | ||
source.TrySetResult(true); | ||
} | ||
|
||
public void Wait() | ||
{ | ||
source.Task.Wait(); | ||
} | ||
} | ||
} | ||
#endif |
104 changes: 104 additions & 0 deletions
104
src/Machine.Specifications/Runner/Impl/AsyncSynchronizationContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#if !NET35 | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Machine.Specifications.Runner.Impl | ||
{ | ||
internal class AsyncSynchronizationContext : SynchronizationContext | ||
{ | ||
private readonly SynchronizationContext inner; | ||
|
||
private readonly AsyncManualResetEvent events = new AsyncManualResetEvent(); | ||
|
||
private int callCount; | ||
|
||
private Exception exception; | ||
|
||
public AsyncSynchronizationContext(SynchronizationContext inner) | ||
{ | ||
this.inner = inner; | ||
} | ||
|
||
private void Execute(SendOrPostCallback callback, object state) | ||
{ | ||
try | ||
{ | ||
callback(state); | ||
} | ||
catch (Exception ex) | ||
{ | ||
exception = ex; | ||
} | ||
finally | ||
{ | ||
OperationCompleted(); | ||
} | ||
} | ||
|
||
public override void OperationCompleted() | ||
{ | ||
var count = Interlocked.Decrement(ref callCount); | ||
|
||
if (count == 0) | ||
{ | ||
events.Set(); | ||
} | ||
} | ||
|
||
public override void OperationStarted() | ||
{ | ||
Interlocked.Increment(ref callCount); | ||
|
||
events.Reset(); | ||
} | ||
|
||
public override void Post(SendOrPostCallback d, object state) | ||
{ | ||
OperationStarted(); | ||
|
||
try | ||
{ | ||
if (inner == null) | ||
{ | ||
ThreadPool.QueueUserWorkItem(_ => Execute(d, state)); | ||
} | ||
else | ||
{ | ||
inner.Post(_ => Execute(d, state), null); | ||
} | ||
} | ||
catch | ||
{ | ||
// ignored | ||
} | ||
} | ||
|
||
public override void Send(SendOrPostCallback d, object state) | ||
{ | ||
try | ||
{ | ||
if (inner == null) | ||
{ | ||
d(state); | ||
} | ||
else | ||
{ | ||
inner.Send(d, state); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
exception = ex; | ||
} | ||
} | ||
|
||
public Exception WaitAsync() | ||
{ | ||
events.Wait(); | ||
|
||
return exception; | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Threading; | ||
|
||
namespace Machine.Specifications.Runner.Impl | ||
{ | ||
internal class DelegateRunner | ||
{ | ||
private readonly Delegate target; | ||
|
||
private readonly object[] args; | ||
|
||
public DelegateRunner(Delegate target, params object[] args) | ||
{ | ||
this.target = target; | ||
this.args = args; | ||
} | ||
|
||
public void Execute() | ||
{ | ||
#if NET35 | ||
target.DynamicInvoke(args); | ||
#else | ||
var currentContext = SynchronizationContext.Current; | ||
|
||
var context = new AsyncSynchronizationContext(currentContext); | ||
|
||
SynchronizationContext.SetSynchronizationContext(context); | ||
|
||
try | ||
{ | ||
target.DynamicInvoke(args); | ||
|
||
var exception = context.WaitAsync(); | ||
|
||
if (exception != null) | ||
{ | ||
throw exception; | ||
} | ||
} | ||
finally | ||
{ | ||
SynchronizationContext.SetSynchronizationContext(currentContext); | ||
} | ||
#endif | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters