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);
}
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();
}
}
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));
}
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();
}
}
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();
}
}
Aggregations