public void ShouldThrottle_WhenCalledAndConsumingAllTokensAtOnce_WillReturnFalse() { var throttler = new RollingWindowThrottler(3, TimeSpan.FromSeconds(1)); var shouldThrottle = throttler.ShouldThrottle(3, out _); shouldThrottle.ShouldBeFalse(); }
public void ShouldThrottle_WhenCalled_WillReturnFalse() { var throttler = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1)); var shouldThrottle = throttler.ShouldThrottle(1, out _); shouldThrottle.ShouldBeFalse(); }
public void ShouldThrottle_WhenCalledWithTokensLessThanOne_WillThrow() { var throttler = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1)); long waitTimeMillis; Assert.Throws <ArgumentOutOfRangeException>(() => throttler.ShouldThrottle(0, out waitTimeMillis)); }
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(); }
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); }
public void ShouldThrottle_WhenCalledWithMoreTokensThanOccurrences_WillReturnTrue() { var throttler = new RollingWindowThrottler(2, TimeSpan.FromSeconds(1)); var shouldThrottle = throttler.ShouldThrottle(3, out _); shouldThrottle.ShouldBeTrue(); }
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); }
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); }
//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; }
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(); }
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(); }
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); }
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); }
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); }
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); }
public void ShouldThrottle_WhenCalledAndConsumingAllTokensAtOnce_WillReturnFalse() { var throttler = new RollingWindowThrottler(3, TimeSpan.FromSeconds(1)); long waitTimeMillis; var shouldThrottle = throttler.ShouldThrottle(3, out waitTimeMillis); shouldThrottle.ShouldBeFalse(); }
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); }
public void ShouldThrottle_WhenCalledWithMoreTokensThanOccurrences_WillReturnTrue() { var throttler = new RollingWindowThrottler(2, TimeSpan.FromSeconds(1)); long waitTimeMillis; var shouldThrottle = throttler.ShouldThrottle(3, out waitTimeMillis); shouldThrottle.ShouldBeTrue(); }
public void ShouldThrottle_WhenCalledWithTokensLessThanOne_WillThrow() { var throttler = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1)); long waitTimeMillis; Assert.Throws<ArgumentOutOfRangeException>(() => throttler.ShouldThrottle(0, out waitTimeMillis)); }
public void ShouldThrottle_WhenCalled_WillReturnFalse() { var throttler = new RollingWindowThrottler(1, TimeSpan.FromSeconds(1)); long waitTimeMillis; var shouldThrottle = throttler.ShouldThrottle(1, out waitTimeMillis); shouldThrottle.ShouldBeFalse(); }