diff --git a/common/src/main/java/dvoraka/avservice/common/service/ExecutorServiceHelper.java b/common/src/main/java/dvoraka/avservice/common/service/ExecutorServiceHelper.java index def8692a..59b5dddf 100644 --- a/common/src/main/java/dvoraka/avservice/common/service/ExecutorServiceHelper.java +++ b/common/src/main/java/dvoraka/avservice/common/service/ExecutorServiceHelper.java @@ -16,15 +16,19 @@ default void shutdownAndAwaitTermination( Logger logger ) { logger.info("Stopping executor service..."); + final String okMessage = "Executor service stopping done."; + executorService.shutdown(); try { if (!executorService.awaitTermination(waitSeconds, TimeUnit.SECONDS)) { executorService.shutdownNow(); if (!executorService.awaitTermination(waitSeconds, TimeUnit.SECONDS)) { logger.warn("Thread pool termination problem!"); + } else { + logger.info(okMessage); } } else { - logger.debug("Thread pool stopping done."); + logger.info(okMessage); } } catch (InterruptedException e) { logger.warn("Stopping interrupted!", e); diff --git a/common/src/test/groovy/dvoraka/avservice/common/service/ExecutorServiceHelperSpec.groovy b/common/src/test/groovy/dvoraka/avservice/common/service/ExecutorServiceHelperSpec.groovy new file mode 100644 index 00000000..8160316a --- /dev/null +++ b/common/src/test/groovy/dvoraka/avservice/common/service/ExecutorServiceHelperSpec.groovy @@ -0,0 +1,79 @@ +package dvoraka.avservice.common.service + +import org.apache.logging.log4j.Logger +import spock.lang.Specification +import spock.lang.Subject + +import java.util.concurrent.ExecutorService + +/** + * ExecutorServiceHelperSpec + */ +class ExecutorServiceHelperSpec extends Specification { + + @Subject + ExecutorServiceHelper helper + + ExecutorService service + Logger logger + + long seconds = 10 + + + def setup() { + helper = Spy() + + service = Mock() + logger = Mock() + } + + def "normal shutdown"() { + when: + helper.shutdownAndAwaitTermination(service, seconds, logger) + + then: + 1 * service.shutdown() + 1 * service.awaitTermination(_, _) >> true + + 2 * logger._ + } + + def "shutdown with troubles"() { + when: + helper.shutdownAndAwaitTermination(service, seconds, logger) + + then: + 1 * service.shutdown() + 2 * service.awaitTermination(_, _) >> false >> true + 1 * service.shutdownNow() + + 2 * logger._ + } + + def "shutdown with big troubles"() { + when: + helper.shutdownAndAwaitTermination(service, seconds, logger) + + then: + 1 * service.shutdown() + 2 * service.awaitTermination(_, _) >> false + 1 * service.shutdownNow() + + 1 * logger.warn(_) + 1 * logger._ + } + + def "shutdown with waiting troubles"() { + when: + helper.shutdownAndAwaitTermination(service, seconds, logger) + + then: + 1 * service.shutdown() + 1 * service.awaitTermination(_, _) >> { throw new InterruptedException() } + + 1 * service.shutdownNow() + + 1 * logger.warn(_, _) + 1 * logger._ + } +}