aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-10-03 23:40:50 -0500
committerGitHub <noreply@github.com>2026-10-03 23:40:50 -0500
commit37c3490b68e835ef7607308c44cd7eca8d0bb135 (patch)
tree3de54f5e8b4001253db53bd20dc153ebe49b271c /tests
parenta97a425c7121f19c1613106c0bab7a1dcf616f92 (diff)
parentfc11d5d691ac008e1507f25ebb5ce3ac7a5cfc60 (diff)
downloadslskd-37c3490b68e835ef7607308c44cd7eca8d0bb135.tar.gz
Merge pull request #1867 from slskd/upload-queue-fixes
Synchronize queue position forecasting and estimation
Diffstat (limited to 'tests')
-rw-r--r--tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs390
1 files changed, 390 insertions, 0 deletions
diff --git a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs
index b1da3a77..7b35232c 100644
--- a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs
+++ b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs
@@ -564,6 +564,368 @@ namespace slskd.Tests.Unit.Transfers.Uploads
}
}
+ public class EstimatePosition
+ {
+ private static readonly DateTime Now = DateTime.UtcNow;
+
+ [Theory]
+ [InlineAutoData(QueueStrategy.RoundRobin)]
+ [InlineAutoData(QueueStrategy.FirstInFirstOut)]
+ public void Throws_NotFoundException_If_User_Has_No_Uploads(QueueStrategy strategy, string username, string filename)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, strategy);
+
+ var ex = Record.Exception(() => queue.EstimatePosition(username, filename));
+
+ Assert.IsType<NotFoundException>(ex);
+ }
+
+ [Theory]
+ [InlineAutoData(QueueStrategy.RoundRobin)]
+ [InlineAutoData(QueueStrategy.FirstInFirstOut)]
+ public void Throws_NotFoundException_If_File_Is_Not_Enqueued(QueueStrategy strategy, string username, string filename, string otherFilename)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, strategy);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+
+ uploads.TryAdd(username, new List<Upload>()
+ {
+ new Upload() { Username = username, Filename = otherFilename, Enqueued = Now },
+ });
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ var ex = Record.Exception(() => queue.EstimatePosition(username, filename));
+
+ Assert.IsType<NotFoundException>(ex);
+ }
+
+ [Theory, AutoData]
+ public void RoundRobin_Returns_Local_Position_If_User_Is_Alone_In_Group(string username)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.RoundRobin);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(username, CreateUploads(username, count: 3));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(0, queue.EstimatePosition(username, "file0"));
+ Assert.Equal(1, queue.EstimatePosition(username, "file1"));
+ Assert.Equal(2, queue.EstimatePosition(username, "file2"));
+ }
+
+ [Theory, AutoData]
+ public void RoundRobin_First_File_Is_Behind_Every_Other_User_With_Queued_Files(string username, string other1, string other2)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.RoundRobin);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other1, CreateUploads(other1, count: 10, offset: -100));
+ uploads.TryAdd(other2, CreateUploads(other2, count: 1, offset: -100));
+ uploads.TryAdd(username, CreateUploads(username, count: 3));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ // pessimistic; both other users have a file in round 0, and are assumed to go first
+ Assert.Equal(2, queue.EstimatePosition(username, "file0"));
+ }
+
+ [Theory, AutoData]
+ public void RoundRobin_Adds_Lesser_Of_Local_Position_And_Upload_Count_For_Each_Other_User(string a, string b, string c, string d)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.RoundRobin);
+
+ // aaaaa
+ // bb
+ // cccccccccccc
+ // ddddddd
+ // ^ (local position 4)
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(a, CreateUploads(a, count: 5));
+ uploads.TryAdd(b, CreateUploads(b, count: 2));
+ uploads.TryAdd(c, CreateUploads(c, count: 12));
+ uploads.TryAdd(d, CreateUploads(d, count: 7));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ // 4 (local) + min(4, 5) + min(4, 2) + min(4, 12), + 1 each for a and c, which have a file in round 4
+ Assert.Equal(4 + 4 + 2 + 4 + 2, queue.EstimatePosition(d, "file4"));
+ }
+
+ [Theory]
+ [InlineAutoData(1, 3)]
+ [InlineAutoData(2, 4)]
+ [InlineAutoData(3, 5)]
+ [InlineAutoData(10, 5)]
+ public void RoundRobin_Counts_Other_User_As_Ahead_Only_If_They_Have_A_File_In_The_Same_Round(int otherCount, int expected, string username, string other)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.RoundRobin);
+
+ // the requested file is at local position 2 (round 2). the other user contends for round 2 only if they
+ // have more than 2 files: 2 (local) + min(2, otherCount) + (otherCount > 2 ? 1 : 0)
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(username, CreateUploads(username, count: 3));
+ uploads.TryAdd(other, CreateUploads(other, count: otherCount));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(expected, queue.EstimatePosition(username, "file2"));
+ }
+
+ [Theory, AutoData]
+ public void RoundRobin_Ignores_Users_In_Other_Groups(string username, string other)
+ {
+ var (queue, mocks) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.RoundRobin);
+
+ mocks.UserService.Setup(m => m.GetGroup(other)).Returns(Application.PrivilegedGroup);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other, CreateUploads(other, count: 10));
+ uploads.TryAdd(username, CreateUploads(username, count: 3));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(2, queue.EstimatePosition(username, "file2"));
+ }
+
+ [Theory, AutoData]
+ public void FirstInFirstOut_Returns_Zero_If_Upload_Is_Earliest_In_Group(string username, string other)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.FirstInFirstOut);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(username, CreateUploads(username, count: 1));
+ uploads.TryAdd(other, CreateUploads(other, count: 5, offset: 10));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(0, queue.EstimatePosition(username, "file0"));
+ }
+
+ [Theory, AutoData]
+ public void FirstInFirstOut_Counts_All_Uploads_In_Group_Enqueued_Earlier(string a, string b, string c)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.FirstInFirstOut);
+
+ // enqueued at:
+ // a: 0, 3, 6
+ // b: 1, 4, 7
+ // c: 2, 5, 8
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(a, CreateUploads(a, count: 3, offset: 0, step: 3));
+ uploads.TryAdd(b, CreateUploads(b, count: 3, offset: 1, step: 3));
+ uploads.TryAdd(c, CreateUploads(c, count: 3, offset: 2, step: 3));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(0, queue.EstimatePosition(a, "file0"));
+ Assert.Equal(4, queue.EstimatePosition(b, "file1"));
+ Assert.Equal(8, queue.EstimatePosition(c, "file2"));
+ }
+
+ [Theory, AutoData]
+ public void FirstInFirstOut_Counts_Uploads_In_Progress(string username, string other)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.FirstInFirstOut);
+
+ var started = CreateUploads(other, count: 2);
+ started.ForEach(u => u.Started = Now);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other, started);
+ uploads.TryAdd(username, CreateUploads(username, count: 1, offset: 10));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(2, queue.EstimatePosition(username, "file0"));
+ }
+
+ [Theory, AutoData]
+ public void FirstInFirstOut_Ignores_Users_In_Other_Groups(string username, string other)
+ {
+ var (queue, mocks) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.FirstInFirstOut);
+
+ mocks.UserService.Setup(m => m.GetGroup(other)).Returns(Application.PrivilegedGroup);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other, CreateUploads(other, count: 10, offset: -100));
+ uploads.TryAdd(username, CreateUploads(username, count: 3));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(2, queue.EstimatePosition(username, "file2"));
+ }
+ }
+
+ public class ForecastPosition
+ {
+ [Theory]
+ [InlineAutoData(QueueStrategy.RoundRobin)]
+ [InlineAutoData(QueueStrategy.FirstInFirstOut)]
+ public void Returns_Zero_If_Slot_Is_Available_Regardless_Of_Queue(QueueStrategy strategy, string username, string other)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, strategy);
+ SetSlotAvailable(queue, true);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other, CreateUploads(other, count: 10));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(0, queue.ForecastPosition(username));
+ }
+
+ [Theory]
+ [InlineAutoData(QueueStrategy.RoundRobin)]
+ [InlineAutoData(QueueStrategy.FirstInFirstOut)]
+ public void Returns_One_If_No_Slot_Is_Available_And_Queue_Is_Empty(QueueStrategy strategy, string username)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, strategy);
+ SetSlotAvailable(queue, false);
+
+ Assert.Equal(1, queue.ForecastPosition(username));
+ }
+
+ [Theory, AutoData]
+ public void RoundRobin_Returns_Number_Of_Users_In_Group_Plus_One(string username, string other1, string other2)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.RoundRobin);
+ SetSlotAvailable(queue, false);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other1, CreateUploads(other1, count: 10));
+ uploads.TryAdd(other2, CreateUploads(other2, count: 1));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ // worst case; the new file is last in the rotation. file counts don't matter
+ Assert.Equal(3, queue.ForecastPosition(username));
+ }
+
+ [Theory, AutoData]
+ public void RoundRobin_Counts_Requesting_User_Once_Regardless_Of_Their_Queued_Files(string username, string other)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.RoundRobin);
+ SetSlotAvailable(queue, false);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(username, CreateUploads(username, count: 10));
+ uploads.TryAdd(other, CreateUploads(other, count: 1));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(3, queue.ForecastPosition(username));
+ }
+
+ [Theory, AutoData]
+ public void RoundRobin_Ignores_Users_In_Other_Groups(string username, string other1, string other2)
+ {
+ var (queue, mocks) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.RoundRobin);
+ SetSlotAvailable(queue, false);
+
+ mocks.UserService.Setup(m => m.GetGroup(other2)).Returns(Application.PrivilegedGroup);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other1, CreateUploads(other1, count: 1));
+ uploads.TryAdd(other2, CreateUploads(other2, count: 1));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(2, queue.ForecastPosition(username));
+ }
+
+ [Theory, AutoData]
+ public void FirstInFirstOut_Returns_Number_Of_Uploads_In_Group_Plus_One(string username, string other1, string other2)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.FirstInFirstOut);
+ SetSlotAvailable(queue, false);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(username, CreateUploads(username, count: 2));
+ uploads.TryAdd(other1, CreateUploads(other1, count: 3));
+ uploads.TryAdd(other2, CreateUploads(other2, count: 4));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ // the new file goes to the back of the queue, behind every upload in the group, including the user's own
+ Assert.Equal(2 + 3 + 4 + 1, queue.ForecastPosition(username));
+ }
+
+ [Theory, AutoData]
+ public void FirstInFirstOut_Counts_Uploads_In_Progress(string username, string other)
+ {
+ var (queue, _) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.FirstInFirstOut);
+ SetSlotAvailable(queue, false);
+
+ var started = CreateUploads(other, count: 2);
+ started.ForEach(u => u.Started = Now);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other, started);
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(3, queue.ForecastPosition(username));
+ }
+
+ [Theory, AutoData]
+ public void FirstInFirstOut_Ignores_Users_In_Other_Groups(string username, string other1, string other2)
+ {
+ var (queue, mocks) = GetFixture();
+
+ SetStrategy(queue, QueueStrategy.FirstInFirstOut);
+ SetSlotAvailable(queue, false);
+
+ mocks.UserService.Setup(m => m.GetGroup(other2)).Returns(Application.PrivilegedGroup);
+
+ var uploads = new ConcurrentDictionary<string, List<Upload>>();
+ uploads.TryAdd(other1, CreateUploads(other1, count: 3));
+ uploads.TryAdd(other2, CreateUploads(other2, count: 10));
+
+ queue.SetProperty("UploadDictionary", uploads);
+
+ Assert.Equal(4, queue.ForecastPosition(username));
+ }
+ }
+
public class Process
{
[Fact]
@@ -765,6 +1127,34 @@ namespace slskd.Tests.Unit.Transfers.Uploads
}
}
+ private static readonly DateTime Now = DateTime.UtcNow;
+
+ private static void SetStrategy(UploadQueue queue, QueueStrategy strategy)
+ {
+ var groups = queue.GetProperty<Dictionary<string, UploadGroup>>("Groups");
+ groups[Application.DefaultGroup].Strategy = strategy;
+ }
+
+ private static void SetSlotAvailable(UploadQueue queue, bool available)
+ {
+ var groups = queue.GetProperty<Dictionary<string, UploadGroup>>("Groups");
+ groups[Application.DefaultGroup].Slots = 1;
+ groups[Application.DefaultGroup].UsedSlots = available ? 0 : 1;
+ }
+
+ // creates uploads named file0..fileN, enqueued at Now + offset + (i * step) seconds, in ascending order
+ private static List<Upload> CreateUploads(string username, int count, int offset = 0, int step = 1)
+ {
+ return Enumerable.Range(0, count)
+ .Select(i => new Upload()
+ {
+ Username = username,
+ Filename = $"file{i}",
+ Enqueued = Now.AddSeconds(offset + (i * step)),
+ })
+ .ToList();
+ }
+
private static (UploadQueue queue, Mocks mocks) GetFixture(Options options = null)
{
var mocks = new Mocks(options);