Search in sources :

Example 1 with BuildId

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

the class HttpArtifactCacheTest method createFinishedEventBuilder.

private static HttpArtifactCacheEvent.Finished.Builder createFinishedEventBuilder() {
    HttpArtifactCacheEvent.Started started = HttpArtifactCacheEvent.newFetchStartedEvent(new RuleKey("1234"));
    started.configure(-1, -1, -1, -1, new BuildId());
    return HttpArtifactCacheEvent.newFinishedEventBuilder(started);
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) BuildId(com.facebook.buck.model.BuildId)

Example 2 with BuildId

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

the class HttpArtifactCacheTest method errorTextReplaced.

@Test
public void errorTextReplaced() throws InterruptedException {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    final String cacheName = "http cache";
    final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
    final RuleKey otherRuleKey = new RuleKey("11111111111111111111111111111111");
    final String data = "data";
    final AtomicBoolean consoleEventReceived = new AtomicBoolean(false);
    argsBuilder.setCacheName(cacheName).setProjectFilesystem(filesystem).setBuckEventBus(new BuckEventBus(new IncrementingFakeClock(), new BuildId()) {

        @Override
        public void post(BuckEvent event) {
            if (event instanceof ConsoleEvent) {
                consoleEventReceived.set(true);
                ConsoleEvent consoleEvent = (ConsoleEvent) event;
                assertThat(consoleEvent.getMessage(), Matchers.containsString(cacheName));
                assertThat(consoleEvent.getMessage(), Matchers.containsString("incorrect key name"));
            }
        }
    }).setFetchClient(withMakeRequest((path, requestBuilder) -> {
        Request request = requestBuilder.url(SERVER + path).build();
        Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(HttpURLConnection.HTTP_OK).body(createResponseBody(ImmutableSet.of(otherRuleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)), data)).build();
        return new OkHttpResponseWrapper(response);
    }));
    HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
    Path output = Paths.get("output/file");
    CacheResult result = cache.fetch(ruleKey, LazyPath.ofInstance(output));
    assertEquals(CacheResultType.ERROR, result.getType());
    assertEquals(Optional.empty(), filesystem.readFileIfItExists(output));
    assertTrue(consoleEventReceived.get());
    cache.close();
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) BuckEventBus(com.facebook.buck.event.BuckEventBus) HttpURLConnection(java.net.HttpURLConnection) DataInputStream(java.io.DataInputStream) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BuckEvent(com.facebook.buck.event.BuckEvent) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) BuildId(com.facebook.buck.model.BuildId) RuleKey(com.facebook.buck.rules.RuleKey) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Response(okhttp3.Response) ByteSource(com.google.common.io.ByteSource) HttpResponse(com.facebook.buck.slb.HttpResponse) Path(java.nio.file.Path) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) HttpService(com.facebook.buck.slb.HttpService) Before(org.junit.Before) Charsets(com.google.common.base.Charsets) Request(okhttp3.Request) Buffer(okio.Buffer) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertTrue(org.junit.Assert.assertTrue) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Matchers(org.hamcrest.Matchers) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) List(java.util.List) Paths(java.nio.file.Paths) LazyPath(com.facebook.buck.io.LazyPath) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) Optional(java.util.Optional) Protocol(okhttp3.Protocol) Assert.assertEquals(org.junit.Assert.assertEquals) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Path(java.nio.file.Path) LazyPath(com.facebook.buck.io.LazyPath) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Request(okhttp3.Request) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) BuckEvent(com.facebook.buck.event.BuckEvent) Response(okhttp3.Response) HttpResponse(com.facebook.buck.slb.HttpResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BuildId(com.facebook.buck.model.BuildId) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) Test(org.junit.Test)

Example 3 with BuildId

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

the class BuildIdSamplerTest method testAcceptHalf.

@Test
public void testAcceptHalf() {
    BuildIdSampler sampler = new BuildIdSampler(SampleRate.of(0.5f));
    for (BuildId buildId : buildIdToExpectedHashMap.keySet()) {
        boolean shouldAccept = buildIdToExpectedHashMap.get(buildId) < 0.5f;
        assertThat(String.format("BuildId %s", buildId), sampler.apply(buildId), Matchers.equalTo(shouldAccept));
    }
}
Also used : BuildId(com.facebook.buck.model.BuildId) Test(org.junit.Test)

Example 4 with BuildId

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

the class Main method runMainThenExit.

/* Define all error handling surrounding main command */
private void runMainThenExit(String[] args, Optional<NGContext> context, final long initTimestamp) {
    installUncaughtExceptionHandler(context);
    Path projectRoot = Paths.get(".");
    int exitCode = FAIL_EXIT_CODE;
    BuildId buildId = getBuildId(context);
    // Only post an overflow event if Watchman indicates a fresh instance event
    // after our initial query.
    WatchmanWatcher.FreshInstanceAction watchmanFreshInstanceAction = daemon == null ? WatchmanWatcher.FreshInstanceAction.NONE : WatchmanWatcher.FreshInstanceAction.POST_OVERFLOW_EVENT;
    // Get the client environment, either from this process or from the Nailgun context.
    ImmutableMap<String, String> clientEnvironment = getClientEnvironment(context);
    try {
        CommandMode commandMode = CommandMode.RELEASE;
        exitCode = runMainWithExitCode(buildId, projectRoot, context, clientEnvironment, commandMode, watchmanFreshInstanceAction, initTimestamp, args);
    } catch (IOException e) {
        if (e.getMessage().startsWith("No space left on device")) {
            (new Console(Verbosity.STANDARD_INFORMATION, stdOut, stdErr, new Ansi(AnsiEnvironmentChecking.environmentSupportsAnsiEscapes(platform, clientEnvironment)))).printBuildFailure(e.getMessage());
        } else {
            LOG.error(e);
        }
    } catch (HumanReadableException e) {
        Console console = new Console(Verbosity.STANDARD_INFORMATION, stdOut, stdErr, new Ansi(AnsiEnvironmentChecking.environmentSupportsAnsiEscapes(platform, clientEnvironment)));
        console.printBuildFailure(e.getHumanReadableErrorMessage());
    } catch (InterruptionFailedException e) {
        // Command could not be interrupted.
        if (context.isPresent()) {
            // Exit process to halt command execution.
            context.get().getNGServer().shutdown(true);
        }
    } catch (BuckIsDyingException e) {
        LOG.warn(e, "Fallout because buck was already dying");
    } catch (Throwable t) {
        LOG.error(t, "Uncaught exception at top level");
    } finally {
        LOG.debug("Done.");
        LogConfig.flushLogs();
        // Exit explicitly so that non-daemon threads (of which we use many) don't
        // keep the VM alive.
        System.exit(exitCode);
    }
}
Also used : ClassPath(com.google.common.reflect.ClassPath) Path(java.nio.file.Path) CommandMode(com.facebook.buck.util.environment.CommandMode) IOException(java.io.IOException) WatchmanWatcher(com.facebook.buck.util.WatchmanWatcher) InterruptionFailedException(com.facebook.buck.util.InterruptionFailedException) BuildId(com.facebook.buck.model.BuildId) BuckIsDyingException(com.facebook.buck.util.BuckIsDyingException) HumanReadableException(com.facebook.buck.util.HumanReadableException) Console(com.facebook.buck.util.Console) Ansi(com.facebook.buck.util.Ansi)

Example 5 with BuildId

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

the class MissingSymbolsHandler method createListener.

/**
   * Instantiate a MissingSymbolsHandler and wrap it in a listener that calls it on the appropriate
   * events. This is done as part of the global listener setup in Main, and it's the entry point
   * into most of the dependency autodetection code.
   */
public static BuckEventListener createListener(ProjectFilesystem projectFilesystem, ImmutableSet<Description<?>> descriptions, BuckConfig config, BuckEventBus buckEventBus, Console console, JavacOptions javacOptions, ImmutableMap<String, String> environment) {
    final MissingSymbolsHandler missingSymbolsHandler = create(projectFilesystem, descriptions, config, buckEventBus, console, javacOptions, environment);
    final Multimap<BuildId, MissingSymbolEvent> missingSymbolEvents = HashMultimap.create();
    BuckEventListener missingSymbolsListener = new BuckEventListener() {

        @Override
        public void outputTrace(BuildId buildId) {
        // If we put {@link #printNeededDependencies} here, it's output won't be visible in buckd.
        // Instead, we listen for BuildEvent.Finished, below.
        }

        @Subscribe
        public void onMissingSymbol(MissingSymbolEvent event) {
            missingSymbolEvents.put(event.getBuildId(), event);
        }

        @Subscribe
        public void onBuildFinished(BuildEvent.Finished event) {
            // Shortcircuit if there aren't any failures.
            if (missingSymbolEvents.get(event.getBuildId()).isEmpty()) {
                return;
            }
            try {
                missingSymbolsHandler.printNeededDependencies(missingSymbolEvents.get(event.getBuildId()));
            } catch (InterruptedException e) {
                LOG.debug(e, "Missing symbols handler did not complete in time.");
            } catch (RuntimeException e) {
                if (e.getCause() instanceof ClosedByInterruptException) {
                    LOG.debug(e.getCause(), "Missing symbols handler did not complete in time.");
                } else {
                    throw e;
                }
            }
            missingSymbolEvents.removeAll(event.getBuildId());
        }
    };
    return missingSymbolsListener;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) BuildId(com.facebook.buck.model.BuildId) MissingSymbolEvent(com.facebook.buck.event.MissingSymbolEvent) BuckEventListener(com.facebook.buck.event.BuckEventListener)

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