public override async Task DisposeReleasesQueuedAcquires() { var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(1, QueueProcessingOrder.OldestFirst, 3, TimeSpan.Zero, 1, autoReplenishment: false)); var lease = limiter.Acquire(1); var wait1 = limiter.WaitAsync(1); var wait2 = limiter.WaitAsync(1); var wait3 = limiter.WaitAsync(1); Assert.False(wait1.IsCompleted); Assert.False(wait2.IsCompleted); Assert.False(wait3.IsCompleted); limiter.Dispose(); lease = await wait1; Assert.False(lease.IsAcquired); lease = await wait2; Assert.False(lease.IsAcquired); lease = await wait3; Assert.False(lease.IsAcquired); // Throws after disposal Assert.Throws <ObjectDisposedException>(() => limiter.Acquire(1)); await Assert.ThrowsAsync <ObjectDisposedException>(() => limiter.WaitAsync(1).AsTask()); }
public override async Task CanDisposeAfterCancelingQueuedRequest() { 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(); var wait = limiter.WaitAndAcquireAsync(1, cts.Token); cts.Cancel(); var ex = await Assert.ThrowsAsync <TaskCanceledException>(() => wait.AsTask()); Assert.Equal(cts.Token, ex.CancellationToken); // Make sure dispose doesn't have any side-effects when dealing with a canceled queued item limiter.Dispose(); }