diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-10-05 09:11:44 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-10-05 09:11:44 -0500 |
| commit | b38d9f01862b4fc27874bf99d4b24b53a6577bd0 (patch) | |
| tree | 1610fcf6b7d453580492af2c4f64759b77def724 | |
| parent | 67999bda3007baf735b5a14926c1b6e4c22e4a55 (diff) | |
| download | slskd-b38d9f01862b4fc27874bf99d4b24b53a6577bd0.tar.gz | |
make forecast zero-based to match estimation (and to make it more suitable for gauging queue depth)
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadQueue.cs | 16 | ||||
| -rw-r--r-- | tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs | 26 |
2 files changed, 23 insertions, 19 deletions
diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index ccd1f5f2..cb071d25 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -97,7 +97,7 @@ namespace slskd.Transfers.Uploads /// <summary> /// Computes the estimated queue position of the specified <paramref name="username"/> if they were to enqueue a file, - /// or zero if the transfer could start immediately. + /// along with the slot counts for the user's group. /// </summary> /// <remarks> /// The returned position is relative to the user's group only. Higher priority groups are not factored in @@ -106,7 +106,7 @@ namespace slskd.Transfers.Uploads /// </remarks> /// <param name="username">The username for which to estimate.</param> /// <returns> - /// The estimated queue position if the user were to enqueue a file, or zero if the transfer could start immediately. + /// The user's group, the group's total and free slots, and the estimated position if the user were to enqueue a file. /// </returns> (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username); } @@ -394,7 +394,7 @@ namespace slskd.Transfers.Uploads /// <summary> /// Computes the estimated queue position of the specified <paramref name="username"/> if they were to enqueue a file, - /// or zero if the transfer could start immediately. + /// along with the slot counts for the user's group. /// </summary> /// <remarks> /// The returned position is relative to the user's group only. Higher priority groups are not factored in @@ -403,7 +403,7 @@ namespace slskd.Transfers.Uploads /// </remarks> /// <param name="username">The username for which to estimate.</param> /// <returns> - /// The estimated queue position if the user were to enqueue a file, or zero if the transfer could start immediately. + /// The user's group, the group's total and free slots, and the estimated position if the user were to enqueue a file. /// </returns> public (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username) { @@ -428,15 +428,15 @@ namespace slskd.Transfers.Uploads var uploadsForGroup = UploadDictionary.Where(kvp => Users.GetGroup(kvp.Key) == groupName); // assuming that the queue will be processed in a true round-robin fashion and that the user will be the last in the - // rotation (worst case), the user's start position will be equal to the number of users downloading or waiting, + 1. + // rotation (worst case), the user's start position will be equal to the number of users downloading or waiting. if (groupRecord.Strategy == QueueStrategy.RoundRobin) { - return (groupName, totalSlots, freeSlots, uploadsForGroup.Count() + 1); + return (groupName, totalSlots, freeSlots, uploadsForGroup.Count()); } // for FIFO queues, the user will enter the queue at the very back. return the total number of uploads in progress and - // enqueued, + 1. - return (groupName, totalSlots, freeSlots, uploadsForGroup.Sum(kvp => kvp.Value.Count) + 1); + // enqueued. + return (groupName, totalSlots, freeSlots, uploadsForGroup.Sum(kvp => kvp.Value.Count)); } private void Configure(Options options) diff --git a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs index fbf9754f..c6fadc82 100644 --- a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs +++ b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs @@ -930,18 +930,22 @@ namespace slskd.Tests.Unit.Transfers.Uploads [Theory] [InlineAutoData(QueueStrategy.RoundRobin)] [InlineAutoData(QueueStrategy.FirstInFirstOut)] - public void Returns_One_If_No_Slot_Is_Available_And_Queue_Is_Empty(QueueStrategy strategy, string username) + public void Returns_Zero_Without_Free_Slot_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).Position); + var (_, _, freeSlots, position) = queue.ForecastPosition(username); + + // nothing is ahead, but the transfer can't start immediately; callers use FreeSlots to tell the difference + Assert.Equal(0, position); + Assert.Equal(0, freeSlots); } [Theory, AutoData] - public void RoundRobin_Returns_Number_Of_Users_In_Group_Plus_One(string username, string other1, string other2) + public void RoundRobin_Returns_Number_Of_Users_In_Group(string username, string other1, string other2) { var (queue, _) = GetFixture(); @@ -955,7 +959,7 @@ namespace slskd.Tests.Unit.Transfers.Uploads 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).Position); + Assert.Equal(2, queue.ForecastPosition(username).Position); } [Theory, AutoData] @@ -972,7 +976,7 @@ namespace slskd.Tests.Unit.Transfers.Uploads queue.SetProperty("UploadDictionary", uploads); - Assert.Equal(3, queue.ForecastPosition(username).Position); + Assert.Equal(2, queue.ForecastPosition(username).Position); } [Theory, AutoData] @@ -991,11 +995,11 @@ namespace slskd.Tests.Unit.Transfers.Uploads queue.SetProperty("UploadDictionary", uploads); - Assert.Equal(2, queue.ForecastPosition(username).Position); + Assert.Equal(1, queue.ForecastPosition(username).Position); } [Theory, AutoData] - public void FirstInFirstOut_Returns_Number_Of_Uploads_In_Group_Plus_One(string username, string other1, string other2) + public void FirstInFirstOut_Returns_Number_Of_Uploads_In_Group(string username, string other1, string other2) { var (queue, _) = GetFixture(); @@ -1010,7 +1014,7 @@ namespace slskd.Tests.Unit.Transfers.Uploads 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).Position); + Assert.Equal(2 + 3 + 4, queue.ForecastPosition(username).Position); } [Theory, AutoData] @@ -1029,7 +1033,7 @@ namespace slskd.Tests.Unit.Transfers.Uploads queue.SetProperty("UploadDictionary", uploads); - Assert.Equal(3, queue.ForecastPosition(username).Position); + Assert.Equal(2, queue.ForecastPosition(username).Position); } [Theory, AutoData] @@ -1048,7 +1052,7 @@ namespace slskd.Tests.Unit.Transfers.Uploads queue.SetProperty("UploadDictionary", uploads); - Assert.Equal(4, queue.ForecastPosition(username).Position); + Assert.Equal(3, queue.ForecastPosition(username).Position); } [Theory] @@ -1137,7 +1141,7 @@ namespace slskd.Tests.Unit.Transfers.Uploads Assert.Equal(1, totalSlots); Assert.Equal(0, freeSlots); - Assert.Equal(1, position); + Assert.Equal(0, position); } [Theory, AutoData] |
