]> source.dussan.org Git - sonarqube.git/blob
2acbf6bada185d783d247a836332d239a32524e5
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.process.cluster.health;
21
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;
32
33 import static java.util.concurrent.TimeUnit.SECONDS;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.verify;
36
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);
51
52   @Test
53   public void schedule() {
54     underTest.schedule(runnable, delay, SECONDS);
55
56     verify(executorService).schedule(runnable, delay, SECONDS);
57   }
58
59   @Test
60   public void schedule1() {
61     underTest.schedule(callable, delay, SECONDS);
62
63     verify(executorService).schedule(callable, delay, SECONDS);
64   }
65
66   @Test
67   public void scheduleAtFixedRate() {
68     underTest.scheduleAtFixedRate(runnable, initialDelay, period, SECONDS);
69     verify(executorService).scheduleAtFixedRate(runnable, initialDelay, period, SECONDS);
70   }
71
72   @Test
73   public void scheduleWithFixeddelay() {
74     underTest.scheduleWithFixedDelay(runnable, initialDelay, delay, TimeUnit.SECONDS);
75     verify(executorService).scheduleWithFixedDelay(runnable, initialDelay, delay, TimeUnit.SECONDS);
76   }
77
78   @Test
79   public void shutdown() {
80     underTest.shutdown();
81     verify(executorService).shutdown();
82   }
83
84   @Test
85   public void shutdownNow() {
86     underTest.shutdownNow();
87     verify(executorService).shutdownNow();
88   }
89
90   @Test
91   public void isShutdown() {
92     underTest.isShutdown();
93     verify(executorService).isShutdown();
94   }
95
96   @Test
97   public void isTerminated() {
98     underTest.isTerminated();
99     verify(executorService).isTerminated();
100   }
101
102   @Test
103   public void awaitTermination() throws InterruptedException {
104     underTest.awaitTermination(timeout, TimeUnit.SECONDS);
105
106     verify(executorService).awaitTermination(timeout, TimeUnit.SECONDS);
107   }
108
109   @Test
110   public void submit() {
111     underTest.submit(callable);
112
113     verify(executorService).submit(callable);
114   }
115
116   @Test
117   public void submit1() {
118     underTest.submit(runnable, result);
119
120     verify(executorService).submit(runnable, result);
121   }
122
123   @Test
124   public void submit2() {
125     underTest.submit(runnable);
126     verify(executorService).submit(runnable);
127   }
128
129   @Test
130   public void invokeAll() throws InterruptedException {
131     underTest.invokeAll(callables);
132     verify(executorService).invokeAll(callables);
133   }
134
135   @Test
136   public void invokeAll1() throws InterruptedException {
137     underTest.invokeAll(callables, timeout, SECONDS);
138     verify(executorService).invokeAll(callables, timeout, SECONDS);
139   }
140
141   @Test
142   public void invokeAny() throws InterruptedException, ExecutionException {
143     underTest.invokeAny(callables);
144     verify(executorService).invokeAny(callables);
145   }
146
147   @Test
148   public void invokeAny2() throws InterruptedException, ExecutionException, TimeoutException {
149     underTest.invokeAny(callables, timeout, SECONDS);
150     verify(executorService).invokeAny(callables, timeout, SECONDS);
151   }
152
153 }