Skip to content

Commit

Permalink
testing mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fajpunk committed Feb 18, 2025
1 parent ca5696e commit 214fb8f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
10 changes: 9 additions & 1 deletion src/mobu/services/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ class RepoManager:
A logger
"""

def __init__(self, logger: BoundLogger) -> None:
def __init__(self, logger: BoundLogger, testing: bool = False) -> None:
self._dir = TemporaryDirectory(delete=False, prefix="mobu-notebooks-")
self._cache: dict[_Key, ClonedRepoInfo] = {}
self._lock = asyncio.Lock()
self._logger = logger
self._references: dict[_Reference, _ReferenceCount] = {}
self._testing = testing

# This is just for testing
self._cloned: list[_Key] = []

async def clone(self, url: str, ref: str) -> ClonedRepoInfo:
"""Clone a git repo or return cached info by url + ref.
Expand Down Expand Up @@ -101,6 +105,10 @@ async def clone(self, url: str, ref: str) -> ClonedRepoInfo:
await git.checkout(ref)
repo_hash = await git.repo_hash()

# If we're in testing mode, record that we actually did a clone
if self._testing:
self._cloned.append(key)

info = ClonedRepoInfo(
dir=repo_dir, path=Path(repo_dir.name), hash=repo_hash
)
Expand Down
26 changes: 18 additions & 8 deletions tests/services/repo_cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ async def test_cache(
await setup_git_repo(repo_path)

mock_logger = MagicMock()
cache = RepoManager(logger=mock_logger)
manager = RepoManager(logger=mock_logger, testing=True)

# Clone the same repo and ref a bunch of times concurrently
clone_tasks = [
cache.clone(url=str(repo_path), ref=repo_ref) for _ in range(100)
manager.clone(url=str(repo_path), ref=repo_ref) for _ in range(100)
]
infos = await gather(*clone_tasks)

Expand All @@ -62,6 +62,10 @@ async def test_cache(
assert "This is a test" in contents
assert "This is a NEW test" not in contents

# ...once
assert len(manager._cloned) == 1
manager._cloned = []

# Change the notebook and git commit it
notebook = repo_path / "test-notebook.ipynb"
contents = notebook.read_text()
Expand All @@ -73,7 +77,7 @@ async def test_cache(
await git.commit("-m", "Updating notebook")

# The repo should be cached (this makes the reference count 101)
cached_info = await cache.clone(url=str(repo_path), ref=repo_ref)
cached_info = await manager.clone(url=str(repo_path), ref=repo_ref)
assert cached_info == original_info
contents = (cached_info.path / "test-notebook.ipynb").read_text()
assert "This is a test" in contents
Expand All @@ -82,12 +86,18 @@ async def test_cache(
# Invalidate this URL and ref. This should make the next clone call clone
# the repo again, but it should not delete the directory of the old
# checkout because there are still 100 references to it.
await cache.invalidate(
await manager.invalidate(
url=str(repo_path), ref=repo_ref, repo_hash=original_info.hash
)

# Clone it again
updated_info = await cache.clone(url=str(repo_path), ref=repo_ref)
# Clone it again and verify stuff
clone_tasks = [
manager.clone(url=str(repo_path), ref=repo_ref) for _ in range(100)
]
infos = await gather(*clone_tasks)
assert len(set(infos)) == 1
assert len(manager._cloned) == 1
updated_info = infos[0]

# We should get different info because the repo should have been recloned
assert updated_info != original_info
Expand All @@ -102,7 +112,7 @@ async def test_cache(

# invalidate the other references
remove_tasks = [
cache.invalidate(
manager.invalidate(
url=str(repo_path), ref=repo_ref, repo_hash=original_info.hash
)
for _ in range(100)
Expand All @@ -113,6 +123,6 @@ async def test_cache(
assert not Path(original_info.dir.name).exists()

# The cache should clean up after itself
cache.close()
manager.close()
assert not original_info.path.exists()
assert not updated_info.path.exists()

0 comments on commit 214fb8f

Please sign in to comment.