Skip to content

Commit abfe7b8

Browse files
author
Dhriti Chopra
committed
refactor(testbench): clean up temporary logs/directory in finally block of stop()
1 parent c5ef4c5 commit abfe7b8

1 file changed

Lines changed: 37 additions & 49 deletions

File tree

  • java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry

java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/TestBench.java

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -318,67 +318,55 @@ public boolean shouldRetry(
318318

319319
@Override
320320
public void stop() {
321-
if (runningOutsideAlready || Boolean.getBoolean("testbench.keepAlive")) {
322-
// if the server was running outside the tests already, or we want to keep it alive, simply return
321+
if (runningOutsideAlready) {
322+
// if the server was running outside the tests already simply return
323323
return;
324324
}
325325
try {
326-
process.destroy();
327-
process.waitFor(2, TimeUnit.SECONDS);
328-
boolean attemptForceStopContainer = true;
329-
try {
330-
int processExitValue = process.exitValue();
331-
if (processExitValue != 0) {
332-
attemptForceStopContainer = true;
333-
}
334-
LOGGER.warn("Container exit value = {}", processExitValue);
335-
} catch (IllegalThreadStateException e) {
336-
attemptForceStopContainer = true;
337-
}
338-
339-
if (attemptForceStopContainer) {
340-
LOGGER.warn("Container did not gracefully exit, attempting to explicitly stop it.");
326+
if (!Boolean.getBoolean("testbench.keepAlive")) {
327+
LOGGER.warn("Attempting to explicitly stop container: {}", containerName);
341328
ImmutableList<String> command = ImmutableList.of("docker", "kill", containerName);
342329
LOGGER.warn(command.toString());
343330
Process shutdownProcess = new ProcessBuilder(command).start();
344331
shutdownProcess.waitFor(5, TimeUnit.SECONDS);
345332
int shutdownProcessExitValue = shutdownProcess.exitValue();
346-
LOGGER.warn("Container exit value = {}", shutdownProcessExitValue);
333+
LOGGER.warn("Container stop exit value = {}", shutdownProcessExitValue);
334+
335+
// wait for the server to shutdown
336+
runWithRetries(
337+
() -> {
338+
try {
339+
listRetryTests();
340+
} catch (SocketException e) {
341+
// desired result
342+
return null;
343+
}
344+
throw new NotShutdownException();
345+
},
346+
RetrySettings.newBuilder()
347+
.setTotalTimeoutDuration(Duration.ofSeconds(30))
348+
.setInitialRetryDelayDuration(Duration.ofMillis(500))
349+
.setRetryDelayMultiplier(1.5)
350+
.setMaxRetryDelayDuration(Duration.ofSeconds(5))
351+
.build(),
352+
new BasicResultRetryAlgorithm<List<?>>() {
353+
@Override
354+
public boolean shouldRetry(Throwable previousThrowable, List<?> previousResponse) {
355+
return previousThrowable instanceof NotShutdownException;
356+
}
357+
},
358+
NanoClock.getDefaultClock());
347359
}
348-
349-
// wait for the server to shutdown
350-
runWithRetries(
351-
() -> {
352-
try {
353-
listRetryTests();
354-
} catch (SocketException e) {
355-
// desired result
356-
return null;
357-
}
358-
throw new NotShutdownException();
359-
},
360-
RetrySettings.newBuilder()
361-
.setTotalTimeoutDuration(Duration.ofSeconds(30))
362-
.setInitialRetryDelayDuration(Duration.ofMillis(500))
363-
.setRetryDelayMultiplier(1.5)
364-
.setMaxRetryDelayDuration(Duration.ofSeconds(5))
365-
.build(),
366-
new BasicResultRetryAlgorithm<List<?>>() {
367-
@Override
368-
public boolean shouldRetry(Throwable previousThrowable, List<?> previousResponse) {
369-
return previousThrowable instanceof NotShutdownException;
370-
}
371-
},
372-
NanoClock.getDefaultClock());
360+
} catch (InterruptedException | IOException e) {
361+
throw new RuntimeException(e);
362+
} finally {
373363
try {
374-
Files.delete(errPath);
375-
Files.delete(outPath);
376-
Files.delete(tempDirectory);
364+
Files.deleteIfExists(errPath);
365+
Files.deleteIfExists(outPath);
366+
Files.deleteIfExists(tempDirectory);
377367
} catch (IOException e) {
378-
throw new RuntimeException(e);
368+
LOGGER.warn("Failed to delete temporary files", e);
379369
}
380-
} catch (InterruptedException | IOException e) {
381-
throw new RuntimeException(e);
382370
}
383371
}
384372

0 commit comments

Comments
 (0)