コード例 #1
0
        public void ShouldThrottle_WhenCalledAndConsumingAllTokensAtOnce_WillReturnFalse()
        {
            var throttler      = new RollingWindowThrottler(3, TimeSpan.FromSeconds(1));
            var shouldThrottle = throttler.ShouldThrottle(3, out _);

            shouldThrottle.ShouldBeFalse();
        }
コード例 #2
0
        public void ShouldThrottle_WhenCalled_WillReturnFalse()
        {
            var throttler      = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1));
            var shouldThrottle = throttler.ShouldThrottle(1, out _);

            shouldThrottle.ShouldBeFalse();
        }
コード例 #3
0
        public void ShouldThrottle_WhenCalledWithTokensLessThanOne_WillThrow()
        {
            var  throttler = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1));
            long waitTimeMillis;

            Assert.Throws <ArgumentOutOfRangeException>(() => throttler.ShouldThrottle(0, out waitTimeMillis));
        }
コード例 #4
0
        public void ShouldThrottle_WhenCalledAtEndOfRollingWindowAndAllows2PerSecond_WillReturnFalse()
        {
            SystemTime.SetCurrentTimeUtc = () => referenceTime;
            var virtualNow = SystemTime.UtcNow;

            var  throttler = new RollingWindowThrottler(2, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var  shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);

            shouldThrottle.ShouldBeFalse();

            //first rolling window expired, init a new one
            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(1.2);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();

            //inside second rolling window, under threshold
            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(1.3);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();

            //second rolling window expired, beginning third window
            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(2.2);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();

            //third window, under threshold
            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(2.3);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();
        }
コード例 #5
0
        public void ShouldThrottle_WhenCalledTwiceinSameSecondAndAllows1PerSecond_WillReturnTrue()
        {
            SystemTime.SetCurrentTimeUtc = () => referenceTime;
            var virtualNow = SystemTime.UtcNow;

            var  throttler = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var  shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);

            //shouldThrottle.ShouldBeFalse();
            Assert.False(shouldThrottle);

            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(0.5);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            //shouldThrottle.ShouldBeTrue();
            Assert.True(shouldThrottle);

            //waitTimeMillis.ShouldEqual(500);
            Assert.Equal(waitTimeMillis, 500);

            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(0.8);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            //shouldThrottle.ShouldBeTrue();
            Assert.True(shouldThrottle);

            //waitTimeMillis.ShouldEqual(200);
            Assert.Equal(waitTimeMillis, 200);
        }
コード例 #6
0
        public void ShouldThrottle_WhenCalledWithMoreTokensThanOccurrences_WillReturnTrue()
        {
            var throttler      = new RollingWindowThrottler(2, TimeSpan.FromSeconds(1));
            var shouldThrottle = throttler.ShouldThrottle(3, out _);

            shouldThrottle.ShouldBeTrue();
        }
コード例 #7
0
        public void ShouldThrottle_WhenCalled_WillReturnFalse()
        {
            var  throttler = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var  shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);

            //shouldThrottle.ShouldBeFalse();
            Assert.False(shouldThrottle);
        }
コード例 #8
0
        public void ShouldThrottle_WhenCalledWithMoreTokensThanOccurrences_WillReturnTrue()
        {
            var  throttler = new RollingWindowThrottler(2, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var  shouldThrottle = throttler.ShouldThrottle(3, out waitTimeMillis);

            //shouldThrottle.ShouldBeTrue();
            Assert.True(shouldThrottle);
        }
コード例 #9
0
 //private readonly ILog _log;
 /// <summary>
 /// 创建 CMPP 连接
 /// </summary>
 /// <param name="host"></param>
 /// <param name="port"></param>
 /// <param name="cpId"></param>
 /// <param name="password"></param>
 /// <param name="serviceId"></param>
 /// <param name="appPhone"></param>
 public CmppConnection(string host, int port, string cpId, string password, string serviceId, string appPhone, RollingWindowThrottler thrott, ILog log)
     : base(host, port, log)
 {
     this.cpId      = cpId;
     this.password  = password;
     this.serviceId = serviceId;
     this.appPhone  = appPhone;
     this.thrott    = thrott;
     this._log      = log;
 }
コード例 #10
0
        public void ShouldThrottle_WhenCalledTwiceinSameSecondAndAllows2PerSecond_WillReturnFalse()
        {
            SystemTime.SetCurrentTimeUtc = () => referenceTime;
            var virtualNow = SystemTime.UtcNow;

            var throttler      = new RollingWindowThrottler(2, TimeSpan.FromSeconds(1));
            var shouldThrottle = throttler.ShouldThrottle(1, out _);

            shouldThrottle.ShouldBeFalse();

            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(0.5);
            shouldThrottle = throttler.ShouldThrottle(1, out _);
            shouldThrottle.ShouldBeFalse();
        }
コード例 #11
0
        public void ShouldThrottle_WhenCalledAfterSecondPassAndAllows1PerSecond_WillReturnFalse()
        {
            SystemTime.SetCurrentTimeUtc = () => referenceTime;
            var virtualNow = SystemTime.UtcNow;

            var throttler = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();

            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(1);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();
        }
コード例 #12
0
        public void ShouldThrottle_WhenCalledAndConsumingAllTokensAtOnceAndThenCalledOnceMore_WillReturnTrue()
        {
            SystemTime.SetCurrentTimeUtc = () => referenceTime;
            var virtualNow = SystemTime.UtcNow;

            var throttler = new RollingWindowThrottler(3, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var shouldThrottle = throttler.ShouldThrottle(3, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();

            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(0.2);
            shouldThrottle = throttler.ShouldThrottle(3, out waitTimeMillis);
            shouldThrottle.ShouldBeTrue();
            waitTimeMillis.ShouldEqual(800);
        }
コード例 #13
0
        public void ShouldThrottle_WhenCalledAfterSecondPassAndAllows1PerSecond_WillReturnFalse()
        {
            SystemTime.SetCurrentTimeUtc = () => referenceTime;
            var virtualNow = SystemTime.UtcNow;

            var  throttler = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var  shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);

            shouldThrottle.ShouldBeFalse();

            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(1);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();
        }
コード例 #14
0
        public void ShouldThrottle_WhenCalledAndConsumingAllTokensAtOnceAndThenCalledOnceMoreAfterRollingWindowEnd_WillReturnFalse()
        {
            SystemTime.SetCurrentTimeUtc = () => referenceTime;
            var virtualNow = SystemTime.UtcNow;

            var  throttler = new RollingWindowThrottler(3, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var  shouldThrottle = throttler.ShouldThrottle(3, out waitTimeMillis);

            shouldThrottle.ShouldBeFalse();

            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(1.1);
            shouldThrottle = throttler.ShouldThrottle(3, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();
            waitTimeMillis.ShouldEqual(0);
        }
コード例 #15
0
        public void ShouldThrottle_WhenCalledAndConsumingAllTokensAtOnceAndThenCalledOnceMore_WillReturnTrue()
        {
            SystemTime.SetCurrentTimeUtc = () => referenceTime;
            var virtualNow = SystemTime.UtcNow;

            var  throttler = new RollingWindowThrottler(3, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var  shouldThrottle = throttler.ShouldThrottle(3, out waitTimeMillis);

            //shouldThrottle.ShouldBeFalse();
            Assert.False(shouldThrottle);

            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(0.2);
            shouldThrottle = throttler.ShouldThrottle(3, out waitTimeMillis);
            //shouldThrottle.ShouldBeTrue()
            Assert.True(shouldThrottle);
            //waitTimeMillis.ShouldEqual(800);
            Assert.Equal(waitTimeMillis, 800);
        }
コード例 #16
0
        public void ShouldThrottle_WhenCalledThreeTimesinSameSecondAndAllows2PerSecond_WillReturnTrue()
        {
            SystemTime.SetCurrentTimeUtc = () => referenceTime;
            var virtualNow = SystemTime.UtcNow;

            var  throttler = new RollingWindowThrottler(2, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var  shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);

            shouldThrottle.ShouldBeFalse();

            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(0.5);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();

            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(0.7);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeTrue();
            waitTimeMillis.ShouldEqual(300);
        }
コード例 #17
0
 public void ShouldThrottle_WhenCalledAndConsumingAllTokensAtOnce_WillReturnFalse()
 {
     var throttler = new RollingWindowThrottler(3, TimeSpan.FromSeconds(1));
     long waitTimeMillis;
     var shouldThrottle = throttler.ShouldThrottle(3, out waitTimeMillis);
     shouldThrottle.ShouldBeFalse();
 }
コード例 #18
0
        public void ShouldThrottle_WhenCalledTwiceinSameSecondAndAllows1PerSecond_WillReturnTrue()
        {
            SystemTime.SetCurrentTimeUtc = () => referenceTime;
            var virtualNow = SystemTime.UtcNow;

            var throttler = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();

            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(0.5);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeTrue();
            waitTimeMillis.ShouldEqual(500);

            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(0.8);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeTrue();
            waitTimeMillis.ShouldEqual(200);
        }
コード例 #19
0
 public void ShouldThrottle_WhenCalledWithMoreTokensThanOccurrences_WillReturnTrue()
 {
     var throttler = new RollingWindowThrottler(2, TimeSpan.FromSeconds(1));
     long waitTimeMillis;
     var shouldThrottle = throttler.ShouldThrottle(3, out waitTimeMillis);
     shouldThrottle.ShouldBeTrue();
 }
コード例 #20
0
 public void ShouldThrottle_WhenCalledWithTokensLessThanOne_WillThrow()
 {
     var throttler = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1));
     long waitTimeMillis;
     Assert.Throws<ArgumentOutOfRangeException>(() => throttler.ShouldThrottle(0, out waitTimeMillis));
 }
コード例 #21
0
        public void ShouldThrottle_WhenCalled_WillReturnFalse()
        {
            var throttler = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);

            shouldThrottle.ShouldBeFalse();
        }
コード例 #22
0
        public void ShouldThrottle_WhenCalledAtEndOfRollingWindowAndAllows2PerSecond_WillReturnFalse()
        {
            SystemTime.SetCurrentTimeUtc = () => referenceTime;
            var virtualNow = SystemTime.UtcNow;

            var throttler = new RollingWindowThrottler(2, TimeSpan.FromSeconds(1));
            long waitTimeMillis;
            var shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();

            //first rolling window expired, init a new one
            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(1.2);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();

            //inside second rolling window, under threshold
            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(1.3);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();

            //second rolling window expired, beginning third window
            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(2.2);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();

            //third window, under threshold
            SystemTime.SetCurrentTimeUtc = () => virtualNow.AddSeconds(2.3);
            shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis);
            shouldThrottle.ShouldBeFalse();
        }