public void Increase_WhenLimitExceeds_ThrowsExceptionDuringWaitTime()
        {
            int nrOfRequests     = 1;
            int allowedTime      = 1;
            int secondsSuspended = 3;

            LimitCounter counter = new LimitCounter(nrOfRequests, TimeSpan.FromSeconds(allowedTime), TimeSpan.FromSeconds(secondsSuspended));

            counter.Increase();

            try
            {
                //t1 = DateTime.Now;
                counter.Increase();
                //t2 = DateTime.Now;
                Assert.Fail("Did not throw exception after exceeding limit");
            }
            catch (RateLimitExceededException)
            {
            }

            System.Threading.Thread.Sleep(1000);

            try
            {
                counter.Increase();
                Assert.Fail("Did not throw exception after 1 second of waiting");
            }
            catch (RateLimitExceededException)
            {
            }

            System.Threading.Thread.Sleep(1000);

            try
            {
                counter.Increase();
                Assert.Fail("Did not throw exception after 2 seconds of waiting");
            }
            catch (RateLimitExceededException)
            {
            }

            System.Threading.Thread.Sleep(985);

            try
            {
                //t3 = DateTime.Now;
                counter.Increase();
                Assert.Fail("Did not throw exception after 3 seconds of waiting");
            }
            catch (RateLimitExceededException)
            {
            }
        }
        public void Increase_WhenLimitDidNotExceed_AllowsAfterAllowedTime()
        {
            int nrOfRequests     = 2;
            int allowedTime      = 1;
            int secondsSuspended = 5;

            LimitCounter counter = new LimitCounter(nrOfRequests, TimeSpan.FromSeconds(allowedTime), TimeSpan.FromSeconds(secondsSuspended));

            counter.Increase();
            counter.Increase();

            System.Threading.Thread.Sleep(1000);

            counter.Increase();
        }
        public void Increase_WhenLimitExceeds_ThrowsException()
        {
            int nrOfRequests     = 1;
            int allowedTime      = 1;
            int secondsSuspended = 5;

            LimitCounter counter = new LimitCounter(nrOfRequests, TimeSpan.FromSeconds(allowedTime), TimeSpan.FromSeconds(secondsSuspended));

            counter.Increase();

            try
            {
                counter.Increase();
                Assert.Fail("Did not throw exception");
            }
            catch (RateLimitExceededException)
            {
            }
        }
        public void Increase_WhenLimitExceeds_AllowsAfterWaitTime()
        {
            int nrOfRequests     = 1;
            int allowedTime      = 1;
            int secondsSuspended = 2;

            LimitCounter counter = new LimitCounter(nrOfRequests, TimeSpan.FromSeconds(allowedTime), TimeSpan.FromSeconds(secondsSuspended));

            counter.Increase();
            try
            {
                counter.Increase();
                Assert.Fail("Did not throw first time");
            }
            catch (RateLimitExceededException)
            {
            }

            System.Threading.Thread.Sleep(2000);

            counter.Increase();
        }
Example #5
0
        static void TestLimitCounter()
        {
            int nrOfRequests     = 90000;
            int allowedTime      = 100;
            int secondsSuspended = 1;

            LimitCounter counter = new LimitCounter(nrOfRequests, TimeSpan.FromSeconds(allowedTime), TimeSpan.FromSeconds(secondsSuspended));

            List <System.Threading.Thread> threads = new List <System.Threading.Thread>();


            for (int x = 0; x < 100; ++x)
            {
                var thread = new System.Threading.Thread(() =>
                {
                    var id = System.Threading.Thread.CurrentThread.ManagedThreadId;
                    for (int y = 0; y < 1000; ++y)
                    {
                        try
                        {
                            counter.Increase();
                            Console.WriteLine(String.Format("T-{0}-{1}", id, y));
                        }
                        catch (RateLimitExceededException e)
                        {
                            Console.WriteLine(String.Format("E{0}-{1}-{2}", id, y, e.Message));
                            System.Threading.Thread.Sleep(1000);
                        }


                        System.Threading.Thread.Yield();
                    }
                });

                threads.Add(thread);
            }
            Console.WriteLine("Starting...");
            foreach (var t in threads)
            {
                t.Start();
            }

            while (threads.TrueForAll(p => p.IsAlive == false))
            {
                System.Threading.Thread.Yield();
                System.Threading.Thread.Sleep(1000);
            }
        }