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

cancel context for each scenario #514

Merged
merged 3 commits into from
Apr 3, 2023

Conversation

draganm
Copy link
Contributor

@draganm draganm commented Dec 27, 2022

🤔 What's changed?

Each Scenario gets a cancellable context that is cancelled at the end of the scenario.

⚡️ What's your motivation?

Cancelled context can be used to shut down mock services that are run in the scope of one Scenario (see #513)

🏷️ What kind of change is this?

  • ⚡ New feature (non-breaking change which adds new behaviour)

♻️ Anything particular you want feedback on?

📋 Checklist:

  • I agree to respect and uphold the Cucumber Community Code of Conduct
  • I've changed the behaviour of the code
    • I have added/updated tests to cover my changes.
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly.
  • Users should know about my change
    • I have added an entry to the "Unreleased" section of the CHANGELOG, linking to this pull request.

This text was originally generated from a template, then edited by hand. You can modify the template here.

@draganm
Copy link
Contributor Author

draganm commented Dec 27, 2022

Not sure how to end-to-end test this though.

@draganm draganm marked this pull request as ready for review December 27, 2022 14:39
@codecov
Copy link

codecov bot commented Jan 5, 2023

Codecov Report

Merging #514 (9c3a47d) into main (aba6a68) will decrease coverage by 0.81%.
The diff coverage is 100.00%.

❗ Current head 9c3a47d differs from pull request most recent head 9ea8682. Consider uploading reports for the commit 9ea8682 to get more accurate results

@@            Coverage Diff             @@
##             main     #514      +/-   ##
==========================================
- Coverage   82.94%   82.13%   -0.81%     
==========================================
  Files          28       27       -1     
  Lines        3371     3320      -51     
==========================================
- Hits         2796     2727      -69     
- Misses        461      484      +23     
+ Partials      114      109       -5     
Impacted Files Coverage Δ
suite.go 88.88% <100.00%> (-0.38%) ⬇️

... and 4 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link

@mirogta mirogta left a comment

Choose a reason for hiding this comment

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

This looks great. Would you mind adding a simple example code as well as part of this PR? I would prefer if each use case (like your new one, however it looks cool) has an appropriate example to follow otherwise some less experienced developers might be a bit lost in how to use it.

@vearutop vearutop force-pushed the cancel-context-for-scenario branch from 9c3a47d to ddb0fc4 Compare April 3, 2023 22:29
@vearutop
Copy link
Member

vearutop commented Apr 3, 2023

I think this is a good idea, although it potentially can break existing scenarios that depend (implicitly or explicitly) on background tasks after the scenario has ended already.
In such (rare?) cases, context cancellation can be canceled 🙃 with help of a simple wrapper.

renovate bot and others added 2 commits April 4, 2023 00:43
cucumber#549)

* Update CI for go1.20 (cucumber#552)

* fix(deps): update module github.com/cucumber/gherkin/go/v26 to v26.1.0

* Tidy modules

---------

Co-authored-by: Viacheslav Poturaev <[email protected]>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
suite.go Outdated Show resolved Hide resolved
@vearutop
Copy link
Member

vearutop commented Apr 3, 2023

Not sure how to end-to-end test this though.

@draganm for example, you can update suite_context_test.go with such test:

func TestScenarioContext_After_cancelled(t *testing.T) {
	ctxDone := make(chan struct{})
	suite := TestSuite{
		ScenarioInitializer: func(scenarioContext *ScenarioContext) {
			scenarioContext.When(`^foo$`, func() { return })
			scenarioContext.After(func(ctx context.Context, sc *Scenario, err error) (context.Context, error) {
				go func() {
					<-ctx.Done()
					close(ctxDone)
				}()

				return ctx, nil
			})
		},
		Options: &Options{
			Format:   "pretty",
			TestingT: t,
			FeatureContents: []Feature{
				{
					Name: "Scenario Context Cancellation",
					Contents: []byte(`
Feature: dummy
  Scenario: Context should be cancelled by the end of scenario
    When foo
`),
				},
			},
		},
	}

	require.Equal(t, 0, suite.Run(), "non-zero status returned, failed to run feature tests")

	select {
	case <-ctxDone:
		return
	case <-time.After(5 * time.Second):
		assert.Fail(t, "failed to wait for context cancellation")
	}
}

@vearutop
Copy link
Member

vearutop commented Apr 3, 2023

Actually, I think this is good to merge, and I'll add a test in a follow-up commit.

@vearutop vearutop merged commit 0dcbfef into cucumber:main Apr 3, 2023
@aslakhellesoy
Copy link
Contributor

Hi @draganm,

Thanks for your making your first contribution to Cucumber, and welcome to the Cucumber committers team! You can now push directly to this repo and all other repos under the cucumber organization! 🍾

In return for this generous offer we hope you will:

  • ✅ Continue to use branches and pull requests. When someone on the core team approves a pull request (yours or someone else's), you're welcome to merge it yourself.
  • 💚 Commit to setting a good example by following and upholding our code of conduct in your interactions with other collaborators and users.
  • 💬 Join the community Slack channel to meet the rest of the team and make yourself at home.
  • ℹ️ Don't feel obliged to help, just do what you can if you have the time and the energy.
  • 🙋 Ask if you need anything. We're looking for feedback about how to make the project more welcoming, so please tell us!

On behalf of the Cucumber core team,
Aslak Hellesøy
Creator of Cucumber

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.

4 participants