Skip to content

Commit a3ed029

Browse files
committed
fix race condition in test
1 parent 2ccc384 commit a3ed029

File tree

2 files changed

+10
-26
lines changed

2 files changed

+10
-26
lines changed

backend/instancemgmt/instance_manager.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ var (
1919
Name: "active_instances",
2020
Help: "The number of active plugin instances",
2121
})
22-
disposeTTL = 5 * time.Second
22+
defaultDisposeTTL = 5 * time.Second
2323

24-
instanceTTL = 24 * time.Hour
25-
instanceCleanup = 48 * time.Hour
24+
defaultInstanceTTL = 24 * time.Hour
25+
defaultInstanceCleanup = 48 * time.Hour
2626
)
2727

2828
// Instance is a marker interface for an instance.
@@ -77,6 +77,10 @@ type InstanceProvider interface {
7777

7878
// New create a new instance manager.
7979
func New(provider InstanceProvider) InstanceManager {
80+
return NewWithOptions(provider, defaultInstanceTTL, defaultInstanceCleanup, defaultDisposeTTL)
81+
}
82+
83+
func NewWithOptions(provider InstanceProvider, instanceTTL, instanceCleanup, disposeTTL time.Duration) InstanceManager {
8084
if provider == nil {
8185
panic("provider cannot be nil")
8286
}

backend/instancemgmt/instance_manager_test.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestInstanceManager(t *testing.T) {
2121
}
2222

2323
tip := &testInstanceProvider{}
24-
im := New(tip)
24+
im := NewWithOptions(tip, defaultInstanceTTL, defaultInstanceCleanup, time.Millisecond)
2525

2626
t.Run("When getting instance should create a new instance", func(t *testing.T) {
2727
instance, err := im.Get(ctx, pCtx)
@@ -43,11 +43,6 @@ func TestInstanceManager(t *testing.T) {
4343
Updated: time.Now(),
4444
},
4545
}
46-
origDisposeTTL := disposeTTL
47-
disposeTTL = time.Millisecond
48-
t.Cleanup(func() {
49-
disposeTTL = origDisposeTTL
50-
})
5146
newInstance, err := im.Get(ctx, pCtxUpdated)
5247

5348
t.Run("New instance should be created", func(t *testing.T) {
@@ -79,17 +74,8 @@ func TestInstanceManagerExpiration(t *testing.T) {
7974
},
8075
}
8176

82-
origInstanceTTL := instanceTTL
83-
instanceTTL = time.Millisecond
84-
origInstanceCleanup := instanceCleanup
85-
instanceCleanup = 2 * time.Millisecond
86-
t.Cleanup(func() {
87-
instanceTTL = origInstanceTTL
88-
instanceCleanup = origInstanceCleanup
89-
})
90-
9177
tip := &testInstanceProvider{}
92-
im := New(tip)
78+
im := NewWithOptions(tip, time.Millisecond, 2*time.Millisecond, defaultDisposeTTL)
9379

9480
instance, err := im.Get(ctx, pCtx)
9581
require.NoError(t, err)
@@ -157,12 +143,6 @@ func TestInstanceManagerConcurrency(t *testing.T) {
157143
})
158144

159145
t.Run("Check possible race condition issues when re-creating instance on settings update", func(t *testing.T) {
160-
origDisposeTTL := disposeTTL
161-
disposeTTL = time.Millisecond
162-
t.Cleanup(func() {
163-
disposeTTL = origDisposeTTL
164-
})
165-
166146
ctx := context.Background()
167147
initialPCtx := backend.PluginContext{
168148
OrgID: 1,
@@ -171,7 +151,7 @@ func TestInstanceManagerConcurrency(t *testing.T) {
171151
},
172152
}
173153
tip := &testInstanceProvider{}
174-
im := New(tip)
154+
im := NewWithOptions(tip, defaultInstanceTTL, defaultInstanceCleanup, time.Millisecond)
175155
// Creating initial instance with old contexts
176156
instanceToDispose, _ := im.Get(ctx, initialPCtx)
177157

0 commit comments

Comments
 (0)