diff options
Diffstat (limited to 'src/test/java/com/google/devtools/build')
3 files changed, 56 insertions, 56 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/actions/DigestUtilsTest.java b/src/test/java/com/google/devtools/build/lib/actions/DigestUtilsTest.java index 94d6aa7c93..70d2f40b22 100644 --- a/src/test/java/com/google/devtools/build/lib/actions/DigestUtilsTest.java +++ b/src/test/java/com/google/devtools/build/lib/actions/DigestUtilsTest.java @@ -25,6 +25,7 @@ import com.google.devtools.build.lib.testutil.TestThread; import com.google.devtools.build.lib.testutil.TestUtils; import com.google.devtools.build.lib.util.BlazeClock; import com.google.devtools.build.lib.vfs.FileSystem; +import com.google.devtools.build.lib.vfs.FileSystem.HashFunction; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; @@ -34,6 +35,7 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import java.io.IOException; +import java.util.Arrays; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -43,8 +45,9 @@ import java.util.concurrent.TimeUnit; @RunWith(JUnit4.class) public class DigestUtilsTest { - private static void assertMd5CalculationConcurrency(boolean expectConcurrent, - final boolean fastDigest, final int fileSize1, final int fileSize2) throws Exception { + private static void assertDigestCalculationConcurrency(boolean expectConcurrent, + final boolean fastDigest, final int fileSize1, final int fileSize2, + HashFunction hf) throws Exception { final CountDownLatch barrierLatch = new CountDownLatch(2); // Used to block test threads. final CountDownLatch readyLatch = new CountDownLatch(1); // Used to block main thread. @@ -64,16 +67,26 @@ public class DigestUtilsTest { } @Override - protected String getFastDigestFunctionType(Path path) { - return "MD5"; + protected byte[] getSHA1Digest(Path path) throws IOException { + try { + barrierLatch.countDown(); + readyLatch.countDown(); + // Either both threads will be inside getSHA1Digest at the same time or they + // both will be blocked. + barrierLatch.await(); + } catch (Exception e) { + throw new IOException(e); + } + return super.getSHA1Digest(path); } @Override - protected byte[] getFastDigest(Path path) throws IOException { - return fastDigest ? super.getMD5Digest(path) : null; + protected byte[] getFastDigest(Path path, HashFunction hashFunction) throws IOException { + return fastDigest ? super.getDigest(path, hashFunction) : null; } }; + FileSystem.setDigestFunctionForTesting(hf); final Path myFile1 = myfs.getPath("/f1.dat"); final Path myFile2 = myfs.getPath("/f2.dat"); FileSystemUtils.writeContentAsLatin1(myFile1, Strings.repeat("a", fileSize1)); @@ -111,13 +124,15 @@ public class DigestUtilsTest { * so machines with rotating drives don't become unusable. */ @Test - public void testMd5CalculationConcurrency() throws Exception { - assertMd5CalculationConcurrency(true, true, 4096, 4096); - assertMd5CalculationConcurrency(true, true, 4097, 4097); - assertMd5CalculationConcurrency(true, false, 4096, 4096); - assertMd5CalculationConcurrency(false, false, 4097, 4097); - assertMd5CalculationConcurrency(true, false, 1024, 4097); - assertMd5CalculationConcurrency(true, false, 1024, 1024); + public void testCalculationConcurrency() throws Exception { + for (HashFunction hf : Arrays.asList(HashFunction.MD5, HashFunction.SHA1)) { + assertDigestCalculationConcurrency(true, true, 4096, 4096, hf); + assertDigestCalculationConcurrency(true, true, 4097, 4097, hf); + assertDigestCalculationConcurrency(true, false, 4096, 4096, hf); + assertDigestCalculationConcurrency(false, false, 4097, 4097, hf); + assertDigestCalculationConcurrency(true, false, 1024, 4097, hf); + assertDigestCalculationConcurrency(true, false, 1024, 1024, hf); + } } @Test @@ -125,21 +140,19 @@ public class DigestUtilsTest { final byte[] malformed = {0, 0, 0}; FileSystem myFS = new InMemoryFileSystem(BlazeClock.instance()) { @Override - protected String getFastDigestFunctionType(Path path) { - return "MD5"; - } - - @Override - protected byte[] getFastDigest(Path path) throws IOException { - // MD5 digests are supposed to be 16 bytes. + protected byte[] getFastDigest(Path path, HashFunction hashFunction) throws IOException { + // Digest functions have more than 3 bytes, usually at least 16. return malformed; } }; Path path = myFS.getPath("/file"); FileSystemUtils.writeContentAsLatin1(path, "a"); - byte[] result = DigestUtils.getDigestOrFail(path, 1); - assertArrayEquals(path.getMD5Digest(), result); - assertNotSame(malformed, result); - assertEquals(16, result.length); + for (HashFunction hf : Arrays.asList(HashFunction.MD5, HashFunction.SHA1)) { + FileSystem.setDigestFunctionForTesting(hf); + byte[] result = DigestUtils.getDigestOrFail(path, 1); + assertArrayEquals(path.getDigest(), result); + assertNotSame(malformed, result); + assertTrue(path.isValidDigest(result)); + } } } diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/ArtifactFunctionTestCase.java b/src/test/java/com/google/devtools/build/lib/skyframe/ArtifactFunctionTestCase.java index e3c6a97a43..0ccaeaf559 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/ArtifactFunctionTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/ArtifactFunctionTestCase.java @@ -28,6 +28,7 @@ import com.google.devtools.build.lib.testutil.TestConstants; import com.google.devtools.build.lib.testutil.TestRuleClassProvider; import com.google.devtools.build.lib.testutil.TestUtils; import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; +import com.google.devtools.build.lib.vfs.FileSystem.HashFunction; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; @@ -158,13 +159,8 @@ abstract class ArtifactFunctionTestCase { /** InMemoryFileSystem that can pretend to do a fast digest. */ protected class CustomInMemoryFs extends InMemoryFileSystem { @Override - protected String getFastDigestFunctionType(Path path) { - return fastDigest ? "MD5" : null; - } - - @Override - protected byte[] getFastDigest(Path path) throws IOException { - return fastDigest ? getMD5Digest(path) : null; + protected byte[] getFastDigest(Path path, HashFunction hashFunction) throws IOException { + return fastDigest ? getDigest(path, hashFunction) : null; } } } diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/FileFunctionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/FileFunctionTest.java index 91dbc6c4ae..5eb13b326d 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/FileFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/FileFunctionTest.java @@ -52,6 +52,7 @@ import com.google.devtools.build.lib.util.Preconditions; import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; import com.google.devtools.build.lib.vfs.FileStatus; import com.google.devtools.build.lib.vfs.FileSystem; +import com.google.devtools.build.lib.vfs.FileSystem.HashFunction; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; @@ -95,13 +96,13 @@ public class FileFunctionTest { private Path pkgRoot; private Path outputBase; private PathPackageLocator pkgLocator; - private boolean fastMd5; + private boolean fastDigest; private ManualClock manualClock; private RecordingDifferencer differencer; @Before public final void createFsAndRoot() throws Exception { - fastMd5 = true; + fastDigest = true; manualClock = new ManualClock(); createFsAndRoot(new CustomInMemoryFs(manualClock)); } @@ -433,12 +434,7 @@ public class FileFunctionTest { createFsAndRoot( new CustomInMemoryFs(manualClock) { @Override - protected String getFastDigestFunctionType(Path path) { - return "magic"; - } - - @Override - protected byte[] getFastDigest(Path path) throws IOException { + protected byte[] getFastDigest(Path path, HashFunction hf) throws IOException { return digest; } }); @@ -479,7 +475,7 @@ public class FileFunctionTest { createFsAndRoot( new CustomInMemoryFs(manualClock) { @Override - protected byte[] getFastDigest(Path path) { + protected byte[] getFastDigest(Path path, HashFunction hf) { return path.getBaseName().equals("unreadable") ? expectedDigest : null; } }); @@ -494,7 +490,7 @@ public class FileFunctionTest { @Test public void testFileModificationModTime() throws Exception { - fastMd5 = false; + fastDigest = false; Path p = file("file"); FileValue a = valueForPath(p); p.setLastModifiedTime(42); @@ -504,7 +500,7 @@ public class FileFunctionTest { @Test public void testFileModificationDigest() throws Exception { - fastMd5 = true; + fastDigest = true; Path p = file("file"); FileValue a = valueForPath(p); FileSystemUtils.writeContentAsLatin1(p, "goop"); @@ -516,9 +512,9 @@ public class FileFunctionTest { public void testModTimeVsDigest() throws Exception { Path p = file("somefile", "fizzley"); - fastMd5 = true; + fastDigest = true; FileValue aMd5 = valueForPath(p); - fastMd5 = false; + fastDigest = false; FileValue aModTime = valueForPath(p); assertThat(aModTime).isNotEqualTo(aMd5); new EqualsTester().addEqualityGroup(aMd5).addEqualityGroup(aModTime).testEquals(); @@ -560,7 +556,7 @@ public class FileFunctionTest { @Test public void testSymlinkTargetContentsChangeModTime() throws Exception { - fastMd5 = false; + fastDigest = false; Path fooPath = file("foo"); FileSystemUtils.writeContentAsLatin1(fooPath, "foo"); Path p = symlink("symlink", "foo"); @@ -572,7 +568,7 @@ public class FileFunctionTest { @Test public void testSymlinkTargetContentsChangeDigest() throws Exception { - fastMd5 = true; + fastDigest = true; Path fooPath = file("foo"); FileSystemUtils.writeContentAsLatin1(fooPath, "foo"); Path p = symlink("symlink", "foo"); @@ -830,14 +826,14 @@ public class FileFunctionTest { assertArrayEquals(digest, value.getDigest()); // Digest is cached -- no filesystem access. assertEquals(expectedCalls, digestCalls.get()); - fastMd5 = false; + fastDigest = false; digestCalls.set(0); value = valueForPath(file); // No new digest calls. assertEquals(0, digestCalls.get()); assertNull(value.getDigest()); assertEquals(0, digestCalls.get()); - fastMd5 = true; + fastDigest = true; Path dir = directory("directory"); try { assertNull(valueForPath(dir).getDigest()); @@ -932,7 +928,7 @@ public class FileFunctionTest { fs.stubStat(path("a"), inconsistentParentFileStatus); // Disable fast-path md5 so that we don't try try to md5 the "a" (since it actually physically // is a directory). - fastMd5 = false; + fastDigest = false; SequentialBuildDriver driver = makeDriver(); SkyKey skyKey = skyKey("a/b"); EvaluationResult<FileValue> result = @@ -1624,21 +1620,16 @@ public class FileFunctionTest { super(manualClock); } - @Override - protected String getFastDigestFunctionType(Path path) { - return fastMd5 ? "MD5" : null; - } - public void stubFastDigestError(Path path, IOException error) { stubbedFastDigestErrors.put(path, error); } @Override - protected byte[] getFastDigest(Path path) throws IOException { + protected byte[] getFastDigest(Path path, HashFunction hashFunction) throws IOException { if (stubbedFastDigestErrors.containsKey(path)) { throw stubbedFastDigestErrors.get(path); } - return fastMd5 ? getMD5Digest(path) : null; + return fastDigest ? getDigest(path) : null; } public void stubStat(Path path, @Nullable FileStatus stubbedResult) { |