Skip to content
generated from bool64/go-template

A helper to synchronize access to a shared resources from concurrent scenarios

License

Notifications You must be signed in to change notification settings

godogx/resource

Repository files navigation

Godog Resource Lock

Build Status Coverage Status GoDevDoc Time Tracker Code lines Comments

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.

Usage

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.

About

A helper to synchronize access to a shared resources from concurrent scenarios

Resources

License

Stars

Watchers

Forks

Packages

No packages published