blob: 6056ab75fc75adcdf61ea25cbd00d127332ae8d9 [file]
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.vfs;
import static com.google.common.truth.Truth.assertThat;
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Tests for {@link DigestUtils}. */
@RunWith(JUnit4.class)
public final class DigestUtilsTest {
@After
public void tearDown() {
DigestUtils.configureCache(/*maximumSize=*/ 0);
}
@Test
public void testCache() throws Exception {
AtomicInteger getFastDigestCounter = new AtomicInteger(0);
AtomicInteger getDigestCounter = new AtomicInteger(0);
FileSystem tracingFileSystem =
new InMemoryFileSystem(DigestHashFunction.SHA256) {
@Override
public byte[] getFastDigest(PathFragment path) {
getFastDigestCounter.incrementAndGet();
return null;
}
@Override
public byte[] getDigest(PathFragment path) throws IOException {
getDigestCounter.incrementAndGet();
return super.getDigest(path);
}
};
DigestUtils.configureCache(/*maximumSize=*/ 100);
Path file = tracingFileSystem.getPath("/file.txt");
FileSystemUtils.writeContentAsLatin1(file, "some contents");
byte[] digest =
DigestUtils.getDigestWithManualFallback(file, SyscallCache.NO_CACHE, /* status= */ null);
assertThat(getFastDigestCounter.get()).isEqualTo(1);
assertThat(getDigestCounter.get()).isEqualTo(1);
assertThat(
DigestUtils.getDigestWithManualFallback(
file, SyscallCache.NO_CACHE, /* status= */ null))
.isEqualTo(digest);
assertThat(getFastDigestCounter.get()).isEqualTo(2);
assertThat(getDigestCounter.get()).isEqualTo(1); // Cached.
DigestUtils.clearCache();
assertThat(
DigestUtils.getDigestWithManualFallback(
file, SyscallCache.NO_CACHE, /* status= */ null))
.isEqualTo(digest);
assertThat(getFastDigestCounter.get()).isEqualTo(3);
assertThat(getDigestCounter.get()).isEqualTo(2); // Not cached.
}
@Test
public void cacheHitWithoutRestatingWhenTheCallerSuppliesTheStat() throws Exception {
AtomicInteger getDigestCounter = new AtomicInteger(0);
AtomicInteger statCounter = new AtomicInteger(0);
FileSystem tracingFileSystem =
new InMemoryFileSystem(DigestHashFunction.SHA256) {
@Override
public byte[] getDigest(PathFragment path) throws IOException {
getDigestCounter.incrementAndGet();
return super.getDigest(path);
}
@Override
public FileStatus stat(PathFragment path, boolean followSymlinks) throws IOException {
statCounter.incrementAndGet();
return super.stat(path, followSymlinks);
}
};
DigestUtils.configureCache(/* maximumSize= */ 100);
Path file = tracingFileSystem.getPath("/file.txt");
FileSystemUtils.writeContentAsLatin1(file, "some contents");
// Without a stat, DigestUtils has to stat the file itself to build the cache key.
byte[] digest = DigestUtils.manuallyComputeDigest(file);
assertThat(getDigestCounter.get()).isEqualTo(1);
assertThat(statCounter.get()).isEqualTo(1);
// A caller that already stat'ed the file gets the cached digest without a further stat, which
// only works if both spellings of the key agree.
FileStatus stat = file.stat();
getDigestCounter.set(0);
statCounter.set(0);
assertThat(DigestUtils.manuallyComputeDigest(file, stat)).isEqualTo(digest);
assertThat(getDigestCounter.get()).isEqualTo(0);
assertThat(statCounter.get()).isEqualTo(0);
}
@Test
public void manuallyComputeDigest() throws Exception {
byte[] digest = {1, 2, 3};
FileSystem noDigestFileSystem =
new InMemoryFileSystem(DigestHashFunction.SHA256) {
@Override
public byte[] getFastDigest(PathFragment path) {
throw new AssertionError("Unexpected call to getFastDigest");
}
@Override
public byte[] getDigest(PathFragment path) {
return digest;
}
};
Path file = noDigestFileSystem.getPath("/f.txt");
FileSystemUtils.writeContentAsLatin1(file, "contents");
assertThat(DigestUtils.manuallyComputeDigest(file)).isEqualTo(digest);
}
@Test
public void combineUnordered_commutative() {
byte[] a = {1, 2, 3};
byte[] b = {4, 5, 6};
assertThat(DigestUtils.combineUnordered(a.clone(), b.clone()))
.isEqualTo(DigestUtils.combineUnordered(b.clone(), a.clone()));
}
@Test
public void combineUnordered_noCancellation() {
byte[] a = {1, 2, 3};
assertThat(DigestUtils.combineUnordered(a.clone(), a.clone()))
.isNotEqualTo(new byte[] {0, 0, 0});
}
}