diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-10-05 10:10:55 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-10-05 10:10:55 -0500 |
| commit | 7c302a753b1814345fcb26eb78dec97db6b698df (patch) | |
| tree | 54bc206b20410009c32a2f8c9aa4579b50ecec78 | |
| parent | 2d432e52cbe7e8483783010fe3f8f1415245c1a5 (diff) | |
| parent | b0ca0504c8c13bbe73401ac00d16e13d5a4fda70 (diff) | |
| download | slskd-7c302a753b1814345fcb26eb78dec97db6b698df.tar.gz | |
Merge pull request #1869 from slskd/upload-queue-fixes
Remove GetGroupInfo, return slot info in ForecastPosition instead
| -rw-r--r-- | src/slskd/Application.cs | 11 | ||||
| -rw-r--r-- | src/slskd/Common/TokenBucket.cs | 4 | ||||
| -rw-r--r-- | src/slskd/Transfers/Downloads/DownloadService.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Transfers/Types/Upload.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Transfers/Types/UploadGroup.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadQueue.cs | 91 | ||||
| -rw-r--r-- | tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs | 321 |
7 files changed, 373 insertions, 60 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 8de4fe53..c34cd316 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1749,7 +1749,7 @@ namespace slskd // note: the following uses cached user data to determine group, so if the user's data // isn't cached they may get a forecast based on the wrong group. this is a hot path though, // and we don't want to incur the massive penalties that would caching data for each request. - var forecastedPosition = Transfers.Uploads.Queue.ForecastPosition(username); + var (groupName, totalSlots, freeSlots, forecastedPosition) = Transfers.Uploads.Queue.ForecastPosition(username); Log.Debug("Sending search response with {Count} files to {Username} for query '{Query}'", results.Count(), username, query.SearchText); @@ -1757,7 +1757,7 @@ namespace slskd Client.Username, token, uploadSpeed: State.CurrentValue.User.Statistics.AverageSpeed, - hasFreeUploadSlot: forecastedPosition == 0, + hasFreeUploadSlot: freeSlots > 0, queueLength: forecastedPosition, fileList: results); } @@ -1957,12 +1957,11 @@ namespace slskd // we are deliberately skipping it here; if the username is watched // leech detection works and they get accurate info, if not, they won't var groupName = await Users.GetOrFetchGroupAsync(username); - var group = Transfers.Uploads.Queue.GetGroupInfo(groupName); // forecast the position at which this user would enter the queue if they were to request // a file at this moment. this will be zero if a slot is available and the transfer would // begin immediately - var forecastedPosition = Transfers.Uploads.Queue.ForecastPosition(username); + var (group, totalSlots, freeSlots, forecastedPosition) = Transfers.Uploads.Queue.ForecastPosition(username); // if i get a user's info to determine whether i want to download files from them, // i want to know how many slots they have, which gives me an idea of how fast their @@ -1971,9 +1970,9 @@ namespace slskd // revisited 3 years later: why was it important to leave this comment?? var info = new UserInfo( description: Options.Soulseek.Description, - uploadSlots: group.Slots, + uploadSlots: totalSlots, queueLength: forecastedPosition, - hasFreeUploadSlot: forecastedPosition == 0, + hasFreeUploadSlot: freeSlots > 0, picture: pictureBytes); return info; diff --git a/src/slskd/Common/TokenBucket.cs b/src/slskd/Common/TokenBucket.cs index 11d64b88..440f25ae 100644 --- a/src/slskd/Common/TokenBucket.cs +++ b/src/slskd/Common/TokenBucket.cs @@ -92,7 +92,7 @@ namespace slskd /// </summary> public class TokenBucket : ITokenBucket, IDisposable { - private TaskCompletionSource<bool> waitForReset = new(); + private TaskCompletionSource<bool> waitForReset = new(TaskCreationOptions.RunContinuationsAsynchronously); /// <summary> /// Initializes a new instance of the <see cref="TokenBucket"/> class. @@ -243,6 +243,6 @@ namespace slskd } private void Reset() - => Interlocked.Exchange(ref waitForReset, new TaskCompletionSource<bool>()).SetResult(true); + => Interlocked.Exchange(ref waitForReset, new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously)).SetResult(true); } } diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 30f3c9b1..cb9423bc 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -509,7 +509,7 @@ namespace slskd.Transfers.Downloads add this to the dictionary before inserting the record, so we are guaranteed to have it in the right place once the transfer hits the UI */ - var enqueuedTcs = new TaskCompletionSource<Transfer>(); + var enqueuedTcs = new TaskCompletionSource<Transfer>(TaskCreationOptions.RunContinuationsAsynchronously); // satisfies condition #3; CancellationTokenSource set cancelled by the user (via API call) var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); diff --git a/src/slskd/Transfers/Types/Upload.cs b/src/slskd/Transfers/Types/Upload.cs index 2e9dc8ef..1e05a88a 100644 --- a/src/slskd/Transfers/Types/Upload.cs +++ b/src/slskd/Transfers/Types/Upload.cs @@ -42,7 +42,7 @@ namespace slskd.Transfers public string Group { get; set; } public DateTime? Ready { get; set; } = null; public DateTime? Started { get; set; } = null; - public TaskCompletionSource TaskCompletionSource { get; set; } = new TaskCompletionSource(); + public TaskCompletionSource TaskCompletionSource { get; set; } = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); public string Username { get; set; } } }
\ No newline at end of file diff --git a/src/slskd/Transfers/Types/UploadGroup.cs b/src/slskd/Transfers/Types/UploadGroup.cs index e416dd00..0b2b617d 100644 --- a/src/slskd/Transfers/Types/UploadGroup.cs +++ b/src/slskd/Transfers/Types/UploadGroup.cs @@ -38,7 +38,7 @@ namespace slskd.Transfers { public string Name { get; init; } public int Priority { get; set; } - public bool SlotAvailable => UsedSlots.Count < Slots; + public bool HasAvailableSlot => UsedSlots.Count < Slots; public int Slots { get; set; } public QueueStrategy Strategy { get; set; } public HashSet<(string Username, string Filename)> UsedSlots { get; set; } diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index b7db1c42..5839fa73 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -82,13 +82,6 @@ namespace slskd.Transfers.Uploads void Enqueue(string username, string filename); /// <summary> - /// Gets information about the specified <paramref name="groupName"/>. - /// </summary> - /// <param name="groupName">The name of the group.</param> - /// <returns>The group information.</returns> - UploadGroup GetGroupInfo(string groupName); - - /// <summary> /// Computes the estimated queue position of the specified <paramref name="filename"/> for the specified <paramref name="username"/>. /// </summary> /// <remarks> @@ -104,13 +97,18 @@ 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 + /// because of the amount of data that would need to be processed to compute a number, and how variable it + /// would be due to the interplay of slot availability, number of higher priority users waiting, etc. + /// </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> - int ForecastPosition(string username); + (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username); } /// <summary> @@ -235,6 +233,13 @@ namespace slskd.Transfers.Uploads } list.Remove(upload); + + if (!upload.TaskCompletionSource.Task.IsCompleted) + { + Log.Debug("Upload {File} for {User} was removed without being completed, so it has been cancelled", Path.GetFileName(upload.Filename)); + upload.TaskCompletionSource.TrySetCanceled(); + } + Log.Debug("Complete: {File} for {User} at {Time}", Path.GetFileName(upload.Filename), upload.Username, upload.Enqueued); // ensure the slot is returned to the group from which it was acquired the group may have been removed during the @@ -294,30 +299,6 @@ namespace slskd.Transfers.Uploads } /// <summary> - /// Gets information about the specified <paramref name="groupName"/>. - /// </summary> - /// <param name="groupName">The name of the group.</param> - /// <returns>The group information.</returns> - public UploadGroup GetGroupInfo(string groupName) - { - if (Groups.TryGetValue(groupName, out var group)) - { - SyncRoot.Wait(); - - try - { - return group with { UsedSlots = [.. group.UsedSlots] }; - } - finally - { - SyncRoot.Release(); - } - } - - throw new NotFoundException($"A group with the name {groupName} could not be found"); - } - - /// <summary> /// Computes the estimated queue position of the specified <paramref name="filename"/> for the specified <paramref name="username"/>. /// </summary> /// <remarks> @@ -332,7 +313,11 @@ namespace slskd.Transfers.Uploads public int EstimatePosition(string username, string filename) { var groupName = Users.GetGroup(username); - var groupRecord = Groups.GetValueOrDefault(groupName); + + if (!Groups.TryGetValue(groupName, out var groupRecord)) + { + throw new SlskdException($"Upload group {groupName} doesn't have an entry in upload dictionary. Please report this on GitHub: {Program.IssuesUrl}"); + } // the Uploads dictionary is keyed by username; gather all of the users that belong to the same group as the requested user // a user's group can change either by user changing the config or updating counts (for leech detection); resist the urge to cache this @@ -416,41 +401,49 @@ 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 + /// because of the amount of data that would need to be processed to compute a number, and how variable it + /// would be due to the interplay of slot availability, number of higher priority users waiting, etc. + /// </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 int ForecastPosition(string username) + public (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username) { var groupName = Users.GetGroup(username); + if (!Groups.TryGetValue(groupName, out var groupRecord)) + { + throw new SlskdException($"Upload group {groupName} doesn't have an entry in upload dictionary. Please report this on GitHub: {Program.IssuesUrl}"); + } + + var totalSlots = Math.Min(GlobalSlots, groupRecord.Slots); + var freeSlots = Math.Max(0, totalSlots - groupRecord.UsedSlots.Count); + // if there's a slot available, the user will enter the queue at position 0 (will start immediately) - if (Groups.TryGetValue(groupName, out var groupRecord) && groupRecord.SlotAvailable) + if (freeSlots > 0) { - return 0; + return (groupName, totalSlots, freeSlots, 0); } // the Uploads dictionary is keyed by username; gather all of the users that belong to the same group as the requested user // a user's group can change either by user changing the config or updating counts (for leech detection); resist the urge to cache this var uploadsForGroup = UploadDictionary.Where(kvp => Users.GetGroup(kvp.Key) == groupName); - if (groupRecord is null) - { - throw new SlskdException($"Upload group {groupName} doesn't have an entry in upload dictionary. Please report this on GitHub: {Program.IssuesUrl}"); - } - // 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 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 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 6174932d..f8b2f183 100644 --- a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs +++ b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs @@ -551,6 +551,60 @@ namespace slskd.Tests.Unit.Transfers.Uploads } [Theory, AutoData] + public async Task Cancels_Task_Of_Upload_That_Is_Waiting_For_A_Slot(string username, string filename, string other, string otherFilename) + { + var (queue, _) = GetFixture(); + + var groups = queue.GetProperty<Dictionary<string, UploadGroup>>("Groups"); + groups[Application.DefaultGroup].Slots = 1; + + queue.Enqueue(other, otherFilename); + await queue.AwaitStartAsync(other, otherFilename); + + // no slot available; the caller is left waiting on this task + queue.Enqueue(username, filename); + var task = queue.AwaitStartAsync(username, filename); + + Assert.False(task.IsCompleted); + + queue.Complete(username, filename); + + // continuations run asynchronously, but the task's status is set synchronously + Assert.True(task.IsCanceled); + } + + [Theory, AutoData] + public void Cancels_Task_Of_Upload_That_Was_Never_Ready(string username, string filename) + { + var (queue, _) = GetFixture(); + + queue.Enqueue(username, filename); + + var uploads = queue.GetProperty<ConcurrentDictionary<string, List<Upload>>>("UploadDictionary"); + var task = uploads[username][0].TaskCompletionSource.Task; + + Assert.False(task.IsCompleted); + + queue.Complete(username, filename); + + Assert.True(task.IsCanceled); + } + + [Theory, AutoData] + public async Task Does_Not_Cancel_Task_Of_Upload_That_Started(string username, string filename) + { + var (queue, _) = GetFixture(); + + queue.Enqueue(username, filename); + var task = queue.AwaitStartAsync(username, filename); + await task; + + queue.Complete(username, filename); + + Assert.True(task.IsCompletedSuccessfully); + } + + [Theory, AutoData] public async Task Releasing_A_Slot_Starts_The_Next_Ready_Upload(string username, string filename, string other, string otherFilename) { var (queue, _) = GetFixture(); @@ -715,6 +769,23 @@ namespace slskd.Tests.Unit.Transfers.Uploads } [Theory, AutoData] + public void Throws_SlskdException_If_Group_Does_Not_Exist(string username, string filename, string groupName) + { + var (queue, mocks) = GetFixture(); + + mocks.UserService.Setup(m => m.GetGroup(username)).Returns(groupName); + + var uploads = new ConcurrentDictionary<string, List<Upload>>(); + uploads.TryAdd(username, CreateUploads(username, count: 1)); + + queue.SetProperty("UploadDictionary", uploads); + + var ex = Record.Exception(() => queue.EstimatePosition(username, "file0")); + + Assert.IsType<SlskdException>(ex); + } + + [Theory, AutoData] public void RoundRobin_Returns_Local_Position_If_User_Is_Alone_In_Group(string username) { var (queue, _) = GetFixture(); @@ -890,6 +961,256 @@ namespace slskd.Tests.Unit.Transfers.Uploads } } + 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).Position); + } + + [Theory] + [InlineAutoData(QueueStrategy.RoundRobin)] + [InlineAutoData(QueueStrategy.FirstInFirstOut)] + 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); + + 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(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(2, queue.ForecastPosition(username).Position); + } + + [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(2, queue.ForecastPosition(username).Position); + } + + [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(1, queue.ForecastPosition(username).Position); + } + + [Theory, AutoData] + public void FirstInFirstOut_Returns_Number_Of_Uploads_In_Group(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, queue.ForecastPosition(username).Position); + } + + [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(2, queue.ForecastPosition(username).Position); + } + + [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(3, queue.ForecastPosition(username).Position); + } + + [Theory] + [InlineAutoData(QueueStrategy.RoundRobin)] + [InlineAutoData(QueueStrategy.FirstInFirstOut)] + public void Returns_Group_And_Slot_Counts_If_No_Slot_Is_Available(QueueStrategy strategy, string username) + { + var (queue, _) = GetFixture(); + + SetStrategy(queue, strategy); + SetSlotAvailable(queue, false); + + var (group, totalSlots, freeSlots, _) = queue.ForecastPosition(username); + + Assert.Equal(Application.DefaultGroup, group); + Assert.Equal(1, totalSlots); + Assert.Equal(0, freeSlots); + } + + [Theory, AutoData] + public void Returns_Group_And_Slot_Counts_If_Slot_Is_Available(string username) + { + var (queue, _) = GetFixture(); + + var groups = queue.GetProperty<Dictionary<string, UploadGroup>>("Groups"); + groups[Application.DefaultGroup].Slots = 3; + FillSlots(groups[Application.DefaultGroup], 1); + + var (group, totalSlots, freeSlots, position) = queue.ForecastPosition(username); + + Assert.Equal(Application.DefaultGroup, group); + Assert.Equal(3, totalSlots); + Assert.Equal(2, freeSlots); + Assert.Equal(0, position); + } + + [Theory, AutoData] + public void Ignores_Slots_Used_By_Other_Groups(string username) + { + var (queue, _) = GetFixture(); + + var groups = queue.GetProperty<Dictionary<string, UploadGroup>>("Groups"); + var globalSlots = queue.GetProperty<int>("GlobalSlots"); + + // every global slot is held by the privileged group, but the default group's own slots are free + FillSlots(groups[Application.PrivilegedGroup], globalSlots); + groups[Application.DefaultGroup].Slots = 2; + + var (group, totalSlots, freeSlots, position) = queue.ForecastPosition(username); + + Assert.Equal(Application.DefaultGroup, group); + Assert.Equal(2, totalSlots); + Assert.Equal(2, freeSlots); + Assert.Equal(0, position); + } + + [Theory, AutoData] + public void Limits_Total_Slots_To_Global_Slots(string username) + { + var (queue, _) = GetFixture(); + + var groups = queue.GetProperty<Dictionary<string, UploadGroup>>("Groups"); + var globalSlots = queue.GetProperty<int>("GlobalSlots"); + + groups[Application.DefaultGroup].Slots = globalSlots + 10; + + var (_, totalSlots, freeSlots, _) = queue.ForecastPosition(username); + + Assert.Equal(globalSlots, totalSlots); + Assert.Equal(globalSlots, freeSlots); + } + + [Theory, AutoData] + public void Free_Slots_Is_Never_Negative(QueueStrategy strategy, string username) + { + var (queue, _) = GetFixture(); + + SetStrategy(queue, strategy); + + // the group holds more slots than it is allowed, for instance after its slot count was reduced at run time + var groups = queue.GetProperty<Dictionary<string, UploadGroup>>("Groups"); + groups[Application.DefaultGroup].Slots = 1; + FillSlots(groups[Application.DefaultGroup], 3); + + var (_, totalSlots, freeSlots, position) = queue.ForecastPosition(username); + + Assert.Equal(1, totalSlots); + Assert.Equal(0, freeSlots); + Assert.Equal(0, position); + } + + [Theory, AutoData] + public void Throws_SlskdException_If_Group_Does_Not_Exist(string username, string groupName) + { + var (queue, mocks) = GetFixture(); + + mocks.UserService.Setup(m => m.GetGroup(username)).Returns(groupName); + + var ex = Record.Exception(() => queue.ForecastPosition(username)); + + Assert.IsType<SlskdException>(ex); + } + } + public class Process { [Fact] |
