Skip to content

Bugfix/async event handlers return instantly#808

Merged
dtchepak merged 5 commits intonsubstitute:mainfrom
jmartschinke:bugfix/async-event-handlers-return-instantly
Mar 16, 2025
Merged

Bugfix/async event handlers return instantly#808
dtchepak merged 5 commits intonsubstitute:mainfrom
jmartschinke:bugfix/async-event-handlers-return-instantly

Conversation

@jmartschinke
Copy link
Copy Markdown
Contributor

When trying to test an asynchronous event handler with Raise.Event, the Raise.Event immediately returns on the first thread change in the asynchronous event handler.
This makes testing the behaviour of the event handler almost impossible as you cannot be sure that the eventhandler is finished. That leads to bad test code, for example just waiting for a set amount of time in hopes the event handler has finished.

I fixed this bug by waiting for the event handler to finish with GetAwaiter().GetResult(); which is the safest way to synchronously wait for an asnychronous method to finish.
As handler.DynamicInvoke(eventArguments) already returned a Task independent of the return value of the event handler, it made this bugfix really small.

@jmartschinke jmartschinke requested a review from dtchepak March 12, 2025 13:05
@304NotModified
Copy link
Copy Markdown
Contributor

Looks like a great improvement 👍

Copy link
Copy Markdown
Member

@dtchepak dtchepak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

@dtchepak dtchepak merged commit d24adb5 into nsubstitute:main Mar 16, 2025
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants