DependencyTransferManager { public void downloadAllDependencies(final Set<DependencyFile> deps) { if (deps.isEmpty()) { return; } ensureIsEnabled(); CompletableFuture[] taskFutures = deps .stream() .map(f -> CompletableFuture.runAsync(() -> downloadDependency(f), threadPool)) .toArray(CompletableFuture[]::new); try { waitForAllToSucceedOrOneToFail(taskFutures); } catch (InterruptedException e) { cancelPendingTasks(taskFutures); throw new DependencyTransferException("Download interrupted.", e); } catch (ExecutionException e) { cancelPendingTasks(taskFutures); if (e.getCause() instanceof DependencyTransferException) { throw (DependencyTransferException) e.getCause(); } throw new RuntimeException(e.getCause()); } } @Inject  DependencyTransferManager(final Props props, final Storage storage); boolean isEnabled(); void downloadAllDependencies(final Set<DependencyFile> deps); final int dependencyMaxDownloadTries; }
@Test public void testDownloadEmptySet() throws Exception { this.dependencyTransferManager.downloadAllDependencies(Collections.emptySet()); }
@Test public void testDownloadDependencySuccess() throws Exception { doAnswer((Answer<InputStream>) invocation -> { DependencyFile depFile = (DependencyFile) invocation.getArguments()[0]; String content = depFile.equals(depA) ? ThinArchiveTestUtils.getDepAContent() : ThinArchiveTestUtils.getDepBContent(); return IOUtils.toInputStream(content); }).when(this.storage).getDependency(any()); this.dependencyTransferManager.downloadAllDependencies(depSetAB); assertEquals(ThinArchiveTestUtils.getDepAContent(), FileUtils.readFileToString(depA.getFile())); assertEquals(ThinArchiveTestUtils.getDepBContent(), FileUtils.readFileToString(depB.getFile())); }
@Test public void testDownloadDependencyHashInvalidOneRetry() throws Exception { doReturn(IOUtils.toInputStream(WRONG_CONTENT)) .doReturn(IOUtils.toInputStream(ThinArchiveTestUtils.getDepAContent())).when(this.storage).getDependency(any()); this.dependencyTransferManager.downloadAllDependencies(depSetA); verify(this.storage, times(2)).getDependency(depEq(depA)); }
@Test(expected = DependencyTransferException.class) public void testDownloadDependencyExceptionForAnyFailure() throws Exception { doAnswer((Answer<InputStream>) invocation -> { DependencyFile depFile = (DependencyFile) invocation.getArguments()[0]; String content = depFile.equals(depA) ? WRONG_CONTENT : ThinArchiveTestUtils.getDepBContent(); return IOUtils.toInputStream(content); }).when(this.storage).getDependency(any()); this.dependencyTransferManager.downloadAllDependencies(depSetAB); }