Skip to content

fix: throw InvalidOperationException when using async void delegates#652

Merged
vbreuss merged 1 commit intomainfrom
topic/throw-for-async-void
Jun 22, 2025
Merged

fix: throw InvalidOperationException when using async void delegates#652
vbreuss merged 1 commit intomainfrom
topic/throw-for-async-void

Conversation

@vbreuss
Copy link
Copy Markdown
Member

@vbreuss vbreuss commented Jun 22, 2025

This pull request ensures that using aweXpect on async void delegates now throws an InvalidOperationException to prevent silent failures when awaiting outcomes.

The using an `async void` delegate, it is not possible to await the outcome. Therefore, it now throws an `InvalidOperationException` in this case.
@vbreuss vbreuss self-assigned this Jun 22, 2025
Copilot AI review requested due to automatic review settings June 22, 2025 05:45
@vbreuss vbreuss added the bug Something isn't working label Jun 22, 2025
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request ensures that using aweXpect on async void delegates now throws an InvalidOperationException to prevent silent failures when awaiting outcomes.

  • Added tests for async void delegate scenarios with and without CancellationToken
  • Modified Expect.cs to use the updated DelegateSource constructors
  • Updated DelegateSource to include a check for async void methods via ThrowIfAsyncVoid

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
Tests/aweXpect.Internal.Tests/ThatTests/DelegateTests.cs Added tests verifying that async void delegates throw with appropriate messages
Source/aweXpect.Core/Expect.cs Updated delegate handling in the factory method to delegate null handling to DelegateSource
Source/aweXpect.Core/Core/Sources/DelegateSource.cs Introduced overloads and ThrowIfAsyncVoid method to guard against async void delegates
Comments suppressed due to low confidence (1)

Source/aweXpect.Core/Core/Sources/DelegateSource.cs:16

  • Consider adding targeted unit tests for the ThrowIfAsyncVoid method to verify that it does not throw for valid synchronous methods and reliably throws for async void implementations.
		ThrowIfAsyncVoid(action?.GetMethodInfo(), "Func<CancellationToken, Task>");

Comment thread Tests/aweXpect.Internal.Tests/ThatTests/DelegateTests.cs
@sonarqubecloud
Copy link
Copy Markdown

@vbreuss vbreuss enabled auto-merge (squash) June 22, 2025 05:50
@github-actions
Copy link
Copy Markdown
Contributor

Test Results

    4 files  ±0      4 suites  ±0   22s ⏱️ ±0s
  897 tests ±0    896 ✅ ±0  1 💤 ±0  0 ❌ ±0 
2 577 runs  ±0  2 576 ✅ ±0  1 💤 ±0  0 ❌ ±0 

Results for commit 22cbb8a. ± Comparison against base commit 7d6a7be.

This pull request removes 54 and adds 54 tests. Note that renamed tests count towards both.
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message733b19dc-f198-4a24-a999-129b87397eda")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "messageca4affdf-da6c-476c-b16c-901bb09d1ffa")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "messagee268e114-53e6-41f6-b0fd-5b6d8dbaf26b")
aweXpect.Core.Tests.Core.Exceptions.SkipExceptionTests ‑ Message_ShouldBeSet(message: "message05aeab24-bc3f-4e9b-9f31-21d790bf5bcd")
aweXpect.Core.Tests.Core.Exceptions.SkipExceptionTests ‑ Message_ShouldBeSet(message: "message3d1ccfae-481b-4a2c-b3b8-c26209d73e04")
aweXpect.Core.Tests.Core.Exceptions.SkipExceptionTests ‑ Message_ShouldBeSet(message: "messagee536bfd9-2d41-4351-938a-85e22296ea95")
aweXpect.Core.Tests.Equivalency.EquivalencyOptionsExtensionsTests ‑ Generic_For_IgnoringMember_ShouldSetOptionForType(memberToIgnore: "memberToIgnore1a52c83b-cf2e-4184-90e1-54c8f5fbccb2")
aweXpect.Core.Tests.Equivalency.EquivalencyOptionsExtensionsTests ‑ Generic_For_IgnoringMember_ShouldSetOptionForType(memberToIgnore: "memberToIgnore1d206c4c-5bf7-4e8e-ae5c-0ac4b53c80a7")
aweXpect.Core.Tests.Equivalency.EquivalencyOptionsExtensionsTests ‑ Generic_For_IgnoringMember_ShouldSetOptionForType(memberToIgnore: "memberToIgnoref3a4e9da-02b1-4534-9c7a-b1765e1001d4")
aweXpect.Core.Tests.FailTests ‑ Test_ShouldThrowException(reason: "reasonaed4819b-14f7-44dc-829c-0ce336383cfe")
…
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message0012e0f8-a6f4-4e0c-b0df-b9e8fe02a5a8")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message1df6339d-aac9-4374-8544-f4e26d35d32e")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message895cf162-ff74-43f3-b45b-e30f79b3ab25")
aweXpect.Core.Tests.Core.Exceptions.SkipExceptionTests ‑ Message_ShouldBeSet(message: "message0f85fe61-9ca7-4007-ba0d-e42b1035fc8b")
aweXpect.Core.Tests.Core.Exceptions.SkipExceptionTests ‑ Message_ShouldBeSet(message: "message899b4c94-5527-4600-a9ba-af626a8b7e4b")
aweXpect.Core.Tests.Core.Exceptions.SkipExceptionTests ‑ Message_ShouldBeSet(message: "messaged979f091-07ba-4242-8dcc-f4b2e8b415d1")
aweXpect.Core.Tests.Equivalency.EquivalencyOptionsExtensionsTests ‑ Generic_For_IgnoringMember_ShouldSetOptionForType(memberToIgnore: "memberToIgnore24ab81f5-3118-4cd1-be03-6ebf62660670")
aweXpect.Core.Tests.Equivalency.EquivalencyOptionsExtensionsTests ‑ Generic_For_IgnoringMember_ShouldSetOptionForType(memberToIgnore: "memberToIgnorea0e82ac8-d588-4e53-8546-3fe88a90cdfc")
aweXpect.Core.Tests.Equivalency.EquivalencyOptionsExtensionsTests ‑ Generic_For_IgnoringMember_ShouldSetOptionForType(memberToIgnore: "memberToIgnoreef6eeb0a-a37b-449f-8968-b2a278c9d25a")
aweXpect.Core.Tests.FailTests ‑ Test_ShouldThrowException(reason: "reason669ab7de-1b68-41f3-927a-a5b544e90236")
…

@vbreuss vbreuss merged commit e61f68f into main Jun 22, 2025
13 checks passed
@vbreuss vbreuss deleted the topic/throw-for-async-void branch June 22, 2025 05:52
@github-actions
Copy link
Copy Markdown
Contributor

This is addressed in release v2.18.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working state: released The issue is released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants