2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.server.computation.monitoring;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.ThreadFactory;
28 import java.util.concurrent.TimeUnit;
29 import org.junit.After;
30 import org.junit.Test;
32 import static org.assertj.core.api.Assertions.assertThat;
34 public class CEQueueStatusImplConcurrentTest {
35 private ExecutorService executorService = Executors.newFixedThreadPool(10, new ThreadFactory() {
39 public Thread newThread(Runnable r) {
40 return new Thread(r, CEQueueStatusImplConcurrentTest.class.getSimpleName() + cnt++);
43 private CEQueueStatusImpl underTest = new CEQueueStatusImpl();
46 public void tearDown() throws Exception {
47 executorService.shutdownNow();
51 public void test_concurrent_modifications_in_any_order() throws InterruptedException {
52 for (Runnable runnable : buildShuffleCallsToUnderTest()) {
53 executorService.submit(runnable);
56 executorService.awaitTermination(1, TimeUnit.SECONDS);
58 assertThat(underTest.getReceivedCount()).isEqualTo(100);
59 assertThat(underTest.getPendingCount()).isEqualTo(2);
60 assertThat(underTest.getInProgressCount()).isEqualTo(1);
61 assertThat(underTest.getErrorCount()).isEqualTo(17);
62 assertThat(underTest.getSuccessCount()).isEqualTo(80);
63 assertThat(underTest.getProcessingTime()).isEqualTo(177);
66 private List<Runnable> buildShuffleCallsToUnderTest() {
67 List<Runnable> res = new ArrayList<>();
68 for (int i = 0; i < 100; i++) {
69 res.add(new AddReceivedRunnable());
71 for (int i = 0; i < 98; i++) {
72 res.add(new AddInProgressRunnable());
74 for (int i = 0; i < 80; i++) {
75 res.add(new AddSuccessRunnable());
77 for (int i = 0; i < 17; i++) {
78 res.add(new AddErrorRunnable());
80 Collections.shuffle(res);
84 private class AddReceivedRunnable implements Runnable {
87 underTest.addReceived();
91 private class AddInProgressRunnable implements Runnable {
94 underTest.addInProgress();
98 private class AddErrorRunnable implements Runnable {
101 underTest.addError(1);
105 private class AddSuccessRunnable implements Runnable {
108 underTest.addSuccess(2);