Search in sources :

Example 31 with FakeFileHashCache

use of com.facebook.buck.testutil.FakeFileHashCache in project buck by facebook.

the class DirArtifactCacheTest method testNoStoreMisses.

@Test
public void testNoStoreMisses() throws IOException {
    Path cacheDir = tmpDir.newFolder();
    Path fileX = tmpDir.newFile("x");
    Path fileY = tmpDir.newFile("y");
    Path fileZ = tmpDir.newFile("z");
    fileHashCache = new FakeFileHashCache(ImmutableMap.of(fileX, HashCode.fromInt(0), fileY, HashCode.fromInt(1), fileZ, HashCode.fromInt(2)));
    dirArtifactCache = new DirArtifactCache("dir", new ProjectFilesystem(cacheDir), Paths.get("."), /* doStore */
    false, /* maxCacheSizeBytes */
    Optional.of(0L));
    Files.write(fileX, "x".getBytes(UTF_8));
    Files.write(fileY, "y".getBytes(UTF_8));
    Files.write(fileZ, "z".getBytes(UTF_8));
    BuildRule inputRuleX = new BuildRuleForTest(fileX);
    BuildRule inputRuleY = new BuildRuleForTest(fileY);
    BuildRule inputRuleZ = new BuildRuleForTest(fileZ);
    assertFalse(inputRuleX.equals(inputRuleY));
    assertFalse(inputRuleX.equals(inputRuleZ));
    assertFalse(inputRuleY.equals(inputRuleZ));
    BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    ruleResolver.addToIndex(inputRuleX);
    ruleResolver.addToIndex(inputRuleY);
    ruleResolver.addToIndex(inputRuleZ);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
    DefaultRuleKeyFactory fakeRuleKeyFactory = new DefaultRuleKeyFactory(0, fileHashCache, resolver, ruleFinder);
    RuleKey ruleKeyX = fakeRuleKeyFactory.build(inputRuleX);
    RuleKey ruleKeyY = fakeRuleKeyFactory.build(inputRuleY);
    RuleKey ruleKeyZ = fakeRuleKeyFactory.build(inputRuleZ);
    assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
    assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyY, LazyPath.ofInstance(fileY)).getType());
    assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyZ, LazyPath.ofInstance(fileZ)).getType());
    dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyX).build(), BorrowablePath.notBorrowablePath(fileX));
    dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyY).build(), BorrowablePath.notBorrowablePath(fileY));
    dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyZ).build(), BorrowablePath.notBorrowablePath(fileZ));
    Files.delete(fileX);
    Files.delete(fileY);
    Files.delete(fileZ);
    assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
    assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyY, LazyPath.ofInstance(fileY)).getType());
    assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyZ, LazyPath.ofInstance(fileZ)).getType());
    assertEquals(inputRuleX, new BuildRuleForTest(fileX));
    assertEquals(inputRuleY, new BuildRuleForTest(fileY));
    assertEquals(inputRuleZ, new BuildRuleForTest(fileZ));
    assertEquals(0, cacheDir.toFile().listFiles().length);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 32 with FakeFileHashCache

use of com.facebook.buck.testutil.FakeFileHashCache in project buck by facebook.

the class DirArtifactCacheTest method testCacheStoresAndBorrowsPaths.

@Test
public void testCacheStoresAndBorrowsPaths() throws IOException {
    Path cacheDir = tmpDir.newFolder();
    Path fileX = tmpDir.newFile("x");
    Path fileY = tmpDir.newFile("y");
    fileHashCache = new FakeFileHashCache(ImmutableMap.of(fileX, HashCode.fromInt(0), fileY, HashCode.fromInt(1)));
    dirArtifactCache = new DirArtifactCache("dir", new ProjectFilesystem(cacheDir), Paths.get("."), /* doStore */
    true, /* maxCacheSizeBytes */
    Optional.empty());
    Files.write(fileX, "x".getBytes(UTF_8));
    Files.write(fileY, "y".getBytes(UTF_8));
    BuildRule inputRuleX = new BuildRuleForTest(fileX);
    BuildRule inputRuleY = new BuildRuleForTest(fileY);
    assertFalse(inputRuleX.equals(inputRuleY));
    BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    ruleResolver.addToIndex(inputRuleX);
    ruleResolver.addToIndex(inputRuleY);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
    DefaultRuleKeyFactory fakeRuleKeyFactory = new DefaultRuleKeyFactory(0, fileHashCache, resolver, ruleFinder);
    RuleKey ruleKeyX = fakeRuleKeyFactory.build(inputRuleX);
    RuleKey ruleKeyY = fakeRuleKeyFactory.build(inputRuleY);
    dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyX).build(), BorrowablePath.borrowablePath(fileX));
    dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyY).build(), BorrowablePath.notBorrowablePath(fileY));
    assertThat(Files.exists(fileX), Matchers.is(false));
    assertThat(Files.exists(fileY), Matchers.is(true));
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 33 with FakeFileHashCache

use of com.facebook.buck.testutil.FakeFileHashCache in project buck by facebook.

the class DirArtifactCacheTest method testCacheStoreMultipleKeys.

@Test
public void testCacheStoreMultipleKeys() throws IOException {
    Path cacheDir = tmpDir.newFolder();
    Path fileX = tmpDir.newFile("x");
    fileHashCache = new FakeFileHashCache(ImmutableMap.of(fileX, HashCode.fromInt(0)));
    dirArtifactCache = new DirArtifactCache("dir", new ProjectFilesystem(cacheDir), Paths.get("."), /* doStore */
    true, /* maxCacheSizeBytes */
    Optional.empty());
    Files.write(fileX, "x".getBytes(UTF_8));
    RuleKey ruleKey1 = new RuleKey("aaaa");
    RuleKey ruleKey2 = new RuleKey("bbbb");
    dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKey1, ruleKey2).build(), BorrowablePath.notBorrowablePath(fileX));
    // Test that artifact is available via both keys.
    assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKey1, LazyPath.ofInstance(fileX)).getType());
    assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKey2, LazyPath.ofInstance(fileX)).getType());
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Example 34 with FakeFileHashCache

use of com.facebook.buck.testutil.FakeFileHashCache in project buck by facebook.

the class DirArtifactCacheTest method testCacheStoreOverwrite.

@Test
public void testCacheStoreOverwrite() throws IOException {
    Path cacheDir = tmpDir.newFolder();
    Path fileX = tmpDir.newFile("x");
    fileHashCache = new FakeFileHashCache(ImmutableMap.of(fileX, HashCode.fromInt(0)));
    dirArtifactCache = new DirArtifactCache("dir", new ProjectFilesystem(cacheDir), Paths.get("."), /* doStore */
    true, /* maxCacheSizeBytes */
    Optional.empty());
    Files.write(fileX, "x".getBytes(UTF_8));
    BuildRule inputRuleX = new BuildRuleForTest(fileX);
    BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    ruleResolver.addToIndex(inputRuleX);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
    RuleKey ruleKeyX = new DefaultRuleKeyFactory(0, fileHashCache, resolver, ruleFinder).build(inputRuleX);
    dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyX).build(), BorrowablePath.notBorrowablePath(fileX));
    // Overwrite.
    dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyX).build(), BorrowablePath.notBorrowablePath(fileX));
    assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
    assertEquals(inputRuleX, new BuildRuleForTest(fileX));
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 35 with FakeFileHashCache

use of com.facebook.buck.testutil.FakeFileHashCache in project buck by facebook.

the class DirArtifactCacheTest method testDeleteAfterStoreIfFull.

@Test
public void testDeleteAfterStoreIfFull() throws IOException {
    Path cacheDir = tmpDir.newFolder();
    Path fileX = tmpDir.newFile("x");
    Path fileY = tmpDir.newFile("y");
    Path fileZ = tmpDir.newFile("z");
    fileHashCache = new FakeFileHashCache(ImmutableMap.of(fileX, HashCode.fromInt(0), fileY, HashCode.fromInt(1), fileZ, HashCode.fromInt(2)));
    // The reason max size is 9 bytes is because a 1-byte entry actually takes 6 bytes to store.
    // If the cache trims the size down to 2/3 (6 bytes) every time it hits the max it means after
    // every store only the most recent artifact should be left.
    dirArtifactCache = new DirArtifactCache("dir", new ProjectFilesystem(cacheDir), cacheDir, /* doStore */
    true, /* maxCacheSizeBytes */
    Optional.of(9L));
    Files.write(fileX, "x".getBytes(UTF_8));
    Files.write(fileY, "y".getBytes(UTF_8));
    Files.write(fileZ, "z".getBytes(UTF_8));
    BuildRule inputRuleX = new BuildRuleForTest(fileX);
    BuildRule inputRuleY = new BuildRuleForTest(fileY);
    BuildRule inputRuleZ = new BuildRuleForTest(fileZ);
    BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    ruleResolver.addToIndex(inputRuleX);
    ruleResolver.addToIndex(inputRuleY);
    ruleResolver.addToIndex(inputRuleZ);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
    DefaultRuleKeyFactory fakeRuleKeyFactory = new DefaultRuleKeyFactory(0, fileHashCache, resolver, ruleFinder);
    RuleKey ruleKeyX = fakeRuleKeyFactory.build(inputRuleX);
    RuleKey ruleKeyY = fakeRuleKeyFactory.build(inputRuleY);
    RuleKey ruleKeyZ = fakeRuleKeyFactory.build(inputRuleZ);
    dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyX).build(), BorrowablePath.notBorrowablePath(fileX));
    assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
    Files.setAttribute(dirArtifactCache.getPathForRuleKey(ruleKeyX, Optional.empty()), "lastAccessTime", FileTime.fromMillis(0));
    Files.setAttribute(dirArtifactCache.getPathForRuleKey(ruleKeyX, Optional.of(".metadata")), "lastAccessTime", FileTime.fromMillis(0));
    dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyY).build(), BorrowablePath.notBorrowablePath(fileY));
    assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
    assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKeyY, LazyPath.ofInstance(fileY)).getType());
    Files.setAttribute(dirArtifactCache.getPathForRuleKey(ruleKeyY, Optional.empty()), "lastAccessTime", FileTime.fromMillis(1000));
    Files.setAttribute(dirArtifactCache.getPathForRuleKey(ruleKeyY, Optional.of(".metadata")), "lastAccessTime", FileTime.fromMillis(1000));
    dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyZ).build(), BorrowablePath.notBorrowablePath(fileZ));
    assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
    assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyY, LazyPath.ofInstance(fileY)).getType());
    assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKeyZ, LazyPath.ofInstance(fileZ)).getType());
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Aggregations

FakeFileHashCache (com.facebook.buck.testutil.FakeFileHashCache)42 Test (org.junit.Test)38 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)24 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)24 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)22 RuleKey (com.facebook.buck.rules.RuleKey)22 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)22 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)20 Path (java.nio.file.Path)20 PathSourcePath (com.facebook.buck.rules.PathSourcePath)19 DefaultRuleKeyFactory (com.facebook.buck.rules.keys.DefaultRuleKeyFactory)18 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)16 SourcePath (com.facebook.buck.rules.SourcePath)16 AddToRuleKey (com.facebook.buck.rules.AddToRuleKey)14 BuildRule (com.facebook.buck.rules.BuildRule)14 FileHashCache (com.facebook.buck.util.cache.FileHashCache)14 HashCode (com.google.common.hash.HashCode)13 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)12 BuildTarget (com.facebook.buck.model.BuildTarget)11 BorrowablePath (com.facebook.buck.io.BorrowablePath)8