|
33 | 33 | import com.google.devtools.build.lib.remote.common.OutputDigestMismatchException; |
34 | 34 | import com.google.devtools.build.lib.remote.common.RemoteCacheClient.ActionKey; |
35 | 35 | import com.google.devtools.build.lib.remote.util.DigestUtil; |
| 36 | +import com.google.devtools.build.lib.testutil.TestUtils; |
36 | 37 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
37 | 38 | import com.google.devtools.build.lib.vfs.FileSystem; |
38 | 39 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
39 | 40 | import com.google.devtools.build.lib.vfs.Path; |
40 | 41 | import com.google.devtools.build.lib.vfs.SyscallCache; |
41 | 42 | import com.google.devtools.build.lib.vfs.bazel.BazelHashFunctions; |
42 | 43 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 44 | +import com.google.devtools.build.lib.vfs.util.FileSystems; |
43 | 45 | import com.google.errorprone.annotations.CanIgnoreReturnValue; |
44 | 46 | import com.google.protobuf.ByteString; |
45 | 47 | import com.google.protobuf.Message; |
46 | 48 | import java.io.IOException; |
| 49 | +import java.util.ArrayList; |
| 50 | +import java.util.concurrent.ExecutionException; |
47 | 51 | import java.util.concurrent.ExecutorService; |
48 | 52 | import java.util.concurrent.Executors; |
| 53 | +import java.util.concurrent.Future; |
49 | 54 | import org.junit.Before; |
50 | 55 | import org.junit.Test; |
51 | 56 | import org.junit.runner.RunWith; |
@@ -347,6 +352,44 @@ public void downloadActionResult_withReferencedTreeFileMissing_returnsNull() thr |
347 | 352 | assertThat(result).isNull(); |
348 | 353 | } |
349 | 354 |
|
| 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 | + |
350 | 393 | private Tree getTreeWithFile(Digest fileDigest) { |
351 | 394 | return Tree.newBuilder() |
352 | 395 | .addChildren(Directory.newBuilder().addFiles(FileNode.newBuilder().setDigest(fileDigest))) |
|
0 commit comments