Search in sources :

Example 31 with Pair

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

the class OfflineScribeLoggerTest method sendStoredLogs.

@Test
public void sendStoredLogs() throws Exception {
    final ImmutableList<String> blacklistCategories = ImmutableList.of();
    final int maxScribeOfflineLogsKB = 2;
    final ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
    final Path logDir = filesystem.getBuckPaths().getOfflineLogDir();
    final ObjectMapper objectMapper = ObjectMappers.newDefaultInstance();
    final String[] ids = { "test1", "test2", "test3" };
    final String[] uniqueCategories = { "cat1", "cat2" };
    final String[] categories = { uniqueCategories[0], uniqueCategories[1], uniqueCategories[0] };
    final String testCategory = "test_category";
    final String line = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    final List<Pair<String, Iterable<String>>> sentData = new ArrayList<>();
    final ScribeLogger succeeddingLogger = new ScribeLogger() {

        @Override
        public ListenableFuture<Void> log(String category, Iterable<String> lines) {
            if (!category.equals(testCategory)) {
                sentData.add(new Pair<>(category, lines));
            }
            return Futures.immediateFuture(null);
        }

        @Override
        public void close() throws IOException {
        }
    };
    // Create 3 dummy logfiles - each will have 3 categories x 4 lines ~ 0.9KB. Hence, when reading
    // and sending the logger should stop after 2 of those 3 files (we set the limit to 2KB).
    filesystem.mkdirs(logDir);
    for (String id : ids) {
        File log = filesystem.resolve(logDir.resolve(LOGFILE_PREFIX + id + LOGFILE_SUFFIX)).toFile();
        BufferedOutputStream logFileStoreStream = new BufferedOutputStream(new FileOutputStream(log));
        for (String category : categories) {
            byte[] scribeData = objectMapper.writeValueAsString(ScribeData.builder().setCategory(category).setLines(ImmutableList.of(line, line, line, line)).build()).getBytes(Charsets.UTF_8);
            logFileStoreStream.write(scribeData);
        }
        logFileStoreStream.close();
    }
    // Get the logger and trigger sending with dummy succeeding log().
    OfflineScribeLogger offlineLogger = new OfflineScribeLogger(succeeddingLogger, blacklistCategories, maxScribeOfflineLogsKB, filesystem, objectMapper, BuckEventBusFactory.newInstance(), new BuildId("sendingLogger"));
    offlineLogger.log(testCategory, ImmutableList.of("line1", "line2"));
    offlineLogger.close();
    //Check read&sent data is as expected - for first category we expect clustered 8 lines from 2x4.
    assertEquals(4, sentData.size());
    final String[] expectedCategories = ObjectArrays.concat(uniqueCategories, uniqueCategories, String.class);
    final String[] seenCategories = new String[sentData.size()];
    for (int i = 0; i < sentData.size(); i++) {
        seenCategories[i] = sentData.get(i).getFirst();
        int expectedCount = (sentData.get(i).getFirst().equals(uniqueCategories[0])) ? 8 : 4;
        assertThat(sentData.get(i).getSecond(), Matchers.allOf(everyItem(equalTo(line)), IsIterableWithSize.<String>iterableWithSize(expectedCount)));
    }
    assertThat(seenCategories, arrayContainingInAnyOrder(expectedCategories));
    // Check that oldest log was not removed (due to exceeding the byte limit when reading&sending).
    ImmutableSortedSet<Path> logs = filesystem.getMtimeSortedMatchingDirectoryContents(logDir, LOGFILE_PATTERN);
    Path notRemovedLog = filesystem.resolve(logDir.resolve(LOGFILE_PREFIX + ids[0] + LOGFILE_SUFFIX));
    assertThat(logs, Matchers.allOf(hasItem(notRemovedLog), IsIterableWithSize.<Path>iterableWithSize(1)));
}
Also used : Path(java.nio.file.Path) FluentIterable(com.google.common.collect.FluentIterable) ArrayList(java.util.ArrayList) ScribeLogger(com.facebook.buck.util.network.ScribeLogger) FakeFailingScribeLogger(com.facebook.buck.util.network.FakeFailingScribeLogger) BuildId(com.facebook.buck.model.BuildId) FileOutputStream(java.io.FileOutputStream) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Pair(com.facebook.buck.model.Pair) Test(org.junit.Test)

Example 32 with Pair

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

the class PrebuiltAppleFramework method getNativeLinkableInput.

@Override
public NativeLinkableInput getNativeLinkableInput(CxxPlatform cxxPlatform, Linker.LinkableDepType type) throws NoSuchBuildTargetException {
    Pair<Flavor, Linker.LinkableDepType> key = new Pair<>(cxxPlatform.getFlavor(), type);
    NativeLinkableInput input = nativeLinkableCache.get(key);
    if (input == null) {
        input = getNativeLinkableInputUncached(cxxPlatform, type);
        nativeLinkableCache.put(key, input);
    }
    return input;
}
Also used : NativeLinkableInput(com.facebook.buck.cxx.NativeLinkableInput) Flavor(com.facebook.buck.model.Flavor) Pair(com.facebook.buck.model.Pair)

Aggregations

Pair (com.facebook.buck.model.Pair)32 Path (java.nio.file.Path)15 SourcePath (com.facebook.buck.rules.SourcePath)11 ImmutableMap (com.google.common.collect.ImmutableMap)10 BuildTarget (com.facebook.buck.model.BuildTarget)8 BuildRule (com.facebook.buck.rules.BuildRule)8 HumanReadableException (com.facebook.buck.util.HumanReadableException)8 Optional (java.util.Optional)8 ImmutableSet (com.google.common.collect.ImmutableSet)7 IOException (java.io.IOException)7 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)6 PathSourcePath (com.facebook.buck.rules.PathSourcePath)6 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)6 ImmutableList (com.google.common.collect.ImmutableList)6 Test (org.junit.Test)6 Flavor (com.facebook.buck.model.Flavor)5 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)5 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 NoSuchBuildTargetException (com.facebook.buck.parser.NoSuchBuildTargetException)4 Elf (com.facebook.buck.cxx.elf.Elf)3