3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.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 long initialPendingCount = 9963L;
53 underTest.initPendingCount(initialPendingCount);
55 for (Runnable runnable : buildShuffleCallsToUnderTest()) {
56 executorService.submit(runnable);
59 executorService.awaitTermination(1, TimeUnit.SECONDS);
61 assertThat(underTest.getReceivedCount()).isEqualTo(100);
62 assertThat(underTest.getPendingCount()).isEqualTo(initialPendingCount + 2);
63 assertThat(underTest.getInProgressCount()).isEqualTo(1);
64 assertThat(underTest.getErrorCount()).isEqualTo(17);
65 assertThat(underTest.getSuccessCount()).isEqualTo(80);
66 assertThat(underTest.getProcessingTime()).isEqualTo(177);
69 private List<Runnable> buildShuffleCallsToUnderTest() {
70 List<Runnable> res = new ArrayList<>();
71 for (int i = 0; i < 100; i++) {
72 res.add(new AddReceivedRunnable());
74 for (int i = 0; i < 98; i++) {
75 res.add(new AddInProgressRunnable());
77 for (int i = 0; i < 80; i++) {
78 res.add(new AddSuccessRunnable());
80 for (int i = 0; i < 17; i++) {
81 res.add(new AddErrorRunnable());
83 Collections.shuffle(res);
87 private class AddReceivedRunnable implements Runnable {
90 underTest.addReceived();
94 private class AddInProgressRunnable implements Runnable {
97 underTest.addInProgress();
101 private class AddErrorRunnable implements Runnable {
104 underTest.addError(1);
108 private class AddSuccessRunnable implements Runnable {
111 underTest.addSuccess(2);