|
2 | 2 |
|
3 | 3 | import json |
4 | 4 | import errno |
| 5 | +import time |
5 | 6 | import pytest |
6 | 7 | from unittest.mock import AsyncMock, MagicMock, patch |
7 | 8 |
|
8 | 9 | import pyfuse3 |
9 | 10 |
|
10 | 11 | from kg_fuse.config import WriteProtectConfig, TagsConfig, JobsConfig |
| 12 | +from kg_fuse.filesystem import WRITE_BACK_DELAY |
11 | 13 | from kg_fuse.models import InodeEntry |
12 | 14 |
|
13 | 15 |
|
@@ -549,6 +551,36 @@ async def test_rename_non_ingestion_raises_eperm(self): |
549 | 551 | await fs.rename(ingest_inode, b"some.concept.md", ingest_inode, b"new.md", 0, ctx) |
550 | 552 | assert exc_info.value.errno == errno.EPERM |
551 | 553 |
|
| 554 | + @pytest.mark.anyio |
| 555 | + async def test_flush_skips_entries_below_delay(self): |
| 556 | + """Flush task should not ingest entries younger than WRITE_BACK_DELAY.""" |
| 557 | + fs = _make_fs() |
| 558 | + ctx = _mock_ctx() |
| 559 | + ingest_inode = self._setup_ingest_dir(fs) |
| 560 | + |
| 561 | + fi, _ = await fs.create(ingest_inode, b"young.md", 0o644, 0, ctx) |
| 562 | + fh = fi.fh |
| 563 | + await fs.write(fh, 0, b"content") |
| 564 | + await fs.release(fh) |
| 565 | + assert len(fs._pending_ingestions) == 1 |
| 566 | + |
| 567 | + # Simulate flush with entry still young (timestamp = now) |
| 568 | + ready = [ |
| 569 | + (k, e) for k, e in fs._pending_ingestions.items() |
| 570 | + if time.monotonic() - e["timestamp"] >= WRITE_BACK_DELAY |
| 571 | + ] |
| 572 | + assert len(ready) == 0, "Entry should not be ready yet" |
| 573 | + |
| 574 | + # Age the entry past the delay |
| 575 | + for entry in fs._pending_ingestions.values(): |
| 576 | + entry["timestamp"] = time.monotonic() - WRITE_BACK_DELAY - 1 |
| 577 | + |
| 578 | + ready = [ |
| 579 | + (k, e) for k, e in fs._pending_ingestions.items() |
| 580 | + if time.monotonic() - e["timestamp"] >= WRITE_BACK_DELAY |
| 581 | + ] |
| 582 | + assert len(ready) == 1, "Entry should be ready after aging" |
| 583 | + |
552 | 584 | @pytest.mark.anyio |
553 | 585 | async def test_destroy_flushes_pending(self): |
554 | 586 | """Unmount should flush all pending ingestions before cleanup.""" |
|
0 commit comments