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
10 changes: 10 additions & 0 deletions src/NSubstitute/Core/WhenCalled.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using NSubstitute.Routing;

namespace NSubstitute.Core;
Expand All @@ -19,6 +20,15 @@ public void Do(Action<CallInfo> callbackWithArguments)
}

/// <summary>
/// Perform this action when called.
/// </summary>
/// <param name="callbackWithArguments"></param>
public void Do(Func<CallInfo, Task> callbackWithArguments)
{
Do(callInfo => callbackWithArguments(callInfo).GetAwaiter().GetResult());
}

/// <summary>
/// Perform this configured callback when called.
/// </summary>
/// <param name="callback"></param>
Expand Down
3 changes: 2 additions & 1 deletion src/NSubstitute/Routing/Handlers/RaiseEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Threading.Tasks;
using NSubstitute.Core;
using NSubstitute.Exceptions;

Expand Down Expand Up @@ -26,7 +27,7 @@ public RouteAction Handle(ICall call)

try
{
handler.DynamicInvoke(eventArguments);
(handler.DynamicInvoke(eventArguments) as Task)?.GetAwaiter().GetResult();
}
catch (TargetInvocationException e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using NUnit.Framework;

namespace NSubstitute.Acceptance.Specs.AsyncEventHandlers;

[TestFixture]
public class AsyncEventHandlersWithNSubstituteTests
{
#region Fields

private bool _waitFinished;

#endregion

#region Setup and Teardown

[SetUp]
public void SetUp()
{
_waitFinished = false;
}

#endregion

#region Public Methods

[Test]
public async Task TestImplementationWithAsynchronousHandler()
{
var testImplementation = new TestImplementation();

testImplementation.TestEvent += AsynchronousEventHandler;

await testImplementation.RaiseEventAsync();

TestContext.WriteLine("Raise.Event finished");

Assert.That(_waitFinished, Is.True);
}

[Test]
public async Task TestSubstituteImplementationWithSynchronousHandler()
{
var testImplementation = new TestImplementation();

testImplementation.TestEvent += SynchronousEventHandler;

await testImplementation.RaiseEventAsync();

TestContext.WriteLine("Raise.Event finished");

Assert.That(_waitFinished, Is.True);
}

[Test]
public void TestSubstituteWithAsynchronousHandler()
{
var testInterface = Substitute.For<ITestInterface>();

testInterface.TestEvent += AsynchronousEventHandler;

testInterface.TestEvent += Raise.Event<TestEventHandler>();

TestContext.WriteLine("Raise.Event finished");

Assert.That(_waitFinished, Is.True);
}

[Test]
public void TestWithSynchronousHandler()
{
var testInterface = Substitute.For<ITestInterface>();

testInterface.TestEvent += SynchronousEventHandler;

testInterface.TestEvent += Raise.Event<TestEventHandler>();

TestContext.WriteLine("Raise.Event finished");

Assert.That(_waitFinished, Is.True);
}

#endregion

#region Private Methods

private async Task AsynchronousEventHandler()
{
TestContext.WriteLine("starting to wait");
await Task.Delay(100);
_waitFinished = true;
TestContext.WriteLine("wait finished");
}

private Task SynchronousEventHandler()
{
TestContext.WriteLine("starting to wait");
Thread.Sleep(100);
_waitFinished = true;
TestContext.WriteLine("wait finished");
return Task.CompletedTask;
}

#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace NSubstitute.Acceptance.Specs.AsyncEventHandlers;

public delegate Task TestEventHandler();

public interface ITestInterface
{
#region Events

event TestEventHandler TestEvent;

#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace NSubstitute.Acceptance.Specs.AsyncEventHandlers;

public class TestImplementation : ITestInterface
{
#region Events

public event TestEventHandler? TestEvent;

#endregion

#region Public Methods

public async Task RaiseEventAsync()
{
if (TestEvent != null)
{
await TestEvent();
}
}

#endregion
}
25 changes: 24 additions & 1 deletion tests/NSubstitute.Acceptance.Specs/WhenCalledDo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using NSubstitute.Acceptance.Specs.Infrastructure;
using NSubstitute.Core;
using NUnit.Framework;
Expand All @@ -9,11 +12,31 @@ public class WhenCalledDo
{
private ISomething _something;


[Test]
public void Execute_when_called_async()
{
var called = false;
_something.When(substitute => substitute.Echo(1)).Do(async info =>
{
await Task.Delay(100);
called = true;
});

Assert.That(called, Is.False, "Called");
_something.Echo(1);
Assert.That(called, Is.True, "Called");
}

[Test]
public void Execute_when_called()
{
var called = false;
_something.When(substitute => substitute.Echo(1)).Do(info => called = true);
_something.When(substitute => substitute.Echo(1)).Do(info =>
{
Thread.Sleep(100);
called = true;
});

Assert.That(called, Is.False, "Called");
_something.Echo(1);
Expand Down