use of java.util.concurrent.Delayed in project elasticsearch by elastic.
the class WorkingBulkByScrollTaskTests method testDelayNeverNegative.
public void testDelayNeverNegative() throws IOException {
// Thread pool that returns a ScheduledFuture that claims to have a negative delay
ThreadPool threadPool = new TestThreadPool("test") {
public ScheduledFuture<?> schedule(TimeValue delay, String name, Runnable command) {
return new ScheduledFuture<Void>() {
@Override
public long getDelay(TimeUnit unit) {
return -1;
}
@Override
public int compareTo(Delayed o) {
throw new UnsupportedOperationException();
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
throw new UnsupportedOperationException();
}
@Override
public boolean isCancelled() {
throw new UnsupportedOperationException();
}
@Override
public boolean isDone() {
throw new UnsupportedOperationException();
}
@Override
public Void get() throws InterruptedException, ExecutionException {
throw new UnsupportedOperationException();
}
@Override
public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
throw new UnsupportedOperationException();
}
};
}
};
try {
// Have the task use the thread pool to delay a task that does nothing
task.delayPrepareBulkRequest(threadPool, timeValueSeconds(0), 1, new AbstractRunnable() {
@Override
protected void doRun() throws Exception {
}
@Override
public void onFailure(Exception e) {
throw new UnsupportedOperationException();
}
});
// Even though the future returns a negative delay we just return 0 because the time is up.
assertEquals(timeValueSeconds(0), task.getStatus().getThrottledUntil());
} finally {
threadPool.shutdown();
}
}
use of java.util.concurrent.Delayed in project intellij-community by JetBrains.
the class LightPlatformTestCase method checkJavaSwingTimersAreDisposed.
private static void checkJavaSwingTimersAreDisposed() throws Exception {
Class<?> TimerQueueClass = Class.forName("javax.swing.TimerQueue");
Method sharedInstance = ReflectionUtil.getMethod(TimerQueueClass, "sharedInstance");
Object timerQueue = sharedInstance.invoke(null);
DelayQueue delayQueue = ReflectionUtil.getField(TimerQueueClass, timerQueue, DelayQueue.class, "queue");
Delayed timer = delayQueue.peek();
if (timer != null) {
long delay = timer.getDelay(TimeUnit.MILLISECONDS);
String text = "(delayed for " + delay + "ms)";
Method getTimer = ReflectionUtil.getDeclaredMethod(timer.getClass(), "getTimer");
Timer swingTimer = (Timer) getTimer.invoke(timer);
text = "Timer (listeners: " + Arrays.asList(swingTimer.getActionListeners()) + ") " + text;
throw new AssertionFailedError("Not disposed java.swing.Timer: " + text + "; queue:" + timerQueue);
}
}
use of java.util.concurrent.Delayed in project vespa by vespa-engine.
the class DelayedResponseTest method basic.
@Test
public void basic() {
ConfigTester tester = new ConfigTester();
final long returnTime = System.currentTimeMillis();
final long timeout = 1;
final String configName = "foo";
final JRTServerConfigRequest request = tester.createRequest(configName, configId, namespace, timeout);
DelayedResponse delayedResponse = new DelayedResponse(request, returnTime);
assertThat(delayedResponse.getRequest(), is(request));
assertThat(delayedResponse.getReturnTime(), is(returnTime));
assertTrue(delayedResponse.getDelay(TimeUnit.SECONDS) < returnTime);
DelayedResponse before = new DelayedResponse(request, returnTime - 1000L);
DelayedResponse after = new DelayedResponse(request, returnTime + 1000L);
assertThat(delayedResponse.compareTo(delayedResponse), is(0));
assertThat(delayedResponse.compareTo(before), is(1));
assertThat(delayedResponse.compareTo(after), is(-1));
assertThat(delayedResponse.compareTo(new Delayed() {
@Override
public long getDelay(TimeUnit unit) {
return 0;
}
@Override
public int compareTo(Delayed o) {
return 0;
}
}), is(0));
}
use of java.util.concurrent.Delayed in project logging-log4j2 by apache.
the class BurstFilterLogDelayTest method testCompareToOverflow.
@Test
public void testCompareToOverflow() {
// no overflow, but close
final Delayed d1 = BurstFilter.createLogDelay(Long.MAX_VALUE - TimeUnit.SECONDS.toNanos(10) - System.nanoTime());
// Overflow
final Delayed d2 = BurstFilter.createLogDelay(Long.MAX_VALUE + TimeUnit.SECONDS.toNanos(10) - System.nanoTime());
assertThat(d2, is(greaterThan(d1)));
}
use of java.util.concurrent.Delayed in project crate by crate.
the class DeterministicTaskQueue method getThreadPool.
/**
* @return A <code>ThreadPool</code> that uses this task queue and wraps <code>Runnable</code>s in the given wrapper.
*/
public ThreadPool getThreadPool(Function<Runnable, Runnable> runnableWrapper) {
return new ThreadPool(settings) {
{
stopCachedTimeThread();
}
@Override
public long relativeTimeInMillis() {
return currentTimeMillis;
}
@Override
public long absoluteTimeInMillis() {
return currentTimeMillis;
}
@Override
public ThreadPoolStats stats() {
throw new UnsupportedOperationException();
}
@Override
public ExecutorService generic() {
return getExecutorService(runnableWrapper);
}
@Override
public ExecutorService executor(String name) {
return getExecutorService(runnableWrapper);
}
@Override
public ScheduledCancellable schedule(Runnable command, TimeValue delay, String executor) {
final int NOT_STARTED = 0;
final int STARTED = 1;
final int CANCELLED = 2;
final AtomicInteger taskState = new AtomicInteger(NOT_STARTED);
scheduleAt(currentTimeMillis + delay.millis(), runnableWrapper.apply(new Runnable() {
@Override
public void run() {
if (taskState.compareAndSet(NOT_STARTED, STARTED)) {
command.run();
}
}
@Override
public String toString() {
return command.toString();
}
}));
return new ScheduledCancellable() {
@Override
public long getDelay(TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public int compareTo(Delayed o) {
throw new UnsupportedOperationException();
}
@Override
public boolean cancel() {
return taskState.compareAndSet(NOT_STARTED, CANCELLED);
}
@Override
public boolean isCancelled() {
return taskState.get() == CANCELLED;
}
};
}
@Override
public Cancellable scheduleWithFixedDelay(Runnable command, TimeValue interval, String executor) {
return super.scheduleWithFixedDelay(command, interval, executor);
}
@Override
public Runnable preserveContext(Runnable command) {
return command;
}
@Override
public void shutdown() {
throw new UnsupportedOperationException();
}
@Override
public void shutdownNow() {
throw new UnsupportedOperationException();
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public ScheduledExecutorService scheduler() {
return new ScheduledExecutorService() {
@Override
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public void shutdown() {
throw new UnsupportedOperationException();
}
@Override
public List<Runnable> shutdownNow() {
throw new UnsupportedOperationException();
}
@Override
public boolean isShutdown() {
throw new UnsupportedOperationException();
}
@Override
public boolean isTerminated() {
throw new UnsupportedOperationException();
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public <T> Future<T> submit(Callable<T> task) {
throw new UnsupportedOperationException();
}
@Override
public <T> Future<T> submit(Runnable task, T result) {
throw new UnsupportedOperationException();
}
@Override
public Future<?> submit(Runnable task) {
throw new UnsupportedOperationException();
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) {
throw new UnsupportedOperationException();
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) {
throw new UnsupportedOperationException();
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public void execute(Runnable command) {
throw new UnsupportedOperationException();
}
};
}
};
}
Aggregations