public void ControlBeforeUpdate()
        {
            var controller = new BandwidthController {
                TargetSpeed = 1024
            };

            Assert.IsFalse(controller.CanTransmit(3453), "Controller should NOT allow more than target speed");
            Assert.IsTrue(controller.CanTransmit(512), "Controller should allow transmit 512");
            controller.SetTransmittion(512);
            Assert.IsTrue(controller.CanTransmit(512), "Controller should allow transmit 512");
            controller.SetTransmittion(512);
            Assert.IsFalse(controller.CanTransmit(512), "Controller should NOT allow transmit 512 after exceed the limit");
        }
        public void ControlAccumulatedBytesAfterUpdate()
        {
            var controller = new BandwidthController {
                TargetSpeed = 1024
            };

            controller.SetTransmittion(0);
            controller.Update(measuredSpeed: 0, deltaTime: TimeSpan.FromSeconds(1));
            Assert.IsTrue(controller.CanTransmit(2048), "Controller should allow transmit 2048");
        }
        public void ControlSmallTimeFrame()
        {
            var controller = new BandwidthController {
                TargetSpeed = 1024
            };

            controller.SetTransmittion(1024);
            controller.Update(measuredSpeed: 1024, deltaTime: TimeSpan.FromSeconds(0.5));
            Assert.IsFalse(controller.CanTransmit(1024), "Controller should NOT allow transmit ");
        }
        public void ControlTooFast()
        {
            var controller = new BandwidthController {
                TargetSpeed = 1024
            };

            controller.SetTransmittion(4 * 1024);
            controller.Update(measuredSpeed: 4 * 1024, deltaTime: TimeSpan.FromSeconds(1));
            Assert.IsFalse(controller.CanTransmit(1), "Controller should NOT allow transmit");
            Assert.IsTrue(controller.CanTransmit(0), "Controller should allow transmit 0 bytes");
        }
        public void ControlTooSlow()
        {
            var controller = new BandwidthController {
                TargetSpeed = 1024
            };

            controller.SetTransmittion(512);
            controller.Update(measuredSpeed: 512, deltaTime: TimeSpan.FromSeconds(1));
            Assert.IsFalse(controller.CanTransmit(2048), "Controller should NOT allow transmit more than 1024 bytes");
            Assert.IsTrue(controller.CanTransmit(1024), "Controller should allow transmit 1024");
        }
        public void ShouldTakeAbout4Seconds()
        {
            var list       = new Queue(Enumerable.Range(1, 4000).ToArray());
            var controller = new BandwidthController {
                TargetSpeed = 1000
            };
            var processed = 0;
            var timer     = new System.Timers.Timer();

            timer.Elapsed += (sender, args) =>
            {
                lock (controller)
                {
                    controller.Update(processed, TimeSpan.FromMilliseconds(250));
                    processed = 0;
                }
            };
            timer.Interval = 250;
            var sw = new Stopwatch();

            timer.Start();
            sw.Start();
            while (list.Count > 0)
            {
                if (controller.CanTransmit(1))
                {
                    list.Dequeue();
                    processed++;
                    controller.SetTransmittion(1);
                }
                else
                {
                    Thread.Sleep(10);
                }
            }
            timer.Stop();
            sw.Stop();
            var       measuredTime = sw.ElapsedMilliseconds;
            const int expectedTime = 4000;

            Assert.IsTrue(Math.Abs(measuredTime - expectedTime) < (expectedTime * 0.3));
        }