Search in sources :

Example 11 with LazyPath

use of com.facebook.buck.io.LazyPath in project buck by facebook.

the class AbstractNetworkCacheTest method testStoreCall.

private void testStoreCall(int expectStoreCallCount, Optional<Long> maxArtifactSizeBytes, int... artifactBytes) throws InterruptedException, IOException, ExecutionException {
    final AtomicInteger storeCallCount = new AtomicInteger(0);
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    ListeningExecutorService service = new FakeListeningExecutorService() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    };
    AbstractNetworkCache cache = new AbstractNetworkCache(NetworkCacheArgs.builder().setCacheName("AbstractNetworkCacheTest").setRepository("some_repository").setScheduleType("some_schedule_type").setFetchClient(EasyMock.createMock(HttpService.class)).setStoreClient(EasyMock.createMock(HttpService.class)).setDoStore(true).setProjectFilesystem(filesystem).setBuckEventBus(EasyMock.createMock(BuckEventBus.class)).setHttpWriteExecutorService(service).setErrorTextTemplate("super error message").setMaxStoreSizeBytes(maxArtifactSizeBytes).setDistributedBuildModeEnabled(false).build()) {

        @Override
        protected CacheResult fetchImpl(RuleKey ruleKey, LazyPath output, HttpArtifactCacheEvent.Finished.Builder eventBuilder) throws IOException {
            return null;
        }

        @Override
        protected void storeImpl(ArtifactInfo info, Path file, HttpArtifactCacheEvent.Finished.Builder eventBuilder) throws IOException {
            storeCallCount.incrementAndGet();
        }
    };
    for (int bytes : artifactBytes) {
        Path path = filesystem.getPathForRelativePath("topspin_" + this.getClass().getName());
        filesystem.writeBytesToPath(new byte[bytes], path);
        ListenableFuture<Void> future = cache.store(ArtifactInfo.builder().build(), BorrowablePath.notBorrowablePath(path));
        future.get();
    }
    Assert.assertEquals(expectStoreCallCount, storeCallCount.get());
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) Path(java.nio.file.Path) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) LazyPath(com.facebook.buck.io.LazyPath) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpService(com.facebook.buck.slb.HttpService) FakeListeningExecutorService(com.facebook.buck.util.concurrent.FakeListeningExecutorService) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) FakeListeningExecutorService(com.facebook.buck.util.concurrent.FakeListeningExecutorService)

Example 12 with LazyPath

use of com.facebook.buck.io.LazyPath in project buck by facebook.

the class TwoLevelArtifactCacheDecoratorTest method testMetadataIsNotShared.

@Test
public void testMetadataIsNotShared() throws InterruptedException, IOException {
    try (InMemoryArtifactCache inMemoryArtifactCache = new InMemoryArtifactCache();
        TwoLevelArtifactCacheDecorator twoLevelCache = new TwoLevelArtifactCacheDecorator(inMemoryArtifactCache, new ProjectFilesystem(tmp.getRoot()), BuckEventBusFactory.newInstance(), /* performTwoLevelStores */
        true, /* minimumTwoLevelStoredArtifactSize */
        0L, /* maximumTwoLevelStoredArtifactSize */
        Optional.empty())) {
        LazyPath dummyFile = LazyPath.ofInstance(tmp.newFile());
        final String testMetadataKey = "testMetaKey";
        twoLevelCache.store(ArtifactInfo.builder().addRuleKeys(dummyRuleKey).setMetadata(ImmutableMap.of(testMetadataKey, "value1")).build(), BorrowablePath.notBorrowablePath(dummyFile.get()));
        twoLevelCache.store(ArtifactInfo.builder().addRuleKeys(dummyRuleKey2).setMetadata(ImmutableMap.of(testMetadataKey, "value2")).build(), BorrowablePath.notBorrowablePath(dummyFile.get()));
        CacheResult fetch1 = twoLevelCache.fetch(dummyRuleKey, dummyFile);
        CacheResult fetch2 = twoLevelCache.fetch(dummyRuleKey2, dummyFile);
        // Content hashes should be the same
        assertEquals(fetch1.getMetadata().get(TwoLevelArtifactCacheDecorator.METADATA_KEY), fetch2.getMetadata().get(TwoLevelArtifactCacheDecorator.METADATA_KEY));
        // But the metadata shouldn't be shared
        assertNotEquals(fetch1.getMetadata().get(testMetadataKey), fetch2.getMetadata().get(testMetadataKey));
    }
}
Also used : LazyPath(com.facebook.buck.io.LazyPath) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Example 13 with LazyPath

use of com.facebook.buck.io.LazyPath in project buck by facebook.

the class TwoLevelArtifactCacheDecoratorTest method testCacheFetch.

@Test
public void testCacheFetch() throws InterruptedException, IOException {
    try (InMemoryArtifactCache inMemoryArtifactCache = new InMemoryArtifactCache();
        TwoLevelArtifactCacheDecorator twoLevelCache = new TwoLevelArtifactCacheDecorator(inMemoryArtifactCache, new ProjectFilesystem(tmp.getRoot()), BuckEventBusFactory.newInstance(), /* performTwoLevelStores */
        true, /* minimumTwoLevelStoredArtifactSize */
        0L, /* maximumTwoLevelStoredArtifactSize */
        Optional.empty())) {
        LazyPath dummyFile = LazyPath.ofInstance(tmp.newFile());
        assertThat(twoLevelCache.fetch(dummyRuleKey, dummyFile).getType(), Matchers.equalTo(CacheResultType.MISS));
        twoLevelCache.store(ArtifactInfo.builder().addRuleKeys(dummyRuleKey).build(), BorrowablePath.notBorrowablePath(dummyFile.get()));
        assertThat(twoLevelCache.fetch(dummyRuleKey, dummyFile).getType(), Matchers.equalTo(CacheResultType.HIT));
        twoLevelCache.store(ArtifactInfo.builder().addRuleKeys(dummyRuleKey2).build(), BorrowablePath.notBorrowablePath(dummyFile.get()));
        assertThat(twoLevelCache.fetch(dummyRuleKey2, dummyFile).getType(), Matchers.equalTo(CacheResultType.HIT));
        assertThat(inMemoryArtifactCache.getArtifactCount(), Matchers.equalTo(3));
    }
}
Also used : LazyPath(com.facebook.buck.io.LazyPath) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Example 14 with LazyPath

use of com.facebook.buck.io.LazyPath in project buck by facebook.

the class TwoLevelArtifactCacheDecoratorTest method testStoreThresholds.

private void testStoreThresholds(int artifactSize, int expectedArtifactsInCache) throws InterruptedException, IOException {
    try (InMemoryArtifactCache inMemoryArtifactCache = new InMemoryArtifactCache();
        TwoLevelArtifactCacheDecorator twoLevelCache = new TwoLevelArtifactCacheDecorator(inMemoryArtifactCache, new ProjectFilesystem(tmp.getRoot()), BuckEventBusFactory.newInstance(), /* performTwoLevelStores */
        true, /* minimumTwoLevelStoredArtifactSize */
        5L, /* maximumTwoLevelStoredArtifactSize */
        Optional.of(10L))) {
        LazyPath lazyPath = LazyPath.ofInstance(tmp.newFile());
        Files.write(lazyPath.get(), new byte[artifactSize]);
        twoLevelCache.store(ArtifactInfo.builder().addRuleKeys(dummyRuleKey).build(), BorrowablePath.notBorrowablePath(lazyPath.get()));
        assertThat(inMemoryArtifactCache.getArtifactCount(), Matchers.equalTo(expectedArtifactsInCache));
    }
}
Also used : LazyPath(com.facebook.buck.io.LazyPath) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem)

Example 15 with LazyPath

use of com.facebook.buck.io.LazyPath in project buck by facebook.

the class TwoLevelArtifactCacheDecoratorTest method testCanRead2LStoresIfStoresDisabled.

@Test
public void testCanRead2LStoresIfStoresDisabled() throws InterruptedException, IOException {
    try (InMemoryArtifactCache inMemoryArtifactCache = new InMemoryArtifactCache();
        TwoLevelArtifactCacheDecorator twoLevelCache = new TwoLevelArtifactCacheDecorator(inMemoryArtifactCache, new ProjectFilesystem(tmp.getRoot()), BuckEventBusFactory.newInstance(), /* performTwoLevelStores */
        true, /* minimumTwoLevelStoredArtifactSize */
        0L, /* maximumTwoLevelStoredArtifactSize */
        Optional.empty());
        TwoLevelArtifactCacheDecorator twoLevelCacheNoStore = new TwoLevelArtifactCacheDecorator(inMemoryArtifactCache, new ProjectFilesystem(tmp.getRoot()), BuckEventBusFactory.newInstance(), /* performTwoLevelStores */
        false, /* minimumTwoLevelStoredArtifactSize */
        0L, /* maximumTwoLevelStoredArtifactSize */
        Optional.empty())) {
        LazyPath dummyFile = LazyPath.ofInstance(tmp.newFile());
        twoLevelCache.store(ArtifactInfo.builder().addRuleKeys(dummyRuleKey).build(), BorrowablePath.notBorrowablePath(dummyFile.get()));
        assertThat(inMemoryArtifactCache.getArtifactCount(), Matchers.equalTo(2));
        assertThat(twoLevelCacheNoStore.fetch(dummyRuleKey, dummyFile).getType(), Matchers.equalTo(CacheResultType.HIT));
    }
}
Also used : LazyPath(com.facebook.buck.io.LazyPath) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Aggregations

LazyPath (com.facebook.buck.io.LazyPath)19 Test (org.junit.Test)12 Path (java.nio.file.Path)10 BorrowablePath (com.facebook.buck.io.BorrowablePath)9 ArtifactCache (com.facebook.buck.artifact_cache.ArtifactCache)8 CacheResult (com.facebook.buck.artifact_cache.CacheResult)8 RuleKey (com.facebook.buck.rules.RuleKey)6 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)5 OutputStream (java.io.OutputStream)3 HttpResponse (com.facebook.buck.slb.HttpResponse)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Request (okhttp3.Request)2 ArtifactCacheBuckConfig (com.facebook.buck.artifact_cache.ArtifactCacheBuckConfig)1 ArtifactInfo (com.facebook.buck.artifact_cache.ArtifactInfo)1 ArtifactMetadata (com.facebook.buck.artifact_cache.thrift.ArtifactMetadata)1 BuckCacheFetchRequest (com.facebook.buck.artifact_cache.thrift.BuckCacheFetchRequest)1 BuckCacheFetchResponse (com.facebook.buck.artifact_cache.thrift.BuckCacheFetchResponse)1 BuckCacheRequest (com.facebook.buck.artifact_cache.thrift.BuckCacheRequest)1 BuckCacheResponse (com.facebook.buck.artifact_cache.thrift.BuckCacheResponse)1