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);
}
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());
}
}
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();
}
}
}
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();
}
}
}
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);
}
}
Aggregations