Search in sources :

Example 81 with FutureTask

use of java.util.concurrent.FutureTask in project SimianArmy by Netflix.

the class TestBasicScheduler method testDelayedStart.

@Test
public void testDelayedStart() throws Exception {
    BasicScheduler sched = new BasicScheduler(1, TimeUnit.HOURS, 1);
    TestMonkeyContext context = new TestMonkeyContext(Enums.MONKEY);
    Monkey mockMonkey = mock(Monkey.class);
    when(mockMonkey.context()).thenReturn(context).thenReturn(context);
    when(mockMonkey.type()).thenReturn(Enums.MONKEY).thenReturn(Enums.MONKEY);
    // first monkey has no previous events, so it runs practically immediately
    FutureTask<Void> task = new FutureTask<Void>(Callables.<Void>returning(null));
    sched.start(mockMonkey, task);
    // make sure that the task gets completed within 100ms
    task.get(100L, TimeUnit.MILLISECONDS);
    sched.stop(mockMonkey);
    // create an event 5 min ago
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, -5);
    BasicRecorderEvent evt = new BasicRecorderEvent(Enums.MONKEY, EventEnums.EVENT, "region", "test-id", cal.getTime().getTime());
    context.recorder().recordEvent(evt);
    // this time when it runs it will not run immediately since it should be scheduled for 55m from now.
    task = new FutureTask<Void>(Callables.<Void>returning(null));
    sched.start(mockMonkey, task);
    try {
        task.get(100, TimeUnit.MILLISECONDS);
        Assert.fail("The task shouldn't have been completed in 100ms");
    } catch (TimeoutException e) {
    // NOPMD - This is an expected exception
    }
    sched.stop(mockMonkey);
}
Also used : TestMonkeyContext(com.netflix.simianarmy.TestMonkeyContext) FutureTask(java.util.concurrent.FutureTask) Monkey(com.netflix.simianarmy.Monkey) Calendar(java.util.Calendar) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 82 with FutureTask

use of java.util.concurrent.FutureTask in project RxJava by ReactiveX.

the class ScheduledRunnableTest method setFutureCancelRace.

@Test
public void setFutureCancelRace() {
    for (int i = 0; i < 500; i++) {
        CompositeDisposable set = new CompositeDisposable();
        final ScheduledRunnable run = new ScheduledRunnable(Functions.EMPTY_RUNNABLE, set);
        set.add(run);
        final FutureTask<Object> ft = new FutureTask<Object>(Functions.EMPTY_RUNNABLE, 0);
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                run.setFuture(ft);
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                run.dispose();
            }
        };
        TestHelper.race(r1, r2);
        assertEquals(0, set.size());
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Test(org.junit.Test)

Example 83 with FutureTask

use of java.util.concurrent.FutureTask in project hadoop by apache.

the class TestRpcServerHandoff method testDeferredResponse.

@Test(timeout = 10000)
public void testDeferredResponse() throws IOException, InterruptedException, ExecutionException {
    ServerForHandoffTest server = new ServerForHandoffTest(2);
    server.start();
    try {
        InetSocketAddress serverAddress = NetUtils.getConnectAddress(server);
        byte[] requestBytes = generateRandomBytes(1024);
        ClientCallable clientCallable = new ClientCallable(serverAddress, conf, requestBytes);
        FutureTask<Writable> future = new FutureTask<Writable>(clientCallable);
        Thread clientThread = new Thread(future);
        clientThread.start();
        server.awaitInvocation();
        awaitResponseTimeout(future);
        server.sendResponse();
        BytesWritable response = (BytesWritable) future.get();
        Assert.assertEquals(new BytesWritable(requestBytes), response);
    } finally {
        if (server != null) {
            server.stop();
        }
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) InetSocketAddress(java.net.InetSocketAddress) Writable(org.apache.hadoop.io.Writable) BytesWritable(org.apache.hadoop.io.BytesWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) Test(org.junit.Test)

Example 84 with FutureTask

use of java.util.concurrent.FutureTask in project hadoop by apache.

the class TestRpcServerHandoff method testDeferredException.

@Test(timeout = 10000)
public void testDeferredException() throws IOException, InterruptedException, ExecutionException {
    ServerForHandoffTest server = new ServerForHandoffTest(2);
    server.start();
    try {
        InetSocketAddress serverAddress = NetUtils.getConnectAddress(server);
        byte[] requestBytes = generateRandomBytes(1024);
        ClientCallable clientCallable = new ClientCallable(serverAddress, conf, requestBytes);
        FutureTask<Writable> future = new FutureTask<Writable>(clientCallable);
        Thread clientThread = new Thread(future);
        clientThread.start();
        server.awaitInvocation();
        awaitResponseTimeout(future);
        server.sendError();
        try {
            future.get();
            Assert.fail("Call succeeded. Was expecting an exception");
        } catch (ExecutionException e) {
            Throwable cause = e.getCause();
            Assert.assertTrue(cause instanceof RemoteException);
            RemoteException re = (RemoteException) cause;
            Assert.assertTrue(re.toString().contains("DeferredError"));
        }
    } finally {
        if (server != null) {
            server.stop();
        }
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) InetSocketAddress(java.net.InetSocketAddress) Writable(org.apache.hadoop.io.Writable) BytesWritable(org.apache.hadoop.io.BytesWritable) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 85 with FutureTask

use of java.util.concurrent.FutureTask in project hadoop by apache.

the class BlockManager method runBlockOp.

// sync batch processing for a full BR.
public <T> T runBlockOp(final Callable<T> action) throws IOException {
    final FutureTask<T> future = new FutureTask<T>(action);
    enqueueBlockOp(future);
    try {
        return future.get();
    } catch (ExecutionException ee) {
        Throwable cause = ee.getCause();
        if (cause == null) {
            cause = ee;
        }
        if (!(cause instanceof IOException)) {
            cause = new IOException(cause);
        }
        throw (IOException) cause;
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new IOException(ie);
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

FutureTask (java.util.concurrent.FutureTask)126 ExecutionException (java.util.concurrent.ExecutionException)44 Test (org.junit.Test)32 IOException (java.io.IOException)28 ExecutorService (java.util.concurrent.ExecutorService)23 Callable (java.util.concurrent.Callable)22 CountDownLatch (java.util.concurrent.CountDownLatch)20 TimeoutException (java.util.concurrent.TimeoutException)14 Handler (android.os.Handler)12 ArrayList (java.util.ArrayList)12 CancellationException (java.util.concurrent.CancellationException)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 Future (java.util.concurrent.Future)8 AccessibleObject (java.lang.reflect.AccessibleObject)6 List (java.util.List)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 FileNotFoundException (java.io.FileNotFoundException)5 InputStream (java.io.InputStream)5 Iterator (java.util.Iterator)5 CancellationSignal (android.os.CancellationSignal)4