Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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}");
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        private void Execute()
        {
            for (var i = 0; i < 100; i++)
            {
                limiterGlobal.IsAllowed("not used", "not used", 0, "not used", "not used");

                if (maxExecuted < limiterGlobal.WindowCount)
                {
                    maxExecuted = limiterGlobal.WindowCount;
                }
            }
        }
Ejemplo n.º 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);
        }