예제 #1
0
        public async Task IsAllowed_MultiThread_DoesNotExceedMax()
        {
            limiterGlobal = new LimiterAux(maxAllowedGlobal, windowSecondsGlobal);

            var tasks = new List <Task>
            {
                Task.Run(Execute), Task.Run(Execute), Task.Run(Execute), Task.Run(Execute)
            };

            await Task.WhenAll(tasks).ConfigureAwait(false);

            Assert.AreEqual(maxAllowedGlobal, maxExecuted);
        }
예제 #2
0
        public void IsAllowed_DoesNotExceedMax()
        {
            const int maxAllowed    = 2;
            const int windowSeconds = 1;

            var limiter = new LimiterAux(maxAllowed, windowSeconds);

            var totalExecuted = Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));

            totalExecuted += Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));
            totalExecuted += Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));
            totalExecuted += Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));
            totalExecuted += Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));

            Assert.AreEqual(maxAllowed, totalExecuted);
        }
예제 #3
0
        public void IsAllowed_WindowCount_IsCorrect()
        {
            const int maxAllowed    = 2;
            const int windowSeconds = 1;

            var limiter = new LimiterAux(maxAllowed, windowSeconds);

            var totalExecuted = Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));

            totalExecuted += Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));
            totalExecuted += Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));
            totalExecuted += Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));
            totalExecuted += Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));

            Assert.AreEqual(totalExecuted, limiter.WindowCount, $"totalExecuted: {totalExecuted} | limiter.WindowCount: {limiter.WindowCount}");
        }
예제 #4
0
        public void IsAllowed_NewWindow_CounterIsReset()
        {
            var       totalExecuted = 0;
            const int maxAllowed    = 2;
            const int windowSeconds = 1;

            var limiter = new LimiterAux(maxAllowed, windowSeconds);

            totalExecuted  = Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));
            totalExecuted += Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));
            totalExecuted += Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));
            totalExecuted += Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));
            totalExecuted += Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used"));

            Assert.AreEqual(maxAllowed, totalExecuted);

            totalExecuted = Convert.ToInt32(limiter.IsAllowedAux(DateTime.Now.AddSeconds(10)));

            Assert.AreEqual(1, totalExecuted);
        }
예제 #5
0
        public void IsAllowed_InvalidDate_ReturnsFalse()
        {
            const int maxAllowed    = 2;
            const int windowSeconds = 1;

            var limiter = new LimiterAux(maxAllowed, windowSeconds);

            var totalExecuted = Convert.ToInt32(limiter.IsAllowed("not used", "not used", 0, "not used", "not used")); Console.WriteLine(limiter.WindowCount);

            Assert.AreEqual(1, totalExecuted);
            Assert.AreEqual(1, limiter.WindowCount);

            var oldWindowCount = limiter.WindowCount;

            totalExecuted  = Convert.ToInt32(limiter.IsAllowedAux(DateTime.Now.AddSeconds(-10))); Console.WriteLine(limiter.WindowCount);
            totalExecuted += Convert.ToInt32(limiter.IsAllowedAux(DateTime.Now.AddSeconds(-10))); Console.WriteLine(limiter.WindowCount);


            Assert.AreEqual(0, totalExecuted);
            Assert.AreEqual(oldWindowCount, limiter.WindowCount);
        }