duck.utils.fileio.test_fileio¶
Tests for FileIOStream and AsyncFileIOStream.
Run directly: python test_fileio.py
No external test runner or duck installation required — all duck dependencies are stubbed inline before the module is imported.
Module Contents¶
Functions¶
Raises AssertionError when |
|
Clears the shared FILE_CACHE so each test starts clean. |
|
Writes content to a named temporary file and returns its path. |
|
Runs a single test function and records the outcome. |
|
AsyncFileIOStream works as an async context manager. |
|
Async hooks registered with hook() are awaited on AsyncFileIOStream reads. |
|
Async hooks registered with hook() are awaited on AsyncFileIOStream writes. |
|
A read of the exact written slice immediately after an async write is served from cache without a disk round-trip. |
|
AsyncFileIOStream: a seeked full read is cached under its own pos key. |
|
AsyncFileIOStream: a seeked write patches overlapping cache entries. |
|
AsyncFileIOStream.write caches the written slice under (write_pos, len(data)). |
|
hook() raises ValueError for unknown event names. |
|
is_modified returns False immediately after a read with no external changes. |
|
is_modified returns False before any read or write has taken place. |
|
is_modified returns True when another process modifies the file after a read. |
|
When maxkeys is exceeded the least-recently-used entry is evicted. |
|
Sync and async hooks can both be registered on the same event and both fire in registration order. |
|
Repeated reads at multiple offsets are all individually cached and served on a second pass without touching the disk. |
|
on_read hooks do not fire on writes and vice versa. |
|
on_read hook fires even when data is served from cache. |
|
on_read hook is called when data is read fresh from disk. |
|
Multiple on_read hooks all fire in registration order. |
|
on_write hook is called after a successful write. |
|
The stream argument passed to the hook is the stream that fired it. |
|
read() raises ValueError when called before open(). |
|
seek moves the file pointer; tell reports the new position. |
|
A full read after seek() is cached under its own (pos, size) key so the same data is returned on a second read without touching the disk. |
|
A partial read from a seeked position occupies its own cache slot and does not collide with a pos-0 read of the same size. |
|
A write that partially overlaps a cached entry at a boundary evicts that entry rather than storing corrupted data. |
|
A seeked write patches cached entries in memory without a disk re-read. |
|
When the file changes on disk, cache_get returns None (stale eviction). |
|
_cache_mtime on the stream is updated after a write so is_modified is accurate without requiring a subsequent read. |
|
FileIOStream opens a file and reads the correct bytes. |
|
A read of the exact written slice immediately after a write is served from the cache without a disk round-trip. |
|
After a sync read, the result is stored in FILE_CACHE. |
|
A second read returns the cached bytes without touching the descriptor. |
|
The mtime stored in cache entries after a write matches the post-flush mtime. |
|
Writing stores the written slice in the cache under (write_pos, len(data)) and patches any overlapping entries in memory. |
|
to_async_fileio_stream copies hooks to the new stream. |
|
_total_read_bytes grows correctly across reads with different sizes. |
Data¶
API¶
- duck.utils.fileio.test_fileio.FAIL¶
‘\x1b[91mFAIL\x1b[0m’
- duck.utils.fileio.test_fileio.PASS¶
‘\x1b[92mPASS\x1b[0m’
- duck.utils.fileio.test_fileio._results: list[tuple[str, bool, str]]¶
[]
- duck.utils.fileio.test_fileio.assert_eq(a, b, msg='')[source]¶
Raises AssertionError when
a != b.- Parameters:
a – Actual value.
b – Expected value.
msg – Optional context shown on failure.
- duck.utils.fileio.test_fileio.fresh_cache()[source]¶
Clears the shared FILE_CACHE so each test starts clean.
- duck.utils.fileio.test_fileio.make_temp_file(content: bytes = b'Hello, Duck!') str[source]¶
Writes content to a named temporary file and returns its path.
- Parameters:
content – Bytes to write into the file.
- Returns:
Absolute path to the temporary file.
- duck.utils.fileio.test_fileio.run(name: str, fn)[source]¶
Runs a single test function and records the outcome.
- Parameters:
name – Human-readable test name shown in the summary.
fn – Zero-argument callable or async coroutine function.
- async duck.utils.fileio.test_fileio.test_async_context_manager()[source]¶
AsyncFileIOStream works as an async context manager.
- async duck.utils.fileio.test_fileio.test_async_hook_on_read()[source]¶
Async hooks registered with hook() are awaited on AsyncFileIOStream reads.
- async duck.utils.fileio.test_fileio.test_async_hook_on_write()[source]¶
Async hooks registered with hook() are awaited on AsyncFileIOStream writes.
- async duck.utils.fileio.test_fileio.test_async_read_after_write_served_from_cache()[source]¶
A read of the exact written slice immediately after an async write is served from cache without a disk round-trip.
- async duck.utils.fileio.test_fileio.test_async_seeked_full_read_independent_slot()[source]¶
AsyncFileIOStream: a seeked full read is cached under its own pos key.
- async duck.utils.fileio.test_fileio.test_async_seeked_write_patches_cache()[source]¶
AsyncFileIOStream: a seeked write patches overlapping cache entries.
- async duck.utils.fileio.test_fileio.test_async_write_updates_cache()[source]¶
AsyncFileIOStream.write caches the written slice under (write_pos, len(data)).
- duck.utils.fileio.test_fileio.test_hook_invalid_event_raises()[source]¶
hook() raises ValueError for unknown event names.
- duck.utils.fileio.test_fileio.test_is_modified_false_after_read_unchanged()[source]¶
is_modified returns False immediately after a read with no external changes.
- duck.utils.fileio.test_fileio.test_is_modified_false_before_read()[source]¶
is_modified returns False before any read or write has taken place.
- duck.utils.fileio.test_fileio.test_is_modified_true_after_external_write()[source]¶
is_modified returns True when another process modifies the file after a read.
- duck.utils.fileio.test_fileio.test_lru_eviction()[source]¶
When maxkeys is exceeded the least-recently-used entry is evicted.
- async duck.utils.fileio.test_fileio.test_mixed_sync_and_async_hooks()[source]¶
Sync and async hooks can both be registered on the same event and both fire in registration order.
- duck.utils.fileio.test_fileio.test_multiple_seek_positions_all_cached()[source]¶
Repeated reads at multiple offsets are all individually cached and served on a second pass without touching the disk.
This is the key benefit of pos-keyed caching: high-seek workloads accumulate warm entries across many positions.
- duck.utils.fileio.test_fileio.test_on_read_and_write_hooks_independent()[source]¶
on_read hooks do not fire on writes and vice versa.
- duck.utils.fileio.test_fileio.test_on_read_hook_fires_on_cache_hit()[source]¶
on_read hook fires even when data is served from cache.
- duck.utils.fileio.test_fileio.test_on_read_hook_fires_on_cache_miss()[source]¶
on_read hook is called when data is read fresh from disk.
- duck.utils.fileio.test_fileio.test_on_read_multiple_hooks()[source]¶
Multiple on_read hooks all fire in registration order.
- duck.utils.fileio.test_fileio.test_on_write_hook_fires()[source]¶
on_write hook is called after a successful write.
- duck.utils.fileio.test_fileio.test_on_write_hook_receives_correct_stream()[source]¶
The stream argument passed to the hook is the stream that fired it.
- duck.utils.fileio.test_fileio.test_read_raises_if_not_open()[source]¶
read() raises ValueError when called before open().
- duck.utils.fileio.test_fileio.test_seek_and_tell()[source]¶
seek moves the file pointer; tell reports the new position.
- duck.utils.fileio.test_fileio.test_seeked_full_read_uses_own_cache_slot()[source]¶
A full read after seek() is cached under its own (pos, size) key so the same data is returned on a second read without touching the disk.
- duck.utils.fileio.test_fileio.test_seeked_partial_read_independent_slot()[source]¶
A partial read from a seeked position occupies its own cache slot and does not collide with a pos-0 read of the same size.
- duck.utils.fileio.test_fileio.test_seeked_write_evicts_boundary_partial_overlaps()[source]¶
A write that partially overlaps a cached entry at a boundary evicts that entry rather than storing corrupted data.
- duck.utils.fileio.test_fileio.test_seeked_write_patches_overlapping_cached_entries()[source]¶
A seeked write patches cached entries in memory without a disk re-read.
Cached entries that overlap the written region are updated in place; non-overlapping entries are left untouched.
- duck.utils.fileio.test_fileio.test_sync_cache_miss_on_mtime_change()[source]¶
When the file changes on disk, cache_get returns None (stale eviction).
- duck.utils.fileio.test_fileio.test_sync_cache_mtime_updated_on_write()[source]¶
_cache_mtime on the stream is updated after a write so is_modified is accurate without requiring a subsequent read.
- duck.utils.fileio.test_fileio.test_sync_open_and_read()[source]¶
FileIOStream opens a file and reads the correct bytes.
- duck.utils.fileio.test_fileio.test_sync_read_after_write_served_from_cache()[source]¶
A read of the exact written slice immediately after a write is served from the cache without a disk round-trip.
- duck.utils.fileio.test_fileio.test_sync_read_populates_cache()[source]¶
After a sync read, the result is stored in FILE_CACHE.
- duck.utils.fileio.test_fileio.test_sync_read_serves_cache_hit()[source]¶
A second read returns the cached bytes without touching the descriptor.
- duck.utils.fileio.test_fileio.test_sync_write_cache_entry_has_fresh_mtime()[source]¶
The mtime stored in cache entries after a write matches the post-flush mtime.
- duck.utils.fileio.test_fileio.test_sync_write_updates_cache()[source]¶
Writing stores the written slice in the cache under (write_pos, len(data)) and patches any overlapping entries in memory.