lio 0.4.1

A platform-independent async I/O library with native support for io_uring (Linux), IOCP (Windows), and kqueue (macOS)
Documentation
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Tests for read and read_at operations.

mod common;

use common::{TempFile, poll_recv, poll_until_recv};
use lio::api::resource::Resource;
use lio::{Lio, api};
use proptest::{prelude::*, test_runner::TestRunner};
use std::ffi::CString;
use std::os::fd::FromRawFd;
use std::sync::mpsc;
use std::time::Duration;

#[test]
fn test_read_basic() {
  let mut lio = Lio::new(64).unwrap();
  let temp = TempFile::new("read_basic");

  // Create file with data
  let test_data = b"Hello, World!";
  unsafe {
    let fd = libc::open(
      temp.path.as_ptr(),
      libc::O_CREAT | libc::O_WRONLY | libc::O_TRUNC,
      0o644,
    );
    libc::write(fd, test_data.as_ptr() as *const libc::c_void, test_data.len());
    libc::close(fd);
  }

  // Open for reading
  let cwd = unsafe { Resource::from_raw_fd(libc::AT_FDCWD) };
  let (sender_open, receiver_open) = mpsc::channel();
  api::openat(&cwd, temp.path.clone(), libc::O_RDONLY)
    .with_lio(&mut lio)
    .send_with(sender_open);

  let fd =
    poll_until_recv(&mut lio, &receiver_open).expect("Failed to open file");

  // Read the data
  let buf = vec![0u8; 64];
  let mut recv = api::read(&fd, buf).with_lio(&mut lio).send();

  let (result, buf) = poll_recv(&mut lio, &mut recv);
  let bytes_read = result.expect("Failed to read") as usize;

  assert_eq!(bytes_read, test_data.len());
  assert_eq!(&buf[..bytes_read], test_data);

  std::mem::forget(cwd);
}

#[test]
fn test_read_large_buffer() {
  let mut lio = Lio::new(64).unwrap();
  let temp = TempFile::new("read_large");

  // Create large data (1MB)
  let large_data: Vec<u8> = (0..1024 * 1024).map(|i| (i % 256) as u8).collect();
  unsafe {
    let fd = libc::open(
      temp.path.as_ptr(),
      libc::O_CREAT | libc::O_RDWR | libc::O_TRUNC,
      0o644,
    );
    libc::write(
      fd,
      large_data.as_ptr() as *const libc::c_void,
      large_data.len(),
    );
    libc::close(fd);
  }

  // Open for reading
  let fd = unsafe {
    Resource::from_raw_fd(libc::open(temp.path.as_ptr(), libc::O_RDONLY))
  };

  // Read it back
  let buf = vec![0u8; 1024 * 1024];
  let mut recv = api::read(&fd, buf).with_lio(&mut lio).send();

  let (result, buf) = poll_recv(&mut lio, &mut recv);
  let bytes_read = result.expect("Failed to read large buffer") as usize;

  assert_eq!(bytes_read, large_data.len());
  assert_eq!(&buf[..bytes_read], large_data.as_slice());
}

#[test]
fn test_read_concurrent() {
  let mut lio = Lio::new(64).unwrap();

  // Test multiple concurrent read operations on different files
  let test_data: Vec<_> = (0..10)
    .map(|i| {
      let temp = TempFile::new(&format!("read_concurrent_{}", i));
      let data = format!("Data for file {}", i);

      unsafe {
        let fd = libc::open(
          temp.path.as_ptr(),
          libc::O_CREAT | libc::O_RDWR | libc::O_TRUNC,
          0o644,
        );
        libc::write(fd, data.as_ptr() as *const libc::c_void, data.len());
        libc::close(fd);
      }

      (temp, data)
    })
    .collect();

  let (sender, receiver) = mpsc::channel();

  for (temp, _) in &test_data {
    let fd = unsafe {
      Resource::from_raw_fd(libc::open(temp.path.as_ptr(), libc::O_RDONLY))
    };
    let buf = vec![0u8; 100];
    api::read(&fd, buf).with_lio(&mut lio).send_with(sender.clone());
  }

  // Collect all results
  for _ in 0..10 {
    let (result, _buf) = poll_until_recv(&mut lio, &receiver);
    let bytes_read = result.expect("Failed to read") as usize;
    assert!(bytes_read > 0);
  }
}

#[test]
fn test_read_at_basic() {
  let mut lio = Lio::new(64).unwrap();
  let temp = TempFile::new("read_at_basic");

  // Create file with data
  let test_data = b"0123456789ABCDEF";
  unsafe {
    let fd = libc::open(
      temp.path.as_ptr(),
      libc::O_CREAT | libc::O_WRONLY | libc::O_TRUNC,
      0o644,
    );
    libc::write(fd, test_data.as_ptr() as *const libc::c_void, test_data.len());
    libc::close(fd);
  }

  let fd = unsafe {
    Resource::from_raw_fd(libc::open(temp.path.as_ptr(), libc::O_RDONLY))
  };

  // Read from offset 5
  let buf = vec![0u8; 5];
  let mut recv = api::read_at(&fd, buf, 5).with_lio(&mut lio).send();

  let (result, buf) = poll_recv(&mut lio, &mut recv);
  let bytes_read = result.expect("Failed to read_at") as usize;

  assert_eq!(bytes_read, 5);
  assert_eq!(&buf[..bytes_read], b"56789");
}

#[test]
fn test_read_at_offset_beyond_file() {
  let mut lio = Lio::new(64).unwrap();
  let temp = TempFile::new("read_at_beyond");

  // Create small file
  let test_data = b"Hello";
  unsafe {
    let fd = libc::open(
      temp.path.as_ptr(),
      libc::O_CREAT | libc::O_WRONLY | libc::O_TRUNC,
      0o644,
    );
    libc::write(fd, test_data.as_ptr() as *const libc::c_void, test_data.len());
    libc::close(fd);
  }

  let fd = unsafe {
    Resource::from_raw_fd(libc::open(temp.path.as_ptr(), libc::O_RDONLY))
  };

  // Read beyond EOF
  let buf = vec![0u8; 10];
  let mut recv = api::read_at(&fd, buf, 100).with_lio(&mut lio).send();

  let (result, _buf) = poll_recv(&mut lio, &mut recv);
  let bytes_read =
    result.expect("read_at beyond EOF should succeed with 0 bytes") as usize;

  assert_eq!(bytes_read, 0, "Reading beyond EOF should return 0 bytes");
}

/// Test reading from empty file.
/// Note: This test is Linux-only because the polling backend on macOS
/// doesn't generate readiness events for reading from empty files.
#[cfg(target_os = "linux")]
#[test]
fn test_read_empty_file() {
  let mut lio = Lio::new(64).unwrap();
  let temp = TempFile::new("read_empty");

  // Create empty file
  unsafe {
    let fd = libc::open(
      temp.path.as_ptr(),
      libc::O_CREAT | libc::O_WRONLY | libc::O_TRUNC,
      0o644,
    );
    libc::close(fd);
  }

  let fd = unsafe {
    Resource::from_raw_fd(libc::open(temp.path.as_ptr(), libc::O_RDONLY))
  };

  // Read from empty file using read_at with explicit offset 0
  // This works better across backends than read() on empty files
  let buf = vec![0u8; 64];
  let mut recv = api::read_at(&fd, buf, 0).with_lio(&mut lio).send();

  let (result, _buf) = poll_recv(&mut lio, &mut recv);
  let bytes_read = result.expect("Reading empty file should succeed") as usize;

  assert_eq!(bytes_read, 0, "Reading empty file should return 0 bytes");
}

#[test]
fn test_read_partial_buffer() {
  let mut lio = Lio::new(64).unwrap();
  let temp = TempFile::new("read_partial");

  // Create file with data
  let test_data = b"0123456789";
  unsafe {
    let fd = libc::open(
      temp.path.as_ptr(),
      libc::O_CREAT | libc::O_WRONLY | libc::O_TRUNC,
      0o644,
    );
    libc::write(fd, test_data.as_ptr() as *const libc::c_void, test_data.len());
    libc::close(fd);
  }

  let fd = unsafe {
    Resource::from_raw_fd(libc::open(temp.path.as_ptr(), libc::O_RDONLY))
  };

  // Read with buffer smaller than file
  let buf = vec![0u8; 5];
  let mut recv = api::read(&fd, buf).with_lio(&mut lio).send();

  let (result, buf) = poll_recv(&mut lio, &mut recv);
  let bytes_read = result.expect("Failed to read") as usize;

  assert_eq!(bytes_read, 5);
  assert_eq!(&buf[..bytes_read], b"01234");
}

#[test]
fn test_read_nonexistent_file() {
  let mut lio = Lio::new(64).unwrap();

  let cwd = unsafe { Resource::from_raw_fd(libc::AT_FDCWD) };
  let path =
    CString::new("/tmp/lio_test_nonexistent_12345_abcdef.txt").unwrap();

  // Try to open non-existent file
  let (sender, receiver) = mpsc::channel();
  api::openat(&cwd, path, libc::O_RDONLY).with_lio(&mut lio).send_with(sender);

  let result = poll_until_recv(&mut lio, &receiver);
  assert!(result.is_err(), "Opening non-existent file should fail");

  std::mem::forget(cwd);
}

/// Property-based test for read operations with various data sizes and offsets.
/// Uses a limited number of cases to avoid long test times.
/// Note: Excludes edge cases where buffer_size=0 or (data_size=0 and offset=0) because
/// these don't generate completion events on the polling backend.
#[test]
fn prop_test_read_arbitrary_data_and_offsets() {
  let mut runner = TestRunner::new(proptest::test_runner::Config {
    cases: 20, // Limit to 20 cases to avoid long test times
    ..Default::default()
  });

  runner
    .run(
      // Use 1..= to avoid 0-byte buffers/files which cause polling issues
      &(0..=4096usize, 0..=2048i64, 0..=2048usize, any::<u64>()),
      |props| {
        let (data_size, read_offset, buffer_size, seed) = props;
        prop_test_read_arbitrary_data_and_offsets_run(
          data_size,
          read_offset,
          buffer_size,
          seed,
        )
      },
    )
    .unwrap();
}

fn prop_test_read_arbitrary_data_and_offsets_run(
  data_size: usize,
  read_offset: i64,
  buffer_size: usize,
  seed: u64,
) -> Result<(), TestCaseError> {
  let mut lio = Lio::new(64)
    .map_err(|e| TestCaseError::fail(format!("Failed to create Lio: {}", e)))?;

  // Generate deterministic random data based on seed
  let test_data: Vec<u8> = (0..data_size)
    .map(|i| ((seed.wrapping_add(i as u64)) % 256) as u8)
    .collect();

  // Create unique test file path
  let path = common::make_temp_path("read", seed);

  // Write test data to file
  let fd = unsafe {
    let fd = libc::open(
      path.as_ptr(),
      libc::O_CREAT | libc::O_RDWR | libc::O_TRUNC,
      0o644,
    );
    if fd < 0 {
      return Err(TestCaseError::fail(
        "Failed to create test file".to_string(),
      ));
    }

    let written = libc::write(
      fd,
      test_data.as_ptr() as *const libc::c_void,
      test_data.len(),
    );
    if written as usize != test_data.len() {
      libc::close(fd);
      libc::unlink(path.as_ptr());
      return Err(TestCaseError::fail("Failed to write all data".to_string()));
    }
    fd
  };

  // Perform the read operation
  let resource = unsafe { Resource::from_raw_fd(fd) };
  let buf = vec![0u8; buffer_size];
  let mut receiver =
    api::read_at(&resource, buf, read_offset).with_lio(&mut lio).send();

  // Poll until done - use same pattern as write proptest
  let (result, result_buf) = {
    let mut attempts = 0;
    loop {
      lio.try_run().map_err(|e| {
        TestCaseError::fail(format!("Lio try_run failed: {}", e))
      })?;
      match receiver.try_recv() {
        Some(result) => break result,
        None => {
          attempts += 1;
          if attempts > 100 {
            unsafe {
              libc::unlink(path.as_ptr());
            }
            return Err(TestCaseError::fail(
              "Read operation did not complete after 100 ticks".to_string(),
            ));
          }
          std::thread::sleep(Duration::from_micros(100));
        }
      }
    }
  };

  let test_result = (|| -> Result<(), TestCaseError> {
    let bytes_read = result.map_err(|e| {
      TestCaseError::fail(format!("Read operation failed: {}", e))
    })? as usize;

    // Calculate expected bytes to read
    let file_size = test_data.len();
    let offset = read_offset as usize;

    if offset >= file_size {
      // Reading beyond EOF should return 0 bytes
      if bytes_read != 0 {
        return Err(TestCaseError::fail(format!(
          "Reading beyond EOF should return 0 bytes, got {}",
          bytes_read
        )));
      }
    } else {
      // Calculate how many bytes we expect to read
      let available_bytes = file_size - offset;
      let expected_bytes = std::cmp::min(buffer_size, available_bytes);

      if bytes_read != expected_bytes {
        return Err(TestCaseError::fail(format!(
          "Read should return min(buffer_size={}, available_bytes={})={}, got {}",
          buffer_size, available_bytes, expected_bytes, bytes_read
        )));
      }

      // Verify the data matches what we wrote
      let expected_data = &test_data[offset..offset + bytes_read];
      if &result_buf[..bytes_read] != expected_data {
        return Err(TestCaseError::fail(
          "Read data should match written data at offset".to_string(),
        ));
      }
    }
    Ok(())
  })();

  // Cleanup
  drop(resource);
  unsafe {
    libc::unlink(path.as_ptr());
  }

  test_result
}