Search in sources :

Example 1 with LazyPath

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

the class ServedCacheIntegrationTest method testStoreAndFetchBorrowable.

@Test
public void testStoreAndFetchBorrowable() throws Exception {
    webServer = new WebServer(/* port */
    0, projectFilesystem, "/static/", MAPPER);
    webServer.updateAndStartIfNeeded(ArtifactCaches.newServedCache(createMockLocalConfig("[cache]", "dir = test-cache", "serve_local_cache = true", "served_local_cache_mode = readwrite"), projectFilesystem));
    ArtifactCache serverBackedCache = createArtifactCache(createMockLocalHttpCacheConfig(webServer.getPort().get()));
    RuleKey ruleKey = new RuleKey("00111222333444");
    ImmutableMap<String, String> metadata = ImmutableMap.of("some key", "some value");
    Path originalDataPath = tmpDir.newFile();
    String data = "you won't believe this!";
    projectFilesystem.writeContentsToPath(data, originalDataPath);
    LazyPath fetchedContents = LazyPath.ofInstance(tmpDir.newFile());
    CacheResult cacheResult = serverBackedCache.fetch(ruleKey, fetchedContents);
    assertThat(cacheResult.getType().isSuccess(), Matchers.is(false));
    serverBackedCache.store(ArtifactInfo.builder().addRuleKeys(ruleKey).setMetadata(metadata).build(), BorrowablePath.borrowablePath(originalDataPath));
    cacheResult = serverBackedCache.fetch(ruleKey, fetchedContents);
    assertThat(cacheResult.getType().isSuccess(), Matchers.is(true));
    assertThat(cacheResult.getMetadata(), Matchers.equalTo(metadata));
    assertThat(projectFilesystem.readFileIfItExists(fetchedContents.get()).get(), Matchers.equalTo(data));
}
Also used : Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) RuleKey(com.facebook.buck.rules.RuleKey) CacheResult(com.facebook.buck.artifact_cache.CacheResult) LazyPath(com.facebook.buck.io.LazyPath) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) Test(org.junit.Test)

Example 2 with LazyPath

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

the class ServedCacheIntegrationTest method whenNoCacheIsServedLookupsAreErrors.

@Test
public void whenNoCacheIsServedLookupsAreErrors() throws Exception {
    webServer = new WebServer(/* port */
    0, projectFilesystem, "/static/", MAPPER);
    webServer.updateAndStartIfNeeded(Optional.empty());
    ArtifactCache serverBackedCache = createArtifactCache(createMockLocalHttpCacheConfig(webServer.getPort().get()));
    LazyPath fetchedContents = LazyPath.ofInstance(tmpDir.newFile());
    CacheResult cacheResult = serverBackedCache.fetch(A_FILE_RULE_KEY, fetchedContents);
    assertThat(cacheResult.getType(), Matchers.equalTo(CacheResultType.ERROR));
}
Also used : CacheResult(com.facebook.buck.artifact_cache.CacheResult) LazyPath(com.facebook.buck.io.LazyPath) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) Test(org.junit.Test)

Example 3 with LazyPath

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

the class ServedCacheIntegrationTest method canSetArtifactCacheWithoutRestartingServer.

@Test
public void canSetArtifactCacheWithoutRestartingServer() throws Exception {
    webServer = new WebServer(/* port */
    0, projectFilesystem, "/static/", MAPPER);
    webServer.updateAndStartIfNeeded(Optional.empty());
    ArtifactCache serverBackedCache = createArtifactCache(createMockLocalHttpCacheConfig(webServer.getPort().get()));
    LazyPath fetchedContents = LazyPath.ofInstance(tmpDir.newFile());
    assertThat(serverBackedCache.fetch(A_FILE_RULE_KEY, fetchedContents).getType(), Matchers.equalTo(CacheResultType.ERROR));
    webServer.updateAndStartIfNeeded(Optional.of(dirCache));
    assertThat(serverBackedCache.fetch(A_FILE_RULE_KEY, fetchedContents).getType(), Matchers.equalTo(CacheResultType.HIT));
    webServer.updateAndStartIfNeeded(Optional.empty());
    assertThat(serverBackedCache.fetch(A_FILE_RULE_KEY, fetchedContents).getType(), Matchers.equalTo(CacheResultType.ERROR));
}
Also used : LazyPath(com.facebook.buck.io.LazyPath) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) Test(org.junit.Test)

Example 4 with LazyPath

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

the class ServedCacheIntegrationTest method testExceptionDuringTheRead.

@Test
public void testExceptionDuringTheRead() throws Exception {
    ProjectFilesystem throwingStreamFilesystem = new ProjectFilesystem(tmpDir.getRoot()) {

        private boolean throwingStreamServed = false;

        @Override
        public InputStream newFileInputStream(Path pathRelativeToProjectRoot) throws IOException {
            InputStream inputStream = super.newFileInputStream(pathRelativeToProjectRoot);
            if (!throwingStreamServed && pathRelativeToProjectRoot.toString().contains("outgoing_rulekey")) {
                throwingStreamServed = true;
                return new ThrowAfterXBytesStream(inputStream, 10L);
            }
            return inputStream;
        }
    };
    webServer = new WebServer(/* port */
    0, throwingStreamFilesystem, "/static/", MAPPER);
    webServer.updateAndStartIfNeeded(Optional.of(dirCache));
    ArtifactCache serverBackedCache = createArtifactCache(createMockLocalHttpCacheConfig(webServer.getPort().get()));
    LazyPath fetchedContents = LazyPath.ofInstance(tmpDir.newFile());
    CacheResult cacheResult = serverBackedCache.fetch(A_FILE_RULE_KEY, fetchedContents);
    assertThat(cacheResult.getType(), Matchers.equalTo(CacheResultType.ERROR));
    // Try again to make sure the exception didn't kill the server.
    cacheResult = serverBackedCache.fetch(A_FILE_RULE_KEY, fetchedContents);
    assertThat(cacheResult.getType(), Matchers.equalTo(CacheResultType.HIT));
}
Also used : Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) CacheResult(com.facebook.buck.artifact_cache.CacheResult) LazyPath(com.facebook.buck.io.LazyPath) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) Test(org.junit.Test)

Example 5 with LazyPath

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

the class ServedCacheIntegrationTest method testStoreAndFetchNotBorrowable.

@Test
public void testStoreAndFetchNotBorrowable() throws Exception {
    webServer = new WebServer(/* port */
    0, projectFilesystem, "/static/", MAPPER);
    webServer.updateAndStartIfNeeded(ArtifactCaches.newServedCache(createMockLocalConfig("[cache]", "dir = test-cache", "serve_local_cache = true", "served_local_cache_mode = readwrite"), projectFilesystem));
    ArtifactCache serverBackedCache = createArtifactCache(createMockLocalHttpCacheConfig(webServer.getPort().get()));
    RuleKey ruleKey = new RuleKey("00111222333444");
    ImmutableMap<String, String> metadata = ImmutableMap.of("some key", "some value");
    Path originalDataPath = tmpDir.newFile();
    String data = "you won't believe this!";
    projectFilesystem.writeContentsToPath(data, originalDataPath);
    LazyPath fetchedContents = LazyPath.ofInstance(tmpDir.newFile());
    CacheResult cacheResult = serverBackedCache.fetch(ruleKey, fetchedContents);
    assertThat(cacheResult.getType().isSuccess(), Matchers.is(false));
    serverBackedCache.store(ArtifactInfo.builder().addRuleKeys(ruleKey).setMetadata(metadata).build(), BorrowablePath.notBorrowablePath(originalDataPath));
    cacheResult = serverBackedCache.fetch(ruleKey, fetchedContents);
    assertThat(cacheResult.getType().isSuccess(), Matchers.is(true));
    assertThat(cacheResult.getMetadata(), Matchers.equalTo(metadata));
    assertThat(projectFilesystem.readFileIfItExists(fetchedContents.get()).get(), Matchers.equalTo(data));
}
Also used : Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) RuleKey(com.facebook.buck.rules.RuleKey) CacheResult(com.facebook.buck.artifact_cache.CacheResult) LazyPath(com.facebook.buck.io.LazyPath) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) 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