aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-03-08 12:03:10 +0100
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-03-08 13:51:46 +0100
commit1ebbacd7bdb13ab4599def95a187f761993dd25e (patch)
tree430b49c433d461bf84cabb579e194d342d3a5571
parent59e3f1c094a78849a52220eacbabb8503eae0fa3 (diff)
downloadsonarqube-1ebbacd7bdb13ab4599def95a187f761993dd25e.tar.gz
sonarqube-1ebbacd7bdb13ab4599def95a187f761993dd25e.zip
SONAR-7417 fix instable CeProcessingSchedulerImplMultiThreadTest
test that as many tasks are scheduled for processing as there is workers using mocks
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeProcessingSchedulerImplMultiThreadTest.java93
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeProcessingSchedulerImplTest.java (renamed from server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeProcessingSchedulerImplSingleThreadTest.java)26
2 files changed, 25 insertions, 94 deletions
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeProcessingSchedulerImplMultiThreadTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeProcessingSchedulerImplMultiThreadTest.java
deleted file mode 100644
index 02e6ca7b7c2..00000000000
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeProcessingSchedulerImplMultiThreadTest.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.computation.taskprocessor;
-
-import java.util.HashSet;
-import java.util.Set;
-import org.junit.Ignore;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.Timeout;
-import org.sonar.server.computation.configuration.CeConfigurationRule;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class CeProcessingSchedulerImplMultiThreadTest {
-
- @Rule
- // due to risks of infinite chaining of tasks/futures, a timeout is required for safety
- public Timeout timeout = Timeout.seconds(60);
- @Rule
- public CeConfigurationRule ceConfiguration = new CeConfigurationRule().setWorkerCount(4);
-
- private ThreadNameRecordingCeWorkerCallable ceWorkerRunnable = new ThreadNameRecordingCeWorkerCallable();
-
- @Ignore("disabled because of instability and until rewritten")
- @Test
- public void when_workerCount_is_more_than_1_CeWorkerCallable_runs_on_as_many_different_threads() throws InterruptedException {
- int workerCount = 2;
-
- ceConfiguration
- .setWorkerCount(workerCount)
- // reduce queue polling delay to not wait for processing to start
- .setQueuePollingDelay(10);
-
- CeProcessingSchedulerExecutorService processingExecutorService = null;
- CeProcessingSchedulerImpl underTest = null;
- try {
- processingExecutorService = new CeProcessingSchedulerExecutorServiceImpl(ceConfiguration);
- underTest = new CeProcessingSchedulerImpl(ceConfiguration, processingExecutorService, ceWorkerRunnable);
-
- underTest.startScheduling();
-
- // scheduling starts only after 10ms, leave 500ms for the tasks to run and make use of all available threads
- Thread.sleep(500);
-
- assertThat(ceWorkerRunnable.getThreadNames()).hasSize(workerCount);
- }
- finally {
- if (underTest != null) {
- underTest.stop();
- }
- if (processingExecutorService != null) {
- processingExecutorService.stop();
- }
- }
- }
-
- private static class ThreadNameRecordingCeWorkerCallable implements CeWorkerCallable {
- private final Set<String> threadNames = new HashSet<>();
- private final int maxCallCount = 100;
- private int callCount = 0;
-
- @Override
- public Boolean call() throws Exception {
- String name = Thread.currentThread().getName();
- threadNames.add(name);
- callCount++;
- // after maxCallCount calls, wait between each call
- return callCount < maxCallCount;
- }
-
- public Set<String> getThreadNames() {
- return threadNames;
- }
- }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeProcessingSchedulerImplSingleThreadTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeProcessingSchedulerImplTest.java
index 782ca62f085..c03aad1af6f 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeProcessingSchedulerImplSingleThreadTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeProcessingSchedulerImplTest.java
@@ -28,6 +28,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Queue;
+import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Delayed;
@@ -44,11 +45,16 @@ import org.junit.Test;
import org.junit.rules.Timeout;
import org.sonar.server.computation.configuration.CeConfigurationRule;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-public class CeProcessingSchedulerImplSingleThreadTest {
+public class CeProcessingSchedulerImplTest {
private static final Error ERROR_TO_INTERRUPT_CHAINING = new Error("Error should stop scheduling");
@Rule
@@ -175,6 +181,24 @@ public class CeProcessingSchedulerImplSingleThreadTest {
);
}
+ @Test
+ public void when_workerCount_is_more_than_1_as_many_CeWorkerCallable_are_scheduled() throws InterruptedException {
+ int workerCount = Math.abs(new Random().nextInt(10)) + 1;
+
+ ceConfiguration.setWorkerCount(workerCount);
+
+ ListenableScheduledFuture listenableScheduledFuture = mock(ListenableScheduledFuture.class);
+ CeProcessingSchedulerExecutorService processingExecutorService = mock(CeProcessingSchedulerExecutorService.class);
+ CeProcessingSchedulerImpl underTest = new CeProcessingSchedulerImpl(ceConfiguration, processingExecutorService, ceWorkerRunnable);
+ when(processingExecutorService.schedule(ceWorkerRunnable, ceConfiguration.getQueuePollingDelay(), MILLISECONDS))
+ .thenReturn(listenableScheduledFuture);
+
+ underTest.startScheduling();
+
+ verify(processingExecutorService, times(workerCount)).schedule(ceWorkerRunnable, ceConfiguration.getQueuePollingDelay(), MILLISECONDS);
+ verify(listenableScheduledFuture, times(workerCount)).addListener(any(Runnable.class), eq(processingExecutorService));
+ }
+
private void startSchedulingAndRun() throws ExecutionException, InterruptedException {
underTest.startScheduling();