ExtractArchiveWorkspaceManager implements WorkspaceManager { @Override public <T> T withExtractedArchive(TaskRequest request, ArchiveProvider archiveProvider, WithWorkspaceAction<T> func) throws IOException { TempDir workspacePath = null; try { try { workspacePath = RetryExecutor.retryExecutor() .retryIf(exception -> true) .withInitialRetryWait(EXTRACT_MIN_RETRY_WAIT_MS) .withMaxRetryWait(EXTRACT_MAX_RETRY_WAIT_MS) .onRetry((exception, retryCount, retryLimit, retryWait) -> logger.warn("Failed to extract archive: retry {} of {}", retryCount, retryLimit, exception)) .withRetryLimit(EXTRACT_RETRIES) .run(() -> { TempDir newWorkSpacePath = null; try { newWorkSpacePath = createNewWorkspace(request); Optional<StorageObject> in = archiveProvider.open(); if (in.isPresent()) { ProjectArchives.extractTarArchive(newWorkSpacePath.get(), in.get().getContentInputStream()); } return newWorkSpacePath; } catch (Throwable e) { if (newWorkSpacePath != null) { newWorkSpacePath.close(); } throw e; } }); } catch (RetryExecutor.RetryGiveupException e) { throw Throwables.propagate(e.getCause()); } return func.run(workspacePath.get()); } finally { if (workspacePath != null) { workspacePath.close(); } } } @Inject  ExtractArchiveWorkspaceManager(TempFileManager tempFiles); @Override T withExtractedArchive(TaskRequest request, ArchiveProvider archiveProvider, WithWorkspaceAction<T> func);  }
@Test public void withExtractedArchive() throws Exception { WorkspaceManager.ArchiveProvider archiveProvider = mock(WorkspaceManager.ArchiveProvider.class); when(archiveProvider.open()).thenReturn(Optional.of(storageObject)); AtomicInteger funcCounter = new AtomicInteger(); extractArchiveWorkspaceManager.withExtractedArchive(taskRequest, archiveProvider, (path) -> funcCounter.incrementAndGet()); verify(archiveProvider, times(1)).open(); assertThat(funcCounter.get(), is(1)); assertEmptyWorkspace(); }
@Test public void withExtractedArchiveWithRetry() throws Exception { WorkspaceManager.ArchiveProvider archiveProvider = mock(WorkspaceManager.ArchiveProvider.class); when(archiveProvider.open()).thenReturn(Optional.of(wrongStorageObject), Optional.of(wrongStorageObject), Optional.of(storageObject)); AtomicInteger funcCounter = new AtomicInteger(); extractArchiveWorkspaceManager.withExtractedArchive(taskRequest, archiveProvider, (path) -> funcCounter.incrementAndGet()); verify(archiveProvider, times(3)).open(); assertThat(funcCounter.get(), is(1)); assertEmptyWorkspace(); }
@Test public void withoutExtractedArchive() throws Exception { WorkspaceManager.ArchiveProvider archiveProvider = mock(WorkspaceManager.ArchiveProvider.class); when(archiveProvider.open()).thenReturn(Optional.absent()); AtomicInteger funcCounter = new AtomicInteger(); extractArchiveWorkspaceManager.withExtractedArchive(taskRequest, archiveProvider, (path) -> funcCounter.incrementAndGet()); verify(archiveProvider, times(1)).open(); assertThat(funcCounter.get(), is(1)); assertEmptyWorkspace(); }