This library provides a simple way to manage sequential access to global resources in your godog tests.
This is useful if godog suite is running with
concurrency
option greater than 1.
Add *resource.Lock to your resource manager.
// StorageSteps manages an example global external storage.
type storageSteps struct {
lock *resource.Lock
}Register *resource.Lock hooks to scenario context.
func (s *storageSteps) Register(sc *godog.ScenarioContext) {
s.lock.Register(sc)
...Upgrade your step definitions to receive context if you don't have it already.
sc.Step(`write file "([^"])" with contents`, func(ctx context.Context, path string, contents string) error {Acquire resource before using it in scenario step. This will block all other scenarios that will try to acquire this resource to wait until current scenario is finished.
func (s *storageSteps) acquireFile(ctx context.Context, path string) error {
ok, err := s.lock.Acquire(ctx, path)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("could not acquire file for scenario: %s", path)
}
return nil
}See complete instrumentation example.