Skip to content

Commit 278b456

Browse files
committed
adjust logic + add tests
1 parent fc93e9a commit 278b456

File tree

4 files changed

+358
-44
lines changed

4 files changed

+358
-44
lines changed

backend/instancemgmt/instance_manager.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ var (
1818
Name: "active_instances",
1919
Help: "The number of active plugin instances",
2020
})
21-
)
22-
23-
const (
2421
disposeTTL = 5 * time.Second
2522
)
2623

backend/instancemgmt/instance_manager_test.go

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ func TestInstanceManager(t *testing.T) {
4444
Updated: time.Now(),
4545
},
4646
}
47+
origDisposeTTL := disposeTTL
48+
disposeTTL = time.Millisecond
49+
t.Cleanup(func() {
50+
disposeTTL = origDisposeTTL
51+
})
4752
newInstance, err := im.Get(ctx, pCtxUpdated)
4853

4954
t.Run("New instance should be created", func(t *testing.T) {
@@ -66,44 +71,6 @@ func TestInstanceManager(t *testing.T) {
6671
})
6772
}
6873

69-
func TestInstanceManagerExpiration(t *testing.T) {
70-
ctx := context.Background()
71-
pCtx := backend.PluginContext{
72-
OrgID: 1,
73-
AppInstanceSettings: &backend.AppInstanceSettings{
74-
Updated: time.Now(),
75-
},
76-
}
77-
78-
tip := &testInstanceProvider{}
79-
im := newTTLInstanceManager(tip, time.Millisecond, 2*time.Millisecond)
80-
81-
instance, err := im.Get(ctx, pCtx)
82-
require.NoError(t, err)
83-
require.NotNil(t, instance)
84-
require.Equal(t, pCtx.OrgID, instance.(*testInstance).orgID)
85-
require.Equal(t, pCtx.AppInstanceSettings.Updated, instance.(*testInstance).updated)
86-
87-
t.Run("After expiration", func(t *testing.T) {
88-
instance.(*testInstance).wg.Wait()
89-
require.True(t, instance.(*testInstance).disposed.Load())
90-
require.Equal(t, int64(1), instance.(*testInstance).disposedTimes.Load())
91-
92-
newInstance, err := im.Get(ctx, pCtx)
93-
94-
t.Run("New instance should be created", func(t *testing.T) {
95-
require.NoError(t, err)
96-
require.NotNil(t, newInstance)
97-
require.Equal(t, pCtx.OrgID, newInstance.(*testInstance).orgID)
98-
require.Equal(t, pCtx.AppInstanceSettings.Updated, newInstance.(*testInstance).updated)
99-
})
100-
101-
t.Run("New instance should not be the same as old instance", func(t *testing.T) {
102-
require.NotSame(t, instance, newInstance)
103-
})
104-
})
105-
}
106-
10774
func TestInstanceManagerConcurrency(t *testing.T) {
10875
t.Run("Check possible race condition issues when initially creating instance", func(t *testing.T) {
10976
ctx := context.Background()
@@ -144,6 +111,12 @@ func TestInstanceManagerConcurrency(t *testing.T) {
144111
})
145112

146113
t.Run("Check possible race condition issues when re-creating instance on settings update", func(t *testing.T) {
114+
origDisposeTTL := disposeTTL
115+
disposeTTL = time.Millisecond
116+
t.Cleanup(func() {
117+
disposeTTL = origDisposeTTL
118+
})
119+
147120
ctx := context.Background()
148121
initialPCtx := backend.PluginContext{
149122
OrgID: 1,
@@ -152,7 +125,7 @@ func TestInstanceManagerConcurrency(t *testing.T) {
152125
},
153126
}
154127
tip := &testInstanceProvider{}
155-
im := newTTLInstanceManager(tip, defaultInstanceTTL, defaultInstanceCleanupInterval)
128+
im := New(tip)
156129
// Creating initial instance with old contexts
157130
instanceToDispose, _ := im.Get(ctx, initialPCtx)
158131

backend/instancemgmt/intance_manager_ttl.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
)
1212

1313
const (
14-
defaultInstanceTTL = 24 * time.Hour
15-
defaultInstanceCleanupInterval = 48 * time.Hour
14+
defaultInstanceTTL = 1 * time.Hour
15+
defaultInstanceCleanupInterval = 2 * time.Hour
1616
)
1717

1818
// NewTTLInstanceManager creates a new instance manager with TTL-based caching.
@@ -69,6 +69,7 @@ func (im *instanceManagerWithTTL) Get(ctx context.Context, pluginContext backend
6969
needsUpdate := im.provider.NeedsUpdate(ctx, pluginContext, ci)
7070

7171
if !needsUpdate {
72+
im.cache.SetDefault(cacheKey, ci)
7273
return ci.instance, nil
7374
}
7475
}
@@ -81,6 +82,7 @@ func (im *instanceManagerWithTTL) Get(ctx context.Context, pluginContext backend
8182
needsUpdate := im.provider.NeedsUpdate(ctx, pluginContext, ci)
8283

8384
if !needsUpdate {
85+
im.cache.SetDefault(cacheKey, ci)
8486
return ci.instance, nil
8587
}
8688

0 commit comments

Comments
 (0)