3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.process.cluster.health;
22 import java.util.Collection;
23 import java.util.Random;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.ScheduledExecutorService;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.TimeoutException;
29 import java.util.stream.Collectors;
30 import java.util.stream.IntStream;
31 import org.junit.Test;
33 import static java.util.concurrent.TimeUnit.SECONDS;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.verify;
37 public class DelegateHealthStateRefresherExecutorServiceTest {
38 private Random random = new Random();
39 private Runnable runnable = mock(Runnable.class);
40 private Callable callable = mock(Callable.class);
41 private Collection<Callable<Object>> callables = IntStream.range(0, random.nextInt(5))
42 .mapToObj(i -> (Callable<Object>) mock(Callable.class))
43 .collect(Collectors.toList());
44 private int initialDelay = random.nextInt(333);
45 private int delay = random.nextInt(333);
46 private int period = random.nextInt(333);
47 private int timeout = random.nextInt(333);
48 private Object result = new Object();
49 private ScheduledExecutorService executorService = mock(ScheduledExecutorService.class);
50 private DelegateHealthStateRefresherExecutorService underTest = new DelegateHealthStateRefresherExecutorService(executorService);
53 public void schedule() {
54 underTest.schedule(runnable, delay, SECONDS);
56 verify(executorService).schedule(runnable, delay, SECONDS);
60 public void schedule1() {
61 underTest.schedule(callable, delay, SECONDS);
63 verify(executorService).schedule(callable, delay, SECONDS);
67 public void scheduleAtFixedRate() {
68 underTest.scheduleAtFixedRate(runnable, initialDelay, period, SECONDS);
69 verify(executorService).scheduleAtFixedRate(runnable, initialDelay, period, SECONDS);
73 public void scheduleWithFixeddelay() {
74 underTest.scheduleWithFixedDelay(runnable, initialDelay, delay, TimeUnit.SECONDS);
75 verify(executorService).scheduleWithFixedDelay(runnable, initialDelay, delay, TimeUnit.SECONDS);
79 public void shutdown() {
81 verify(executorService).shutdown();
85 public void shutdownNow() {
86 underTest.shutdownNow();
87 verify(executorService).shutdownNow();
91 public void isShutdown() {
92 underTest.isShutdown();
93 verify(executorService).isShutdown();
97 public void isTerminated() {
98 underTest.isTerminated();
99 verify(executorService).isTerminated();
103 public void awaitTermination() throws InterruptedException {
104 underTest.awaitTermination(timeout, TimeUnit.SECONDS);
106 verify(executorService).awaitTermination(timeout, TimeUnit.SECONDS);
110 public void submit() {
111 underTest.submit(callable);
113 verify(executorService).submit(callable);
117 public void submit1() {
118 underTest.submit(runnable, result);
120 verify(executorService).submit(runnable, result);
124 public void submit2() {
125 underTest.submit(runnable);
126 verify(executorService).submit(runnable);
130 public void invokeAll() throws InterruptedException {
131 underTest.invokeAll(callables);
132 verify(executorService).invokeAll(callables);
136 public void invokeAll1() throws InterruptedException {
137 underTest.invokeAll(callables, timeout, SECONDS);
138 verify(executorService).invokeAll(callables, timeout, SECONDS);
142 public void invokeAny() throws InterruptedException, ExecutionException {
143 underTest.invokeAny(callables);
144 verify(executorService).invokeAny(callables);
148 public void invokeAny2() throws InterruptedException, ExecutionException, TimeoutException {
149 underTest.invokeAny(callables, timeout, SECONDS);
150 verify(executorService).invokeAny(callables, timeout, SECONDS);