diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2018-06-22 16:31:50 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2018-06-29 09:10:14 +0200 |
commit | 1653051ae4b32591b04691cb4c22bcb75d4a404c (patch) | |
tree | c4ae2dd9ed3226f23332152dbfa66b2dc9627ce9 /server/sonar-server-common/src/test | |
parent | 697e8d18e6a8b9dca3e345fa355c4b6228e89bdc (diff) | |
download | sonarqube-1653051ae4b32591b04691cb4c22bcb75d4a404c.tar.gz sonarqube-1653051ae4b32591b04691cb4c22bcb75d4a404c.zip |
move some classes from sonar-server to sonar-server-common
Diffstat (limited to 'server/sonar-server-common/src/test')
-rw-r--r-- | server/sonar-server-common/src/test/java/org/sonar/server/util/AbstractStoppableExecutorServiceTest.java | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/util/AbstractStoppableExecutorServiceTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/util/AbstractStoppableExecutorServiceTest.java new file mode 100644 index 00000000000..6fa78b273eb --- /dev/null +++ b/server/sonar-server-common/src/test/java/org/sonar/server/util/AbstractStoppableExecutorServiceTest.java @@ -0,0 +1,200 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.util; + +import com.google.common.collect.ImmutableList; +import org.junit.Test; +import org.mockito.InOrder; +import org.mockito.Mockito; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class AbstractStoppableExecutorServiceTest { + private static final Callable<String> SOME_CALLABLE = new Callable<String>() { + @Override + public String call() { + return null; + } + }; + private static final Runnable SOME_RUNNABLE = new Runnable() { + @Override + public void run() { + + } + }; + private static final String SOME_STRING = "some string"; + private static final long SOME_LONG = 100l; + private static final int TIMEOUT = 5; + private static final TimeUnit TIMEOUT_UNIT = TimeUnit.SECONDS; + + private ExecutorService executorService = mock(ExecutorService.class); + private InOrder inOrder = Mockito.inOrder(executorService); + private AbstractStoppableExecutorService underTest = new AbstractStoppableExecutorService(executorService) { + }; + public static final ImmutableList<Callable<String>> CALLABLES = ImmutableList.of(SOME_CALLABLE); + + @Test + public void stop_calls_shutdown_and_verify_termination() throws InterruptedException { + when(executorService.awaitTermination(TIMEOUT, TIMEOUT_UNIT)).thenReturn(true); + + underTest.stop(); + + inOrder.verify(executorService).shutdown(); + inOrder.verify(executorService).awaitTermination(TIMEOUT, TIMEOUT_UNIT); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void stop_calls_shutdown_then_shutdownNow_if_not_terminated_and_check_termination_again() throws InterruptedException { + when(executorService.awaitTermination(TIMEOUT, TIMEOUT_UNIT)).thenReturn(false).thenReturn(true); + + underTest.stop(); + + inOrder.verify(executorService).shutdown(); + inOrder.verify(executorService).awaitTermination(TIMEOUT, TIMEOUT_UNIT); + inOrder.verify(executorService).shutdownNow(); + inOrder.verify(executorService).awaitTermination(TIMEOUT, TIMEOUT_UNIT); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void stop_calls_shutdownnow_if_interrupted_exception_is_raised() throws InterruptedException { + when(executorService.awaitTermination(TIMEOUT, TIMEOUT_UNIT)).thenThrow(new InterruptedException()); + + underTest.stop(); + + inOrder.verify(executorService).shutdown(); + inOrder.verify(executorService).awaitTermination(TIMEOUT, TIMEOUT_UNIT); + inOrder.verify(executorService).shutdownNow(); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void shutdown_delegates_to_executorService() { + underTest.shutdown(); + + inOrder.verify(executorService).shutdown(); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void shutdownNow_delegates_to_executorService() { + underTest.shutdownNow(); + + inOrder.verify(executorService).shutdownNow(); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void isShutdown_delegates_to_executorService() { + underTest.isShutdown(); + + inOrder.verify(executorService).isShutdown(); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void isTerminated_delegates_to_executorService() { + underTest.isTerminated(); + + inOrder.verify(executorService).isTerminated(); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void awaitTermination_delegates_to_executorService() throws InterruptedException { + underTest.awaitTermination(SOME_LONG, SECONDS); + + inOrder.verify(executorService).awaitTermination(SOME_LONG, SECONDS); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void submit_callable_delegates_to_executorService() { + underTest.submit(SOME_CALLABLE); + + inOrder.verify(executorService).submit(SOME_CALLABLE); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void submit_runnable_delegates_to_executorService() { + underTest.submit(SOME_RUNNABLE); + + inOrder.verify(executorService).submit(SOME_RUNNABLE); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void submit_runnable_with_result_delegates_to_executorService() { + underTest.submit(SOME_RUNNABLE, SOME_STRING); + + inOrder.verify(executorService).submit(SOME_RUNNABLE, SOME_STRING); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void invokeAll_delegates_to_executorService() throws InterruptedException { + underTest.invokeAll(CALLABLES); + + inOrder.verify(executorService).invokeAll(CALLABLES); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void invokeAll1_delegates_to_executorService() throws InterruptedException { + underTest.invokeAll(CALLABLES, SOME_LONG, SECONDS); + + inOrder.verify(executorService).invokeAll(CALLABLES, SOME_LONG, SECONDS); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void invokeAny_delegates_to_executorService() throws ExecutionException, InterruptedException { + underTest.invokeAny(CALLABLES); + + inOrder.verify(executorService).invokeAny(CALLABLES); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void invokeAny1_delegates_to_executorService() throws InterruptedException, ExecutionException, TimeoutException { + underTest.invokeAny(CALLABLES, SOME_LONG, SECONDS); + + inOrder.verify(executorService).invokeAny(CALLABLES, SOME_LONG, SECONDS); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void execute_delegates_to_executorService() { + underTest.execute(SOME_RUNNABLE); + + inOrder.verify(executorService).execute(SOME_RUNNABLE); + inOrder.verifyNoMoreInteractions(); + } +} |