Search in sources :

Example 11 with ListeningExecutorService

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));
}
Also used : ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Cell(com.facebook.buck.rules.Cell) Test(org.junit.Test)

Example 12 with ListeningExecutorService

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();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ExecutionException(java.util.concurrent.ExecutionException) Cell(com.facebook.buck.rules.Cell) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 13 with ListeningExecutorService

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();
    }
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) Cell(com.facebook.buck.rules.Cell) Test(org.junit.Test)

Example 14 with ListeningExecutorService

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));
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Cell(com.facebook.buck.rules.Cell) Test(org.junit.Test)

Example 15 with ListeningExecutorService

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);
}
Also used : ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService)

Aggregations

ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)201 Test (org.junit.Test)115 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)75 ArrayList (java.util.ArrayList)43 CountDownLatch (java.util.concurrent.CountDownLatch)29 ExecutorService (java.util.concurrent.ExecutorService)28 IOException (java.io.IOException)25 ExecutionException (java.util.concurrent.ExecutionException)25 Interval (org.joda.time.Interval)25 DateTime (org.joda.time.DateTime)23 List (java.util.List)21 Callable (java.util.concurrent.Callable)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 DruidServer (io.druid.client.DruidServer)18 DataSegment (io.druid.timeline.DataSegment)18 DruidServer (org.apache.druid.client.DruidServer)17 ImmutableMap (com.google.common.collect.ImmutableMap)16 File (java.io.File)16 Map (java.util.Map)16 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)15