package net.floodlightcontroller.core.test; import java.util.ArrayDeque; import java.util.Queue; import java.util.concurrent.TimeUnit; import com.google.common.collect.ImmutableList; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelPromise; import io.netty.channel.DefaultChannelPromise; import io.netty.channel.EventLoop; import io.netty.channel.EventLoopGroup; import io.netty.util.concurrent.AbstractScheduledEventExecutor; import io.netty.util.concurrent.Future; public final class TestEventLoop extends AbstractScheduledEventExecutor implements EventLoop { private final Queue<Runnable> tasks = new ArrayDeque<Runnable>(2); @Override public void execute(Runnable command) { if (command == null) { throw new NullPointerException("command"); } tasks.add(command); } public void runTasks() { for (;;) { Runnable task = tasks.poll(); if (task == null) { break; } task.run(); } } long runScheduledTasks() { long time = AbstractScheduledEventExecutor.nanoTime(); for (;;) { Runnable task = pollScheduledTask(time); if (task == null) { return nextScheduledTaskNano(); } task.run(); } } long nextScheduledTask() { return nextScheduledTaskNano(); } @Override protected void cancelScheduledTasks() { super.cancelScheduledTasks(); } @Override public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) { throw new UnsupportedOperationException(); } @Override public Future<?> terminationFuture() { throw new UnsupportedOperationException(); } @Override @Deprecated public void shutdown() { throw new UnsupportedOperationException(); } @Override public boolean isShuttingDown() { return false; } @Override public boolean isShutdown() { return false; } @Override public boolean isTerminated() { return false; } @Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { Thread.sleep(unit.toMillis(timeout)); return false; } @Override public ChannelFuture register(Channel channel) { return register(channel, new DefaultChannelPromise(channel, this)); } @Override public ChannelFuture register(Channel channel, ChannelPromise promise) { channel.unsafe().register(this, promise); return promise; } @Override public boolean inEventLoop() { return true; } @Override public boolean inEventLoop(Thread thread) { return true; } @Override public EventLoop next() { return this; } @Override public EventLoopGroup parent() { return this; } public ImmutableList<Runnable> getQueuedTasks() { return ImmutableList.copyOf(tasks); } }