示例#1
0
        public override async Task GetStatisticsWithZeroPermitCount()
        {
            var limiter = new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
            {
                PermitLimit          = 100,
                QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
                QueueLimit           = 50,
                Window            = TimeSpan.Zero,
                AutoReplenishment = false
            });
            var lease = limiter.AttemptAcquire(0);

            Assert.True(lease.IsAcquired);
            Assert.Equal(1, limiter.GetStatistics().TotalSuccessfulLeases);
            Assert.Equal(100, limiter.GetStatistics().CurrentAvailablePermits);

            lease = await limiter.AcquireAsync(0);

            Assert.True(lease.IsAcquired);
            Assert.Equal(2, limiter.GetStatistics().TotalSuccessfulLeases);
            Assert.Equal(100, limiter.GetStatistics().CurrentAvailablePermits);

            lease = limiter.AttemptAcquire(100);
            Assert.True(lease.IsAcquired);
            Assert.Equal(3, limiter.GetStatistics().TotalSuccessfulLeases);
            Assert.Equal(0, limiter.GetStatistics().CurrentAvailablePermits);

            var lease2 = limiter.AttemptAcquire(0);

            Assert.False(lease2.IsAcquired);
            Assert.Equal(3, limiter.GetStatistics().TotalSuccessfulLeases);
            Assert.Equal(1, limiter.GetStatistics().TotalFailedLeases);
            Assert.Equal(0, limiter.GetStatistics().CurrentAvailablePermits);
        }
示例#2
0
        public void TryReplenishWithAutoReplenish_ReturnsFalse()
        {
            var limiter = new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
            {
                PermitLimit          = 2,
                QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
                QueueLimit           = 1,
                Window            = TimeSpan.FromSeconds(1),
                AutoReplenishment = true
            });

            Assert.Equal(2, limiter.GetStatistics().CurrentAvailablePermits);
            Assert.False(limiter.TryReplenish());
            Assert.Equal(2, limiter.GetStatistics().CurrentAvailablePermits);
        }
示例#3
0
        public override async Task CanAcquireResourceAsync_QueuesAndGrabsOldest()
        {
            var limiter = new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
            {
                PermitLimit          = 1,
                QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
                QueueLimit           = 2,
                Window            = TimeSpan.Zero,
                AutoReplenishment = false
            });
            var lease = await limiter.AcquireAsync();

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

            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.GetStatistics().CurrentAvailablePermits);
            Assert.True(limiter.TryReplenish());

            lease = await wait2;
            Assert.True(lease.IsAcquired);
        }
示例#4
0
        public override async Task CanAcquireResourcesWithAcquireAsyncWithQueuedItemsIfNewestFirst()
        {
            var limiter = new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
            {
                PermitLimit          = 2,
                QueueProcessingOrder = QueueProcessingOrder.NewestFirst,
                QueueLimit           = 2,
                Window            = TimeSpan.Zero,
                AutoReplenishment = false
            });

            var lease = limiter.AttemptAcquire(1);

            Assert.True(lease.IsAcquired);

            var wait = limiter.AcquireAsync(2);

            Assert.False(wait.IsCompleted);

            Assert.Equal(1, limiter.GetStatistics().CurrentAvailablePermits);
            lease = await limiter.AcquireAsync(1);

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

            limiter.TryReplenish();

            lease = await wait;
            Assert.True(lease.IsAcquired);
        }
示例#5
0
        public override async Task CanCancelAcquireAsyncBeforeQueuing()
        {
            var limiter = new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
            {
                PermitLimit          = 1,
                QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
                QueueLimit           = 1,
                Window            = TimeSpan.Zero,
                AutoReplenishment = false
            });
            var lease = limiter.AttemptAcquire(1);

            Assert.True(lease.IsAcquired);

            var cts = new CancellationTokenSource();

            cts.Cancel();

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

            Assert.Equal(cts.Token, ex.CancellationToken);

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

            Assert.Equal(1, limiter.GetStatistics().CurrentAvailablePermits);
        }
示例#6
0
        public override void GetStatisticsReturnsNewInstances()
        {
            var limiter = new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
            {
                PermitLimit          = 1,
                QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
                QueueLimit           = 1,
                Window            = TimeSpan.Zero,
                AutoReplenishment = false
            });

            var stats = limiter.GetStatistics();

            Assert.Equal(1, stats.CurrentAvailablePermits);

            var lease = limiter.AttemptAcquire(1);

            var stats2 = limiter.GetStatistics();

            Assert.NotSame(stats, stats2);
            Assert.Equal(1, stats.CurrentAvailablePermits);
            Assert.Equal(0, stats2.CurrentAvailablePermits);
        }
示例#7
0
        public async Task AutoReplenish_ReplenishesCounters()
        {
            var limiter = new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
            {
                PermitLimit          = 2,
                QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
                QueueLimit           = 1,
                Window            = TimeSpan.FromMilliseconds(1000),
                AutoReplenishment = true
            });

            Assert.Equal(2, limiter.GetStatistics().CurrentAvailablePermits);
            limiter.AttemptAcquire(2);

            var lease = await limiter.AcquireAsync(1);

            Assert.True(lease.IsAcquired);
        }
示例#8
0
        public override async Task CanAcquireResourceAsync_QueuesAndGrabsNewest()
        {
            var limiter = new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
            {
                PermitLimit          = 2,
                QueueProcessingOrder = QueueProcessingOrder.NewestFirst,
                QueueLimit           = 3,
                Window            = TimeSpan.FromMinutes(0),
                AutoReplenishment = false
            });

            var lease = await limiter.AcquireAsync(2);

            Assert.True(lease.IsAcquired);

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

            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(1, limiter.GetStatistics().CurrentAvailablePermits);
            Assert.True(limiter.TryReplenish());

            lease = await wait1;
            Assert.True(lease.IsAcquired);
        }
示例#9
0
        public override async Task GetStatisticsHasCorrectValues()
        {
            var limiter = new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions
            {
                PermitLimit          = 100,
                QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
                QueueLimit           = 50,
                Window            = TimeSpan.Zero,
                AutoReplenishment = false
            });

            var stats = limiter.GetStatistics();

            Assert.Equal(100, stats.CurrentAvailablePermits);
            Assert.Equal(0, stats.CurrentQueuedCount);
            Assert.Equal(0, stats.TotalFailedLeases);
            Assert.Equal(0, stats.TotalSuccessfulLeases);

            // success from acquire + available
            var lease1 = limiter.AttemptAcquire(60);

            stats = limiter.GetStatistics();
            Assert.Equal(40, stats.CurrentAvailablePermits);
            Assert.Equal(0, stats.CurrentQueuedCount);
            Assert.Equal(0, stats.TotalFailedLeases);
            Assert.Equal(1, stats.TotalSuccessfulLeases);

            // queue
            var lease2Task = limiter.AcquireAsync(50);

            stats = limiter.GetStatistics();
            Assert.Equal(40, stats.CurrentAvailablePermits);
            Assert.Equal(50, stats.CurrentQueuedCount);
            Assert.Equal(0, stats.TotalFailedLeases);
            Assert.Equal(1, stats.TotalSuccessfulLeases);

            // failure from wait
            var lease3 = await limiter.AcquireAsync(1);

            Assert.False(lease3.IsAcquired);
            stats = limiter.GetStatistics();
            Assert.Equal(40, stats.CurrentAvailablePermits);
            Assert.Equal(50, stats.CurrentQueuedCount);
            Assert.Equal(1, stats.TotalFailedLeases);
            Assert.Equal(1, stats.TotalSuccessfulLeases);

            // failure from acquire
            var lease4 = limiter.AttemptAcquire(100);

            Assert.False(lease4.IsAcquired);
            stats = limiter.GetStatistics();
            Assert.Equal(40, stats.CurrentAvailablePermits);
            Assert.Equal(50, stats.CurrentQueuedCount);
            Assert.Equal(2, stats.TotalFailedLeases);
            Assert.Equal(1, stats.TotalSuccessfulLeases);

            limiter.TryReplenish();
            await lease2Task;

            // success from wait + available + queue
            stats = limiter.GetStatistics();
            Assert.Equal(50, stats.CurrentAvailablePermits);
            Assert.Equal(0, stats.CurrentQueuedCount);
            Assert.Equal(2, stats.TotalFailedLeases);
            Assert.Equal(2, stats.TotalSuccessfulLeases);
        }