示例#1
0
        public void AddApiCallTest()
        {
            var limit         = new RateLimit();
            var privateObject = new PrivateObject(limit);

            limit.AddApiCall();
            Thread.Sleep(500);
            limit.AddApiCall();
            Thread.Sleep(500);
            limit.AddApiCall();

            Assert.AreEqual(3, (privateObject.GetFieldOrProperty("_webServiceCalls") as List <DateTime>).Count);
        }
示例#2
0
        public async Task Delay_OverLimit_Delay()
        {
            // arrange
            const int REQUESTS_PER_HOUR = 1000;
            var       rateLimit         = new RateLimit(true);

            rateLimit.AddApiCall();
            rateLimit.AddApiCall();

            // act
            var result = await rateLimit.Delay(REQUESTS_PER_HOUR);

            Assert.NotEqual(0, result);
        }
示例#3
0
    public async static Task HandleRateLimit()
    {
        int delayInMilliseconds = _apiRateLimit.GetDelay(MtgApiController.RatelimitLimit);

        if (delayInMilliseconds > 0)
        {
            await Task.Delay(delayInMilliseconds);
        }

        _apiRateLimit.AddApiCall();
    }
示例#4
0
        public void GetDelayTest()
        {
            var limit = new RateLimit();

            // Test passing 0 for the request per hour.
            Assert.AreEqual(0, limit.GetDelay(0));

            // No web calls yet.
            Assert.AreEqual(0, limit.GetDelay(2000));

            // Test being under the limit.
            limit.AddApiCall();
            Thread.Sleep(500);
            limit.AddApiCall();
            Assert.AreEqual(0, limit.GetDelay(2000));

            // Test being over the limit.
            Thread.Sleep(500);
            limit.AddApiCall();
            Thread.Sleep(500);
            limit.AddApiCall();
            Thread.Sleep(500);
            limit.AddApiCall();
            Thread.Sleep(500);
            limit.AddApiCall();
            Assert.AreNotEqual(0, limit.GetDelay(2000));
        }
        public async Task Delay_UnderLimit_NoDelay()
        {
            // arrange
            const int REQUESTS_PER_HOUR = 2000;
            var       rateLimit         = new RateLimit(true);

            rateLimit.AddApiCall();

            // act
            var result = await rateLimit.Delay(REQUESTS_PER_HOUR, default);

            Assert.Equal(0, result);
        }
示例#6
0
    public async static Task HandleRateLimit()
    {
        await _semaphoreSlim.WaitAsync().ConfigureAwait(false);

        try
        {
            int delayInMilliseconds = _apiRateLimit.GetDelay(RatelimitLimit);

            if (delayInMilliseconds > 0)
            {
                await Task.Delay(delayInMilliseconds).ConfigureAwait(false);
            }

            _apiRateLimit.AddApiCall();
        }
        finally
        {
            _semaphoreSlim.Release();
        }
    }