public void TestScheduling() { var ch = new EmbeddedChannel(new ChannelHandlerAdapter()); var latch = new CountdownEvent(2); Task future = ch.EventLoop.ScheduleAsync(() => latch.Signal(), TimeSpan.FromSeconds(1)); future.ContinueWith(t => latch.Signal()); var next = ch.RunScheduledPendingTasks(); Assert.True(next > PreciseTime.Zero); // Sleep for the nanoseconds but also give extra 50ms as the clock my not be very precise and so fail the test // otherwise. Thread.Sleep(PreciseTime.ToTimeSpan(next) + TimeSpan.FromMilliseconds(50)); Assert.Equal(PreciseTime.MinusOne, ch.RunScheduledPendingTasks()); latch.Wait(); }
public void GracefulShutdownQuietPeriod() { Task task = this.eventLoop.ShutdownGracefullyAsync(TimeSpan.FromSeconds(1), TimeSpan.MaxValue); // Keep Scheduling tasks for another 2 seconds. for (int i = 0; i < 20; i++) { Thread.Sleep(100); this.eventLoop.Execute(new NoOp()); } long startTime = PreciseTime.NanoTime(); Assert.True(this.eventLoop.IsShuttingDown); Assert.False(this.eventLoop.IsShutdown); Assert.True(task.Wait(DefaultTimeout), "Loop shutdown timed out"); Assert.True(this.eventLoop.IsShuttingDown); Assert.True(this.eventLoop.IsShutdown); long duration = (long)PreciseTime.ToTimeSpan(PreciseTime.NanoTime() - startTime).TotalMilliseconds; Assert.True(duration >= 1000, $"Expecting shutdown quite period >= 1000 milliseconds, but was {duration}"); }