-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathpool_test.go
More file actions
244 lines (215 loc) · 6.71 KB
/
pool_test.go
File metadata and controls
244 lines (215 loc) · 6.71 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
/*
Copyright 2025 The llm-d 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.
*/
//nolint:testpackage // need to test internal types
package tokenization
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"k8s.io/client-go/util/workqueue"
types "github.com/llm-d/llm-d-kv-cache/pkg/tokenization/types"
)
// MockTokenizer implements the Tokenizer interface for testing.
type MockTokenizer struct {
mock.Mock
}
func (m *MockTokenizer) RenderChat(renderReq *types.RenderChatRequest) ([]uint32, []types.Offset, error) {
args := m.Called(renderReq)
tokenIface := args.Get(0)
if tokenIface == nil {
return nil, nil, args.Error(2)
}
tokens, ok := tokenIface.([]uint32)
if !ok {
panic("MockTokenizer.RenderChat: expected []uint32 from mock, got unexpected type")
}
offsetIface := args.Get(1)
if offsetIface == nil {
return nil, nil, args.Error(2)
}
offsets, ok := offsetIface.([]types.Offset)
if !ok {
panic("MockTokenizer.RenderChat: expected []types.Offset from mock, got unexpected type")
}
return tokens, offsets, args.Error(2)
}
func (m *MockTokenizer) Render(prompt string) ([]uint32, []types.Offset, error) {
args := m.Called(prompt)
tokenIface := args.Get(0)
if tokenIface == nil {
return nil, nil, args.Error(2)
}
tokens, ok := tokenIface.([]uint32)
if !ok {
panic("MockTokenizer.Render: expected []uint32 from mock, got unexpected type")
}
offsetIface := args.Get(1)
if offsetIface == nil {
return nil, nil, args.Error(2)
}
offsets, ok := offsetIface.([]types.Offset)
if !ok {
panic("MockTokenizer.Render: expected []types.Offset from mock, got unexpected type")
}
return tokens, offsets, args.Error(2)
}
func (m *MockTokenizer) Close() error {
return nil
}
func (m *MockTokenizer) Type() string {
return "mock"
}
func TestPool_ProcessTask(t *testing.T) {
mockTokenizer := &MockTokenizer{}
pool := &Pool{
modelName: "test-model",
workers: 1,
tokenizer: mockTokenizer,
}
task := Task{
Prompt: "hello world",
}
// Setup specific mock return values
expectedTokens := []uint32{12345, 67890, 11111}
expectedOffsets := []types.Offset{{0, 5}, {6, 11}}
mockTokenizer.On("Render", task.Prompt).
Return(expectedTokens, expectedOffsets, nil)
// Execute
err := pool.processTask(task)
// Assert
assert.NoError(t, err)
mockTokenizer.AssertExpectations(t)
}
func TestPool_WorkerLoop(t *testing.T) {
specs := map[string]struct {
setupMocks func(*MockTokenizer)
genTasks func() ([]Task, chan tokenizationResponse)
verify func(t *testing.T, pool *Pool, tasks []Task, resultChan chan tokenizationResponse)
}{
"successful task processing": {
setupMocks: func(mt *MockTokenizer) {
mt.On("Render", "test prompt").
Return([]uint32{1, 2, 3}, []types.Offset{{0, 4}}, nil)
},
genTasks: func() ([]Task, chan tokenizationResponse) {
return []Task{{Prompt: "test prompt"}}, nil
},
verify: func(t *testing.T, pool *Pool, tasks []Task, resultChan chan tokenizationResponse) {}, //nolint:thelper // noop
},
"task with result channel": {
setupMocks: func(mt *MockTokenizer) {
mt.On("Render", "test with channel").
Return([]uint32{10, 20, 30}, []types.Offset{{0, 4}}, nil)
},
genTasks: func() ([]Task, chan tokenizationResponse) {
ch := make(chan tokenizationResponse, 1)
return []Task{{
Prompt: "test with channel",
ResultCh: ch,
}}, ch
},
verify: func(t *testing.T, pool *Pool, tasks []Task, resultCh chan tokenizationResponse) {
t.Helper()
require.Eventually(t, func() bool {
if result, ok := <-resultCh; ok {
assert.Equal(t, []uint32{10, 20, 30}, result.Tokens)
return true
}
return false
}, time.Second, 10*time.Millisecond)
// Verify channel is closed
require.Eventually(t, func() bool {
_, ok := <-resultCh
return !ok
}, time.Second, 10*time.Millisecond)
},
},
"multiple tasks processing": {
setupMocks: func(mt *MockTokenizer) {
for i := range 5 {
prompt := "prompt " + string(rune('a'+i))
tokens := []uint32{uint32(i), uint32(i + 1)} // i is bounded by range 5, no overflow
offsets := []types.Offset{{0, 6}}
mt.On("Render", prompt).
Return(tokens, offsets, nil).Once()
}
},
genTasks: func() ([]Task, chan tokenizationResponse) {
tasks := make([]Task, 5)
for i := range 5 {
tasks[i] = Task{Prompt: "prompt " + string(rune('a'+i))}
}
return tasks, nil
},
verify: func(t *testing.T, pool *Pool, tasks []Task, resultChan chan tokenizationResponse) {
t.Helper()
require.Eventually(t, func() bool {
return pool.queue.Len() == 0
}, time.Second, 10*time.Millisecond, "queue should be drained")
},
},
"max retries exceeded": {
setupMocks: func(mt *MockTokenizer) {
// Mock will fail every time, causing retries
mt.On("Render", "failing prompt").Return(
[]uint32{}, []types.Offset{}, assert.AnError)
},
genTasks: func() ([]Task, chan tokenizationResponse) {
ch := make(chan tokenizationResponse, 1)
return []Task{{
Prompt: "failing prompt",
ResultCh: ch,
}}, ch
},
verify: func(t *testing.T, pool *Pool, tasks []Task, resultCh chan tokenizationResponse) {
t.Helper()
require.Eventually(t, func() bool { // channel is closed, when max retries exceeded
if result, ok := <-resultCh; !ok {
assert.Equal(t, tokenizationResponse{}, result)
return true
}
return false
}, time.Second, 10*time.Millisecond)
require.Eventually(t, func() bool {
return pool.queue.Len() == 0
}, time.Second, 10*time.Millisecond)
},
},
}
for name, tt := range specs {
t.Run(name, func(t *testing.T) {
mockTokenizer := &MockTokenizer{}
tt.setupMocks(mockTokenizer)
pool := &Pool{
modelName: "test-model",
workers: 1,
queue: workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedControllerRateLimiter[Task]()),
tokenizer: mockTokenizer,
}
tasks, resultChan := tt.genTasks()
for _, task := range tasks {
pool.queue.Add(task)
}
pool.wg.Add(1)
go pool.workerLoop(0)
tt.verify(t, pool, tasks, resultChan)
// Shutdown
pool.queue.ShutDown()
pool.wg.Wait()
// Assert expectations
mockTokenizer.AssertExpectations(t)
})
}
}