Skip to content

Commit

Permalink
Use godogx/resource lock (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jan 10, 2022
1 parent 9e02b1a commit e823cdc
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 131 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/bool64/shared v0.1.4
github.com/bool64/sqluct v0.1.9
github.com/cucumber/godog v0.12.3
github.com/godogx/resource v0.1.0
github.com/jmoiron/sqlx v1.3.4
github.com/stretchr/testify v1.7.0
github.com/swaggest/form/v5 v5.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/godogx/resource v0.1.0 h1:sKb8UtuYERQW9BKfvlXCMTye3EDXS7dTkd3KZWrDf0s=
github.com/godogx/resource v0.1.0/go.mod h1:yzBWW0+dz7/BA0VPpLXexgA4MfJYLMxsV5iK9nwObBk=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
Expand Down
9 changes: 5 additions & 4 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ import (
"github.com/bool64/shared"
"github.com/bool64/sqluct"
"github.com/cucumber/godog"
"github.com/godogx/resource"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/swaggest/form/v5"
Expand All @@ -140,7 +141,7 @@ const Default = "default"

// RegisterSteps adds database manager context to test suite.
func (m *Manager) RegisterSteps(s *godog.ScenarioContext) {
m.sync.register(s)
m.lock.Register(s)
m.registerPrerequisites(s)
m.registerAssertions(s)
}
Expand Down Expand Up @@ -228,14 +229,14 @@ func NewManager() *Manager {
return &Manager{
TableMapper: NewTableMapper(),
Instances: make(map[string]Instance),
sync: newSynchronized(nil),
lock: resource.NewLock(nil),
Vars: &shared.Vars{},
}
}

// Manager owns database connections.
type Manager struct {
sync *synchronized
lock *resource.Lock

TableMapper *TableMapper
Instances map[string]Instance
Expand Down Expand Up @@ -291,7 +292,7 @@ func (m *Manager) instance(ctx context.Context, tableName, dbName string) (Insta
}

// Locking per table.
_, err := m.sync.acquireLock(ctx, dbName+"::"+tableName)
_, err := m.lock.Acquire(ctx, dbName+"::"+tableName)
if err != nil {
return Instance{}, nil, ctx, err
}
Expand Down
13 changes: 2 additions & 11 deletions manager_concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ import (
"github.com/stretchr/testify/assert"
)

func (s *synchronized) isLocked(ctx context.Context, service string) bool {
s.mu.Lock()
defer s.mu.Unlock()

lock := s.locks[service]

return lock != nil && lock != ctx.Value(s.ctxKey).(chan struct{})
}

func TestNewManager_concurrent(t *testing.T) {
dbm := NewManager()

Expand Down Expand Up @@ -61,7 +52,7 @@ func TestNewManager_concurrent(t *testing.T) {
ScenarioInitializer: func(s *godog.ScenarioContext) {
dbm.RegisterSteps(s)
s.Step(`^I should not be blocked for "([^"]*)"$`, func(ctx context.Context, key string) error {
if dbm.sync.isLocked(ctx, key) {
if dbm.lock.IsLocked(ctx, key) {
return fmt.Errorf("%s is locked", key)
}

Expand Down Expand Up @@ -122,7 +113,7 @@ func TestNewManager_concurrent_blocked(t *testing.T) {
ScenarioInitializer: func(s *godog.ScenarioContext) {
dbm.RegisterSteps(s)
s.Step(`^I should not be blocked for "([^"]*)"$`, func(ctx context.Context, key string) error {
if dbm.sync.isLocked(ctx, key) {
if dbm.lock.IsLocked(ctx, key) {
return fmt.Errorf("%s is locked", key)
}

Expand Down
116 changes: 0 additions & 116 deletions sync.go

This file was deleted.

0 comments on commit e823cdc

Please sign in to comment.