public void CreatesTimer() { using TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(30), 10); TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(90)); Assert.IsNotNull(timer); }
public async Task MultipleTimeouts() { const int timerTimeout = 100; const int buckets = 20; const int resolution = 50; TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(resolution), buckets); // 20 buckets of 50 ms go up to 1000ms List <Task <(int, long)> > tasks = new List <Task <(int, long)> >(); for (int i = 0; i < 10; i++) { int estimatedTimeout = (i + 1) * timerTimeout; TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(estimatedTimeout)); tasks.Add(Task.Run(async() => { Stopwatch stopwatch = Stopwatch.StartNew(); await timer.StartTimerAsync(); stopwatch.Stop(); return(estimatedTimeout, stopwatch.ElapsedMilliseconds); })); } await Task.WhenAll(tasks); foreach (Task <(int, long)> task in tasks) { Assert.IsTrue(task.Result.Item2 >= task.Result.Item1 - resolution && task.Result.Item2 <= task.Result.Item1 + resolution, $"Timer configured with {task.Result.Item1} took {task.Result.Item2} to fire."); } }
private void StartCongestionControlTimer() { this.congestionControlTimer = this.timerWheel.CreateTimer(BatchAsyncStreamer.congestionControllerDelay); this.congestionControlTask = this.congestionControlTimer.StartTimerAsync().ContinueWith(async(task) => { await this.RunCongestionControlAsync(); }, this.cancellationTokenSource.Token); }
public void DisposedCannotStartTimers() { TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(30), 3); TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(90)); Assert.IsNotNull(timer); wheel.Dispose(); Assert.ThrowsException <ObjectDisposedException>(() => timer.StartTimerAsync()); }
public void DisposeCancelsTimers() { TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(30), 3); TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(90)); Task timerTask = timer.StartTimerAsync(); wheel.Dispose(); Assert.AreEqual(TaskStatus.Canceled, timerTask.Status); }
public override void SubscribeForTimeouts(TimerWheelTimer timer) { this.ThrowIfDisposed(); long timerTimeoutInTicks = timer.Timeout.Ticks; int bucket = (int)timerTimeoutInTicks / this.resolutionInTicks; int index = this.GetIndexForTimeout(bucket); ConcurrentQueue <TimerWheelTimer> timerQueue = this.timers[index]; timerQueue.Enqueue(timer); }
private void ResetTimer() { this.currentTimer = this.timerWheel.CreateTimer(BatchAsyncStreamer.batchTimeout); this.timerTask = this.currentTimer.StartTimerAsync().ContinueWith((task) => { if (task.IsCompleted) { this.DispatchTimer(); } }, this.cancellationTokenSource.Token); }
public async Task TimeoutFires() { const int timerTimeout = 2000; const int resolution = 500; using TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(resolution), 10); // 10 buckets of 500 ms go up to 5000ms TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(timerTimeout)); Stopwatch stopwatch = Stopwatch.StartNew(); await timer.StartTimerAsync(); stopwatch.Stop(); }
public async Task TimeoutFires() { const int timerTimeout = 200; const int resolution = 50; TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(resolution), 10); // 10 buckets of 50 ms go up to 500ms TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(timerTimeout)); Stopwatch stopwatch = Stopwatch.StartNew(); await timer.StartTimerAsync(); stopwatch.Stop(); Assert.IsTrue(stopwatch.ElapsedMilliseconds >= timerTimeout - resolution && stopwatch.ElapsedMilliseconds <= timerTimeout + resolution); }
public async Task TimeoutFires_SameTimeout() { const int timerTimeout = 200; const int resolution = 50; TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(resolution), 10); // 10 buckets of 50 ms go up to 500ms TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(timerTimeout)); TimerWheelTimer timer2 = wheel.CreateTimer(TimeSpan.FromMilliseconds(timerTimeout)); Stopwatch stopwatch = Stopwatch.StartNew(); await Task.WhenAll(timer.StartTimerAsync(), timer2.StartTimerAsync()); stopwatch.Stop(); Assert.IsTrue(stopwatch.ElapsedMilliseconds >= timerTimeout - resolution, $"{stopwatch.ElapsedMilliseconds} >= {timerTimeout - resolution}, timerTimeout: {timerTimeout}, resolution: {resolution}"); Assert.IsTrue(stopwatch.ElapsedMilliseconds <= timerTimeout + resolution, $"{stopwatch.ElapsedMilliseconds} <= {timerTimeout + resolution}, timerTimeout: {timerTimeout}, resolution: {resolution}"); }
public async Task TenK_WithTimerWheel() { TimerWheel wheel = TimerWheel.CreateTimerWheel(TimeSpan.FromMilliseconds(1000), 10); List <Task> timers = new List <Task>(this.timeouts.Count); for (int i = 0; i < this.timeouts.Count; i++) { TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(this.timeouts[i])); timers.Add(timer.StartTimerAsync()); } await Task.WhenAll(timers); wheel.Dispose(); }
public void Dispose() { this.cancellationTokenSource.Cancel(); this.cancellationTokenSource.Dispose(); this.currentTimer.CancelTimer(); this.currentTimer = null; this.timerTask = null; if (this.congestionControlTimer != null) { this.congestionControlTimer.CancelTimer(); this.congestionControlTimer = null; this.congestionControlTask = null; } }
public override void SubscribeForTimeouts(TimerWheelTimer timer) { this.ThrowIfDisposed(); long timerTimeoutInTicks = timer.Timeout.Ticks; int bucket = (int)timerTimeoutInTicks / this.resolutionInTicks; lock (this.subscriptionLock) { int index = this.GetIndexForTimeout(bucket); ConcurrentQueue <TimerWheelTimer> timerQueue = this.timers.GetOrAdd(index, _ => { return(new ConcurrentQueue <TimerWheelTimer>()); }); timerQueue.Enqueue(timer); } }
public void IndexMovesAsTimerPasses() { using TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(30), 3, timer: null); // deactivate timer to fire manually TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(90)); Task timerTask = timer.StartTimerAsync(); wheel.OnTimer(null); Assert.AreEqual(TaskStatus.WaitingForActivation, timerTask.Status); wheel.OnTimer(null); Assert.AreEqual(TaskStatus.WaitingForActivation, timerTask.Status); TimerWheelTimer secondTimer = wheel.CreateTimer(TimeSpan.FromMilliseconds(60)); Task secondTimerTask = secondTimer.StartTimerAsync(); wheel.OnTimer(null); Assert.AreEqual(TaskStatus.RanToCompletion, timerTask.Status); wheel.OnTimer(null); Assert.AreEqual(TaskStatus.RanToCompletion, secondTimerTask.Status); }
public async Task MultipleTimeouts() { const int timerTimeout = 1000; const int buckets = 20; const int resolution = 500; using TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(resolution), buckets); // 20 buckets of 500 ms go up to 10000ms List <Task <(int, long)> > tasks = new List <Task <(int, long)> >(); for (int i = 0; i < 10; i++) { int estimatedTimeout = (i + 1) * timerTimeout; TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(estimatedTimeout)); tasks.Add(Task.Run(async() => { Stopwatch stopwatch = Stopwatch.StartNew(); await timer.StartTimerAsync(); stopwatch.Stop(); return(estimatedTimeout, stopwatch.ElapsedMilliseconds); })); } await Task.WhenAll(tasks); }
public abstract void SubscribeForTimeouts(TimerWheelTimer timer);
public async Task One_WithTimerWheel() { TimerWheelTimer timer = this.mainWheel.CreateTimer(TimeSpan.FromMilliseconds(1000)); await timer.StartTimerAsync(); }
private void ResetTimer() { this.currentTimer = this.timerWheel.CreateTimer(BatchAsyncStreamer.batchTimeout); this.timerTask = this.GetTimerTaskAsync(); }
public async Task One_WithTimerWheel() { TimerWheelTimer timer = this.mainWheel.CreateTimer(TimerWheelBenchmark.resolution); await timer.StartTimerAsync(); }