use of com.google.common.util.concurrent.Service in project weave by continuuity.
the class ControllerTest method testControllerBefore.
// Test controller created before service starts.
@Test
public void testControllerBefore() throws InterruptedException {
InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
zkServer.startAndWait();
LOG.info("ZKServer: " + zkServer.getConnectionStr());
try {
RunId runId = RunIds.generate();
ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClientService.startAndWait();
final CountDownLatch runLatch = new CountDownLatch(1);
final CountDownLatch stopLatch = new CountDownLatch(1);
WeaveController controller = getController(zkClientService, runId);
controller.addListener(new ServiceListenerAdapter() {
@Override
public void running() {
runLatch.countDown();
}
@Override
public void terminated(Service.State from) {
stopLatch.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Service service = createService(zkClientService, runId);
service.start();
Assert.assertTrue(runLatch.await(2, TimeUnit.SECONDS));
Assert.assertFalse(stopLatch.await(2, TimeUnit.SECONDS));
service.stop();
Assert.assertTrue(stopLatch.await(2, TimeUnit.SECONDS));
} finally {
zkServer.stopAndWait();
}
}
use of com.google.common.util.concurrent.Service in project weave by continuuity.
the class ControllerTest method testControllerListener.
// Test controller listener receive first state change without state transition from service
@Test
public void testControllerListener() throws InterruptedException {
InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
zkServer.startAndWait();
LOG.info("ZKServer: " + zkServer.getConnectionStr());
try {
RunId runId = RunIds.generate();
ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClientService.startAndWait();
Service service = createService(zkClientService, runId);
service.startAndWait();
final CountDownLatch runLatch = new CountDownLatch(1);
WeaveController controller = getController(zkClientService, runId);
controller.addListener(new ServiceListenerAdapter() {
@Override
public void running() {
runLatch.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Assert.assertTrue(runLatch.await(2, TimeUnit.SECONDS));
service.stopAndWait();
zkClientService.stopAndWait();
} finally {
zkServer.stopAndWait();
}
}
use of com.google.common.util.concurrent.Service in project weave by continuuity.
the class ServicesTest method testChain.
@Test
public void testChain() throws ExecutionException, InterruptedException {
AtomicBoolean transiting = new AtomicBoolean(false);
Service s1 = new DummyService("s1", transiting);
Service s2 = new DummyService("s2", transiting);
Service s3 = new DummyService("s3", transiting);
Futures.allAsList(Services.chainStart(s1, s2, s3).get()).get();
Futures.allAsList(Services.chainStop(s3, s2, s1).get()).get();
}
use of com.google.common.util.concurrent.Service in project weave by continuuity.
the class ServicesTest method testCompletion.
@Test
public void testCompletion() throws ExecutionException, InterruptedException {
Service service = new DummyService("s1", new AtomicBoolean());
ListenableFuture<Service.State> completion = Services.getCompletionFuture(service);
service.start();
service.stop();
completion.get();
AtomicBoolean transiting = new AtomicBoolean();
service = new DummyService("s2", transiting);
completion = Services.getCompletionFuture(service);
service.startAndWait();
transiting.set(true);
service.stop();
try {
completion.get();
Assert.assertTrue(false);
} catch (ExecutionException e) {
// Expected
}
}
use of com.google.common.util.concurrent.Service in project cdap by caskdata.
the class ReferenceCountedSupplierTests method testGetSupplier.
/**
* Starts a few threads to perform get operation on the given {@link CacheSupplier}s. It verifies that subsequent
* 'get' operation returns the same instance as it did during the first invocation. And it also verifies that the
* service is running.
*
* @param supplierList list of {@link CacheSupplier}
* @throws Exception if an error occurs during testing
*/
private void testGetSupplier(final List<CacheSupplier> supplierList) throws Exception {
// Get one instance now, for later comparisons
final List<Service> serviceList = new ArrayList<>();
for (CacheSupplier supplier : supplierList) {
serviceList.add(supplier.get());
}
final AtomicInteger numOps = new AtomicInteger(NUM_OPS);
final Random random = new Random(System.currentTimeMillis());
// Start threads that will 'get' DummyService
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
List<Future> futureList = new ArrayList<>();
for (int i = 0; i < NUM_THREADS; i++) {
futureList.add(executor.submit(new Runnable() {
@Override
public void run() {
// Perform NUM_OPS 'gets' of DummyService
while (numOps.decrementAndGet() > 0) {
for (int i = 0; i < supplierList.size(); i++) {
CacheSupplier supplier = supplierList.get(i);
Service newService = supplier.get();
Assert.assertTrue(newService == serviceList.get(i));
Assert.assertTrue(newService.isRunning());
}
int waitTime = random.nextInt(10);
try {
TimeUnit.MICROSECONDS.sleep(waitTime);
} catch (InterruptedException e) {
LOG.warn("Received an exception.", e);
}
}
}
}));
}
for (Future future : futureList) {
future.get(5, TimeUnit.SECONDS);
}
executor.shutdown();
executor.awaitTermination(2, TimeUnit.SECONDS);
}
Aggregations