Search in sources :

Example 1 with Delayed

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();
    }
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) Delayed(java.util.concurrent.Delayed) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) TimeUnit(java.util.concurrent.TimeUnit) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) TimeValue(org.elasticsearch.common.unit.TimeValue) ScheduledFuture(java.util.concurrent.ScheduledFuture) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with Delayed

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);
    }
}
Also used : Delayed(java.util.concurrent.Delayed) Method(java.lang.reflect.Method) AssertionFailedError(junit.framework.AssertionFailedError) DelayQueue(java.util.concurrent.DelayQueue)

Example 3 with Delayed

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));
}
Also used : JRTServerConfigRequest(com.yahoo.vespa.config.protocol.JRTServerConfigRequest) Delayed(java.util.concurrent.Delayed) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.Test)

Example 4 with Delayed

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)));
}
Also used : Delayed(java.util.concurrent.Delayed) Test(org.junit.jupiter.api.Test)

Example 5 with Delayed

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();
                }
            };
        }
    };
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Delayed(java.util.concurrent.Delayed) ThreadPool(org.elasticsearch.threadpool.ThreadPool) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TimeUnit(java.util.concurrent.TimeUnit) Collection(java.util.Collection) ScheduledFuture(java.util.concurrent.ScheduledFuture) Future(java.util.concurrent.Future) TimeValue(io.crate.common.unit.TimeValue)

Aggregations

Delayed (java.util.concurrent.Delayed)6 TimeUnit (java.util.concurrent.TimeUnit)3 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 ThreadPool (org.elasticsearch.threadpool.ThreadPool)2 JRTServerConfigRequest (com.yahoo.vespa.config.protocol.JRTServerConfigRequest)1 TimeValue (io.crate.common.unit.TimeValue)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Callable (java.util.concurrent.Callable)1 DelayQueue (java.util.concurrent.DelayQueue)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AssertionFailedError (junit.framework.AssertionFailedError)1 TimeValue (org.elasticsearch.common.unit.TimeValue)1 AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)1