use of com.google.common.util.concurrent.ListeningExecutorService in project buck by facebook.
the class ProjectBuildFileParserPoolTest method closesCreatedParsers.
@Test
public void closesCreatedParsers() throws Exception {
final int parsersCount = 4;
final AtomicInteger parserCount = new AtomicInteger(0);
Cell cell = EasyMock.createMock(Cell.class);
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(parsersCount));
final CountDownLatch createParserLatch = new CountDownLatch(parsersCount);
try (ProjectBuildFileParserPool parserPool = new ProjectBuildFileParserPool(parsersCount, input -> {
parserCount.incrementAndGet();
final ProjectBuildFileParser parser = EasyMock.createMock(ProjectBuildFileParser.class);
try {
EasyMock.expect(parser.getAllRulesAndMetaRules(EasyMock.anyObject(Path.class))).andAnswer(() -> {
createParserLatch.countDown();
createParserLatch.await();
return ImmutableList.of();
}).anyTimes();
parser.close();
EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() {
@Override
public Void answer() throws Throwable {
parserCount.decrementAndGet();
return null;
}
});
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
EasyMock.replay(parser);
return parser;
})) {
Futures.allAsList(scheduleWork(cell, parserPool, executorService, parsersCount * 2)).get();
assertThat(parserCount.get(), Matchers.is(4));
} finally {
executorService.shutdown();
}
// Parser shutdown is async.
for (int i = 0; i < 10; ++i) {
if (parserCount.get() == 0) {
break;
}
Thread.sleep(100);
}
assertThat(parserCount.get(), Matchers.is(0));
}
use of com.google.common.util.concurrent.ListeningExecutorService in project buck by facebook.
the class ProjectBuildFileParserPoolTest method workThatThrows.
@Test
public void workThatThrows() throws Exception {
Cell cell = EasyMock.createMock(Cell.class);
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
final String exceptionMessage = "haha!";
final AtomicBoolean throwWhileParsing = new AtomicBoolean(true);
try (ProjectBuildFileParserPool parserPool = new ProjectBuildFileParserPool(/* maxParsers */
2, createMockParserFactory(() -> {
if (throwWhileParsing.get()) {
throw new Exception(exceptionMessage);
}
return ImmutableList.of();
}))) {
ImmutableSet<ListenableFuture<?>> failedWork = scheduleWork(cell, parserPool, executorService, 5);
for (ListenableFuture<?> failedFuture : failedWork) {
try {
failedFuture.get();
fail("Expected ExecutionException to be thrown.");
} catch (ExecutionException e) {
assertThat(e.getCause().getMessage(), Matchers.equalTo(exceptionMessage));
}
}
// Make sure it's still possible to do work.
throwWhileParsing.set(false);
Futures.allAsList(scheduleWork(cell, parserPool, executorService, 5)).get();
} finally {
executorService.shutdown();
}
}
use of com.google.common.util.concurrent.ListeningExecutorService in project buck by facebook.
the class ProjectBuildFileParserPoolTest method ignoresCancellation.
@Test
public void ignoresCancellation() throws Exception {
Cell cell = EasyMock.createMock(Cell.class);
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
int numberOfJobs = 5;
final CountDownLatch waitTillAllWorkIsDone = new CountDownLatch(numberOfJobs);
final CountDownLatch waitTillCanceled = new CountDownLatch(1);
try (ProjectBuildFileParserPool parserPool = new ProjectBuildFileParserPool(/* maxParsers */
1, createMockParserFactory(() -> {
waitTillCanceled.await();
waitTillAllWorkIsDone.countDown();
return ImmutableList.of();
}))) {
ImmutableSet<ListenableFuture<?>> futures = scheduleWork(cell, parserPool, executorService, numberOfJobs);
for (ListenableFuture<?> future : futures) {
future.cancel(true);
}
waitTillCanceled.countDown();
// We're making sure cancel is ignored by the pool by waiting for the supposedly canceled
// work to go through.
waitTillAllWorkIsDone.await(1, TimeUnit.SECONDS);
} finally {
executorService.shutdown();
}
}
use of com.google.common.util.concurrent.ListeningExecutorService in project buck by facebook.
the class ProjectBuildFileParserPoolTest method closeWhenRunningJobs.
@Test
public void closeWhenRunningJobs() throws Exception {
Cell cell = EasyMock.createMock(Cell.class);
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
final CountDownLatch waitTillClosed = new CountDownLatch(1);
final CountDownLatch firstJobRunning = new CountDownLatch(1);
final AtomicInteger postCloseWork = new AtomicInteger(0);
ImmutableSet<ListenableFuture<?>> futures;
try (ProjectBuildFileParserPool parserPool = new ProjectBuildFileParserPool(/* maxParsers */
1, createMockParserFactory(() -> {
firstJobRunning.countDown();
waitTillClosed.await();
return ImmutableList.of();
}))) {
futures = scheduleWork(cell, parserPool, executorService, 5);
for (ListenableFuture<?> future : futures) {
Futures.addCallback(future, new FutureCallback<Object>() {
@Override
public void onSuccess(@Nullable Object result) {
postCloseWork.incrementAndGet();
}
@Override
public void onFailure(Throwable t) {
}
});
}
firstJobRunning.await(1, TimeUnit.SECONDS);
}
waitTillClosed.countDown();
List<Object> futureResults = Futures.successfulAsList(futures).get(1, TimeUnit.SECONDS);
// The threadpool is of size 1, so we had 1 job in the 'running' state. That one job completed
// normally, the rest should have been cancelled.
int expectedCompletedJobs = 1;
int completedJobs = FluentIterable.from(futureResults).filter(Objects::nonNull).size();
assertThat(completedJobs, Matchers.equalTo(expectedCompletedJobs));
executorService.shutdown();
assertThat(executorService.awaitTermination(1, TimeUnit.SECONDS), Matchers.is(true));
assertThat(postCloseWork.get(), Matchers.equalTo(expectedCompletedJobs));
}
use of com.google.common.util.concurrent.ListeningExecutorService in project pinpoint by naver.
the class ActiveTraceRepositoryTest method executeTransactions.
private ListenableFuture<List<TraceThreadTuple>> executeTransactions(CountDownLatch awaitLatch, CountDownLatch executeLatch, int newTransactionCount, int sampledContinuationCount, int unsampledContinuationCount) {
final int totalTransactionCount = newTransactionCount + sampledContinuationCount + unsampledContinuationCount;
final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(totalTransactionCount));
final List<ListenableFuture<TraceThreadTuple>> futures = new ArrayList<ListenableFuture<TraceThreadTuple>>();
for (int i = 0; i < newTransactionCount; ++i) {
futures.add(executeNewTrace(executor, awaitLatch, executeLatch));
}
for (int i = 0; i < sampledContinuationCount; ++i) {
futures.add(executeSampledContinuedTrace(executor, awaitLatch, executeLatch, i));
}
for (int i = 0; i < unsampledContinuationCount; ++i) {
futures.add(executeUnsampledContinuedTrace(executor, awaitLatch, executeLatch));
}
return Futures.allAsList(futures);
}
Aggregations