public void TryReplenishWithAllTokensAvailable_Noops()
        {
            var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(2, QueueProcessingOrder.OldestFirst, 1,
                                                                                       TimeSpan.Zero, 1, autoReplenishment: false));

            Assert.Equal(2, limiter.GetAvailablePermits());
            Assert.True(limiter.TryReplenish());
            Assert.Equal(2, limiter.GetAvailablePermits());
        }
        public void TryReplenishWithAutoReplenish_ReturnsFalse()
        {
            var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(2, QueueProcessingOrder.OldestFirst, 1,
                                                                                       TimeSpan.FromSeconds(1), 1, autoReplenishment: true));

            Assert.Equal(2, limiter.GetAvailablePermits());
            Assert.False(limiter.TryReplenish());
            Assert.Equal(2, limiter.GetAvailablePermits());
        }
        public void TryReplenishHonorsTokensPerPeriod()
        {
            var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(7, QueueProcessingOrder.OldestFirst, 1,
                                                                                       TimeSpan.Zero, 3, autoReplenishment: false));

            Assert.True(limiter.Acquire(5).IsAcquired);
            Assert.False(limiter.Acquire(3).IsAcquired);

            Assert.Equal(2, limiter.GetAvailablePermits());
            Assert.True(limiter.TryReplenish());
            Assert.Equal(5, limiter.GetAvailablePermits());

            Assert.True(limiter.TryReplenish());
            Assert.Equal(7, limiter.GetAvailablePermits());
        }
        public override async Task CanAcquireResourceAsync_QueuesAndGrabsNewest()
        {
            var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(2, QueueProcessingOrder.NewestFirst, 3,
                                                                                       TimeSpan.FromMinutes(0), 1, autoReplenishment: false));

            var lease = await limiter.WaitAsync(2);

            Assert.True(lease.IsAcquired);

            var wait1 = limiter.WaitAsync(2);
            var wait2 = limiter.WaitAsync();

            Assert.False(wait1.IsCompleted);
            Assert.False(wait2.IsCompleted);

            lease.Dispose();
            Assert.True(limiter.TryReplenish());

            // second queued item completes first with NewestFirst
            lease = await wait2;
            Assert.True(lease.IsAcquired);
            Assert.False(wait1.IsCompleted);

            lease.Dispose();
            Assert.Equal(0, limiter.GetAvailablePermits());
            Assert.True(limiter.TryReplenish());
            Assert.True(limiter.TryReplenish());

            lease = await wait1;
            Assert.True(lease.IsAcquired);
        }
        public override async Task CanAcquireResourceAsync_QueuesAndGrabsOldest()
        {
            var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(1, QueueProcessingOrder.OldestFirst, 2,
                                                                                       TimeSpan.Zero, 1, autoReplenishment: false));
            var lease = await limiter.WaitAsync();

            Assert.True(lease.IsAcquired);
            var wait1 = limiter.WaitAsync();
            var wait2 = limiter.WaitAsync();

            Assert.False(wait1.IsCompleted);
            Assert.False(wait2.IsCompleted);

            lease.Dispose();
            Assert.True(limiter.TryReplenish());

            lease = await wait1;
            Assert.True(lease.IsAcquired);
            Assert.False(wait2.IsCompleted);

            lease.Dispose();
            Assert.Equal(0, limiter.GetAvailablePermits());
            Assert.True(limiter.TryReplenish());

            lease = await wait2;
            Assert.True(lease.IsAcquired);
        }
        public override async Task CanAcquireResourcesWithWaitAsyncWithQueuedItemsIfNewestFirst()
        {
            var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(2, QueueProcessingOrder.NewestFirst, 2,
                                                                                       TimeSpan.Zero, 2, autoReplenishment: false));

            var lease = limiter.Acquire(1);

            Assert.True(lease.IsAcquired);

            var wait = limiter.WaitAsync(2);

            Assert.False(wait.IsCompleted);

            Assert.Equal(1, limiter.GetAvailablePermits());
            lease = await limiter.WaitAsync(1).DefaultTimeout();

            Assert.True(lease.IsAcquired);
            Assert.False(wait.IsCompleted);

            limiter.TryReplenish();

            lease = await wait.DefaultTimeout();

            Assert.True(lease.IsAcquired);
        }
        public async Task AutoReplenish_ReplenishesTokens()
        {
            var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(2, QueueProcessingOrder.OldestFirst, 1,
                                                                                       TimeSpan.FromMilliseconds(1000), 1, autoReplenishment: true));

            Assert.Equal(2, limiter.GetAvailablePermits());
            limiter.Acquire(2);

            var lease = await limiter.WaitAsync(1);

            Assert.True(lease.IsAcquired);
        }
        public override async Task CanCancelWaitAsyncBeforeQueuing()
        {
            var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(1, QueueProcessingOrder.OldestFirst, 1,
                                                                                       TimeSpan.Zero, 1, autoReplenishment: false));
            var lease = limiter.Acquire(1);

            Assert.True(lease.IsAcquired);

            var cts = new CancellationTokenSource();

            cts.Cancel();

            await Assert.ThrowsAsync <TaskCanceledException>(() => limiter.WaitAsync(1, cts.Token).AsTask());

            lease.Dispose();
            Assert.True(limiter.TryReplenish());

            Assert.Equal(1, limiter.GetAvailablePermits());
        }
        public async Task ReplenishWorksWhenTicksWrap()
        {
            var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(10, QueueProcessingOrder.OldestFirst, 2,
                                                                                       TimeSpan.FromMilliseconds(2), 1, autoReplenishment: false));

            var lease = limiter.Acquire(10);

            Assert.True(lease.IsAcquired);

            var wait = limiter.WaitAsync(1);

            Assert.False(wait.IsCompleted);

            var replenishInternalMethod = typeof(TokenBucketRateLimiter).GetMethod("ReplenishInternal", Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance) !;

            // This will set the last tick to the max value
            replenishInternalMethod.Invoke(limiter, new object[] { uint.MaxValue });

            lease = await wait;
            Assert.True(lease.IsAcquired);

            wait = limiter.WaitAsync(1);
            Assert.False(wait.IsCompleted);

            // ticks wrapped, should replenish
            replenishInternalMethod.Invoke(limiter, new object[] { 2U });
            lease = await wait;
            Assert.True(lease.IsAcquired);

            replenishInternalMethod.Invoke(limiter, new object[] { uint.MaxValue });

            wait = limiter.WaitAsync(2);
            Assert.False(wait.IsCompleted);

            // ticks wrapped, but only 1 millisecond passed, make sure the wrapping behaves correctly and replenish doesn't happen
            replenishInternalMethod.Invoke(limiter, new object[] { 1U });
            Assert.False(wait.IsCompleted);
            Assert.Equal(1, limiter.GetAvailablePermits());
        }
        public async Task ReplenishWorksWhenTicksWrap()
        {
            var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(10, QueueProcessingOrder.OldestFirst, 2,
                                                                                       TimeSpan.FromMilliseconds(2), 1, autoReplenishment: false));

            var lease = limiter.Acquire(10);

            Assert.True(lease.IsAcquired);

            var wait = limiter.WaitAsync(1);

            Assert.False(wait.IsCompleted);

            // This will set the last tick to the max value
            limiter.ReplenishInternal(uint.MaxValue);

            lease = await wait.DefaultTimeout();

            Assert.True(lease.IsAcquired);

            wait = limiter.WaitAsync(1);
            Assert.False(wait.IsCompleted);

            // ticks wrapped, should replenish
            limiter.ReplenishInternal(2);
            lease = await wait.DefaultTimeout();

            Assert.True(lease.IsAcquired);

            limiter.ReplenishInternal(uint.MaxValue);

            wait = limiter.WaitAsync(2);
            Assert.False(wait.IsCompleted);

            // ticks wrapped, but only 1 millisecond passed, make sure the wrapping behaves correctly and replenish doesn't happen
            limiter.ReplenishInternal(1);
            Assert.False(wait.IsCompleted);
            Assert.Equal(1, limiter.GetAvailablePermits());
        }