Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-134] - detecting async Received.InOrder callbacks #137

Merged
merged 1 commit into from
Mar 15, 2020

Conversation

tpodolak
Copy link
Member

Closes #134

@tpodolak tpodolak force-pushed the GH-134-async-callbacs-received-in-order branch from b94d400 to 8645223 Compare February 23, 2020 20:58
@coveralls
Copy link

coveralls commented Feb 23, 2020

Pull Request Test Coverage Report for Build 381

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 95.589%

Totals Coverage Status
Change from base Build 378: 0.04%
Covered Lines: 2254
Relevant Lines: 2358

💛 - Coveralls


namespace NSubstitute.Analyzers.Shared.DiagnosticAnalyzers
{
internal abstract class AbstractAsyncReceivedInOrderCallbackAnalyzer<TSyntaxKind, TInvocationExpressionSyntax> : AbstractDiagnosticAnalyzer
Copy link
Member Author

@tpodolak tpodolak Feb 23, 2020

Choose a reason for hiding this comment

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

@dtchepak @zvirja this will detect async callback in following usages

Received.InOrder(async () => { // rest of the code })
Received.InOrder(async delegate { // rest of the code })

should I also make an attempt to detect async void callbacks like

Received.InOrder(Calls);
  
private async void Calls()
{
    await Task.CompletedTask;
}

?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe hold off for now. I haven't done much with async/await so I'm not sure, but maybe people will want to try asserting calls on a substitute that is returned from another async call:

private async void Calls() {
  var anotherSub = await sub.GetThingoe();
  anotherSub.SomeCall();
}

AFAICT the problem for NSubstitute is mainly when using Received.InOrder(async () => ...); it seems ok without the async which I think is what we need to avoid. Keeping the option to extract this to a separate method might be a good workaround if people really need this. WDYT @zvirja ?

But I'm happy to be corrected on this as I only have basic experience with async/await.

Copy link

Choose a reason for hiding this comment

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

@dtchepak I like you idea of keeping Received.InOrder(Calls); for now in case clients have weird reason for that. This way we could also collect the feedback and see whether it's the case at all.

Also it should help keep code simpler and less chance of false-positive :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for your feedback guys. I am leaving this as it is


## Rule description

A violation of this rule occurs when async callback is used in `Received.InOrder` method.
Copy link
Member

Choose a reason for hiding this comment

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

I think we should add a description of why this is a problem. Maybe just link to the issue?

A violation of this rule occurs when async callback is used in Received.InOrder method. Received.InOrder is used to specify calls which should be received by one or more substitutes. Running/awaiting these calls is not required for this, and can cause some problems as described in issue #604.

A violation of this rule occurs when async callback is used in `Received.InOrder` method.  `Received.InOrder` is used to specify calls which should be received by one or more substitutes. Running/awaiting these calls is not required for this, and can cause some problems as described in issue [#604](https://github.com/nsubstitute/NSubstitute/issues/604).

Copy link
Member Author

@tpodolak tpodolak Mar 4, 2020

Choose a reason for hiding this comment

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

Changed as suggested


namespace NSubstitute.Analyzers.Shared.DiagnosticAnalyzers
{
internal abstract class AbstractAsyncReceivedInOrderCallbackAnalyzer<TSyntaxKind, TInvocationExpressionSyntax> : AbstractDiagnosticAnalyzer
Copy link
Member

Choose a reason for hiding this comment

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

Maybe hold off for now. I haven't done much with async/await so I'm not sure, but maybe people will want to try asserting calls on a substitute that is returned from another async call:

private async void Calls() {
  var anotherSub = await sub.GetThingoe();
  anotherSub.SomeCall();
}

AFAICT the problem for NSubstitute is mainly when using Received.InOrder(async () => ...); it seems ok without the async which I think is what we need to avoid. Keeping the option to extract this to a separate method might be a good workaround if people really need this. WDYT @zvirja ?

But I'm happy to be corrected on this as I only have basic experience with async/await.

@tpodolak tpodolak force-pushed the GH-134-async-callbacs-received-in-order branch from 8645223 to eb440e9 Compare March 4, 2020 22:21
@tpodolak tpodolak merged commit 2faefe7 into dev Mar 15, 2020
@tpodolak tpodolak deleted the GH-134-async-callbacs-received-in-order branch March 15, 2020 13:44
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.

Detect async callback in Received.InOrder
4 participants