Ejemplo n.º 1
0
        public async Task GetAsync_Returns_Available_Tokens_If_Request_Exceeds_Available_Count()
        {
            using (var t = new TokenBucket(10, 10000))
            {
                await t.GetAsync(6);

                var count = await t.GetAsync(6);

                Assert.Equal(4, count);
            }
        }
Ejemplo n.º 2
0
        public async Task GetAsync_Waits_For_Reset_If_Bucket_Is_Depleted()
        {
            using (var t = new TokenBucket(1, 10))
            {
                await t.GetAsync(1);

                await t.GetAsync(1);

                await t.GetAsync(1);

                Assert.True(true);
            }
        }
Ejemplo n.º 3
0
        public async Task GetAsync_Decrements_Count_By_Requested_Count()
        {
            using (var t = new TokenBucket(10, 10000))
            {
                await t.GetAsync(5);

                Assert.Equal(5, t.GetProperty <long>("CurrentCount"));
            }
        }
Ejemplo n.º 4
0
        public async Task GetAsync_Returns_Capacity_If_Request_Exceeds_Capacity()
        {
            using (var t = new TokenBucket(10, 10000))
            {
                int tokens = 0;
                var ex     = await Record.ExceptionAsync(async() => tokens = await t.GetAsync(11));

                Assert.Null(ex);
                Assert.Equal(10, tokens);
            }
        }
Ejemplo n.º 5
0
        public async Task Return_Adds_Given_Value()
        {
            using (var t = new TokenBucket(10, 1000000))
            {
                await t.GetAsync(5);

                Assert.Equal(5, t.GetProperty <long>("CurrentCount"));

                t.Return(5);

                Assert.Equal(10, t.GetProperty <long>("CurrentCount"));
            }
        }
Ejemplo n.º 6
0
        public async Task Return_Does_Not_Change_Count_Given_Negative()
        {
            using (var t = new TokenBucket(10, 1000000))
            {
                await t.GetAsync(5);

                Assert.Equal(5, t.GetProperty <long>("CurrentCount"));

                t.Return(-5);

                Assert.Equal(5, t.GetProperty <long>("CurrentCount"));
            }
        }