Test that the song cache fills up to the cache size setting

Covers a cache size above 1: songs are preloaded until the setting is
reached, playing a song tops the cache up again, and the cache keeps
filling after a failed download once the server answers again.
This commit is contained in:
Sansan committed 2026-10-06 08:32:56 +02:00
1 parent c1fc24adf9
commit 16724922ed
2 files changed
+52

No files matched your search

+13
View File
@@ -12,6 +12,7 @@ var requested_paths: Array[String] = []
var requested_methods: Array[int] = []
var _responses: Dictionary = {}
var _successes_left: Dictionary = {}
var _previous_transport: HttpTransport
@@ -39,6 +40,13 @@ func respond_json(path: String, data: Variant, response_code: int = 200) -> void
## Answers requests to [param path] with [param body] as-is.
func respond_bytes(path: String, body: PackedByteArray, response_code: int = 200) -> void:
_responses[path] = {"code": response_code, "body": body}
_successes_left.erase(path)
## Lets [param successes] more requests to [param path] succeed, then answers 503 until
## a new response is registered for the path.
func fail_after(path: String, successes: int) -> void:
_successes_left[path] = successes
## Returns how many times [param path] has been requested.
@@ -50,6 +58,11 @@ func request(url: String, method: int, on_complete: Callable) -> void:
var path: String = url.trim_prefix(Settings.default_path)
requested_paths.append(path)
requested_methods.append(method)
if _successes_left.has(path):
if _successes_left[path] == 0:
on_complete.call(503, PackedByteArray())
return
_successes_left[path] -= 1
if _responses.has(path):
on_complete.call(_responses[path].code, _responses[path].body)
else:
+39
View File
@@ -119,3 +119,42 @@ func test_failed_song_info_retries_only_that_step() -> void:
assert_int(Playlist.song_object_array.size()).is_equal(songs_before + 1)
assert_str(Playlist.song_object_array.back().song_title).is_equal("Dancing Mad")
assert_bool(Playlist.is_song_downloading).is_false()
func test_preloads_as_many_songs_as_the_cache_setting() -> void:
Playlist.number_of_tracks_to_preload = 3
_runner.invoke("fetch_song")
assert_int(Playlist.get_preloaded_songs()).is_equal(3)
assert_bool(Playlist.is_song_downloading).is_false()
func test_playing_a_song_tops_the_cache_up_again() -> void:
Playlist.number_of_tracks_to_preload = 3
_runner.invoke("fetch_song")
await _play_first_song()
assert_int(Playlist.get_preloaded_songs()).is_equal(3)
assert_int(Playlist.song_object_array.size()).is_equal(4)
func test_cache_keeps_filling_after_a_failed_download() -> void:
_window.song_fetch_retry_seconds = 0.05
Playlist.number_of_tracks_to_preload = 3
var mp3: PackedByteArray = FileAccess.get_file_as_bytes("res://tests/data/short.mp3")
# One more download works, then the server stops answering
_server.fail_after("/music/rand", 1)
_runner.invoke("fetch_song")
await await_millis(150)
assert_int(Playlist.get_preloaded_songs()).is_equal(2)
assert_bool(Playlist.is_song_downloading).is_true()
_server.respond_bytes("/music/rand", mp3)
await await_millis(150)
assert_int(Playlist.get_preloaded_songs()).is_equal(3)
assert_bool(Playlist.is_song_downloading).is_false()