Search in sources :

Example 1 with ScribeLogger

use of com.facebook.buck.util.network.ScribeLogger 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)

Aggregations

ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 BuildId (com.facebook.buck.model.BuildId)1 Pair (com.facebook.buck.model.Pair)1 FakeFailingScribeLogger (com.facebook.buck.util.network.FakeFailingScribeLogger)1 ScribeLogger (com.facebook.buck.util.network.ScribeLogger)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FluentIterable (com.google.common.collect.FluentIterable)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1