public void GetNextAllowedCallTime_WhenOnePreviousCallTrackedForCurrentTime_ShouldBeNowPlusTimespanRange(int millisecondsTimespan)
        {
            var currentDate = new DateTime(2018, 01, 01);

            ReferenceTime.FreezeAtUtc(currentDate);

            var callTracker = new Tracker(_store);

            callTracker.CallWillHappenIn(0);

            var standardRateLimit = new StandardLimit(1, TimeSpan.FromMilliseconds(millisecondsTimespan));

            var nextAllowedCallTime = standardRateLimit.GetNextAllowedCallTime(callTracker);

            Assert.Equal(millisecondsTimespan, nextAllowedCallTime);

            ReferenceTime.Unfreeze();
        }
        public void GetNextAllowedCallTime_WhenOnePreviousCallWasMadeMoreThanTimespanAgo_ReturnsZero(int lastCallMillisecondsAgo)
        {
            var currentDate = new DateTime(2018, 01, 01);

            ReferenceTime.FreezeAtUtc(currentDate);

            var callTracker = new Tracker(_store);

            callTracker.CallWillHappenIn(-lastCallMillisecondsAgo);

            var standardRateLimit = new StandardLimit(1, TimeSpan.FromMilliseconds(lastCallMillisecondsAgo - 100));

            var nextAllowedCallTime = standardRateLimit.GetNextAllowedCallTime(callTracker);

            Assert.Equal(0, nextAllowedCallTime);

            ReferenceTime.Unfreeze();
        }
        public void GetNextAllowedCallTime_ForTwoPreviousCalls_ReturnsCorrectTimespan()
        {
            var currentDate = new DateTime(2018, 01, 01);

            ReferenceTime.FreezeAtUtc(currentDate);

            var callTracker = new Tracker(_store);

            callTracker.CallWillHappenIn(10);
            callTracker.CallWillHappenIn(30);

            var standardRateLimit   = new StandardLimit(2, TimeSpan.FromMilliseconds(100));
            var nextAllowedCallTime = standardRateLimit.GetNextAllowedCallTime(callTracker);

            // 2 calls allowed every 100 milliseconds, so next call should be allowed in 110 milliseconds
            Assert.Equal(110, nextAllowedCallTime);

            ReferenceTime.Unfreeze();
        }
Exemplo n.º 4
0
        public void TrimCallsForRateLimits_ForOneRateLimit_KeepsTheMostRecentCalls()
        {
            var now = new DateTime(2018, 01, 01);

            ReferenceTime.FreezeAtUtc(now);

            var callTracker = new Tracker(_store);

            callTracker.CallWillHappenIn(0);
            callTracker.CallWillHappenIn(1000);

            var rateLimit = new StandardLimit(1, TimeSpan.FromSeconds(1));

            callTracker.TrimCallsForRateLimits(rateLimit);

            Assert.Single(callTracker.History);
            Assert.Equal(now.AddMilliseconds(1000), callTracker.History.Single());

            ReferenceTime.Unfreeze();
        }