Skip to content

cancel context at the end of the scenario #513

@draganm

Description

@draganm

🤔 What's the problem you're trying to solve?

When setting up mock servers running in separate go-routines, it would be useful when the context passed to the scenario Before hooks would be cancellable and be cancelled at the end of the scenario.

✨ What's your proposed solution?

Wrap the scenario context with context.WithCancel() before Before hooks are run and call the cancel() after last After() hook is being called.

⛏ Have you considered any alternatives or workarounds?

currently I achieve this with:

func InitializeScenario(ctx *godog.ScenarioContext) {
	var cancel context.CancelFunc

	ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
		ctx, cancel = context.WithCancel(ctx)
		return ctx, nil

	})

	ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
		cancel()
		return ctx, nil
	})

}

it's not very elegant and gets repeated every time I use godog

📚 Any additional context?

My expectation was that this happens automatically, but only after trawling through the source code and running a couple of experiments it turned out it is not the case.


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

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