Skip to content

Commit f88a934

Browse files
committed
fix test race
1 parent 8fb0043 commit f88a934

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

coderd/parameters_test.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package coderd_test
33
import (
44
"context"
55
"os"
6+
"sync"
67
"testing"
78

89
"github.com/google/uuid"
910
"github.com/stretchr/testify/require"
11+
"go.uber.org/atomic"
1012
"golang.org/x/xerrors"
1113

1214
"github.com/coder/coder/v2/coderd"
@@ -199,8 +201,15 @@ func TestDynamicParametersWithTerraformValues(t *testing.T) {
199201
modulesArchive, err := terraform.GetModulesArchive(os.DirFS("testdata/parameters/modules"))
200202
require.NoError(t, err)
201203

204+
c := atomic.NewInt32(0)
205+
reject := &dbRejectGitSSHKey{Store: db, hook: func(d *dbRejectGitSSHKey) {
206+
if c.Add(1) > 1 {
207+
// Second call forward, reject
208+
d.SetReject(true)
209+
}
210+
}}
202211
setup := setupDynamicParamsTest(t, setupDynamicParamsTestParams{
203-
db: &dbRejectGitSSHKey{Store: db},
212+
db: reject,
204213
ps: ps,
205214
provisionerDaemonVersion: provProto.CurrentVersion.String(),
206215
mainTF: dynamicParametersTerraformSource,
@@ -444,8 +453,30 @@ func setupDynamicParamsTest(t *testing.T, args setupDynamicParamsTestParams) dyn
444453
// that is generally impossible to force an error.
445454
type dbRejectGitSSHKey struct {
446455
database.Store
456+
rejectMu sync.RWMutex
457+
reject bool
458+
hook func(d *dbRejectGitSSHKey)
459+
}
460+
461+
// SetReject toggles whether GetGitSSHKey should return an error or passthrough to the underlying store.
462+
func (d *dbRejectGitSSHKey) SetReject(reject bool) {
463+
d.rejectMu.Lock()
464+
defer d.rejectMu.Unlock()
465+
d.reject = reject
447466
}
448467

449-
func (*dbRejectGitSSHKey) GetGitSSHKey(_ context.Context, _ uuid.UUID) (database.GitSSHKey, error) {
450-
return database.GitSSHKey{}, xerrors.New("forcing a fake error")
468+
func (d *dbRejectGitSSHKey) GetGitSSHKey(ctx context.Context, userID uuid.UUID) (database.GitSSHKey, error) {
469+
if d.hook != nil {
470+
d.hook(d)
471+
}
472+
473+
d.rejectMu.RLock()
474+
reject := d.reject
475+
d.rejectMu.RUnlock()
476+
477+
if reject {
478+
return database.GitSSHKey{}, xerrors.New("forcing a fake error")
479+
}
480+
481+
return d.Store.GetGitSSHKey(ctx, userID)
451482
}

0 commit comments

Comments
 (0)