Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/Samples/TestStack.BDDfy.Samples/CanRunAsyncSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace TestStack.BDDfy.Samples
{
public class CanRunAsyncSteps
{
private Sut _sut;
private object _sut;

internal async void GivenSomeAsyncSetup()
{
Expand All @@ -25,23 +25,19 @@ internal async void AndThenBddfyShouldCaptureExceptionsThrownInAsyncMethod()
throw new Exception("Exception in async void method!!");
}

private async Task<Sut> CreateSut()
private static async Task<object> CreateSut()
{
await Task.Delay(500);
return new Sut();
return new object();
}

[Fact]
public void Run()
{
var engine = this.LazyBDDfy();
var exception = Should.Throw<Exception>(() => engine.Run());
var exception = Should.Throw<Exception>(engine.Run);

exception.Message.ShouldBe("Exception in async void method!!");
}

internal class Sut
{
}
}
}
18 changes: 0 additions & 18 deletions src/Samples/TestStack.BDDfy.Samples/CanRunAsyncVoidSteps.cs

This file was deleted.

24 changes: 17 additions & 7 deletions src/TestStack.BDDfy/Processors/AsyncTestSyncContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ namespace TestStack.BDDfy
/// </summary>
internal class AsyncTestSyncContext : SynchronizationContext
{
readonly ManualResetEvent _event = new(initialState: true);
readonly object _lock = new();
Exception _exception;
int _operationCount;

public override void OperationCompleted()
{
var result = Interlocked.Decrement(ref _operationCount);
if (result == 0)
_event.Set();
lock (_lock)
{
_operationCount--;
if (_operationCount == 0)
Monitor.PulseAll(_lock);
}
}

public override void OperationStarted()
{
Interlocked.Increment(ref _operationCount);
_event.Reset();
lock (_lock)
{
_operationCount++;
}
}

public override void Post(SendOrPostCallback d, object state)
Expand Down Expand Up @@ -59,7 +64,12 @@ public override void Send(SendOrPostCallback d, object state)

public Exception WaitForCompletion()
{
_event.WaitOne();
lock (_lock)
{
while (_operationCount > 0)
Monitor.Wait(_lock);
}

return _exception;
}
}
Expand Down
Loading