Skip to content

AsyncFunctionAssertions methods contain redundant multithreading #1020

@davidomid

Description

@davidomid

Fixed in PR #1027

Description

The AsyncFunctionAssertions class contains methods with redundant multithreading.

These methods are:

  • public void NotThrow(string because = "", params object[] becauseArgs)
  • public async Task NotThrowAsync(string because = "", params object[] becauseArgs)
  • public void NotThrow(string because = "", params object[] becauseArgs)
    where TException : Exception
  • public async Task NotThrowAsync(string because = "", params object[] becauseArgs)
    where TException : Exception
  • private Exception InvokeSubjectWithInterception()
  • private async Task InvokeSubjectWithInterceptionAsync()

Complete minimal example reproducing the issue

E.g. this is how NotThrowAsync currently looks:

public async Task NotThrowAsync(string because = "", params object[] becauseArgs)
{
     try
     {
          await Task.Run(Subject);
     }
     catch (Exception exception)
     {
          NotThrow(exception, because, becauseArgs);
     }
 }

This is how I would expect it to look:

public async Task NotThrowAsync(string because = "", params object[] becauseArgs)
{
     try
     {
          await Subject();
     }
     catch (Exception exception)
     {
          NotThrow(exception, because, becauseArgs);
     }
 }

Expected behavior:

The method should await the task returned by the Subject func.

In the other async methods listed above, I expect similar behaviour. In the non-async methods, I expect the task to be awaited in a blocking way (i.e. using .Wait() instead of await).

Actual behavior:

The method awaits a new task returned by Task.Run, which is sent the Subject func.
This schedules the work to be done on the thread pool, potentially using a different thread.
This adds overhead and is otherwise redundant.

This behaviour is similar for other methods listed above.

Versions

Any version

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions