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
377
378
379
380
381
382
383
384
385
386
387
//! Macro for generating comprehensive IoDriver tests
//!
//! This macro generates a test suite for any IoDriver implementation.
//! Usage:
//! ```ignore
//! test_io_driver!(my_driver_instance);
//! ```
/// Generates a comprehensive test suite for an IoDriver implementation.
///
/// # Example
/// ```ignore
/// use lio::backends::{test_io_driver, dummy::DummyDriver};
///
/// test_io_driver!(DummyDriver);
/// ```
#[macro_export]
macro_rules! test_io_driver {
($driver:ident) => {
mod io_driver_tests {
use super::*;
use $crate::api::ops::Nop;
use $crate::backends::OpStore;
use $crate::backends::{IoBackend, IoDriver, IoSubmitter, SubmitErr};
use $crate::registration::StoredOp;
//
// #[test]
// fn test_state_lifecycle() {
// // Test that state can be created and dropped without errors
// let state = $driver::new_state().expect("Failed to create state");
// $driver::drop_state(state);
// }
// //
// #[test]
// fn test_new_from_state() {
// // Test that submitter and handler can be created from state
// let state = $driver::new_state().expect("Failed to create state");
// let (submitter, handler) =
// $driver::new(state).expect("Failed to create submitter and handler");
//
// // Ensure they are created (type check is compile-time)
// drop(submitter);
// drop(handler);
//
// $driver::drop_state(state);
// }
//
// #[test]
// fn test_submit_single_operation() {
// // Test submitting a single operation
// let state = $driver::new_state().expect("Failed to create state");
// let (mut submitter, mut handler) =
// $driver::new(state).expect("Failed to create submitter and handler");
//
// let store = OpStore::new();
// let op = StoredOp::new_waker(Nop);
// let id = store.insert(op);
//
// // Submit the operation
// let result = submitter.submit(state, id, &Nop);
//
// match result {
// Ok(()) => {
// // If submission succeeded, tick the handler to complete it
// let completed = handler
// .try_tick(state, &store)
// .expect("Handler tick failed");
//
// // The operation should be completed (or at least the handler should not error)
// assert!(
// completed.iter().any(|c| c.op_id == id) || completed.is_empty(),
// "Operation should be completed or handler should return empty (async completion)"
// );
// }
// Err(SubmitErr::NotCompatible) => {
// // This is acceptable - the driver doesn't support this operation
// eprintln!("Driver does not support Nop operation (NotCompatible)");
// }
// Err(e) => {
// panic!("Unexpected submit error: {:?}", e);
// }
// };
//
// drop(submitter);
// drop(handler);
//
// $driver::drop_state(state);
// }
//
// #[test]
// fn test_submit_multiple_operations() {
// // Test submitting multiple operations in sequence
// let state = $driver::new_state().expect("Failed to create state");
// let (mut submitter, mut handler) =
// $driver::new(state).expect("Failed to create submitter and handler");
//
// let store = OpStore::new();
// let mut ids = Vec::new();
//
// // Submit 10 operations
// for _ in 0..10 {
// let op = StoredOp::new_waker(Nop);
// let id = store.insert(op);
//
// match submitter.submit(state, id, &Nop) {
// Ok(()) => {
// ids.push(id);
// }
// Err(SubmitErr::NotCompatible) => {
// // Driver doesn't support this operation
// break;
// }
// Err(SubmitErr::Full) => {
// // Queue is full, that's acceptable for this test
// break;
// }
// Err(e) => {
// panic!("Unexpected submit error: {:?}", e);
// }
// }
// }
//
// // Tick handler to process operations
// let completed =
// handler.try_tick(state, &store).expect("Handler tick failed");
//
// // Some operations should be completed or the handler should not error
// assert!(
// completed.len() <= ids.len(),
// "Completed more operations than submitted"
// );
//
// drop(submitter);
// drop(handler);
//
// $driver::drop_state(state);
// }
// //
// #[test]
// fn test_try_tick_without_operations() {
// // Test that try_tick works when no operations are pending
// let state = $driver::new_state().expect("Failed to create state");
// let (_submitter, mut handler) =
// $driver::new(state).expect("Failed to create submitter and handler");
//
// let store = OpStore::new();
//
// // Tick without submitting any operations
// let completed =
// handler.try_tick(state, &store).expect("Handler try_tick failed");
//
// assert_eq!(
// completed.len(),
// 0,
// "No operations should be completed when none are submitted"
// );
//
// drop(_submitter);
// drop(handler);
//
// $driver::drop_state(state);
// }
// #[test]
// fn test_tick_without_operations() {
// // Test that tick works when no operations are pending
// let state = $driver::new_state().expect("Failed to create state");
// let (_submitter, mut handler) =
// $driver::new(state).expect("Failed to create submitter and handler");
//
// let store = OpStore::new();
//
// // Use spawn and timeout to avoid blocking forever
// let handle = std::thread::spawn(move || {
// let completed =
// handler.tick(state, &store).expect("Handler tick failed");
// (completed, state)
// });
//
// // Wait with timeout
// let result = std::thread::spawn(move || {
// std::thread::sleep(std::time::Duration::from_millis(500));
// handle.join()
// })
// .join();
//
// match result {
// Ok(Ok((completed, state))) => {
// assert_eq!(
// completed.len(),
// 0,
// "No operations should be completed when none are submitted"
// );
// $driver::drop_state(state);
// }
// _ => {
// // If the thread is still running, that's acceptable - tick() can block
// eprintln!(
// "tick() blocked as expected when no operations are pending"
// );
// }
// }
// }
// #[test]
// fn test_notify() {
// // Test that notify doesn't panic
// let state = $driver::new_state().expect("Failed to create state");
// let (mut submitter, _handler) =
// $driver::new(state).expect("Failed to create submitter and handler");
//
// // Notify should not panic even if no operations are pending
// let result = submitter.notify(state);
//
// match result {
// Ok(()) => {
// // Success
// }
// Err(e) => {
// eprintln!("Notify returned error (acceptable): {:?}", e);
// }
// }
//
// drop(submitter);
// drop(_handler);
// $driver::drop_state(state);
// }
//
// #[test]
// fn test_completed_operation_has_valid_id() {
// // Test that completed operations have the correct operation ID
// let state = $driver::new_state().expect("Failed to create state");
// let (mut submitter, mut handler) =
// $driver::new(state).expect("Failed to create submitter and handler");
//
// let store = OpStore::new();
// let op = StoredOp::new_waker(Nop);
// let id = store.insert(op);
//
// match submitter.submit(state, id, &Nop) {
// Ok(()) => {
// // Tick to complete the operation
// let completed =
// handler.try_tick(state, &store).expect("Handler tick failed");
//
// if let Some(comp) = completed.iter().find(|c| c.op_id == id) {
// assert_eq!(
// comp.op_id, id,
// "Completed operation should have the correct ID"
// );
// }
// }
// Err(SubmitErr::NotCompatible) => {
// // Driver doesn't support this operation
// eprintln!("Driver does not support Nop operation");
// }
// Err(e) => {
// panic!("Unexpected submit error: {:?}", e);
// }
// }
//
// drop(handler);
// drop(submitter);
//
// $driver::drop_state(state);
// }
//
// #[test]
// fn test_multiple_state_instances() {
// // Test that multiple state instances can be created independently
// let state1 = $driver::new_state().expect("Failed to create state1");
// let state2 = $driver::new_state().expect("Failed to create state2");
//
// let (mut sub1, mut handler1) =
// $driver::new(state1).expect("Failed to create from state1");
// let (mut sub2, mut handler2) =
// $driver::new(state2).expect("Failed to create from state2");
//
// let store = OpStore::new();
//
// // Submit to both
// let op1 = StoredOp::new_waker(Nop);
// let id1 = store.insert(op1);
// let op2 = StoredOp::new_waker(Nop);
// let id2 = store.insert(op2);
//
// let _ = sub1.submit(state1, id1, &Nop);
// let _ = sub2.submit(state2, id2, &Nop);
//
// // Tick both
// let _ = handler1.try_tick(state1, &store);
// let _ = handler2.try_tick(state2, &store);
//
// drop(sub1);
// drop(handler1);
//
// drop(sub2);
// drop(handler2);
//
// $driver::drop_state(state1);
// $driver::drop_state(state2);
// }
#[test]
fn test_submit_after_notify() {
// Test that operations can be submitted after notify is called
let state = Box::into_raw(Box::new(
$driver::new_state().expect("Failed to create state"),
));
let (mut submitter, mut handler) = $driver::new(unsafe { &mut *state })
.expect("Failed to create submitter and handler");
let ptr = state as *const ();
let store = OpStore::new();
// Notify first
let _ = submitter.notify(ptr);
// Then submit
let op = StoredOp::new_waker(Nop);
let id = store.insert(op);
let result = submitter.submit(ptr, id, &Nop);
match result {
Ok(()) => {
let _ = handler.try_tick(ptr, &store);
}
Err(SubmitErr::NotCompatible) => {
// Acceptable
}
Err(e) => {
panic!("Unexpected error after notify: {:?}", e);
}
};
drop(submitter);
drop(handler);
drop(ptr);
drop(state);
}
#[test]
fn test_operation_result_values() {
// Test that operation results have reasonable values
let state = $driver::new_state().expect("Failed to create state");
let state_ptr = Box::into_raw(Box::new(state));
let (mut submitter, mut handler) =
$driver::new(unsafe { &mut *state_ptr })
.expect("Failed to create submitter and handler");
let state_ptr = state_ptr as *const ();
let store = OpStore::new();
let op = StoredOp::new_waker(Nop);
let id = store.insert(op);
match submitter.submit(state_ptr, id, &Nop) {
Ok(()) => {
let completed =
handler.try_tick(state_ptr, &store).expect("Handler tick failed");
if let Some(comp) = completed.iter().find(|c| c.op_id == id) {
// Result should be a valid isize (could be positive, zero, or negative errno)
// Just verify it's within reasonable bounds for an isize
assert!(
comp.result >= isize::MIN && comp.result <= isize::MAX,
"Result should be a valid isize value"
);
}
}
Err(SubmitErr::NotCompatible) => {
// Acceptable
}
Err(e) => {
panic!("Unexpected submit error: {:?}", e);
}
};
drop(submitter);
drop(handler);
let _ = unsafe {
Box::from_raw(state_ptr as *mut <$driver as IoBackend>::State)
};
}
}
};
}