public SingleThreadEventLoop(IEventLoopGroup parent, IThreadFactory threadFactory, IRejectedExecutionHandler rejectedHandler, TimeSpan breakoutInterval) : base(parent, threadFactory, false, int.MaxValue, rejectedHandler) { _emptyEvent = new ManualResetEventSlim(false, 1); _breakoutNanosInterval = PreciseTime.ToDelayNanos(breakoutInterval); Start(); }
public void ScheduleLaggyTaskAtFixedRate() { var timestamps = new BlockingCollection <long>(); int expectedTimeStamps = 5; var allTimeStampsLatch = new CountdownEvent(expectedTimeStamps); var f = this.eventLoop.ScheduleAtFixedRate(() => { var empty = timestamps.Count == 0; timestamps.Add(Stopwatch.GetTimestamp()); if (empty) { try { Thread.Sleep(401); } catch { } } allTimeStampsLatch.Signal(); }, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100)); Assert.True(allTimeStampsLatch.Wait(TimeSpan.FromMinutes(1))); Assert.True(f.Cancel()); Thread.Sleep(300); Assert.Equal(expectedTimeStamps, timestamps.Count); // Check if the task was run with lag. int i = 0; long?previousTimestamp = null; foreach (long t in timestamps) { if (previousTimestamp == null) { previousTimestamp = t; continue; } long diff = t - previousTimestamp.Value; if (i == 0) { Assert.True(diff >= PreciseTime.ToDelayNanos(TimeSpan.FromMilliseconds(400))); } else { //Assert.True(diff <= PreciseTime.ToDelayNanos(TimeSpan.FromMilliseconds(10 + 2))); var diffMs = PreciseTime.ToMilliseconds(diff); Assert.True(diffMs <= 10 + 40); // libuv 多加 40,确保测试通过 } previousTimestamp = t; i++; } }
private void TestScheduleTask(IEventLoop loopA) { long startTime = Stopwatch.GetTimestamp(); AtomicLong endTime = new AtomicLong(); var f = loopA.Schedule(() => { endTime.Value = Stopwatch.GetTimestamp(); }, TimeSpan.FromMilliseconds(500)); f.Completion.GetAwaiter().GetResult(); Assert.True(endTime.Value - startTime >= PreciseTime.ToDelayNanos(TimeSpan.FromMilliseconds(500))); }
public void ScheduleTaskAtFixedRate() { var timestamps = new BlockingCollection <long>(); int expectedTimeStamps = 5; var allTimeStampsLatch = new CountdownEvent(expectedTimeStamps); var f = this.eventLoop.ScheduleAtFixedRate(() => { timestamps.Add(Stopwatch.GetTimestamp()); try { Thread.Sleep(50); } catch { } allTimeStampsLatch.Signal(); }, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100)); Assert.True(allTimeStampsLatch.Wait(TimeSpan.FromMinutes(1))); Assert.True(f.Cancel()); Thread.Sleep(300); Assert.Equal(expectedTimeStamps, timestamps.Count); // Check if the task was run without a lag. long?firstTimestamp = null; int cnt = 0; foreach (long t in timestamps) { if (firstTimestamp == null) { firstTimestamp = t; continue; } long timepoint = t - firstTimestamp.Value; Assert.True(timepoint >= PreciseTime.ToDelayNanos(TimeSpan.FromMilliseconds(100 * cnt + 80))); Assert.True(timepoint <= PreciseTime.ToDelayNanos(TimeSpan.FromMilliseconds(100 * (cnt + 1) + 20))); cnt++; } }
public void TestGracefulShutdownQuietPeriod() { _loopA.ShutdownGracefullyAsync(TimeSpan.FromSeconds(1), TimeSpan.FromDays(1)); // Keep Scheduling tasks for another 2 seconds. for (int i = 0; i < 20; i++) { Thread.Sleep(100); _loopA.Execute(NOOP.Instance); } long startTime = PreciseTime.NanoTime(); Assert.True(_loopA.IsShuttingDown); Assert.False(_loopA.IsShutdown); while (!_loopA.IsTerminated) { _loopA.WaitTermination(TimeSpan.FromDays(1)); } Assert.True(PreciseTime.NanoTime() - startTime >= PreciseTime.ToDelayNanos(TimeSpan.FromSeconds(1))); }
public void ScheduleTaskWithFixedDelay() { var timestamps = new BlockingCollection <long>(); int expectedTimeStamps = 3; var allTimeStampsLatch = new CountdownEvent(expectedTimeStamps); var f = this.eventLoop.ScheduleWithFixedDelay(() => { timestamps.Add(Stopwatch.GetTimestamp()); try { Thread.Sleep(51); } catch { } allTimeStampsLatch.Signal(); }, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100)); Assert.True(allTimeStampsLatch.Wait(TimeSpan.FromMinutes(1))); Assert.True(f.Cancel()); Thread.Sleep(300); Assert.Equal(expectedTimeStamps, timestamps.Count); // Check if the task was run without a lag. long?previousTimestamp = null; foreach (long t in timestamps) { if (previousTimestamp is null) { previousTimestamp = t; continue; } Assert.True(t - previousTimestamp.Value >= PreciseTime.ToDelayNanos(TimeSpan.FromMilliseconds(150))); previousTimestamp = t; } }
public override Task ScheduleWithFixedDelayAsync(Action <object, object> action, object context, object state, TimeSpan initialDelay, TimeSpan delay, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(TaskUtil.Cancelled); } if (!cancellationToken.CanBeCanceled) { return(ScheduleWithFixedDelay(action, context, state, initialDelay, delay).Completion); } if (action is null) { return(ThrowHelper.FromArgumentNullException(ExceptionArgument.action)); } if (delay <= TimeSpan.Zero) { return(ThrowHelper.FromArgumentException_DelayMustBeGreaterThanZero()); } return(Schedule(new StateActionWithContextScheduledAsyncTask(this, action, context, state, PreciseTime.DeadlineNanos(initialDelay), -PreciseTime.ToDelayNanos(delay), cancellationToken)).Completion); }
public override Task ScheduleAtFixedRateAsync(Action <object> action, object state, TimeSpan initialDelay, TimeSpan period, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(TaskUtil.Cancelled); } if (!cancellationToken.CanBeCanceled) { return(ScheduleAtFixedRate(action, state, initialDelay, period).Completion); } if (action is null) { return(ThrowHelper.FromArgumentNullException(ExceptionArgument.action)); } if (period <= TimeSpan.Zero) { return(ThrowHelper.FromArgumentException_PeriodMustBeGreaterThanZero()); } return(Schedule(new StateActionScheduledAsyncTask(this, action, state, PreciseTime.DeadlineNanos(initialDelay), PreciseTime.ToDelayNanos(period), cancellationToken)).Completion); }
public override IScheduledTask ScheduleWithFixedDelay(Action <object, object> action, object context, object state, TimeSpan initialDelay, TimeSpan delay) { if (action is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.action); } if (delay <= TimeSpan.Zero) { ThrowHelper.ThrowArgumentException_DelayMustBeGreaterThanZero(); } return(Schedule(new StateActionWithContextScheduledTask(this, action, context, state, PreciseTime.DeadlineNanos(initialDelay), -PreciseTime.ToDelayNanos(delay)))); }
public override IScheduledTask ScheduleWithFixedDelay(IRunnable action, TimeSpan initialDelay, TimeSpan delay) { if (action is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.action); } if (delay <= TimeSpan.Zero) { ThrowHelper.ThrowArgumentException_DelayMustBeGreaterThanZero(); } return(Schedule(new RunnableScheduledTask(this, action, PreciseTime.DeadlineNanos(initialDelay), -PreciseTime.ToDelayNanos(delay)))); }
public override IScheduledTask ScheduleAtFixedRate(Action <object> action, object state, TimeSpan initialDelay, TimeSpan period) { if (action is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.action); } if (period <= TimeSpan.Zero) { ThrowHelper.ThrowArgumentException_PeriodMustBeGreaterThanZero(); } return(Schedule(new StateActionScheduledTask(this, action, state, PreciseTime.DeadlineNanos(initialDelay), PreciseTime.ToDelayNanos(period)))); }
protected override void RunTasks0() { RunAllTasks(PreciseTime.ToDelayNanos(TimeSpan.FromMinutes(1))); }