|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using NETworkManager.Models.Network; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace NETworkManager.Tests.Network; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Slots of the blocking NetApi requests, shared by all scans: the max. number is passed with every request. |
| 11 | +/// </summary> |
| 12 | +public class WindowsNetApiSlotsTests |
| 13 | +{ |
| 14 | + private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(5); |
| 15 | + private static readonly TimeSpan ShortTimeout = TimeSpan.FromMilliseconds(100); |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public async Task WaitAsync_TakesSlotsUpToTheLimit() |
| 19 | + { |
| 20 | + var slots = new WindowsNetApiSlots(); |
| 21 | + |
| 22 | + Assert.True(await slots.WaitAsync(2, Timeout, CancellationToken.None)); |
| 23 | + Assert.True(await slots.WaitAsync(2, Timeout, CancellationToken.None)); |
| 24 | + Assert.False(await slots.WaitAsync(2, ShortTimeout, CancellationToken.None)); |
| 25 | + Assert.Equal(2, slots.Used); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public async Task WaitAsync_StartsWhenASlotIsReleased() |
| 30 | + { |
| 31 | + var slots = new WindowsNetApiSlots(); |
| 32 | + |
| 33 | + Assert.True(await slots.WaitAsync(1, Timeout, CancellationToken.None)); |
| 34 | + |
| 35 | + var waiting = slots.WaitAsync(1, Timeout, CancellationToken.None); |
| 36 | + |
| 37 | + Assert.False(waiting.IsCompleted); |
| 38 | + |
| 39 | + slots.Release(); |
| 40 | + |
| 41 | + Assert.True(await waiting); |
| 42 | + Assert.Equal(1, slots.Used); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public async Task WaitAsync_HigherLimitAppliesWhileSlotsAreUsed() |
| 47 | + { |
| 48 | + var slots = new WindowsNetApiSlots(); |
| 49 | + |
| 50 | + Assert.True(await slots.WaitAsync(1, Timeout, CancellationToken.None)); |
| 51 | + |
| 52 | + // E.g. the setting was raised: calls of the earlier scan still count |
| 53 | + Assert.True(await slots.WaitAsync(2, Timeout, CancellationToken.None)); |
| 54 | + Assert.False(await slots.WaitAsync(2, ShortTimeout, CancellationToken.None)); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public async Task WaitAsync_LowerLimitWaitsUntilEnoughSlotsAreReleased() |
| 59 | + { |
| 60 | + var slots = new WindowsNetApiSlots(); |
| 61 | + |
| 62 | + for (var i = 0; i < 3; i++) |
| 63 | + Assert.True(await slots.WaitAsync(3, Timeout, CancellationToken.None)); |
| 64 | + |
| 65 | + // E.g. the setting was lowered: no new call until less than 1 slot is used |
| 66 | + var waiting = slots.WaitAsync(1, Timeout, CancellationToken.None); |
| 67 | + |
| 68 | + slots.Release(); |
| 69 | + slots.Release(); |
| 70 | + |
| 71 | + await Task.Delay(ShortTimeout); |
| 72 | + Assert.False(waiting.IsCompleted); |
| 73 | + |
| 74 | + slots.Release(); |
| 75 | + |
| 76 | + Assert.True(await waiting); |
| 77 | + Assert.Equal(1, slots.Used); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public async Task WaitAsync_Canceled_Throws() |
| 82 | + { |
| 83 | + var slots = new WindowsNetApiSlots(); |
| 84 | + |
| 85 | + Assert.True(await slots.WaitAsync(1, Timeout, CancellationToken.None)); |
| 86 | + |
| 87 | + using var cancellationTokenSource = new CancellationTokenSource(ShortTimeout); |
| 88 | + |
| 89 | + await Assert.ThrowsAnyAsync<OperationCanceledException>(() => |
| 90 | + slots.WaitAsync(1, Timeout, cancellationTokenSource.Token)); |
| 91 | + Assert.Equal(1, slots.Used); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void Release_WithoutSlot_Throws() |
| 96 | + { |
| 97 | + Assert.Throws<InvalidOperationException>(() => new WindowsNetApiSlots().Release()); |
| 98 | + } |
| 99 | +} |
0 commit comments