Ejemplo n.º 1
0
 public void TestOperations()
 {
     var timer = RateLimiter.Timer = new TestTimer();
      var limiter = (RateLimiter)null;
      // no throttling needed
      {
     timer = RateLimiter.Timer = new TestTimer();
     limiter = new RateLimiter(1);
     limiter.Throttle();
     Assert.AreEqual(timer.Seconds, 0);
     for (var i = 0; i < 1000; i++)
     {
        timer.Sleep(1);
        limiter.Process(1);
        limiter.Throttle();
        Assert.AreEqual(timer.Seconds, i + 1);
        Assert.IsTrue(limiter.InControl);
        Assert.IsFalse(limiter.OutOfControl);
     }
      }
      // manual throttling
      {
     timer = RateLimiter.Timer = new TestTimer();
     limiter = new RateLimiter(1);
     for (var i = 0; i < 1000; i++)
     {
        limiter.Process(1);
        Assert.IsFalse(limiter.InControl);
        Assert.IsTrue(limiter.OutOfControl);
        limiter.Throttle();
        Assert.AreEqual(timer.Seconds, i + 1);
        Assert.IsTrue(limiter.InControl);
        Assert.IsFalse(limiter.OutOfControl);
     }
      }
      // automatic throttling
      {
     timer = RateLimiter.Timer = new TestTimer();
     limiter = new RateLimiter(1);
     for (var i = 0; i < 1000; i++)
     {
        limiter.ProcessAndThrottle(1);
        Assert.AreEqual(timer.Seconds, i + 1);
        Assert.IsTrue(limiter.InControl);
        Assert.IsFalse(limiter.OutOfControl);
     }
      }
      // multi-bps throttling
      {
     timer = RateLimiter.Timer = new TestTimer();
     limiter = new RateLimiter(3);
     for (var i = 0; i < 1000; i++)
     {
        limiter.ProcessAndThrottle(1);
        Assert.AreEqual(timer.Seconds, (i + 1) / 3);
        Assert.IsTrue(limiter.InControl);
        Assert.IsFalse(limiter.OutOfControl);
     }
      }
      // multi-byte throttling
      {
     timer = RateLimiter.Timer = new TestTimer();
     limiter = new RateLimiter(3);
     for (var i = 0; i < 1000; i++)
     {
        limiter.ProcessAndThrottle(5);
        Assert.AreEqual(timer.Seconds, 5 * (i + 1) / 3);
        Assert.IsTrue(limiter.InControl);
        Assert.IsFalse(limiter.OutOfControl);
     }
      }
      // stream throttling
      {
     timer = RateLimiter.Timer = new TestTimer();
     limiter = new RateLimiter(3);
     using (var stream = limiter.CreateStreamFilter(new System.IO.MemoryStream()))
     for (var i = 0; i < 1000; i++)
     {
        stream.Write(new Byte[5], 0, 5);
        Assert.AreEqual(timer.Seconds, 5 * (i + 1) / 3);
        Assert.IsTrue(limiter.InControl);
        Assert.IsFalse(limiter.OutOfControl);
     }
      }
 }
Ejemplo n.º 2
0
 public void TestProperties()
 {
     var timer = RateLimiter.Timer = new TestTimer();
      var limiter = (RateLimiter)null;
      // control flags
      Assert.IsTrue(new RateLimiter(1).InControl);
      Assert.IsTrue(new RateLimiter(Int32.MaxValue).InControl);
      Assert.IsFalse(new RateLimiter(1).OutOfControl);
      Assert.IsFalse(new RateLimiter(Int32.MaxValue).OutOfControl);
      limiter = new RateLimiter(1);
      Assert.IsTrue(limiter.InControl);
      Assert.IsFalse(limiter.OutOfControl);
      limiter.Process(1);
      Assert.IsFalse(limiter.InControl);
      Assert.IsTrue(limiter.OutOfControl);
 }