Exemplo n.º 1
0
        public void NoLimit()
        {
            var tps = new PerSecondLimiter(0);

            for (var i = 0; i < 100; i++)
            {
                Assert.True(tps.Wait(0));
            }
        }
Exemplo n.º 2
0
        public void Wait()
        {
            var tps = new PerSecondLimiter(1);

            var sw = new Stopwatch();

            sw.Start();
            tps.Wait();
            tps.Wait();
            tps.Wait();
            Assert.True(sw.ElapsedMilliseconds > 1000);
        }
Exemplo n.º 3
0
        public void WaitWithTimeout()
        {
            var tps = new PerSecondLimiter(1);

            var sw = new Stopwatch();

            sw.Start();
            tps.Wait(500);
            tps.Wait();
            tps.Wait();
            Assert.False(tps.Wait(1));  // just wait for 1 ms to check if all is ok
            Assert.True(sw.ElapsedMilliseconds > 1000);
        }
Exemplo n.º 4
0
        public void Basic()
        {
            var tps = new PerSecondLimiter(1);

            while (DateTime.Now.Millisecond > 1)
            {
                Thread.Sleep(1);
            }                                                          // just wait to get more of a second

            Assert.True(tps.Wait(0), "Should be ready (1)");
            Assert.False(tps.Wait(0), "Should be not ready (1)");

            Thread.Sleep(1000);

            Assert.True(tps.Wait(0), "Should be ready (2)");
            Assert.False(tps.Wait(0), "Should be not ready (2)");
        }
Exemplo n.º 5
0
        public static void Run()
        {
            Terminal.Clear();
            Terminal.WriteLine(" Terminal ", ConsoleColor.Yellow, ConsoleColor.DarkGray);
            Terminal.WriteLine();

            using var counter = new PerSecondCounter();
            counter.Tick      = delegate {
                Console.WriteLine($"TPS: {counter.ValuePerSecond} /s");
            };

            var limiter = new PerSecondLimiter(13);

            while (true)
            {
                foreach (var key in Terminal.ReadAvailableKeys())
                {
                    switch (key)
                    {
                    case ConsoleKey.OemPlus: limiter.PerSecondRate += 1; break;

                    case ConsoleKey.OemMinus:
                        if (limiter.PerSecondRate > 1)
                        {
                            limiter.PerSecondRate -= 1;
                        }
                        break;

                    case ConsoleKey.Escape: return;
                    }
                }

                limiter.Wait();
                counter.Increment();
            }
        }
Exemplo n.º 6
0
 public void ThrowOnNegative()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => {
         var _ = new PerSecondLimiter(-1);
     });
 }