Ejemplo n.º 1
0
        public async Task Create_NoLimiter()
        {
            var partition = RateLimitPartition.CreateNoLimiter(1);

            var factoryProperty = typeof(RateLimitPartition <int>).GetField("Factory", Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance) !;
            var factory         = (Func <int, RateLimiter>)factoryProperty.GetValue(partition);
            var limiter         = factory(1);

            // How do we test an internal implementation of a limiter that doesn't limit? Just try some stuff that normal limiters would probably block on and see if it works.
            var available = limiter.GetAvailablePermits();
            var lease     = limiter.Acquire(int.MaxValue);

            Assert.True(lease.IsAcquired);
            Assert.Equal(available, limiter.GetAvailablePermits());

            lease = limiter.Acquire(int.MaxValue);
            Assert.True(lease.IsAcquired);

            var wait = limiter.WaitAsync(int.MaxValue);

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

            lease.Dispose();
        }
Ejemplo n.º 2
0
        public async Task Create_NoLimiter()
        {
            var partition = RateLimitPartition.CreateNoLimiter(1);

            var limiter = partition.Factory(1);

            // How do we test an internal implementation of a limiter that doesn't limit? Just try some stuff that normal limiters would probably block on and see if it works.
            var available = limiter.GetAvailablePermits();
            var lease     = limiter.Acquire(int.MaxValue);

            Assert.True(lease.IsAcquired);
            Assert.Equal(available, limiter.GetAvailablePermits());

            lease = limiter.Acquire(int.MaxValue);
            Assert.True(lease.IsAcquired);

            var wait = limiter.WaitAsync(int.MaxValue);

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

            lease.Dispose();
        }