Here are the examples of the java api class edu.emory.mathcs.backport.java.util.concurrent.FutureTask taken from open source projects.
1. ServiceManagerRegistryTest#isRegisteredWaiting()
Project: geronimo-xbean
File: ServiceManagerRegistryTest.java
File: ServiceManagerRegistryTest.java
private FutureTask isRegisteredWaiting() throws InterruptedException { final CountDownLatch isRegisteredSignal = new CountDownLatch(1); FutureTask isRegisteredTask = new FutureTask(new Callable() { public Object call() throws Exception { isRegisteredSignal.countDown(); return Boolean.valueOf(registry.isRegistered(SERVICE_NAME)); } }); Thread isRegisteredThread = new Thread(isRegisteredTask, "isRegisteredTask"); isRegisteredThread.setDaemon(true); isRegisteredThread.start(); // wait for thread started isRegisteredSignal.await(TIMEOUT_DURATION, TIMEOUT_UNITS); return isRegisteredTask; }
2. ServiceManagerRegistryTest#getServiceWaiting()
Project: geronimo-xbean
File: ServiceManagerRegistryTest.java
File: ServiceManagerRegistryTest.java
private FutureTask getServiceWaiting() throws InterruptedException { final CountDownLatch getServiceManagerSignal = new CountDownLatch(1); FutureTask getServiceManagerTask = new FutureTask(new Callable() { public Object call() throws Exception { getServiceManagerSignal.countDown(); return registry.getServiceManager(SERVICE_NAME); } }); Thread getServiceManagerThread = new Thread(getServiceManagerTask, "getServiceManagerTask"); getServiceManagerThread.setDaemon(true); getServiceManagerThread.start(); // wait for thread started getServiceManagerSignal.await(TIMEOUT_DURATION, TIMEOUT_UNITS); return getServiceManagerTask; }
3. ServiceManagerRegistryTest#destroyWaitingTask()
Project: geronimo-xbean
File: ServiceManagerRegistryTest.java
File: ServiceManagerRegistryTest.java
private FutureTask destroyWaitingTask() throws InterruptedException { serviceManager.setWait(); FutureTask unregisterTask = new FutureTask(new Callable() { public Object call() throws Exception { destroy(); return Boolean.TRUE; } }); Thread destroyThread = new Thread(unregisterTask, "destroyTask"); destroyThread.setDaemon(true); destroyThread.start(); // wait for register to block assertTrue(serviceManager.awaitEnterSignal()); return unregisterTask; }
4. ServiceManagerRegistryTest#unregisterWaitingTask()
Project: geronimo-xbean
File: ServiceManagerRegistryTest.java
File: ServiceManagerRegistryTest.java
private FutureTask unregisterWaitingTask(final Throwable throwable) throws InterruptedException { serviceManager.setWait(); FutureTask unregisterTask = new FutureTask(new Callable() { public Object call() throws Exception { unregister(throwable); return Boolean.valueOf(throwable == null); } }); Thread unregisterThread = new Thread(unregisterTask, throwable == null ? "unregisterTask" : "unregisterExceptionTask"); unregisterThread.setDaemon(true); unregisterThread.start(); // wait for register to block assertTrue(serviceManager.awaitEnterSignal()); return unregisterTask; }
5. ServiceManagerRegistryTest#registerWaitingTask()
Project: geronimo-xbean
File: ServiceManagerRegistryTest.java
File: ServiceManagerRegistryTest.java
private FutureTask registerWaitingTask(final Throwable throwable) throws InterruptedException { serviceManager.setWait(); FutureTask registerTask = new FutureTask(new Callable() { public Object call() throws Exception { register(throwable); return Boolean.valueOf(throwable == null); } }); Thread registerThread = new Thread(registerTask, throwable == null ? "registerTask" : "registerExceptionTask"); registerThread.setDaemon(true); registerThread.start(); // wait for register to block assertTrue(serviceManager.awaitEnterSignal()); return registerTask; }
6. ServiceManagerRegistryTest#unregisterWaiting()
Project: geronimo-xbean
File: ServiceManagerRegistryTest.java
File: ServiceManagerRegistryTest.java
private void unregisterWaiting(Throwable throwable) throws Exception { // start thread to unregister and wait FutureTask unregisterTask = unregisterWaitingTask(throwable); // start thread to attempt getService FutureTask getServiceManagerTask = getServiceWaiting(); // start thread to attempt isRegistered FutureTask isRegisteredTask = isRegisteredWaiting(); // not necessary, but sleep a bit anyway Thread.sleep(100); // verify all are not done assertFalse(unregisterTask.isDone()); assertFalse(getServiceManagerTask.isDone()); assertFalse(isRegisteredTask.isDone()); // finish register serviceManager.signalExit(); if (throwable == null) { // verify registration worked assertEquals(Boolean.TRUE, unregisterTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); assertFalse(registry.isRegistered(SERVICE_NAME)); try { assertNull(registry.getServiceManager(SERVICE_NAME)); fail("should have thrown an exception"); } catch (ServiceNotFoundException expected) { assertEquals(SERVICE_NAME, expected.getServiceName()); } // verify waiting isRegistered worked assertEquals(Boolean.FALSE, isRegisteredTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); // verify getServiceManager worked try { getServiceManagerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS); fail("should have thrown an exception"); } catch (ExecutionException e) { assertTrue(e.getCause() instanceof ServiceNotFoundException); ServiceNotFoundException serviceNotFoundException = (ServiceNotFoundException) e.getCause(); assertSame(SERVICE_NAME, serviceNotFoundException.getServiceName()); } } else { // verify unregistration failed assertEquals(Boolean.FALSE, unregisterTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); assertTrue(registry.isRegistered(SERVICE_NAME)); assertEquals(serviceManager, registry.getServiceManager(SERVICE_NAME)); // verify waiting isRegistered worked assertEquals(Boolean.TRUE, isRegisteredTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); // verify getServiceManager worked assertEquals(serviceManager, getServiceManagerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); } }
7. ServiceManagerRegistryTest#registerWaiting()
Project: geronimo-xbean
File: ServiceManagerRegistryTest.java
File: ServiceManagerRegistryTest.java
private void registerWaiting(Throwable throwable) throws Exception { // start thread to register and wait FutureTask registerTask = registerWaitingTask(throwable); // start thread to attempt getService FutureTask getServiceManagerTask = getServiceWaiting(); // start thread to attempt isRegistered FutureTask isRegisteredTask = isRegisteredWaiting(); // not necessary, but sleep a bit anyway Thread.sleep(100); // verify all are not done assertFalse(registerTask.isDone()); assertFalse(getServiceManagerTask.isDone()); assertFalse(isRegisteredTask.isDone()); // finish register serviceManager.signalExit(); if (throwable == null) { // verify registration worked assertEquals(Boolean.TRUE, registerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); assertTrue(registry.isRegistered(SERVICE_NAME)); assertEquals(serviceManager, registry.getServiceManager(SERVICE_NAME)); // verify waiting isRegistered worked assertEquals(Boolean.TRUE, isRegisteredTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); // verify getServiceManager worked assertEquals(serviceManager, getServiceManagerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); } else { // verify registration failed assertEquals(Boolean.FALSE, registerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); assertFalse(registry.isRegistered(SERVICE_NAME)); try { assertNull(registry.getServiceManager(SERVICE_NAME)); fail("should have thrown an exception"); } catch (ServiceNotFoundException expected) { assertEquals(SERVICE_NAME, expected.getServiceName()); } // verify waiting isRegistered worked assertEquals(Boolean.FALSE, isRegisteredTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); // verify getServiceManager worked try { getServiceManagerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS); fail("should have thrown an exception"); } catch (ExecutionException e) { assertTrue(e.getCause() instanceof ServiceNotFoundException); ServiceNotFoundException serviceNotFoundException = (ServiceNotFoundException) e.getCause(); assertSame(SERVICE_NAME, serviceNotFoundException.getServiceName()); } } }
8. ServiceManagerRegistryTest#testDoubleRegisterWaiting()
Project: geronimo-xbean
File: ServiceManagerRegistryTest.java
File: ServiceManagerRegistryTest.java
/** * Tests that when a blocking service throws an exception during registration a nother service can wait be waiting * to take over the registration from the failed thread. * * @throws Exception if there is a failure */ public void testDoubleRegisterWaiting() throws Exception { // start thread to attempt to register but throw an exception FutureTask registerFailTask = registerWaitingTask(new Exception("register exception")); // start thread to successfully register final CountDownLatch registerStartedSignal = new CountDownLatch(1); final MockServiceManager newServiceManager = new MockServiceManager(); newServiceManager.setWait(); FutureTask registerTask = new FutureTask(new Callable() { public Object call() throws Exception { registerStartedSignal.countDown(); register(null, newServiceManager); return Boolean.TRUE; } }); Thread registerThread = new Thread(registerTask, "registerTask"); registerThread.setDaemon(true); registerThread.start(); registerStartedSignal.await(TIMEOUT_DURATION, TIMEOUT_UNITS); // sleep a bit to assure that the register thread entered the registry code Thread.sleep(100); // verify all are not done assertFalse(registerFailTask.isDone()); assertFalse(registerTask.isDone()); // finish register fail, and verify it failed serviceManager.signalExit(); assertEquals(Boolean.FALSE, registerFailTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); // verify success registration and verify itworked newServiceManager.signalExit(); assertEquals(Boolean.TRUE, registerTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); assertTrue(registry.isRegistered(SERVICE_NAME)); assertEquals(newServiceManager, registry.getServiceManager(SERVICE_NAME)); }
9. ServiceManagerTest#testNotRestartableBlockWaitingStop()
Project: geronimo-xbean
File: ServiceManagerTest.java
File: ServiceManagerTest.java
/** * Tests the BLOCK stop strategy on a non-restartable service. * @throws Exception if a problem occurs */ public void testNotRestartableBlockWaitingStop() throws Exception { serviceFactory.restartable = false; initialize(); stopCondition.satisfied = false; stopCondition.isStatisfiedSignal = new CountDownLatch(1); FutureTask destroyTask = new FutureTask(new Callable() { public Object call() throws Exception { destroy(StopStrategies.BLOCK); return Boolean.TRUE; } }); Thread destroyThread = new Thread(destroyTask, "DestroyTask"); destroyThread.setDaemon(true); destroyThread.start(); // wait for the stop thread to reach the stopContion initialize method assertTrue(stopCondition.isStatisfiedSignal.await(5, TimeUnit.SECONDS)); // we should blocked waiting to stop assertSame(ServiceState.RUNNING, serviceManager.getState()); assertSame(serviceName, serviceManager.getServiceName()); assertSame(serviceFactory, serviceManager.getServiceFactory()); assertSame(CLASSLOADER, serviceManager.getClassLoader()); assertTrue(serviceManager.getStartTime() > 0); assertNotNull(serviceManager.getService()); assertNull(serviceMonitor.registered); assertNull(serviceMonitor.starting); assertNull(serviceMonitor.waitingToStart); assertNull(serviceMonitor.startError); assertNull(serviceMonitor.running); assertNull(serviceMonitor.stopping); assertNull(serviceMonitor.waitingToStop); assertNull(serviceMonitor.stopError); assertNull(serviceMonitor.stopped); assertNull(serviceMonitor.unregistered); // wait for the destroy task to complete stopCondition.satisfied = true; stopCondition.context.setSatisfied(); assertEquals(Boolean.TRUE, destroyTask.get(5, TimeUnit.SECONDS)); // we should be STOPPED assertSame(ServiceState.STOPPED, serviceManager.getState()); assertEquals(0, serviceManager.getStartTime()); assertNull(serviceManager.getService()); assertNull(serviceMonitor.registered); assertNull(serviceMonitor.starting); assertNull(serviceMonitor.waitingToStart); assertNull(serviceMonitor.startError); assertNull(serviceMonitor.running); assertNotNull(serviceMonitor.stopping); assertNull(serviceMonitor.waitingToStop); assertNull(serviceMonitor.stopError); assertNotNull(serviceMonitor.stopped); assertNotNull(serviceMonitor.unregistered); assertEquals(serviceMonitor.stopping.getEventId() + 1, serviceMonitor.stopped.getEventId()); assertEquals(serviceMonitor.stopping.getEventId() + 2, serviceMonitor.unregistered.getEventId()); assertFalse(startCondition.initializeCalled); assertFalse(startCondition.isSatisfiedCalled); assertTrue(startCondition.destroyCalled); assertTrue(stopCondition.initializeCalled); assertTrue(stopCondition.isSatisfiedCalled); assertTrue(stopCondition.destroyCalled); }
10. ServiceManagerTest#testBlockStopWaiting()
Project: geronimo-xbean
File: ServiceManagerTest.java
File: ServiceManagerTest.java
/** * Tests the BLOCK stop strategy. * @throws Exception if a problem occurs */ public void testBlockStopWaiting() throws Exception { start(false, StartStrategies.SYNCHRONOUS); stopCondition.satisfied = false; stopCondition.isStatisfiedSignal = new CountDownLatch(1); FutureTask stopTask = new FutureTask(new Callable() { public Object call() throws Exception { stop(StopStrategies.BLOCK); return Boolean.TRUE; } }); Thread stopThread = new Thread(stopTask, "StopTask"); stopThread.setDaemon(true); stopThread.start(); // wait for the stop thread to reach the stopContion initialize method assertTrue(stopCondition.isStatisfiedSignal.await(5, TimeUnit.SECONDS)); // we should blocked waiting to stop assertSame(ServiceState.STOPPING, serviceManager.getState()); assertTrue(serviceManager.getStartTime() > 0); assertSame(SERVICE, serviceManager.getService()); assertNull(serviceMonitor.waitingToStop); assertNull(serviceMonitor.stopError); assertNull(serviceMonitor.stopped); assertNull(serviceMonitor.unregistered); assertFalse(startCondition.initializeCalled); assertFalse(startCondition.isSatisfiedCalled); assertFalse(startCondition.destroyCalled); assertTrue(stopCondition.initializeCalled); assertTrue(stopCondition.isSatisfiedCalled); assertFalse(stopCondition.destroyCalled); // wait for the start task to complete stopCondition.satisfied = true; stopCondition.context.setSatisfied(); assertEquals(Boolean.TRUE, stopTask.get(5, TimeUnit.SECONDS)); // we should be STOPPED assertSame(ServiceState.STOPPED, serviceManager.getState()); assertEquals(0, serviceManager.getStartTime()); assertNull(serviceManager.getService()); assertNotNull(serviceMonitor.stopping); assertNull(serviceMonitor.waitingToStop); assertNull(serviceMonitor.stopError); assertNotNull(serviceMonitor.stopped); assertNull(serviceMonitor.unregistered); assertEquals(serviceMonitor.stopping.getEventId() + 1, serviceMonitor.stopped.getEventId()); assertFalse(startCondition.initializeCalled); assertFalse(startCondition.isSatisfiedCalled); assertTrue(startCondition.destroyCalled); assertTrue(stopCondition.initializeCalled); assertTrue(stopCondition.isSatisfiedCalled); assertTrue(stopCondition.destroyCalled); }
11. ServiceManagerTest#testBlockStartWaiting()
Project: geronimo-xbean
File: ServiceManagerTest.java
File: ServiceManagerTest.java
/** * Tests the BLOCK start stragegy. * @throws Exception if a problem occurs */ public void testBlockStartWaiting() throws Exception { startCondition.satisfied = false; startCondition.isSatisfiedSignal = new CountDownLatch(1); FutureTask startTask = new FutureTask(new Callable() { public Object call() throws Exception { start(false, StartStrategies.BLOCK); return Boolean.TRUE; } }); Thread startThread = new Thread(startTask, "StartTask"); startThread.setDaemon(true); startThread.start(); // wait for the start thread to reach the startContion initialize method assertTrue(startCondition.isSatisfiedSignal.await(5, TimeUnit.SECONDS)); // we should not have a service instance and be in the starting state assertSame(ServiceState.STARTING, serviceManager.getState()); assertNull(serviceManager.getService()); assertEquals(0, serviceManager.getStartTime()); assertNotNull(serviceMonitor.starting); assertNull(serviceMonitor.waitingToStart); assertNull(serviceMonitor.startError); assertNull(serviceMonitor.running); assertNull(serviceMonitor.stopping); assertNull(serviceMonitor.stopError); assertNull(serviceMonitor.stopped); assertTrue(startCondition.initializeCalled); assertTrue(startCondition.isSatisfiedCalled); assertFalse(startCondition.destroyCalled); long now = System.currentTimeMillis(); // introduce a bit of delay so subsequent times are much less likely to equal to not Thread.sleep(500); startCondition.satisfied = true; startCondition.context.setSatisfied(); // wait for the start task to complete assertEquals(Boolean.TRUE, startTask.get(5, TimeUnit.SECONDS)); // we should be running assertSame(ServiceState.RUNNING, serviceManager.getState()); assertSame(SERVICE, serviceManager.getService()); assertTrue(serviceManager.getStartTime() >= now); assertNotNull(serviceMonitor.starting); assertNull(serviceMonitor.waitingToStart); assertNull(serviceMonitor.startError); assertNotNull(serviceMonitor.running); assertNull(serviceMonitor.stopping); assertNull(serviceMonitor.stopError); assertNull(serviceMonitor.stopped); assertTrue(startCondition.initializeCalled); assertTrue(startCondition.isSatisfiedCalled); assertFalse(startCondition.destroyCalled); stop(StopStrategies.SYNCHRONOUS); }
12. ServiceManagerRegistryTest#testDestroyWaiting()
Project: geronimo-xbean
File: ServiceManagerRegistryTest.java
File: ServiceManagerRegistryTest.java
/** * Tests that when a service manager blocks during destroy, that the registry does not block isRegistered and * getServiceManager calls, and then returns the correct values for an unregistered service. * * @throws Exception if there is a failure */ public void testDestroyWaiting() throws Exception { register(); // start thread to destroy and wait FutureTask destroyTask = destroyWaitingTask(); // verify all are not done assertFalse(destroyTask.isDone()); // verify that the service already appears to be unregistered assertFalse(registry.isRegistered(SERVICE_NAME)); try { assertNull(registry.getServiceManager(SERVICE_NAME)); fail("should have thrown an exception"); } catch (ServiceNotFoundException expected) { assertEquals(SERVICE_NAME, expected.getServiceName()); } // finish register serviceManager.signalExit(); // verify registration worked assertEquals(Boolean.TRUE, destroyTask.get(TIMEOUT_DURATION, TIMEOUT_UNITS)); assertFalse(registry.isRegistered(SERVICE_NAME)); try { assertNull(registry.getServiceManager(SERVICE_NAME)); fail("should have thrown an exception"); } catch (ServiceNotFoundException expected) { assertEquals(SERVICE_NAME, expected.getServiceName()); } }