diff --git a/src/Samples/TestStack.BDDfy.Samples/CanRunAsyncSteps.cs b/src/Samples/TestStack.BDDfy.Samples/CanRunAsyncSteps.cs index 47516cc1..194c27ca 100644 --- a/src/Samples/TestStack.BDDfy.Samples/CanRunAsyncSteps.cs +++ b/src/Samples/TestStack.BDDfy.Samples/CanRunAsyncSteps.cs @@ -7,7 +7,7 @@ namespace TestStack.BDDfy.Samples { public class CanRunAsyncSteps { - private Sut _sut; + private object _sut; internal async void GivenSomeAsyncSetup() { @@ -25,23 +25,19 @@ internal async void AndThenBddfyShouldCaptureExceptionsThrownInAsyncMethod() throw new Exception("Exception in async void method!!"); } - private async Task CreateSut() + private static async Task CreateSut() { await Task.Delay(500); - return new Sut(); + return new object(); } [Fact] public void Run() { var engine = this.LazyBDDfy(); - var exception = Should.Throw(() => engine.Run()); + var exception = Should.Throw(engine.Run); exception.Message.ShouldBe("Exception in async void method!!"); } - - internal class Sut - { - } } } \ No newline at end of file diff --git a/src/Samples/TestStack.BDDfy.Samples/CanRunAsyncVoidSteps.cs b/src/Samples/TestStack.BDDfy.Samples/CanRunAsyncVoidSteps.cs deleted file mode 100644 index 36afc1e5..00000000 --- a/src/Samples/TestStack.BDDfy.Samples/CanRunAsyncVoidSteps.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Threading.Tasks; -using TestStack.BDDfy.Configuration; -using Xunit; - -namespace TestStack.BDDfy.Samples -{ - public class CanRunAsyncVoidSteps - { - [Fact] - internal void Run() => this.BDDfy(); - internal void SetUp() => Configurator.AsyncVoidSupportEnabled = false; - internal void TearDown() => Configurator.AsyncVoidSupportEnabled = true; - - internal async void GivenNonAsyncStep() => await Task.CompletedTask; - internal async void WhenSomethingHappens() => await Task.CompletedTask; - internal async void ThenAssertSomething() => await Task.CompletedTask; - } -} \ No newline at end of file diff --git a/src/TestStack.BDDfy/Processors/AsyncTestSyncContext.cs b/src/TestStack.BDDfy/Processors/AsyncTestSyncContext.cs index ecdbfe62..bab669be 100644 --- a/src/TestStack.BDDfy/Processors/AsyncTestSyncContext.cs +++ b/src/TestStack.BDDfy/Processors/AsyncTestSyncContext.cs @@ -8,21 +8,26 @@ namespace TestStack.BDDfy /// 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) @@ -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; } }