From fd483df9ffaef59b7d5bd2337557cf681d8cfd37 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 1 Mar 2026 16:50:06 -0600 Subject: add additional tests for exponential comp --- tests/slskd.Tests.Unit/Common/ComputeTests.cs | 55 ++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/tests/slskd.Tests.Unit/Common/ComputeTests.cs b/tests/slskd.Tests.Unit/Common/ComputeTests.cs index 021d4faa..9090ad36 100644 --- a/tests/slskd.Tests.Unit/Common/ComputeTests.cs +++ b/tests/slskd.Tests.Unit/Common/ComputeTests.cs @@ -5,17 +5,54 @@ namespace slskd.Tests.Unit.Common public class ComputeTests { [Theory] - [InlineData(1, 300000, 0)] - [InlineData(2, 300000, 1000)] - [InlineData(3, 300000, 3000)] - [InlineData(4, 300000, 7000)] - [InlineData(5, 300000, 15000)] - [InlineData(6, 300000, 31000)] - [InlineData(999999, 300000, 300000)] - public void ExponentialBackoffDelay(int iteration, int maxDelayInMs, int expectedDelay) + // 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, maxDelayInMs); + 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); + } } } -- cgit v1.2.3