2121import static org .mockito .Mockito .when ;
2222
2323import build .bazel .remote .execution .v2 .Digest ;
24+ import com .google .common .base .Supplier ;
2425import com .google .common .collect .ImmutableList ;
2526import com .google .common .collect .ImmutableMap ;
2627import com .google .common .collect .Maps ;
4344import com .google .devtools .build .lib .remote .util .DigestUtil ;
4445import com .google .devtools .build .lib .remote .util .InMemoryCacheClient ;
4546import com .google .devtools .build .lib .remote .util .StaticMetadataProvider ;
47+ import com .google .devtools .build .lib .remote .util .TempPathGenerator ;
4648import com .google .devtools .build .lib .vfs .DigestHashFunction ;
4749import com .google .devtools .build .lib .vfs .FileSystem ;
4850import com .google .devtools .build .lib .vfs .FileSystemUtils ;
@@ -70,6 +72,7 @@ public class RemoteActionInputFetcherTest {
7072 private static final DigestHashFunction HASH_FUNCTION = DigestHashFunction .SHA256 ;
7173
7274 private Path execRoot ;
75+ private TempPathGenerator tempPathGenerator ;
7376 private ArtifactRoot artifactRoot ;
7477 private RemoteOptions options ;
7578 private DigestUtil digestUtil ;
@@ -79,6 +82,9 @@ public void setUp() throws IOException {
7982 FileSystem fs = new InMemoryFileSystem (new JavaClock (), HASH_FUNCTION );
8083 execRoot = fs .getPath ("/exec" );
8184 execRoot .createDirectoryAndParents ();
85+ Path tempDir = fs .getPath ("/tmp" );
86+ tempDir .createDirectoryAndParents ();
87+ tempPathGenerator = new TempPathGenerator (tempDir );
8288 Path dev = fs .getPath ("/dev" );
8389 dev .createDirectory ();
8490 dev .setWritable (false );
@@ -98,7 +104,7 @@ public void testFetching() throws Exception {
98104 MetadataProvider metadataProvider = new StaticMetadataProvider (metadata );
99105 RemoteCache remoteCache = newCache (options , digestUtil , cacheEntries );
100106 RemoteActionInputFetcher actionInputFetcher =
101- new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot );
107+ new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot , tempPathGenerator );
102108
103109 // act
104110 wait (actionInputFetcher .prefetchFiles (metadata .keySet (), metadataProvider ));
@@ -121,7 +127,7 @@ public void testStagingVirtualActionInput() throws Exception {
121127 MetadataProvider metadataProvider = new StaticMetadataProvider (new HashMap <>());
122128 RemoteCache remoteCache = newCache (options , digestUtil , new HashMap <>());
123129 RemoteActionInputFetcher actionInputFetcher =
124- new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot );
130+ new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot , tempPathGenerator );
125131 VirtualActionInput a = ActionsTestUtil .createVirtualActionInput ("file1" , "hello world" );
126132
127133 // act
@@ -141,7 +147,7 @@ public void testStagingEmptyVirtualActionInput() throws Exception {
141147 MetadataProvider metadataProvider = new StaticMetadataProvider (new HashMap <>());
142148 RemoteCache remoteCache = newCache (options , digestUtil , new HashMap <>());
143149 RemoteActionInputFetcher actionInputFetcher =
144- new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot );
150+ new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot , tempPathGenerator );
145151
146152 // act
147153 wait (
@@ -164,7 +170,7 @@ public void testFileNotFound() throws Exception {
164170 MetadataProvider metadataProvider = new StaticMetadataProvider (metadata );
165171 RemoteCache remoteCache = newCache (options , digestUtil , new HashMap <>());
166172 RemoteActionInputFetcher actionInputFetcher =
167- new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot );
173+ new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot , tempPathGenerator );
168174
169175 // act
170176 assertThrows (
@@ -188,7 +194,7 @@ public void testIgnoreNoneRemoteFiles() throws Exception {
188194 MetadataProvider metadataProvider = new StaticMetadataProvider (ImmutableMap .of (a , f ));
189195 RemoteCache remoteCache = newCache (options , digestUtil , new HashMap <>());
190196 RemoteActionInputFetcher actionInputFetcher =
191- new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot );
197+ new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot , tempPathGenerator );
192198
193199 // act
194200 wait (actionInputFetcher .prefetchFiles (ImmutableList .of (a ), metadataProvider ));
@@ -206,7 +212,7 @@ public void testDownloadFile() throws Exception {
206212 Artifact a1 = createRemoteArtifact ("file1" , "hello world" , metadata , cacheEntries );
207213 RemoteCache remoteCache = newCache (options , digestUtil , cacheEntries );
208214 RemoteActionInputFetcher actionInputFetcher =
209- new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot );
215+ new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot , tempPathGenerator );
210216
211217 // act
212218 actionInputFetcher .downloadFile (a1 .getPath (), metadata .get (a1 ));
@@ -227,23 +233,15 @@ public void testDownloadFile_onInterrupt_deletePartialDownloadedFile() throws Ex
227233 Map <Digest , ByteString > cacheEntries = new HashMap <>();
228234 Artifact a1 = createRemoteArtifact ("file1" , "hello world" , metadata , cacheEntries );
229235 RemoteCache remoteCache = mock (RemoteCache .class );
230- when (remoteCache .downloadFile (any (), any (), any ()))
231- .thenAnswer (
232- invocation -> {
233- Path path = invocation .getArgument (1 );
234- Digest digest = invocation .getArgument (2 );
235- ByteString content = cacheEntries .get (digest );
236- if (content == null ) {
237- return Futures .immediateFailedFuture (new IOException ("Not found" ));
238- }
239- content .writeTo (path .getOutputStream ());
240-
241- startSemaphore .release ();
242- return SettableFuture
243- .create (); // A future that never complete so we can interrupt later
244- });
236+ mockDownload (
237+ remoteCache ,
238+ cacheEntries ,
239+ () -> {
240+ startSemaphore .release ();
241+ return SettableFuture .create (); // A future that never complete so we can interrupt later
242+ });
245243 RemoteActionInputFetcher actionInputFetcher =
246- new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot );
244+ new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot , tempPathGenerator );
247245
248246 AtomicBoolean interrupted = new AtomicBoolean (false );
249247 Thread t =
@@ -265,6 +263,7 @@ public void testDownloadFile_onInterrupt_deletePartialDownloadedFile() throws Ex
265263
266264 assertThat (interrupted .get ()).isTrue ();
267265 assertThat (a1 .getPath ().exists ()).isFalse ();
266+ assertThat (tempPathGenerator .getTempDir ().getDirectoryEntries ()).isEmpty ();
268267 }
269268
270269 @ Test
@@ -279,9 +278,9 @@ public void testPrefetchFiles_multipleThreads_downloadIsNotCancelledByOtherThrea
279278 MetadataProvider metadataProvider = new StaticMetadataProvider (metadata );
280279 SettableFuture <Void > download = SettableFuture .create ();
281280 RemoteCache remoteCache = mock (RemoteCache .class );
282- when (remoteCache . downloadFile ( any (), any (), any ())). thenAnswer ( invocation -> download );
281+ mockDownload (remoteCache , cacheEntries , () -> download );
283282 RemoteActionInputFetcher actionInputFetcher =
284- new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot );
283+ new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot , tempPathGenerator );
285284 Thread cancelledThread =
286285 new Thread (
287286 () -> {
@@ -326,6 +325,8 @@ public void testPrefetchFiles_multipleThreads_downloadIsNotCancelledByOtherThrea
326325
327326 // assert
328327 assertThat (successful .get ()).isTrue ();
328+ assertThat (FileSystemUtils .readContent (artifact .getPath (), StandardCharsets .UTF_8 ))
329+ .isEqualTo ("hello world" );
329330 }
330331
331332 @ Test
@@ -340,9 +341,9 @@ public void testPrefetchFiles_multipleThreads_downloadIsCancelled() throws Excep
340341
341342 SettableFuture <Void > download = SettableFuture .create ();
342343 RemoteCache remoteCache = mock (RemoteCache .class );
343- when (remoteCache . downloadFile ( any (), any (), any ())). thenAnswer ( invocation -> download );
344+ mockDownload (remoteCache , cacheEntries , () -> download );
344345 RemoteActionInputFetcher actionInputFetcher =
345- new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot );
346+ new RemoteActionInputFetcher ("none" , "none" , remoteCache , execRoot , tempPathGenerator );
346347
347348 Thread cancelledThread1 =
348349 new Thread (
@@ -376,6 +377,8 @@ public void testPrefetchFiles_multipleThreads_downloadIsCancelled() throws Excep
376377
377378 // assert
378379 assertThat (download .isCancelled ()).isTrue ();
380+ assertThat (artifact .getPath ().exists ()).isFalse ();
381+ assertThat (tempPathGenerator .getTempDir ().getDirectoryEntries ()).isEmpty ();
379382 }
380383
381384 private Artifact createRemoteArtifact (
@@ -420,4 +423,23 @@ private static void wait(ListenableFuture<Void> future) throws IOException, Inte
420423 throw e ;
421424 }
422425 }
426+
427+ private static void mockDownload (
428+ RemoteCache remoteCache ,
429+ Map <Digest , ByteString > cacheEntries ,
430+ Supplier <ListenableFuture <Void >> resultSupplier )
431+ throws IOException {
432+ when (remoteCache .downloadFile (any (), any (), any ()))
433+ .thenAnswer (
434+ invocation -> {
435+ Path path = invocation .getArgument (1 );
436+ Digest digest = invocation .getArgument (2 );
437+ ByteString content = cacheEntries .get (digest );
438+ if (content == null ) {
439+ return Futures .immediateFailedFuture (new IOException ("Not found" ));
440+ }
441+ FileSystemUtils .writeContent (path , content .toByteArray ());
442+ return resultSupplier .get ();
443+ });
444+ }
423445}
0 commit comments