-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_linux_test.go
More file actions
376 lines (312 loc) · 9.55 KB
/
handler_linux_test.go
File metadata and controls
376 lines (312 loc) · 9.55 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// ©Hayabusa Cloud Co., Ltd. 2026. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build linux
package uring_test
import (
"errors"
"testing"
"code.hybscloud.com/iofd"
"code.hybscloud.com/uring"
)
// Test MultishotSubscriber builder pattern
func TestMultishotSubscriber(t *testing.T) {
resultCalled := false
errorCalled := false
stopCalled := false
sub := uring.NewMultishotSubscriber().
OnStep(func(step uring.MultishotStep) uring.MultishotAction {
if step.Err == nil {
resultCalled = true
if step.CQE.Res >= 0 {
return uring.MultishotContinue
}
}
errorCalled = true
return uring.MultishotStop
}).
OnStop(func(error, bool) {
stopCalled = true
})
handler := sub.Handler()
// Test result step.
if handler.OnMultishotStep(uring.MultishotStep{CQE: uring.CQEView{Res: 42, Flags: uring.IORING_CQE_F_MORE}}) != uring.MultishotContinue {
t.Error("result step should return MultishotContinue for positive result")
}
if !resultCalled {
t.Error("result callback not called")
}
// Test error step.
if handler.OnMultishotStep(uring.MultishotStep{CQE: uring.CQEView{Res: -1, Flags: uring.IORING_CQE_F_MORE}, Err: errors.New("boom")}) != uring.MultishotStop {
t.Error("error step should return MultishotStop")
}
if !errorCalled {
t.Error("error callback not called")
}
// Test stop callback.
handler.OnMultishotStop(nil, false)
if !stopCalled {
t.Error("stop callback not called")
}
}
// Test ListenerSubscriber builder pattern
func TestListenerSubscriber(t *testing.T) {
socketCreatedCalled := false
boundCalled := false
listeningCalled := false
errorCalled := false
sub := uring.NewListenerSubscriber().
OnSocketCreated(func(fd iofd.FD) bool {
socketCreatedCalled = true
return fd >= 0
}).
OnBound(func() bool {
boundCalled = true
return true
}).
OnListening(func() {
listeningCalled = true
}).
OnError(func(op uint8, err error) {
errorCalled = true
})
handler := sub.Handler()
// Test OnSocketCreated
if !handler.OnSocketCreated(5) {
t.Error("OnSocketCreated should return true for valid fd")
}
if !socketCreatedCalled {
t.Error("OnSocketCreated callback not called")
}
// Test OnBound
if !handler.OnBound() {
t.Error("OnBound should return true")
}
if !boundCalled {
t.Error("OnBound callback not called")
}
// Test OnListening
handler.OnListening()
if !listeningCalled {
t.Error("OnListening callback not called")
}
// Test OnError
handler.OnError(1, nil)
if !errorCalled {
t.Error("OnError callback not called")
}
}
// Test IncrementalSubscriber builder pattern
func TestIncrementalSubscriber(t *testing.T) {
dataCalled := false
completeCalled := false
errorCalled := false
sub := uring.NewIncrementalSubscriber().
OnData(func(buf []byte, hasMore bool) {
dataCalled = true
}).
OnComplete(func() {
completeCalled = true
}).
OnError(func(err error) {
errorCalled = true
})
handler := sub.Handler()
// Test OnData
handler.OnData([]byte("test"), true)
if !dataCalled {
t.Error("OnData callback not called")
}
// Test OnComplete
handler.OnComplete()
if !completeCalled {
t.Error("OnComplete callback not called")
}
// Test OnError
handler.OnError(nil)
if !errorCalled {
t.Error("OnError callback not called")
}
}
// Test ZCSubscriber builder pattern
func TestZCSubscriber(t *testing.T) {
completedCalled := false
notificationCalled := false
sub := uring.NewZCSubscriber().
OnCompleted(func(result int32) {
completedCalled = true
}).
OnNotification(func(result int32) {
notificationCalled = true
})
handler := sub.Handler()
// Test OnCompleted
handler.OnCompleted(100)
if !completedCalled {
t.Error("OnCompleted callback not called")
}
// Test OnNotification
handler.OnNotification(0)
if !notificationCalled {
t.Error("OnNotification callback not called")
}
}
// Test NoopMultishotHandler default behavior
func TestNoopMultishotHandler(t *testing.T) {
var handler uring.NoopMultishotHandler
// Result steps should continue.
if handler.OnMultishotStep(uring.MultishotStep{CQE: uring.CQEView{Flags: uring.IORING_CQE_F_MORE}}) != uring.MultishotContinue {
t.Error("NoopMultishotHandler.OnMultishotStep(result) should return MultishotContinue")
}
// Error steps should stop.
if handler.OnMultishotStep(uring.MultishotStep{CQE: uring.CQEView{Res: -1, Flags: uring.IORING_CQE_F_MORE}, Err: errors.New("boom")}) != uring.MultishotStop {
t.Error("NoopMultishotHandler.OnMultishotStep(error) should return MultishotStop")
}
// Stop callbacks should not panic.
handler.OnMultishotStop(nil, false)
}
// Test NoopListenerHandler default behavior
func TestNoopListenerHandler(t *testing.T) {
var handler uring.NoopListenerHandler
// OnSocketCreated should return true
if !handler.OnSocketCreated(5) {
t.Error("NoopListenerHandler.OnSocketCreated should return true")
}
// OnBound should return true
if !handler.OnBound() {
t.Error("NoopListenerHandler.OnBound should return true")
}
// OnListening should not panic
handler.OnListening()
// OnError should not panic
handler.OnError(0, nil)
}
// Test NoopIncrementalHandler default behavior
func TestNoopIncrementalHandler(t *testing.T) {
var handler uring.NoopIncrementalHandler
// All methods should not panic
handler.OnData([]byte("test"), true)
handler.OnComplete()
handler.OnError(nil)
}
// Test NoopZCHandler default behavior
func TestNoopZCHandler(t *testing.T) {
var handler uring.NoopZCHandler
// All methods should not panic
handler.OnCompleted(100)
handler.OnNotification(0)
}
// Test embedding NoopMultishotHandler
func TestNoopMultishotHandlerEmbedding(t *testing.T) {
type customHandler struct {
uring.NoopMultishotHandler
count int
}
handler := &customHandler{}
// Override result handling.
onResult := func(cqe uring.CQEView) uring.MultishotAction {
handler.count++
return handler.NoopMultishotHandler.OnMultishotStep(uring.MultishotStep{CQE: cqe})
}
// Simulate calling the overridden method
if onResult(uring.CQEView{Res: 1}) != uring.MultishotContinue {
t.Error("embedded handler should return MultishotContinue")
}
if handler.count != 1 {
t.Errorf("count = %d, want 1", handler.count)
}
}
// Test MultishotSubscriber default handlers
func TestMultishotSubscriberDefaults(t *testing.T) {
// Test with NO overrides - use default handlers
sub := uring.NewMultishotSubscriber()
handler := sub.Handler()
// Default successful step handling returns continue.
if handler.OnMultishotStep(uring.MultishotStep{CQE: uring.CQEView{Res: 1, Flags: uring.IORING_CQE_F_MORE}}) != uring.MultishotContinue {
t.Error("default successful step handling should return MultishotContinue")
}
// Default error step handling returns stop.
if handler.OnMultishotStep(uring.MultishotStep{CQE: uring.CQEView{Res: -1, Flags: uring.IORING_CQE_F_MORE}, Err: errors.New("boom")}) != uring.MultishotStop {
t.Error("default error step handling should return MultishotStop")
}
// Default stop handling is no-op (should not panic).
handler.OnMultishotStop(nil, false)
}
// Test ListenerSubscriber default handlers
func TestListenerSubscriberDefaults(t *testing.T) {
// Test with NO overrides - use default handlers
sub := uring.NewListenerSubscriber()
handler := sub.Handler()
// Default OnSocketCreated returns true
if !handler.OnSocketCreated(5) {
t.Error("default OnSocketCreated should return true")
}
// Default OnBound returns true
if !handler.OnBound() {
t.Error("default OnBound should return true")
}
// Default OnListening is no-op (should not panic)
handler.OnListening()
// Default OnError is no-op (should not panic)
handler.OnError(0, nil)
}
// Test MultishotSubscriber direct callback methods
func TestMultishotSubscriberCallbacks(t *testing.T) {
resultCalled := false
errorCalled := false
stopCalled := false
sub := uring.NewMultishotSubscriber().
OnStep(func(step uring.MultishotStep) uring.MultishotAction {
if step.Err == nil {
resultCalled = true
return uring.MultishotContinue
}
errorCalled = true
return uring.MultishotStop
}).
OnStop(func(error, bool) {
stopCalled = true
})
// Test OnMultishotStep directly.
if sub.OnMultishotStep(uring.MultishotStep{CQE: uring.CQEView{Res: -1, Flags: uring.IORING_CQE_F_MORE}, Err: errors.New("boom")}) != uring.MultishotStop {
t.Error("error step should return MultishotStop")
}
if !errorCalled {
t.Error("error callback not called")
}
if sub.OnMultishotStep(uring.MultishotStep{CQE: uring.CQEView{Res: 1, Flags: uring.IORING_CQE_F_MORE}}) != uring.MultishotContinue {
t.Error("result step should return MultishotContinue")
}
if !resultCalled {
t.Error("result callback not called")
}
// Test stop callback directly.
sub.OnMultishotStop(nil, false)
if !stopCalled {
t.Error("stop callback not called")
}
}
// =============================================================================
// handler_noop.go coverage exercises noop handler methods.
// =============================================================================
func TestNoopHandlerMethods(t *testing.T) {
// NoopMultishotHandler
var mh uring.NoopMultishotHandler
mh.OnMultishotStop(nil, false)
mh.OnMultishotStop(errors.New("test"), true)
// NoopListenerHandler
var lh uring.NoopListenerHandler
lh.OnListening()
lh.OnError(0, errors.New("test"))
// NoopIncrementalHandler
var ih uring.NoopIncrementalHandler
ih.OnData(nil, false)
ih.OnData([]byte("data"), true)
ih.OnComplete()
ih.OnError(errors.New("test"))
// NoopZCHandler
var zh uring.NoopZCHandler
zh.OnCompleted(0)
zh.OnCompleted(42)
zh.OnNotification(0)
}