Skip to content

Commit

Permalink
introduce wait on virtualfs cover art tests to prevent gh actions fla…
Browse files Browse the repository at this point in the history
…king (probably b/c too few cores)
  • Loading branch information
azuline committed Nov 1, 2023
1 parent 407266e commit 31975bd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 33 deletions.
10 changes: 10 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import shutil
import sqlite3
import time
from collections.abc import Iterator
from pathlib import Path

Expand Down Expand Up @@ -193,3 +194,12 @@ def source_dir(config: Config) -> Path:
shutil.copytree(TEST_PLAYLIST_1, config.music_source_dir / "!playlists")
update_cache(config)
return config.music_source_dir


def retry_for_sec(timeout_sec: float) -> Iterator[None]:
start = time.time()
while True:
yield
time.sleep(0.01)
if time.time() - start >= timeout_sec:
break
73 changes: 50 additions & 23 deletions rose/virtualfs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pytest

from conftest import retry_for_sec
from rose.audiotags import AudioTags
from rose.config import Config
from rose.virtualfs import mount_virtualfs, unmount_virtualfs
Expand Down Expand Up @@ -248,28 +249,41 @@ def test_virtual_filesystem_release_cover_art_actions(
# First write.
with (release_dir / "folder.jpg").open("w") as fp:
fp.write("hi")
assert (release_dir / "cover.jpg").is_file()
with (release_dir / "cover.jpg").open("r") as fp:
assert fp.read() == "hi"
for _ in retry_for_sec(0.2):
if not (release_dir / "cover.jpg").is_file():
continue
with (release_dir / "cover.jpg").open("r") as fp:
if fp.read() != "hi":
continue
break

# Second write to same filename.
with (release_dir / "cover.jpg").open("w") as fp:
fp.write("hi")
with (release_dir / "cover.jpg").open("r") as fp:
assert fp.read() == "hi"
for _ in retry_for_sec(0.2):
with (release_dir / "cover.jpg").open("r") as fp:
if fp.read() == "hi":
break

# Third write to different filename.
with (release_dir / "front.png").open("w") as fp:
fp.write("hi")
assert (release_dir / "cover.png").is_file()
with (release_dir / "cover.png").open("r") as fp:
assert fp.read() == "hi"
# Because of ghost writes, getattr succeeds, so we shouldn't check exists().
assert "cover.jpg" not in [f.name for f in release_dir.iterdir()]
for _ in retry_for_sec(0.2):
if not (release_dir / "cover.png").is_file():
continue
with (release_dir / "cover.png").open("r") as fp:
if fp.read() != "hi":
continue
# Because of ghost writes, getattr succeeds, so we shouldn't check exists().
if "cover.jpg" not in [f.name for f in release_dir.iterdir()]:
continue
break

# Now delete the cover art.
(release_dir / "cover.png").unlink()
assert not (release_dir / "cover.png").exists()
for _ in retry_for_sec(0.2):
if not (release_dir / "cover.png").exists():
break


def test_virtual_filesystem_playlist_cover_art_actions(
Expand All @@ -283,28 +297,41 @@ def test_virtual_filesystem_playlist_cover_art_actions(
# First write.
with (playlist_dir / "folder.jpg").open("w") as fp:
fp.write("hi")
assert (playlist_dir / "cover.jpg").is_file()
with (playlist_dir / "cover.jpg").open("r") as fp:
assert fp.read() == "hi"
for _ in retry_for_sec(0.2):
if not (playlist_dir / "cover.jpg").is_file():
continue
with (playlist_dir / "cover.jpg").open("r") as fp:
if fp.read() != "hi":
continue
break

# Second write to same filename.
with (playlist_dir / "cover.jpg").open("w") as fp:
fp.write("hi")
with (playlist_dir / "cover.jpg").open("r") as fp:
assert fp.read() == "hi"
fp.write("bye")
for _ in retry_for_sec(0.2):
with (playlist_dir / "cover.jpg").open("r") as fp:
if fp.read() == "bye":
break

# Third write to different filename.
with (playlist_dir / "front.png").open("w") as fp:
fp.write("hi")
assert (playlist_dir / "cover.png").is_file()
with (playlist_dir / "cover.png").open("r") as fp:
assert fp.read() == "hi"
# Because of ghost writes, getattr succeeds, so we shouldn't check exists().
assert "cover.jpg" not in [f.name for f in playlist_dir.iterdir()]
for _ in retry_for_sec(0.2):
if not (playlist_dir / "cover.png").is_file():
continue
with (playlist_dir / "cover.png").open("r") as fp:
if fp.read() != "hi":
continue
# Because of ghost writes, getattr succeeds, so we shouldn't check exists().
if not "cover.jpg" not in [f.name for f in playlist_dir.iterdir()]:
continue
break

# Now delete the cover art.
(playlist_dir / "cover.png").unlink()
assert not (playlist_dir / "cover.png").exists()
for _ in retry_for_sec(0.2):
if not (playlist_dir / "cover.png").exists():
break


def test_virtual_filesystem_delete_release(config: Config, source_dir: Path) -> None:
Expand Down
11 changes: 1 addition & 10 deletions rose/watcher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from contextlib import contextmanager
from multiprocessing import Process

from conftest import TEST_COLLAGE_1, TEST_PLAYLIST_1, TEST_RELEASE_2, TEST_RELEASE_3
from conftest import TEST_COLLAGE_1, TEST_PLAYLIST_1, TEST_RELEASE_2, TEST_RELEASE_3, retry_for_sec
from rose.cache import connect
from rose.config import Config
from rose.watcher import start_watchdog
Expand All @@ -21,15 +21,6 @@ def start_watcher(c: Config) -> Iterator[None]:
process.terminate()


def retry_for_sec(timeout_sec: float) -> Iterator[None]:
start = time.time()
while True:
yield
time.sleep(0.01)
if time.time() - start >= timeout_sec:
break


def test_watchdog_events(config: Config) -> None:
src = config.music_source_dir
with start_watcher(config):
Expand Down

0 comments on commit 31975bd

Please sign in to comment.