Search in sources :

Example 11 with BuildId

use of com.facebook.buck.model.BuildId in project buck by facebook.

the class CounterRegistryImplTest method noEventsFlushedIfNoCountersRegistered.

@Test
public void noEventsFlushedIfNoCountersRegistered() 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)) {
        assertThat("No events should be flushed before timer fires", listener.snapshotEvents, empty());
        fakeExecutor.drain();
        assertThat("No events should be flushed after timer fires", listener.snapshotEvents, empty());
    }
    assertThat("No events should be flushed after registry closed", listener.snapshotEvents, empty());
}
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 12 with BuildId

use of com.facebook.buck.model.BuildId in project buck by facebook.

the class CounterRegistryImplTest method eventIsFlushedIfCounterRegisteredWithData.

@Test
public void eventIsFlushedIfCounterRegisteredWithData() 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());
        fakeExecutor.drain();
        assertThat("One event should be flushed after timer fires", listener.snapshotEvents, hasSize(1));
        assertThat("Counter with expected value should be flushed after 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 13 with BuildId

use of com.facebook.buck.model.BuildId in project buck by facebook.

the class ChromeTraceBuildListenerTest method testBuildJson.

@Test
public void testBuildJson() throws IOException {
    ProjectFilesystem projectFilesystem = new ProjectFilesystem(tmpDir.getRoot().toPath());
    ObjectMapper mapper = ObjectMappers.newDefaultInstance();
    BuildId buildId = new BuildId("ChromeTraceBuildListenerTestBuildId");
    ChromeTraceBuildListener listener = new ChromeTraceBuildListener(projectFilesystem, invocationInfo, new FakeClock(TIMESTAMP_NANOS), mapper, Locale.US, TimeZone.getTimeZone("America/Los_Angeles"), /* tracesToKeep */
    42, false);
    BuildTarget target = BuildTargetFactory.newInstance("//fake:rule");
    FakeBuildRule rule = new FakeBuildRule(target, new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()))), ImmutableSortedSet.of());
    RuleKey ruleKey = new RuleKey("abc123");
    String stepShortName = "fakeStep";
    String stepDescription = "I'm a Fake Step!";
    UUID stepUuid = UUID.randomUUID();
    ImmutableSet<BuildTarget> buildTargets = ImmutableSet.of(target);
    Iterable<String> buildArgs = Iterables.transform(buildTargets, Object::toString);
    Clock fakeClock = new IncrementingFakeClock(TimeUnit.MILLISECONDS.toNanos(1));
    BuckEventBus eventBus = BuckEventBusFactory.newInstance(fakeClock, buildId);
    eventBus.register(listener);
    CommandEvent.Started commandEventStarted = CommandEvent.started("party", ImmutableList.of("arg1", "arg2"), true, 23L);
    eventBus.post(commandEventStarted);
    eventBus.post(new PerfStatsTracking.MemoryPerfStatsEvent(/* freeMemoryBytes */
    1024 * 1024L, /* totalMemoryBytes */
    3 * 1024 * 1024L, /* timeSpentInGcMs */
    -1, /* currentMemoryBytesUsageByPool */
    ImmutableMap.of("flower", 42L * 1024 * 1024)));
    ArtifactCacheConnectEvent.Started artifactCacheConnectEventStarted = ArtifactCacheConnectEvent.started();
    eventBus.post(artifactCacheConnectEventStarted);
    eventBus.post(ArtifactCacheConnectEvent.finished(artifactCacheConnectEventStarted));
    BuildEvent.Started buildEventStarted = BuildEvent.started(buildArgs);
    eventBus.post(buildEventStarted);
    HttpArtifactCacheEvent.Started artifactCacheEventStarted = HttpArtifactCacheEvent.newFetchStartedEvent(ruleKey);
    eventBus.post(artifactCacheEventStarted);
    eventBus.post(HttpArtifactCacheEvent.newFinishedEventBuilder(artifactCacheEventStarted).setFetchDataBuilder(HttpArtifactCacheEventFetchData.builder().setFetchResult(CacheResult.hit("http"))).build());
    ArtifactCompressionEvent.Started artifactCompressionStartedEvent = ArtifactCompressionEvent.started(ArtifactCompressionEvent.Operation.COMPRESS, ImmutableSet.of(ruleKey));
    eventBus.post(artifactCompressionStartedEvent);
    eventBus.post(ArtifactCompressionEvent.finished(artifactCompressionStartedEvent));
    BuildRuleEvent.Started started = BuildRuleEvent.started(rule, durationTracker);
    eventBus.post(started);
    eventBus.post(StepEvent.started(stepShortName, stepDescription, stepUuid));
    JavacPhaseEvent.Started runProcessorsStartedEvent = JavacPhaseEvent.started(target, JavacPhaseEvent.Phase.RUN_ANNOTATION_PROCESSORS, ImmutableMap.of());
    eventBus.post(runProcessorsStartedEvent);
    String annotationProcessorName = "com.facebook.FakeProcessor";
    AnnotationProcessingEvent.Operation operation = AnnotationProcessingEvent.Operation.PROCESS;
    int annotationRound = 1;
    boolean isLastRound = false;
    AnnotationProcessingEvent.Started annotationProcessingEventStarted = AnnotationProcessingEvent.started(target, annotationProcessorName, operation, annotationRound, isLastRound);
    eventBus.post(annotationProcessingEventStarted);
    HttpArtifactCacheEvent.Scheduled httpScheduled = HttpArtifactCacheEvent.newStoreScheduledEvent(Optional.of("TARGET_ONE"), ImmutableSet.of(ruleKey));
    HttpArtifactCacheEvent.Started httpStarted = HttpArtifactCacheEvent.newStoreStartedEvent(httpScheduled);
    eventBus.post(httpStarted);
    HttpArtifactCacheEvent.Finished httpFinished = HttpArtifactCacheEvent.newFinishedEventBuilder(httpStarted).build();
    eventBus.post(httpFinished);
    final CompilerPluginDurationEvent.Started processingPartOneStarted = CompilerPluginDurationEvent.started(target, annotationProcessorName, "processingPartOne", ImmutableMap.of());
    eventBus.post(processingPartOneStarted);
    eventBus.post(CompilerPluginDurationEvent.finished(processingPartOneStarted, ImmutableMap.of()));
    eventBus.post(AnnotationProcessingEvent.finished(annotationProcessingEventStarted));
    eventBus.post(JavacPhaseEvent.finished(runProcessorsStartedEvent, ImmutableMap.of()));
    eventBus.post(StepEvent.finished(StepEvent.started(stepShortName, stepDescription, stepUuid), 0));
    eventBus.post(BuildRuleEvent.finished(started, BuildRuleKeys.of(ruleKey), BuildRuleStatus.SUCCESS, CacheResult.miss(), Optional.of(BuildRuleSuccessType.BUILT_LOCALLY), Optional.empty(), Optional.empty()));
    try (final SimplePerfEvent.Scope scope1 = SimplePerfEvent.scope(eventBus, PerfEventId.of("planning"), ImmutableMap.<String, Object>of("nefarious", "true"))) {
        try (final SimplePerfEvent.Scope scope2 = SimplePerfEvent.scope(eventBus, PerfEventId.of("scheming"))) {
            scope2.appendFinishedInfo("success", "false");
        }
    }
    eventBus.post(BuildEvent.finished(buildEventStarted, 0));
    eventBus.post(CommandEvent.finished(commandEventStarted, /* exitCode */
    0));
    listener.outputTrace(new BuildId("BUILD_ID"));
    File resultFile = new File(tmpDir.getRoot(), "buck-out/log/build.trace");
    List<ChromeTraceEvent> originalResultList = mapper.readValue(resultFile, new TypeReference<List<ChromeTraceEvent>>() {
    });
    List<ChromeTraceEvent> resultListCopy = new ArrayList<>();
    resultListCopy.addAll(originalResultList);
    ImmutableMap<String, String> emptyArgs = ImmutableMap.of();
    assertNextResult(resultListCopy, "process_name", ChromeTraceEvent.Phase.METADATA, ImmutableMap.of("name", "buck"));
    assertNextResult(resultListCopy, "party", ChromeTraceEvent.Phase.BEGIN, ImmutableMap.of("command_args", "arg1 arg2"));
    assertNextResult(resultListCopy, "memory", ChromeTraceEvent.Phase.COUNTER, ImmutableMap.of("used_memory_mb", "2", "free_memory_mb", "1", "total_memory_mb", "3", "time_spent_in_gc_sec", "0", "pool_flower_mb", "42"));
    assertNextResult(resultListCopy, "artifact_connect", ChromeTraceEvent.Phase.BEGIN, emptyArgs);
    assertNextResult(resultListCopy, "artifact_connect", ChromeTraceEvent.Phase.END, emptyArgs);
    assertNextResult(resultListCopy, "build", ChromeTraceEvent.Phase.BEGIN, emptyArgs);
    assertNextResult(resultListCopy, "http_artifact_fetch", ChromeTraceEvent.Phase.BEGIN, ImmutableMap.of("rule_key", "abc123"));
    assertNextResult(resultListCopy, "http_artifact_fetch", ChromeTraceEvent.Phase.END, ImmutableMap.of("rule_key", "abc123", "success", "true", "cache_result", "HTTP_HIT"));
    assertNextResult(resultListCopy, "artifact_compress", ChromeTraceEvent.Phase.BEGIN, ImmutableMap.of("rule_key", "abc123"));
    assertNextResult(resultListCopy, "artifact_compress", ChromeTraceEvent.Phase.END, ImmutableMap.of("rule_key", "abc123"));
    // BuildRuleEvent.Started
    assertNextResult(resultListCopy, "//fake:rule", ChromeTraceEvent.Phase.BEGIN, ImmutableMap.of());
    assertNextResult(resultListCopy, "fakeStep", ChromeTraceEvent.Phase.BEGIN, emptyArgs);
    assertNextResult(resultListCopy, "run annotation processors", ChromeTraceEvent.Phase.BEGIN, emptyArgs);
    assertNextResult(resultListCopy, "com.facebook.FakeProcessor.process", ChromeTraceEvent.Phase.BEGIN, emptyArgs);
    assertNextResult(resultListCopy, "http_artifact_store", ChromeTraceEvent.Phase.BEGIN, ImmutableMap.of("rule_key", "abc123"));
    assertNextResult(resultListCopy, "http_artifact_store", ChromeTraceEvent.Phase.END, ImmutableMap.of("success", "true", "rule_key", "abc123"));
    assertNextResult(resultListCopy, "processingPartOne", ChromeTraceEvent.Phase.BEGIN, emptyArgs);
    assertNextResult(resultListCopy, "processingPartOne", ChromeTraceEvent.Phase.END, emptyArgs);
    assertNextResult(resultListCopy, "com.facebook.FakeProcessor.process", ChromeTraceEvent.Phase.END, emptyArgs);
    assertNextResult(resultListCopy, "run annotation processors", ChromeTraceEvent.Phase.END, emptyArgs);
    assertNextResult(resultListCopy, "fakeStep", ChromeTraceEvent.Phase.END, ImmutableMap.of("description", "I'm a Fake Step!", "exit_code", "0"));
    assertNextResult(resultListCopy, "//fake:rule", ChromeTraceEvent.Phase.END, ImmutableMap.of("cache_result", "miss", "success_type", "BUILT_LOCALLY"));
    assertNextResult(resultListCopy, "planning", ChromeTraceEvent.Phase.BEGIN, ImmutableMap.of("nefarious", "true"));
    assertNextResult(resultListCopy, "scheming", ChromeTraceEvent.Phase.BEGIN, emptyArgs);
    assertNextResult(resultListCopy, "scheming", ChromeTraceEvent.Phase.END, ImmutableMap.of("success", "false"));
    assertNextResult(resultListCopy, "planning", ChromeTraceEvent.Phase.END, emptyArgs);
    assertNextResult(resultListCopy, "build", ChromeTraceEvent.Phase.END, emptyArgs);
    assertNextResult(resultListCopy, "party", ChromeTraceEvent.Phase.END, ImmutableMap.of("command_args", "arg1 arg2", "daemon", "true"));
    assertEquals(0, resultListCopy.size());
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) ChromeTraceEvent(com.facebook.buck.event.ChromeTraceEvent) ArrayList(java.util.ArrayList) Clock(com.facebook.buck.timing.Clock) FakeClock(com.facebook.buck.timing.FakeClock) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTarget(com.facebook.buck.model.BuildTarget) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) UUID(java.util.UUID) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) PerfStatsTracking(com.facebook.buck.util.perf.PerfStatsTracking) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RuleKey(com.facebook.buck.rules.RuleKey) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) ArtifactCompressionEvent(com.facebook.buck.event.ArtifactCompressionEvent) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildEvent(com.facebook.buck.rules.BuildEvent) BuildRuleEvent(com.facebook.buck.rules.BuildRuleEvent) File(java.io.File) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) JavacPhaseEvent(com.facebook.buck.jvm.java.tracing.JavacPhaseEvent) CommandEvent(com.facebook.buck.event.CommandEvent) AnnotationProcessingEvent(com.facebook.buck.jvm.java.AnnotationProcessingEvent) FakeClock(com.facebook.buck.timing.FakeClock) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) CompilerPluginDurationEvent(com.facebook.buck.event.CompilerPluginDurationEvent) BuildId(com.facebook.buck.model.BuildId) HttpArtifactCacheEvent(com.facebook.buck.artifact_cache.HttpArtifactCacheEvent) ArtifactCacheConnectEvent(com.facebook.buck.artifact_cache.ArtifactCacheConnectEvent) Test(org.junit.Test)

Example 14 with BuildId

use of com.facebook.buck.model.BuildId in project buck by facebook.

the class BroadcastEventListenerTest method tryBroadcastInMultipleBuses.

@Test
public void tryBroadcastInMultipleBuses() {
    BuckEventBus bus1 = BuckEventBusFactory.newInstance(new IncrementingFakeClock(TimeUnit.SECONDS.toNanos(1)), new BuildId("bus1"));
    BuckEventBus bus2 = BuckEventBusFactory.newInstance(new IncrementingFakeClock(TimeUnit.SECONDS.toNanos(1)), new BuildId("bus2"));
    bus1.register(new Object() {

        @Subscribe
        public void actionGraphCacheMiss(ActionGraphEvent.Cache.Miss event) {
            trackedEvents.add(event);
        }
    });
    bus2.register(new Object() {

        @Subscribe
        public void actionGraphCacheHit(ActionGraphEvent.Cache.Hit event) {
            trackedEvents.add(event);
        }

        @Subscribe
        public void actionGraphCacheMiss(ActionGraphEvent.Cache.Miss event) {
            trackedEvents.add(event);
        }
    });
    BroadcastEventListener listener = new BroadcastEventListener();
    listener.addEventBus(bus1);
    listener.addEventBus(bus2);
    listener.broadcast(ActionGraphEvent.Cache.hit());
    listener.broadcast(ActionGraphEvent.Cache.miss(/* cacheWasEmpty */
    false));
    assertEquals(countEventsOf(ActionGraphEvent.Cache.Miss.class), 2);
    assertEquals(countEventsOf(ActionGraphEvent.Cache.Hit.class), 1);
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) BuildId(com.facebook.buck.model.BuildId) ActionGraphEvent(com.facebook.buck.event.ActionGraphEvent) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) Subscribe(com.google.common.eventbus.Subscribe) Test(org.junit.Test)

Example 15 with BuildId

use of com.facebook.buck.model.BuildId in project buck by facebook.

the class JUnitStepTest method testGetShellCommand.

@Test
public void testGetShellCommand() throws IOException {
    String testClass1 = "com.facebook.buck.shell.JUnitCommandTest";
    String testClass2 = "com.facebook.buck.shell.InstrumentCommandTest";
    Set<String> testClassNames = ImmutableSet.of(testClass1, testClass2);
    String vmArg1 = "-Dname1=value1";
    String vmArg2 = "-Dname1=value2";
    ImmutableList<String> vmArgs = ImmutableList.of(vmArg1, vmArg2);
    BuildId pretendBuildId = new BuildId("pretend-build-id");
    String buildIdArg = String.format("-Dcom.facebook.buck.buildId=%s", pretendBuildId);
    Path modulePath = Paths.get("module/submodule");
    String modulePathArg = String.format("-Dcom.facebook.buck.moduleBasePath=%s", modulePath);
    Path directoryForTestResults = Paths.get("buck-out/gen/theresults/");
    Path testRunnerClasspath = Paths.get("build/classes/junit");
    ProjectFilesystem filesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
    Path classpathFile = filesystem.resolve("foo");
    JUnitJvmArgs args = JUnitJvmArgs.builder().setBuildId(pretendBuildId).setBuckModuleBaseSourceCodePath(modulePath).setClasspathFile(classpathFile).setTestRunnerClasspath(testRunnerClasspath).setExtraJvmArgs(vmArgs).setTestType(TestType.JUNIT).setDirectoryForTestResults(directoryForTestResults).addAllTestClasses(testClassNames).build();
    JUnitStep junit = new JUnitStep(filesystem, /* nativeLibsEnvironment */
    ImmutableMap.of(), /* testRuleTimeoutMs */
    Optional.empty(), /* testCaseTimeoutMs */
    Optional.empty(), ImmutableMap.of(), new ExternalJavaRuntimeLauncher("/foo/bar/custom/java"), args);
    ExecutionContext executionContext = TestExecutionContext.newBuilder().setConsole(new TestConsole(Verbosity.ALL)).setDefaultTestTimeoutMillis(5000L).build();
    assertEquals(executionContext.getVerbosity(), Verbosity.ALL);
    assertEquals(executionContext.getDefaultTestTimeoutMillis(), 5000L);
    List<String> observedArgs = junit.getShellCommand(executionContext);
    MoreAsserts.assertListEquals(ImmutableList.of("/foo/bar/custom/java", "-Dbuck.testrunner_classes=" + testRunnerClasspath, buildIdArg, modulePathArg, "-Dapple.awt.UIElement=true", vmArg1, vmArg2, "-verbose", "-classpath", "@" + classpathFile + File.pathSeparator + MorePaths.pathWithPlatformSeparators("build/classes/junit"), FileClassPathRunner.class.getName(), "com.facebook.buck.testrunner.JUnitMain", "--output", directoryForTestResults.toString(), "--default-test-timeout", "5000", testClass1, testClass2), observedArgs);
}
Also used : Path(java.nio.file.Path) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) BuildId(com.facebook.buck.model.BuildId) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Aggregations

BuildId (com.facebook.buck.model.BuildId)41 Test (org.junit.Test)25 BuckEventBus (com.facebook.buck.event.BuckEventBus)22 Path (java.nio.file.Path)13 FakeClock (com.facebook.buck.timing.FakeClock)12 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)10 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)9 IOException (java.io.IOException)8 IncrementingFakeClock (com.facebook.buck.timing.IncrementingFakeClock)7 BuildTarget (com.facebook.buck.model.BuildTarget)6 Before (org.junit.Before)6 DefaultClock (com.facebook.buck.timing.DefaultClock)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ArtifactCache (com.facebook.buck.artifact_cache.ArtifactCache)4 BuildRuleDurationTracker (com.facebook.buck.rules.BuildRuleDurationTracker)4 Clock (com.facebook.buck.timing.Clock)4 NullFileHashCache (com.facebook.buck.util.cache.NullFileHashCache)4 ImmutableList (com.google.common.collect.ImmutableList)4 RuleKey (com.facebook.buck.rules.RuleKey)3 FakeExecutor (com.facebook.buck.testutil.FakeExecutor)3