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.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 int SOME_RANDOM_MAX = 96535;
31 private static final int SOME_PROCESSING_TIME = 8723;
32 private static final long INITIAL_PENDING_COUNT = 996L;
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 expectedException.expect(IllegalStateException.class);
59 expectedException.expectMessage("Method initPendingCount must be used before any other method and can not be called twice");
61 underTest.initPendingCount(10);
62 underTest.initPendingCount(10);
66 public void addReceived_throws_ISE_if_called_before_initPendingCount() {
67 expectedException.expect(IllegalStateException.class);
68 expectedException.expectMessage("Method initPendingCount must be used before addReceived can be called");
70 underTest.addReceived();
74 public void addReceived_sets_received_and_pending_counts_to_1_when_initPendingCount_has_not_been_called() {
75 underTest.initPendingCount(INITIAL_PENDING_COUNT);
77 underTest.addReceived();
79 assertThat(underTest.getReceivedCount()).isEqualTo(1);
80 assertThat(underTest.getPendingCount()).isEqualTo(INITIAL_PENDING_COUNT + 1);
84 public void addReceived_any_number_of_call_adds_1_per_call() {
85 underTest.initPendingCount(INITIAL_PENDING_COUNT);
87 int calls = new Random().nextInt(SOME_RANDOM_MAX);
88 for (int i = 0; i < calls; i++) {
89 underTest.addReceived();
92 assertThat(underTest.getReceivedCount()).isEqualTo(calls);
93 assertThat(underTest.getPendingCount()).isEqualTo(INITIAL_PENDING_COUNT + calls);
97 public void addInProgress_throws_ISE_if_called_before_initPendingCount() {
98 expectedException.expect(IllegalStateException.class);
99 expectedException.expectMessage("Method initPendingCount must be used before addInProgress can be called");
101 underTest.addInProgress();
105 public void addInProgress_increases_InProgress_and_decreases_Pending_by_1_without_check_on_Pending() {
106 underTest.initPendingCount(INITIAL_PENDING_COUNT);
108 underTest.addInProgress();
110 assertThat(underTest.getReceivedCount()).isEqualTo(0);
111 assertThat(underTest.getPendingCount()).isEqualTo(INITIAL_PENDING_COUNT - 1);
112 assertThat(underTest.getInProgressCount()).isEqualTo(1);
113 assertThat(underTest.getErrorCount()).isEqualTo(0);
114 assertThat(underTest.getSuccessCount()).isEqualTo(0);
115 assertThat(underTest.getProcessingTime()).isEqualTo(0);
119 public void addInProgress_any_number_of_call_change_by_1_per_call() {
120 underTest.initPendingCount(INITIAL_PENDING_COUNT);
122 int calls = new Random().nextInt(SOME_RANDOM_MAX);
123 for (int i = 0; i < calls; i++) {
124 underTest.addInProgress();
127 assertThat(underTest.getInProgressCount()).isEqualTo(calls);
128 assertThat(underTest.getPendingCount()).isEqualTo(INITIAL_PENDING_COUNT - calls);
129 assertThat(underTest.getProcessingTime()).isEqualTo(0);
133 public void addError_throws_IAE_if_time_is_less_than_0() {
134 expectedException.expect(IllegalArgumentException.class);
135 expectedException.expectMessage("Processing time can not be < 0");
137 underTest.addError(-1);
141 public void addError_increases_Error_and_decreases_InProgress_by_1_without_check_on_InProgress() {
142 underTest.addError(SOME_PROCESSING_TIME);
144 assertThat(underTest.getReceivedCount()).isEqualTo(0);
145 assertThat(underTest.getPendingCount()).isEqualTo(0);
146 assertThat(underTest.getInProgressCount()).isEqualTo(-1);
147 assertThat(underTest.getErrorCount()).isEqualTo(1);
148 assertThat(underTest.getSuccessCount()).isEqualTo(0);
149 assertThat(underTest.getProcessingTime()).isEqualTo(SOME_PROCESSING_TIME);
153 public void addError_any_number_of_call_change_by_1_per_call() {
154 int calls = new Random().nextInt(SOME_RANDOM_MAX);
155 for (int i = 0; i < calls; i++) {
156 underTest.addError(1);
159 assertThat(underTest.getErrorCount()).isEqualTo(calls);
160 assertThat(underTest.getInProgressCount()).isEqualTo(-calls);
161 assertThat(underTest.getProcessingTime()).isEqualTo(calls);
165 public void addSuccess_throws_IAE_if_time_is_less_than_0() {
166 expectedException.expect(IllegalArgumentException.class);
167 expectedException.expectMessage("Processing time can not be < 0");
169 underTest.addSuccess(-1);
173 public void addSuccess_increases_Error_and_decreases_InProgress_by_1_without_check_on_InProgress() {
174 underTest.addSuccess(SOME_PROCESSING_TIME);
176 assertThat(underTest.getReceivedCount()).isEqualTo(0);
177 assertThat(underTest.getPendingCount()).isEqualTo(0);
178 assertThat(underTest.getInProgressCount()).isEqualTo(-1);
179 assertThat(underTest.getErrorCount()).isEqualTo(0);
180 assertThat(underTest.getSuccessCount()).isEqualTo(1);
181 assertThat(underTest.getProcessingTime()).isEqualTo(SOME_PROCESSING_TIME);
185 public void addSuccess_any_number_of_call_change_by_1_per_call() {
186 int calls = new Random().nextInt(SOME_RANDOM_MAX);
187 for (int i = 0; i < calls; i++) {
188 underTest.addSuccess(1);
191 assertThat(underTest.getSuccessCount()).isEqualTo(calls);
192 assertThat(underTest.getInProgressCount()).isEqualTo(-calls);
193 assertThat(underTest.getProcessingTime()).isEqualTo(calls);