aboutsummaryrefslogtreecommitdiffstats
path: root/tests/slskd.Tests.Unit/Common/ComputeTests.cs
blob: 9090ad36aefa5a05c6f606f3b43ee0a28de7d985 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
namespace slskd.Tests.Unit.Common
{
    using Xunit;

    public class ComputeTests
    {
        [Theory]
        // base = 1000 (original cases)
        [InlineData(1, 1000, 300000, 0)]
        [InlineData(2, 1000, 300000, 1000)]
        [InlineData(3, 1000, 300000, 3000)]
        [InlineData(4, 1000, 300000, 7000)]
        [InlineData(5, 1000, 300000, 15000)]
        [InlineData(6, 1000, 300000, 31000)]
        [InlineData(999999, 1000, 300000, 300000)]
        // base = 500 — delay should be half of base=1000 cases
        [InlineData(1, 500, 300000, 0)]
        [InlineData(2, 500, 300000, 500)]
        [InlineData(3, 500, 300000, 1500)]
        [InlineData(4, 500, 300000, 3500)]
        [InlineData(5, 500, 300000, 7500)]
        [InlineData(6, 500, 300000, 15500)]
        [InlineData(999999, 500, 300000, 300000)]
        // base = 2000 — delay should be double of base=1000 cases
        [InlineData(1, 2000, 300000, 0)]
        [InlineData(2, 2000, 300000, 2000)]
        [InlineData(3, 2000, 300000, 6000)]
        [InlineData(4, 2000, 300000, 14000)]
        [InlineData(5, 2000, 300000, 30000)]
        [InlineData(6, 2000, 300000, 62000)]
        [InlineData(999999, 2000, 300000, 300000)]
        public void ExponentialBackoffDelay_Returns_Expected_Delay(int iteration, int baseDelayInMilliseconds, int maxDelayInMs, int expectedDelay)
        {
            var (computedDelay, _) = Compute.ExponentialBackoffDelay(iteration, baseDelayInMilliseconds, maxDelayInMs);
            Assert.Equal(expectedDelay, computedDelay);
        }

        [Fact]
        public void ExponentialBackoffDelay_Uses_Default_Base_Delay_Of_1000()
        {
            var (withDefault, _) = Compute.ExponentialBackoffDelay(iteration: 3);
            var (withExplicit, _) = Compute.ExponentialBackoffDelay(iteration: 3, baseDelayInMilliseconds: 1000);

            Assert.Equal(withExplicit, withDefault);
        }

        [Theory]
        [InlineData(1000)]
        [InlineData(500)]
        [InlineData(2000)]
        public void ExponentialBackoffDelay_Returns_Jitter_Within_Base_Delay(int baseDelayInMilliseconds)
        {
            var (_, jitter) = Compute.ExponentialBackoffDelay(5, baseDelayInMilliseconds, int.MaxValue);

            Assert.InRange(jitter, 0, baseDelayInMilliseconds - 1);
        }
    }
}