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

Fix #2600 by preserving ExecutionContext and reuse it for subsequent binding method invocations #2658

Merged
merged 7 commits into from
Nov 9, 2022

Conversation

gasparnagy
Copy link
Contributor

@gasparnagy gasparnagy commented Oct 21, 2022

This PR fixes #2600 by saving and reusing the ExecutionContext for the subsequent binding method invocations. ExecutionContext is pretty complex and hard to understand and also the behavior of AsyncLocal<T> is not straightforward to understand.

The bottom line is: AsyncLocal<T> now works as in SpecFlow v3 (and is it supposed to): you can change the value in ctor and sync step definitions, but the changes in async step definitions are discarded (this is how ExecutionContext works).

If you want to use AsyncLocal<T> and be able to change it in sync/async step definitions, you should use AsyncLocal<StrongBox<T>> and initialize it in ctor, like:

private readonly AsyncLocal<StrongBox<string>> _boxedAsyncLocal = new() { Value = new StrongBox<string>("ctor-value") };

A bit more tech detail (also included in BindingDelegateInvoker as comment):

To be able to simulate the behavior of sequential async or sync steps in a test, we need to ensure that the next step continues with the ExecutionContext that the previous step finished with.

Without preserving the ExecutionContext this would not happen because the async methods all the way up to the generated test method (for example this method) are discarding the ExecutionContext changes at the end, so the next step would start with an "empty" ExecutionContext again.

It is important that no methods from here (until the user's binding method) is marked with 'async' otherwise that would again discard the ExecutionContext.

The ExecutionContext only flows down, so async binding methods cannot directly change it, but even if all binding method is async the constructor of the binding classes are run in sync, so they should be able to change the ExecutionContext. (The binding classes are created as part of the 'bindingDelegate' this method receives.

Types of changes

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).
  • Performance improvement
  • Refactoring (so no functional change)
  • Other (docs, build config, etc)

Checklist:

  • I've added tests for my code. (most of the time mandatory)
  • I have added an entry to the changelog. (mandatory)
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

This is to avoid a breaking change in v4, so no need to add an entry to chanelog.

@gasparnagy gasparnagy changed the title first idea to fix #2600 Fix #2600 by preserving ExecutionContext and reuse it for subsequent binding method invocations Oct 27, 2022
* master:
  Report binding problems to binding registry and show them as errors during test execution (#2663)
  Fix for SF2649: Aggregate exceptions lost from async step definitions (#2667)

# Conflicts:
#	TechTalk.SpecFlow/Bindings/BindingInvoker.cs
#	Tests/TechTalk.SpecFlow.RuntimeTests/Bindings/BindingInvokerTests.cs
Copy link
Contributor

@SabotageAndi SabotageAndi left a comment

Choose a reason for hiding this comment

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

I never dived this deep into async/await, so TBH I have no clue about the code changes.
When the tests are green, I am happy.

@gasparnagy gasparnagy merged commit a2567a6 into master Nov 9, 2022
@gasparnagy gasparnagy deleted the fix_async_binding_calls_2600 branch November 9, 2022 12:38
Code-Grump pushed a commit to Code-Grump/SpecFlow that referenced this pull request Mar 29, 2023
…subsequent binding method invocations (SpecFlowOSS#2658)

* first idea to fix SpecFlowOSS#2600

* Improve idea to work on .NET 4

* Make BindingInvoker work with Task<T>

* fix execution for failing binding methods

* Keep ExecutionContext in ScenarioContext

* Cleanup unit tests
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.

ExecutionContext does not flow between hooks or steps in latest beta
3 participants