public async Task ThrottlingLimit10CallsPerSecondTest()
        {
            // Arrange
            var tickStart = DateTime.UtcNow.Ticks;

            async Task TestExecute() // Local function thats called by this test.
            {
                await Task.FromResult <object>(null);
            }

            // We need to use an instanced version of it so we can test in isolation.
            var throttle = new ThrottleInstance(2, 1);
            // Act
            await throttle.DoAsync(() => TestExecute());

            await throttle.DoAsync(() => TestExecute());

            await throttle.DoAsync(() => TestExecute());

            // Assert
            long     tickEnd  = DateTime.UtcNow.Ticks;
            TimeSpan timeSpan = TimeSpan.FromTicks(tickEnd - tickStart);

            Assert.IsTrue(timeSpan.Duration().TotalSeconds >= 1);
        }
Esempio n. 2
0
        public static async Task <T> DoAsync <T>(Func <Task <T> > func)
        {
            await _semaphore.WaitAsync();

            try
            {
                if (_throttleInstance == null)
                {
                    _throttleInstance = new ThrottleInstance(CallLimit, CallLimitSeconds);
                }
            }
            finally
            {
                _semaphore.Release();
            }

            return(await _throttleInstance.DoAsync(func));
        }