Search in sources :

Example 46 with Cell

use of com.facebook.buck.rules.Cell in project buck by facebook.

the class ParserTest method whenBuckConfigEntryChangesThenCachedRulesAreInvalidated.

@Test
public void whenBuckConfigEntryChangesThenCachedRulesAreInvalidated() throws Exception {
    Path buckFile = cellRoot.resolve("BUCK");
    Files.write(buckFile, Joiner.on("").join(ImmutableList.of("read_config('foo', 'bar')\n", "genrule(name = 'cake', out = 'file.txt', cmd = 'touch $OUT')\n")).getBytes(UTF_8));
    BuckConfig config = FakeBuckConfig.builder().setSections(ImmutableMap.of("foo", ImmutableMap.of("bar", "value"))).setFilesystem(filesystem).build();
    Cell cell = new TestCellBuilder().setFilesystem(filesystem).setBuckConfig(config).build();
    parser.getAllTargetNodes(eventBus, cell, false, executorService, buckFile);
    // Call filterAllTargetsInProject to request cached rules.
    config = FakeBuckConfig.builder().setFilesystem(filesystem).setSections(ImmutableMap.of("foo", ImmutableMap.of("bar", "other value"))).build();
    cell = new TestCellBuilder().setFilesystem(filesystem).setBuckConfig(config).build();
    parser.getAllTargetNodes(eventBus, cell, false, executorService, buckFile);
    // Test that the second parseBuildFile call repopulated the cache.
    assertEquals("Should have invalidated.", 2, counter.calls);
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) BuckConfig(com.facebook.buck.cli.BuckConfig) FakeBuckConfig(com.facebook.buck.cli.FakeBuckConfig) Cell(com.facebook.buck.rules.Cell) TestCellBuilder(com.facebook.buck.rules.TestCellBuilder) Test(org.junit.Test)

Example 47 with Cell

use of com.facebook.buck.rules.Cell in project buck by facebook.

the class ProjectBuildFileParserPoolTest method fuzzForConcurrentAccess.

@Test
public void fuzzForConcurrentAccess() throws Exception {
    final int parsersCount = 3;
    Cell cell = EasyMock.createMock(Cell.class);
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(4));
    try (ProjectBuildFileParserPool parserPool = new ProjectBuildFileParserPool(parsersCount, input -> {
        final AtomicInteger sleepCallCount = new AtomicInteger(0);
        return createMockParser(() -> {
            int numCalls = sleepCallCount.incrementAndGet();
            Preconditions.checkState(numCalls == 1);
            try {
                Thread.sleep(10);
            } finally {
                sleepCallCount.decrementAndGet();
            }
            return ImmutableList.of();
        });
    })) {
        Futures.allAsList(scheduleWork(cell, parserPool, executorService, 142)).get();
    } finally {
        executorService.shutdown();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Cell(com.facebook.buck.rules.Cell) Test(org.junit.Test)

Example 48 with Cell

use of com.facebook.buck.rules.Cell 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 49 with Cell

use of com.facebook.buck.rules.Cell 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 50 with Cell

use of com.facebook.buck.rules.Cell 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)

Aggregations

Cell (com.facebook.buck.rules.Cell)87 Test (org.junit.Test)57 Path (java.nio.file.Path)46 TestCellBuilder (com.facebook.buck.rules.TestCellBuilder)41 BuildTarget (com.facebook.buck.model.BuildTarget)25 BuckConfig (com.facebook.buck.cli.BuckConfig)24 FakeBuckConfig (com.facebook.buck.cli.FakeBuckConfig)22 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)21 PathSourcePath (com.facebook.buck.rules.PathSourcePath)15 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)14 ImmutableSet (com.google.common.collect.ImmutableSet)14 BuckEventBus (com.facebook.buck.event.BuckEventBus)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 BroadcastEventListener (com.facebook.buck.event.listener.BroadcastEventListener)11 ParserConfig (com.facebook.buck.parser.ParserConfig)11 IOException (java.io.IOException)11 Map (java.util.Map)11 TargetNode (com.facebook.buck.rules.TargetNode)10 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)10 ImmutableList (com.google.common.collect.ImmutableList)10