Skip to content

Commit

Permalink
support optionFns to make changing refresh interval in tests more ele…
Browse files Browse the repository at this point in the history
…gant
  • Loading branch information
jayjanssen committed Jun 26, 2024
1 parent b98904f commit 34be40b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions pkg/dbconn/metadatalock.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type MetadataLock struct {
refreshInterval time.Duration
}

func NewMetadataLock(ctx context.Context, dsn string, lockName string, logger loggers.Advanced) (*MetadataLock, error) {
func NewMetadataLock(ctx context.Context, dsn string, lockName string, logger loggers.Advanced, optionFns ...func(*MetadataLock)) (*MetadataLock, error) {
if len(lockName) == 0 {
return nil, errors.New("metadata lock name is empty")
}
Expand All @@ -34,6 +34,11 @@ func NewMetadataLock(ctx context.Context, dsn string, lockName string, logger lo
refreshInterval: refreshInterval,
}

// Apply option functions
for _, optionFn := range optionFns {
optionFn(mdl)
}

// Setup the dedicated connection for this lock
dbConfig := NewDBConfig()
dbConfig.MaxOpenConnections = 1
Expand Down Expand Up @@ -71,9 +76,6 @@ func NewMetadataLock(ctx context.Context, dsn string, lockName string, logger lo
ctx, mdl.cancel = context.WithCancel(ctx)
mdl.closeCh = make(chan error)
go func() {
// The only reason to sleep here is to give our tests a chance to override the mdl.refreshInterval
time.Sleep(1 * time.Second)

ticker := time.NewTicker(mdl.refreshInterval)
defer ticker.Stop()
for {
Expand Down
8 changes: 4 additions & 4 deletions pkg/dbconn/metadatalock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func TestMetadataLockContextCancel(t *testing.T) {
func TestMetadataLockRefresh(t *testing.T) {
lockName := "test-refresh"
logger := logrus.New()
mdl, err := NewMetadataLock(context.Background(), testutils.DSN(), lockName, logger)
mdl, err := NewMetadataLock(context.Background(), testutils.DSN(), lockName, logger, func(mdl *MetadataLock) {
// override the refresh interval for faster testing
mdl.refreshInterval = 2 * time.Second
})
assert.NoError(t, err)
assert.NotNil(t, mdl)

// override the refresh interval to 5 seconds
mdl.refreshInterval = 2 * time.Second

// wait for the refresh to happen
time.Sleep(5 * time.Second)

Expand Down

0 comments on commit 34be40b

Please sign in to comment.