diff options
| author | Shadowghost <Ghost_of_Stone@web.de> | 2026-09-01 21:17:07 +0200 |
|---|---|---|
| committer | Shadowghost <Ghost_of_Stone@web.de> | 2026-09-01 22:04:06 +0200 |
| commit | e5dc3b8a54fdad218ba062f8adb58fb5e42d8f9d (patch) | |
| tree | 36071dc7892b86e951054d85dfbc255a407bb554 /tests | |
| parent | 0e6c52f4311f334a98a61191fe850f226b37d79b (diff) | |
Bound the directory caches a singleton would otherwise hold for the process lifetime
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/Jellyfin.Controller.Tests/DirectoryServiceTests.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/Jellyfin.Controller.Tests/DirectoryServiceTests.cs b/tests/Jellyfin.Controller.Tests/DirectoryServiceTests.cs index 1f59908a86..bc03bfc33b 100644 --- a/tests/Jellyfin.Controller.Tests/DirectoryServiceTests.cs +++ b/tests/Jellyfin.Controller.Tests/DirectoryServiceTests.cs @@ -1,3 +1,4 @@ +using System.Globalization; using System.Linq; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.IO; @@ -248,5 +249,65 @@ namespace Jellyfin.Controller.Tests Assert.Equal(cachedPaths, result); Assert.Equal(newPaths, secondResult); } + + [Fact] + public void GetFileSystemEntries_RepeatedPath_ReadsTheFileSystemOnce() + { + var fileSystemMock = new Mock<IFileSystem>(MockBehavior.Strict); + fileSystemMock.Setup(f => f.GetFileSystemEntries(LowerCasePath)) + .Returns(_lowerCaseFileSystemMetadata); + + var directoryService = new DirectoryService(fileSystemMock.Object); + + directoryService.GetFileSystemEntries(LowerCasePath); + directoryService.GetFileSystemEntries(LowerCasePath); + + fileSystemMock.Verify(f => f.GetFileSystemEntries(LowerCasePath), Times.Once); + } + + [Fact] + public void GetFileSystemEntries_FarMorePathsThanTheCacheHolds_EvictsInsteadOfGrowing() + { + // The service is a singleton, so the cache has to give entries back rather than hold every + // path the server ever saw. Asking for far more paths than it can hold must push the first + // one out, which shows up as the file system being read for it a second time. + const int PathCount = 40000; + + var fileSystemMock = new Mock<IFileSystem>(); + fileSystemMock.Setup(f => f.GetFileSystemEntries(It.IsAny<string>())) + .Returns(_lowerCaseFileSystemMetadata); + + var directoryService = new DirectoryService(fileSystemMock.Object); + + var firstPath = "/music/artist0"; + directoryService.GetFileSystemEntries(firstPath); + + for (var i = 1; i < PathCount; i++) + { + directoryService.GetFileSystemEntries("/music/artist" + i.ToString(CultureInfo.InvariantCulture)); + } + + directoryService.GetFileSystemEntries(firstPath); + + fileSystemMock.Verify(f => f.GetFileSystemEntries(firstPath), Times.Exactly(2)); + } + + [Fact] + public void GetFileSystemEntry_MissingPath_IsNotRemembered() + { + const string MissingPath = "/music/not-here"; + + var fileSystemMock = new Mock<IFileSystem>(); + fileSystemMock.SetupSequence(f => f.GetFileSystemInfo(MissingPath)) + .Returns(new FileSystemMetadata { FullName = MissingPath, Exists = false }) + .Returns(new FileSystemMetadata { FullName = MissingPath, Exists = true }); + + var directoryService = new DirectoryService(fileSystemMock.Object); + + Assert.Null(directoryService.GetFileSystemEntry(MissingPath)); + + // The one answer that changes on its own: the file turning up has to be visible. + Assert.NotNull(directoryService.GetFileSystemEntry(MissingPath)); + } } } |
