-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathplugin_state_test.go
More file actions
300 lines (242 loc) · 9.42 KB
/
Copy pathplugin_state_test.go
File metadata and controls
300 lines (242 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package plugin
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
logutil "github.com/llm-d/llm-d-router/pkg/common/observability/logging"
)
// pluginTestData implements the StateData interface for testing purposes.
// It provides a simple string value that can be stored and retrieved.
type pluginTestData struct {
value string
}
// Clone implements the StateData interface, creating a deep copy of the data.
func (d *pluginTestData) Clone() StateData {
if d == nil {
return nil
}
return &pluginTestData{value: d.value}
}
type evictableTestData struct {
pluginTestData
evictedID string
evictedKey StateKey
}
func (d *evictableTestData) OnEvicted(requestID string, key StateKey) {
d.evictedID = requestID
d.evictedKey = key
}
// Clone implements the StateData interface, ensuring that the cloned data
// remains evictable (OnEvicted is not lost).
func (d *evictableTestData) Clone() StateData {
if d == nil {
return nil
}
return &evictableTestData{
pluginTestData: pluginTestData{value: d.value},
evictedID: d.evictedID,
evictedKey: d.evictedKey,
}
}
func TestEvictableTestData_Clone(t *testing.T) {
data := &evictableTestData{
pluginTestData: pluginTestData{value: "test"},
}
cloned := data.Clone()
evictable, ok := cloned.(EvictableStateData)
assert.True(t, ok, "cloned data should satisfy EvictableStateData")
evictable.OnEvicted("req-1", "key-1")
assert.Equal(t, "req-1", cloned.(*evictableTestData).evictedID)
assert.Equal(t, StateKey("key-1"), cloned.(*evictableTestData).evictedKey)
}
// TestPluginState_EvictionCallback verifies that OnEvicted is called when data is removed.
func TestPluginState_EvictionCallback(t *testing.T) {
ctx, cancel := context.WithCancel(logutil.NewTestLoggerIntoContext(context.Background()))
t.Cleanup(cancel)
state := NewPluginState(ctx)
requestID := "req-evict"
key := StateKey("foo")
data := &evictableTestData{pluginTestData: pluginTestData{value: "bar"}}
state.Write(requestID, key, data)
// Case 1: DeleteKey
state.DeleteKey(requestID, key)
assert.Equal(t, requestID, data.evictedID)
assert.Equal(t, key, data.evictedKey)
// Reset
data.evictedID = ""
data.evictedKey = ""
state.Write(requestID, key, data)
// Case 2: Delete (request wide)
state.Delete(requestID)
assert.Equal(t, requestID, data.evictedID)
assert.Equal(t, key, data.evictedKey)
// Case 3: Cleanup (stale request)
data.evictedID = ""
data.evictedKey = ""
state.Write(requestID, key, data)
state.requestToLastAccessTime.Store(requestID, time.Now().Add(-2*defaultStalenessThreshold))
state.cleanStaleRequests()
assert.Equal(t, requestID, data.evictedID)
assert.Equal(t, key, data.evictedKey)
}
// TestPluginState_Touch verifies that Touch extends request lifetime.
func TestPluginState_Touch(t *testing.T) {
ctx, cancel := context.WithCancel(logutil.NewTestLoggerIntoContext(context.Background()))
t.Cleanup(cancel)
state := NewPluginState(ctx)
requestID := "req-touch"
key := StateKey("foo")
data := &pluginTestData{value: "bar"}
state.Write(requestID, key, data)
// Set last access time to near-stale
nearStale := time.Now().Add(-defaultStalenessThreshold + time.Second*10)
state.requestToLastAccessTime.Store(requestID, nearStale)
// Touch it
state.Touch(requestID)
// Verify access time was updated to now
val, ok := state.requestToLastAccessTime.Load(requestID)
assert.True(t, ok)
lastAccess := val.(time.Time)
assert.True(t, lastAccess.After(nearStale))
assert.True(t, time.Since(lastAccess) < time.Second)
// Manually cleanup, should NOT be removed
state.cleanStaleRequests()
_, err := state.Read(requestID, key)
assert.NoError(t, err)
}
// TestPluginState_ReadWrite verifies the basic operations of PluginState:
// - Writing data for a request
// - Reading the data back
// - Deleting the data and confirming it's removed
func TestPluginState_ReadWrite(t *testing.T) {
ctx, cancel := context.WithCancel(logutil.NewTestLoggerIntoContext(context.Background()))
t.Cleanup(cancel)
state := NewPluginState(ctx)
req1 := "req1"
key := StateKey("key")
data1 := "bar1"
req2 := "req2"
data2 := "bar2"
// Write data to the state storage
state.Write(req1, key, &pluginTestData{value: data1})
state.Write(req2, key, &pluginTestData{value: data2})
// Read back the req1 data and verify its content
readData, err := state.Read(req1, key)
assert.NoError(t, err)
td, ok := readData.(*pluginTestData)
assert.True(t, ok, "should be able to cast to pluginTestData")
assert.Equal(t, data1, td.value)
// Delete the req2 data and verify content that was read before is still valid
readData, err = state.Read(req2, key)
assert.NoError(t, err)
state.Delete(req2)
td, ok = readData.(*pluginTestData)
assert.True(t, ok, "should be able to cast to pluginTestData")
assert.Equal(t, data2, td.value)
// try to read again aftet deletion, verify error
readData, err = state.Read(req2, key)
assert.Equal(t, ErrNotFound, err)
assert.Nil(t, readData, "expected no data after delete")
// Read back the req1 data and verify its content after the req2 deleted
readData, err = state.Read(req1, key)
assert.NoError(t, err)
td, ok = readData.(*pluginTestData)
assert.True(t, ok, "should be able to cast to pluginTestData")
assert.Equal(t, data1, td.value)
}
// TestReadPluginStateKey tests the generic helper function ReadPluginStateKey which provides
// type-safe access to stored data. It verifies:
// - Successful type assertion and data retrieval
// - Error handling for non-existent keys
func TestReadPluginStateKey(t *testing.T) {
ctx, cancel := context.WithCancel(logutil.NewTestLoggerIntoContext(context.Background()))
t.Cleanup(cancel)
state := NewPluginState(ctx)
requestID := "req-1"
key := StateKey("foo")
data := &pluginTestData{value: "bar"}
state.Write(requestID, key, data)
// Read
val, err := ReadPluginStateKey[*pluginTestData](state, requestID, key)
assert.NoError(t, err)
assert.Equal(t, "bar", val.value)
// Not Found
_, err = ReadPluginStateKey[*pluginTestData](state, "not-exist", key)
assert.Equal(t, ErrNotFound, err)
}
// TestPluginState_Cleanup verifies the automatic cleanup of stale data.
// It tests that data which hasn't been accessed for longer than the staleness threshold
// is properly removed from the storage.
func TestPluginState_Cleanup(t *testing.T) {
ctx, cancel := context.WithCancel(logutil.NewTestLoggerIntoContext(context.Background()))
t.Cleanup(cancel)
state := NewPluginState(ctx)
requestID := "req-stale"
key := StateKey("foo")
data := &pluginTestData{value: "bar"}
state.Write(requestID, key, data)
// Manually set last access time to far in the past
state.requestToLastAccessTime.Store(requestID, time.Now().Add(-2*defaultStalenessThreshold))
// Manually CleanUp
state.cleanStaleRequests()
_, err := state.Read(requestID, key)
assert.Equal(t, ErrNotFound, err)
}
// TestSetDefaultStalenessThreshold verifies that the process-wide default is applied to new
// PluginState instances, that a configured value drives reaping, and that non-positive values are
// ignored.
func TestSetDefaultStalenessThreshold(t *testing.T) {
orig := defaultStalenessThreshold
t.Cleanup(func() { defaultStalenessThreshold = orig })
SetDefaultStalenessThreshold(30 * time.Second)
assert.Equal(t, 30*time.Second, defaultStalenessThreshold)
ctx, cancel := context.WithCancel(logutil.NewTestLoggerIntoContext(context.Background()))
t.Cleanup(cancel)
state := NewPluginState(ctx)
assert.Equal(t, 30*time.Second, state.stalenessThreshold, "new state should inherit the configured default")
// A one-minute-old request is stale under the 30s configured threshold (but would survive the 5m built-in default).
requestID := "req-configured-threshold"
key := StateKey("foo")
state.Write(requestID, key, &pluginTestData{value: "bar"})
state.requestToLastAccessTime.Store(requestID, time.Now().Add(-time.Minute))
state.cleanStaleRequests()
_, err := state.Read(requestID, key)
assert.Equal(t, ErrNotFound, err, "request older than the configured threshold should be reaped")
SetDefaultStalenessThreshold(0)
assert.Equal(t, 30*time.Second, defaultStalenessThreshold, "non-positive value must be ignored")
}
// TestPluginState_DeleteKey verifies that DeleteKey correctly removes only the specified key for a request.
func TestPluginState_DeleteKey(t *testing.T) {
ctx, cancel := context.WithCancel(logutil.NewTestLoggerIntoContext(context.Background()))
t.Cleanup(cancel)
state := NewPluginState(ctx)
requestID := "req-1"
key1 := StateKey("key1")
key2 := StateKey("key2")
data1 := &pluginTestData{value: "val1"}
data2 := &pluginTestData{value: "val2"}
state.Write(requestID, key1, data1)
state.Write(requestID, key2, data2)
// Delete key1
state.DeleteKey(requestID, key1)
// Verify key1 is gone
_, err := state.Read(requestID, key1)
assert.Equal(t, ErrNotFound, err)
// Verify key2 is still there
val2, err := state.Read(requestID, key2)
assert.NoError(t, err)
assert.Equal(t, "val2", val2.(*pluginTestData).value)
}