Search in sources :

Example 51 with BuckEventBus

use of com.facebook.buck.event.BuckEventBus in project buck by facebook.

the class CounterRegistryImplTest method closingRegistryBeforeTimerFiresFlushesCounters.

@Test
public void closingRegistryBeforeTimerFiresFlushesCounters() throws IOException {
    BuckEventBus fakeEventBus = new BuckEventBus(new FakeClock(0), false, new BuildId("12345"), 1000);
    SnapshotEventListener listener = new SnapshotEventListener();
    fakeEventBus.register(listener);
    FakeExecutor fakeExecutor = new FakeExecutor();
    try (CounterRegistryImpl registry = new CounterRegistryImpl(fakeExecutor, fakeEventBus)) {
        IntegerCounter counter = registry.newIntegerCounter(CATEGORY, NAME, TAGS);
        counter.inc(42);
        assertThat("No events should be flushed before timer fires", listener.snapshotEvents, empty());
    }
    // We explicitly do not call fakeExecutor.drain() here, because we want to simulate what
    // happens when the registry is closed before the executor fires.
    assertThat("One snapshot event should be flushed when registry closed before timer fires", listener.snapshotEvents, hasSize(1));
    assertThat("Expected snapshot should be flushed when registry closed before timer fires", listener.snapshotEvents.get(0).getSnapshots(), hasItem(CounterSnapshot.builder().setCategory(CATEGORY).setTags(TAGS).putValues(NAME, 42).build()));
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) BuildId(com.facebook.buck.model.BuildId) FakeExecutor(com.facebook.buck.testutil.FakeExecutor) FakeClock(com.facebook.buck.timing.FakeClock) Test(org.junit.Test)

Example 52 with BuckEventBus

use of com.facebook.buck.event.BuckEventBus in project buck by facebook.

the class Parser method buildTargetGraph.

@SuppressWarnings("PMD.PrematureDeclaration")
protected TargetGraph buildTargetGraph(final PerBuildState state, final BuckEventBus eventBus, final Iterable<BuildTarget> toExplore, final boolean ignoreBuckAutodepsFiles) throws IOException, InterruptedException, BuildFileParseException, BuildTargetException {
    if (Iterables.isEmpty(toExplore)) {
        return TargetGraph.EMPTY;
    }
    final Map<BuildTarget, TargetGroup> groups = Maps.newHashMap();
    for (TargetGroup group : state.getAllGroups()) {
        groups.put(group.getBuildTarget(), group);
    }
    final MutableDirectedGraph<TargetNode<?, ?>> graph = new MutableDirectedGraph<>();
    final Map<BuildTarget, TargetNode<?, ?>> index = new HashMap<>();
    ParseEvent.Started parseStart = ParseEvent.started(toExplore);
    eventBus.post(parseStart);
    GraphTraversable<BuildTarget> traversable = target -> {
        TargetNode<?, ?> node;
        try {
            node = state.getTargetNode(target);
        } catch (BuildFileParseException | BuildTargetException e) {
            throw new RuntimeException(e);
        }
        if (ignoreBuckAutodepsFiles) {
            return Collections.emptyIterator();
        }
        for (BuildTarget dep : node.getDeps()) {
            try {
                state.getTargetNode(dep);
            } catch (BuildFileParseException | BuildTargetException | HumanReadableException e) {
                throw new HumanReadableException(e, "Couldn't get dependency '%s' of target '%s':\n%s", dep, target, e.getMessage());
            }
        }
        return node.getDeps().iterator();
    };
    GraphTraversable<BuildTarget> groupExpander = target -> {
        TargetGroup group = Preconditions.checkNotNull(groups.get(target), "SANITY FAILURE: Tried to expand group %s but it doesn't exist.", target);
        return Iterators.filter(group.iterator(), groups::containsKey);
    };
    AcyclicDepthFirstPostOrderTraversal<BuildTarget> targetGroupExpansion = new AcyclicDepthFirstPostOrderTraversal<>(groupExpander);
    AcyclicDepthFirstPostOrderTraversal<BuildTarget> targetNodeTraversal = new AcyclicDepthFirstPostOrderTraversal<>(traversable);
    TargetGraph targetGraph = null;
    try {
        for (BuildTarget target : targetNodeTraversal.traverse(toExplore)) {
            TargetNode<?, ?> targetNode = state.getTargetNode(target);
            Preconditions.checkNotNull(targetNode, "No target node found for %s", target);
            graph.addNode(targetNode);
            MoreMaps.putCheckEquals(index, target, targetNode);
            if (target.isFlavored()) {
                BuildTarget unflavoredTarget = BuildTarget.of(target.getUnflavoredBuildTarget());
                MoreMaps.putCheckEquals(index, unflavoredTarget, state.getTargetNode(unflavoredTarget));
            }
            for (BuildTarget dep : targetNode.getDeps()) {
                graph.addEdge(targetNode, state.getTargetNode(dep));
            }
        }
        for (BuildTarget groupTarget : targetGroupExpansion.traverse(groups.keySet())) {
            ImmutableMap<BuildTarget, Iterable<BuildTarget>> replacements = Maps.toMap(groupExpander.findChildren(groupTarget), target -> {
                TargetGroup group = groups.get(target);
                return Preconditions.checkNotNull(group, "SANITY FAILURE: Tried to expand group %s but it doesn't exist.", target);
            });
            if (!replacements.isEmpty()) {
                // TODO(tophyr): Stop duplicating target lists
                groups.put(groupTarget, Preconditions.checkNotNull(groups.get(groupTarget)).withReplacedTargets(replacements));
            }
        }
        targetGraph = new TargetGraph(graph, ImmutableMap.copyOf(index), ImmutableSet.copyOf(groups.values()));
        state.ensureConcreteFilesExist(eventBus);
        return targetGraph;
    } catch (AcyclicDepthFirstPostOrderTraversal.CycleException e) {
        throw new HumanReadableException(e.getMessage());
    } catch (RuntimeException e) {
        throw propagateRuntimeCause(e);
    } finally {
        eventBus.post(ParseEvent.finished(parseStart, Optional.ofNullable(targetGraph)));
    }
}
Also used : BroadcastEventListener(com.facebook.buck.event.listener.BroadcastEventListener) PerfEventId(com.facebook.buck.event.PerfEventId) BuckEvent(com.facebook.buck.event.BuckEvent) MoreMaps(com.facebook.buck.util.MoreMaps) TypeCoercerFactory(com.facebook.buck.rules.coercer.TypeCoercerFactory) Map(java.util.Map) AcyclicDepthFirstPostOrderTraversal(com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal) Cell(com.facebook.buck.rules.Cell) Path(java.nio.file.Path) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) Collection(java.util.Collection) BuildTargetException(com.facebook.buck.model.BuildTargetException) WatchEvents(com.facebook.buck.io.WatchEvents) BuildTarget(com.facebook.buck.model.BuildTarget) HasDefaultFlavors(com.facebook.buck.model.HasDefaultFlavors) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) Optional(java.util.Optional) SortedMap(java.util.SortedMap) ImplicitFlavorsInferringDescription(com.facebook.buck.rules.ImplicitFlavorsInferringDescription) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) BuckEventBus(com.facebook.buck.event.BuckEventBus) Iterables(com.google.common.collect.Iterables) TargetGraphAndBuildTargets(com.facebook.buck.rules.TargetGraphAndBuildTargets) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) MutableDirectedGraph(com.facebook.buck.graph.MutableDirectedGraph) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) GraphTraversable(com.facebook.buck.graph.GraphTraversable) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) TargetGroup(com.facebook.buck.rules.TargetGroup) Subscribe(com.google.common.eventbus.Subscribe) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Logger(com.facebook.buck.log.Logger) Counter(com.facebook.buck.counters.Counter) TargetNode(com.facebook.buck.rules.TargetNode) WatchEvent(java.nio.file.WatchEvent) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) HumanReadableException(com.facebook.buck.util.HumanReadableException) Maps(com.google.common.collect.Maps) ExecutionException(java.util.concurrent.ExecutionException) Futures(com.google.common.util.concurrent.Futures) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) TargetNode(com.facebook.buck.rules.TargetNode) HashMap(java.util.HashMap) TargetGraph(com.facebook.buck.rules.TargetGraph) BuildTarget(com.facebook.buck.model.BuildTarget) HumanReadableException(com.facebook.buck.util.HumanReadableException) TargetGroup(com.facebook.buck.rules.TargetGroup) AcyclicDepthFirstPostOrderTraversal(com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal) MutableDirectedGraph(com.facebook.buck.graph.MutableDirectedGraph)

Example 53 with BuckEventBus

use of com.facebook.buck.event.BuckEventBus in project buck by facebook.

the class ArtifactCachesTest method testCreateMultipleDirCaches.

@Test
public void testCreateMultipleDirCaches() throws Exception {
    ArtifactCacheBuckConfig cacheConfig = ArtifactCacheBuckConfigTest.createFromText("[cache]", "dir_cache_names = dir1, dir2", "[cache#dir1]", "dir = dir1", "dir_mode = readwrite", "[cache#dir2]", "dir = dir2", "dir_mode = readonly");
    ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    BuckEventBus buckEventBus = BuckEventBusFactory.newInstance();
    ArtifactCache artifactCache = stripDecorators(new ArtifactCaches(cacheConfig, buckEventBus, projectFilesystem, Optional.empty(), MoreExecutors.newDirectExecutorService(), Optional.empty()).newInstance());
    assertThat(artifactCache, Matchers.instanceOf(MultiArtifactCache.class));
    MultiArtifactCache multiArtifactCache = (MultiArtifactCache) artifactCache;
    assertThat(multiArtifactCache.getArtifactCaches().size(), Matchers.equalTo(2));
    ArtifactCache c1 = stripDecorators(multiArtifactCache.getArtifactCaches().get(0));
    ArtifactCache c2 = stripDecorators(multiArtifactCache.getArtifactCaches().get(1));
    assertThat(c1, Matchers.instanceOf(DirArtifactCache.class));
    assertThat(c2, Matchers.instanceOf(DirArtifactCache.class));
    DirArtifactCache dir1 = (DirArtifactCache) c1;
    assertThat(dir1.getCacheDir(), Matchers.equalTo(Paths.get("dir1").toAbsolutePath()));
    assertThat(dir1.isStoreSupported(), Matchers.equalTo(true));
    DirArtifactCache dir2 = (DirArtifactCache) c2;
    assertThat(dir2.getCacheDir(), Matchers.equalTo(Paths.get("dir2").toAbsolutePath()));
    assertThat(dir2.isStoreSupported(), Matchers.equalTo(false));
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Test(org.junit.Test)

Example 54 with BuckEventBus

use of com.facebook.buck.event.BuckEventBus in project buck by facebook.

the class ArtifactCachesTest method testCreateDirCacheOnlyWhenOnBlacklistedWifi.

@Test
public void testCreateDirCacheOnlyWhenOnBlacklistedWifi() throws Exception {
    ArtifactCacheBuckConfig cacheConfig = ArtifactCacheBuckConfigTest.createFromText("[cache]", "mode = dir, http", "blacklisted_wifi_ssids = weevil, evilwifi");
    ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    BuckEventBus buckEventBus = BuckEventBusFactory.newInstance();
    ArtifactCache artifactCache = new ArtifactCaches(cacheConfig, buckEventBus, projectFilesystem, Optional.of("evilwifi"), MoreExecutors.newDirectExecutorService(), Optional.empty()).newInstance();
    assertThat(stripDecorators(artifactCache), Matchers.instanceOf(DirArtifactCache.class));
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Test(org.junit.Test)

Example 55 with BuckEventBus

use of com.facebook.buck.event.BuckEventBus in project buck by facebook.

the class ArtifactCachesTest method testCreateDirCacheOnly.

@Test
public void testCreateDirCacheOnly() throws Exception {
    ArtifactCacheBuckConfig cacheConfig = ArtifactCacheBuckConfigTest.createFromText("[cache]", "mode = dir");
    ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    BuckEventBus buckEventBus = BuckEventBusFactory.newInstance();
    ArtifactCache artifactCache = new ArtifactCaches(cacheConfig, buckEventBus, projectFilesystem, Optional.empty(), MoreExecutors.newDirectExecutorService(), Optional.empty()).newInstance();
    assertThat(stripDecorators(artifactCache), Matchers.instanceOf(DirArtifactCache.class));
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Test(org.junit.Test)

Aggregations

BuckEventBus (com.facebook.buck.event.BuckEventBus)75 Test (org.junit.Test)58 IncrementingFakeClock (com.facebook.buck.timing.IncrementingFakeClock)25 BuildId (com.facebook.buck.model.BuildId)21 Clock (com.facebook.buck.timing.Clock)21 FakeClock (com.facebook.buck.timing.FakeClock)20 BuildTarget (com.facebook.buck.model.BuildTarget)19 TestConsole (com.facebook.buck.testutil.TestConsole)16 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)15 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)15 Path (java.nio.file.Path)13 IOException (java.io.IOException)12 BuildEvent (com.facebook.buck.rules.BuildEvent)11 ParseEvent (com.facebook.buck.parser.ParseEvent)10 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)10 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)10 RuleKey (com.facebook.buck.rules.RuleKey)10 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)10 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)10 ActionGraphEvent (com.facebook.buck.event.ActionGraphEvent)9