コード例 #1
0
        public void Choke_ProvidesExpectedRate(int speedBytesPerSecond, int testDurationSeconds, double stepSizeMs, int requestSize)
        {
            // Choke algorithm deliberately rounds speeds down when fractions are encountered, to avoid overspeed.
            const int tolerancePackets = 3;

            var startTime   = DateTimeOffset.UtcNow;
            var currentTime = startTime;

            _timeSource.GetCurrentTime().Returns(ci => currentTime);

            var bytesAllowed = 0;

            var choke = new Choke(DataRate.FromBytesPerSecond(speedBytesPerSecond), new TimeSourceStopwatch(_timeSource));

            for (double i = 0; i < testDurationSeconds * 1000; i += stepSizeMs)
            {
                // Every N milliseconds we request more data.
                while (choke.RequestBytes((ushort)requestSize))
                {
                    bytesAllowed += requestSize;
                }

                currentTime = startTime.AddMilliseconds(i);
            }

            Assert.AreEqual(speedBytesPerSecond * testDurationSeconds + choke.BucketSizeBytes, bytesAllowed, delta: requestSize * tolerancePackets);
        }
コード例 #2
0
        public void Operator_div_should_work_correctly()
        {
            var    val1   = new DataSize(30);
            int    val2I  = 2;
            long   val2L  = 2;
            double val2D  = 2;
            var    val2TS = TimeSpan.FromSeconds(2);
            var    val2DR = DataRate.FromBytesPerSecond(2);
            var    res    = 15;

            (val1 / val2I).Should().Be(new DataSize(res));
            (val1 / val2L).Should().Be(new DataSize(res));
            (val1 / val2D).Should().Be(new DataSize(res));
            (val1 / val2TS).Should().Be(new DataRate(res));
            (val1 / val2DR).Should().Be(new TimeSpan(0, 0, 0, res));
        }
コード例 #3
0
        public void Choke_SatisfiesRequestsWheNotDepleted()
        {
            var startTime = DateTimeOffset.UtcNow;

            _timeSource.GetCurrentTime().Returns(startTime);

            var choke = new Choke(DataRate.FromBytesPerSecond(10000), new TimeSourceStopwatch(_timeSource));

            // The test is ignorant of the internal buffers but this must surely deplete it.
            for (var i = 0; i < 100; i++)
            {
                choke.RequestBytes(100);
            }

            Assert.IsFalse(choke.RequestBytes(100));

            // After a second there should be some capacity available gain.
            _timeSource.GetCurrentTime().Returns(startTime.AddSeconds(1));

            Assert.IsTrue(choke.RequestBytes(100));
        }
コード例 #4
0
        public void Should_return_FromBytesPerSecond()
        {
            var val = 100L;

            DataRate.FromBytesPerSecond(val).Should().Be(new DataRate(val));
        }
コード例 #5
0
        public void Choke_WithHugeRequest_ThrowsException()
        {
            var choke = new Choke(DataRate.FromBytesPerSecond(1234), new TimeSourceStopwatch(_timeSource));

            Assert.ThrowsException <NotSupportedException>(() => choke.RequestBytes(12345));
        }