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.Random;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
27 import static org.assertj.core.api.Assertions.assertThat;
29 public class CEQueueStatusImplTest {
30 private static final String ISE_initPendingCount_CALL_MSG = "Method initPendingCount must be used before any other method and can not be called twice";
31 private static final int SOME_RANDOM_MAX = 96535;
32 private static final int SOME_PROCESSING_TIME = 8723;
35 public ExpectedException expectedException = ExpectedException.none();
37 private CEQueueStatusImpl underTest = new CEQueueStatusImpl();
40 public void verify_just_created_instance_metrics() {
41 assertThat(underTest.getReceivedCount()).isEqualTo(0);
42 assertThat(underTest.getPendingCount()).isEqualTo(0);
43 assertThat(underTest.getInProgressCount()).isEqualTo(0);
44 assertThat(underTest.getErrorCount()).isEqualTo(0);
45 assertThat(underTest.getSuccessCount()).isEqualTo(0);
46 assertThat(underTest.getProcessingTime()).isEqualTo(0);
50 public void initPendingCount_sets_value_of_pendingCount() {
51 underTest.initPendingCount(10);
53 assertThat(underTest.getPendingCount()).isEqualTo(10);
57 public void initPendingCount_throws_ISE_if_called_twice() {
58 expectISEForIllegalCallToInitPendingCount();
60 underTest.initPendingCount(10);
61 underTest.initPendingCount(10);
65 public void initPendingCount_throws_ISE_if_called_after_getPendingCount() {
66 expectISEForIllegalCallToInitPendingCount();
68 underTest.getPendingCount();
69 underTest.initPendingCount(10);
73 public void initPendingCount_throws_ISE_if_called_after_addReceived() {
74 expectISEForIllegalCallToInitPendingCount();
76 underTest.addReceived();
77 underTest.initPendingCount(10);
81 public void initPendingCount_throws_ISE_if_called_after_addInProgress() {
82 expectISEForIllegalCallToInitPendingCount();
84 underTest.addInProgress();
85 underTest.initPendingCount(10);
88 private void expectISEForIllegalCallToInitPendingCount() {
89 expectedException.expect(IllegalStateException.class);
90 expectedException.expectMessage(ISE_initPendingCount_CALL_MSG);
94 public void addReceived_sets_received_and_pending_counts_to_1_when_initPendingCount_has_not_been_called() {
95 underTest.addReceived();
97 assertThat(underTest.getReceivedCount()).isEqualTo(1);
98 assertThat(underTest.getPendingCount()).isEqualTo(1);
102 public void addReceived_any_number_of_call_adds_1_per_call() {
103 int calls = new Random().nextInt(SOME_RANDOM_MAX);
104 for (int i = 0; i < calls; i++) {
105 underTest.addReceived();
108 assertThat(underTest.getReceivedCount()).isEqualTo(calls);
109 assertThat(underTest.getPendingCount()).isEqualTo(calls);
113 public void addInProgress_increases_InProgress_and_decreases_Pending_by_1_without_check_on_Pending() {
114 underTest.addInProgress();
116 assertThat(underTest.getReceivedCount()).isEqualTo(0);
117 assertThat(underTest.getPendingCount()).isEqualTo(-1);
118 assertThat(underTest.getInProgressCount()).isEqualTo(1);
119 assertThat(underTest.getErrorCount()).isEqualTo(0);
120 assertThat(underTest.getSuccessCount()).isEqualTo(0);
121 assertThat(underTest.getProcessingTime()).isEqualTo(0);
125 public void addInProgress_any_number_of_call_change_by_1_per_call() {
126 int calls = new Random().nextInt(SOME_RANDOM_MAX);
127 for (int i = 0; i < calls; i++) {
128 underTest.addInProgress();
131 assertThat(underTest.getInProgressCount()).isEqualTo(calls);
132 assertThat(underTest.getPendingCount()).isEqualTo(-calls);
133 assertThat(underTest.getProcessingTime()).isEqualTo(0);
137 public void addError_throws_IAE_if_time_is_less_than_0() {
138 expectedException.expect(IllegalArgumentException.class);
139 expectedException.expectMessage("Processing time can not be < 0");
141 underTest.addError(-1);
145 public void addError_increases_Error_and_decreases_InProgress_by_1_without_check_on_InProgress() {
146 underTest.addError(SOME_PROCESSING_TIME);
148 assertThat(underTest.getReceivedCount()).isEqualTo(0);
149 assertThat(underTest.getPendingCount()).isEqualTo(0);
150 assertThat(underTest.getInProgressCount()).isEqualTo(-1);
151 assertThat(underTest.getErrorCount()).isEqualTo(1);
152 assertThat(underTest.getSuccessCount()).isEqualTo(0);
153 assertThat(underTest.getProcessingTime()).isEqualTo(SOME_PROCESSING_TIME);
157 public void addError_any_number_of_call_change_by_1_per_call() {
158 int calls = new Random().nextInt(SOME_RANDOM_MAX);
159 for (int i = 0; i < calls; i++) {
160 underTest.addError(1);
163 assertThat(underTest.getErrorCount()).isEqualTo(calls);
164 assertThat(underTest.getInProgressCount()).isEqualTo(-calls);
165 assertThat(underTest.getProcessingTime()).isEqualTo(calls);
169 public void addSuccess_throws_IAE_if_time_is_less_than_0() {
170 expectedException.expect(IllegalArgumentException.class);
171 expectedException.expectMessage("Processing time can not be < 0");
173 underTest.addSuccess(-1);
177 public void addSuccess_increases_Error_and_decreases_InProgress_by_1_without_check_on_InProgress() {
178 underTest.addSuccess(SOME_PROCESSING_TIME);
180 assertThat(underTest.getReceivedCount()).isEqualTo(0);
181 assertThat(underTest.getPendingCount()).isEqualTo(0);
182 assertThat(underTest.getInProgressCount()).isEqualTo(-1);
183 assertThat(underTest.getErrorCount()).isEqualTo(0);
184 assertThat(underTest.getSuccessCount()).isEqualTo(1);
185 assertThat(underTest.getProcessingTime()).isEqualTo(SOME_PROCESSING_TIME);
189 public void addSuccess_any_number_of_call_change_by_1_per_call() {
190 int calls = new Random().nextInt(SOME_RANDOM_MAX);
191 for (int i = 0; i < calls; i++) {
192 underTest.addSuccess(1);
195 assertThat(underTest.getSuccessCount()).isEqualTo(calls);
196 assertThat(underTest.getInProgressCount()).isEqualTo(-calls);
197 assertThat(underTest.getProcessingTime()).isEqualTo(calls);