Skip to content

Commit

Permalink
#225 Improve tests and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dvoraka committed May 7, 2017
1 parent d1825f4 commit 1f73e39
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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._
}
}

0 comments on commit 1f73e39

Please sign in to comment.