Skip to content

Commit 453bb2c

Browse files
committed
Add test
1 parent 7e716b3 commit 453bb2c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/test/java/com/google/devtools/build/lib/remote/disk/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ java_test(
3030
"//src/test/java/com/google/devtools/build/lib:test_runner",
3131
"//src/test/java/com/google/devtools/build/lib/testutil:TestUtils",
3232
"//src/test/java/com/google/devtools/build/lib/testutil:external_file_system_lock",
33+
"//src/test/java/com/google/devtools/build/lib/vfs/util:util_internal",
3334
"//third_party:error_prone_annotations",
3435
"//third_party:guava",
3536
"//third_party:junit4",

src/test/java/com/google/devtools/build/lib/remote/disk/DiskCacheClientTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,24 @@
3333
import com.google.devtools.build.lib.remote.common.OutputDigestMismatchException;
3434
import com.google.devtools.build.lib.remote.common.RemoteCacheClient.ActionKey;
3535
import com.google.devtools.build.lib.remote.util.DigestUtil;
36+
import com.google.devtools.build.lib.testutil.TestUtils;
3637
import com.google.devtools.build.lib.vfs.DigestHashFunction;
3738
import com.google.devtools.build.lib.vfs.FileSystem;
3839
import com.google.devtools.build.lib.vfs.FileSystemUtils;
3940
import com.google.devtools.build.lib.vfs.Path;
4041
import com.google.devtools.build.lib.vfs.SyscallCache;
4142
import com.google.devtools.build.lib.vfs.bazel.BazelHashFunctions;
4243
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
44+
import com.google.devtools.build.lib.vfs.util.FileSystems;
4345
import com.google.errorprone.annotations.CanIgnoreReturnValue;
4446
import com.google.protobuf.ByteString;
4547
import com.google.protobuf.Message;
4648
import java.io.IOException;
49+
import java.util.ArrayList;
50+
import java.util.concurrent.ExecutionException;
4751
import java.util.concurrent.ExecutorService;
4852
import java.util.concurrent.Executors;
53+
import java.util.concurrent.Future;
4954
import org.junit.Before;
5055
import org.junit.Test;
5156
import org.junit.runner.RunWith;
@@ -347,6 +352,44 @@ public void downloadActionResult_withReferencedTreeFileMissing_returnsNull() thr
347352
assertThat(result).isNull();
348353
}
349354

355+
@Test
356+
public void concurrentUploadDownload()
357+
throws IOException, ExecutionException, InterruptedException {
358+
var nativeDiskCacheDir = TestUtils.createUniqueTmpDir(FileSystems.getNativeFileSystem());
359+
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
360+
var nativeClient =
361+
new DiskCacheClient(
362+
nativeDiskCacheDir, DIGEST_UTIL, executor, /* verifyDownloads= */ false);
363+
var tasks = new ArrayList<Future<?>>();
364+
for (int attempt = 0; attempt < 100; attempt++) {
365+
var contentString = "file contents " + attempt;
366+
var contentBytes = ByteString.copyFromUtf8(contentString);
367+
var contentDigest = getDigest(contentString);
368+
// Start multiple concurrent upload/download tasks in an attempt to exercise the
369+
// interleaving in which both multiple uploadBlob calls initially see the entry as missing
370+
// and thus try to create it.
371+
for (int concurrentOp = 0; concurrentOp < 5; concurrentOp++) {
372+
tasks.add(
373+
executor.submit(
374+
() -> {
375+
nativeClient.uploadBlob(contentDigest, contentBytes);
376+
try (var out = ByteString.newOutput()) {
377+
getFromFuture(nativeClient.downloadBlob(contentDigest, out));
378+
var downloadedBytes = out.toByteString();
379+
assertThat(downloadedBytes).isEqualTo(contentBytes);
380+
} catch (CacheNotFoundException ignored) {
381+
// This task won the race over the upload task.
382+
}
383+
return null;
384+
}));
385+
}
386+
}
387+
for (var task : tasks) {
388+
task.get();
389+
}
390+
}
391+
}
392+
350393
private Tree getTreeWithFile(Digest fileDigest) {
351394
return Tree.newBuilder()
352395
.addChildren(Directory.newBuilder().addFiles(FileNode.newBuilder().setDigest(fileDigest)))

0 commit comments

Comments
 (0)