]> source.dussan.org Git - sonarqube.git/blob
15b69ad344a34aee497ce2ad1b8e434a69e4bf27
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.monitoring;
21
22 import java.util.Random;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26
27 import static org.assertj.core.api.Assertions.assertThat;
28
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;
33
34   @Rule
35   public ExpectedException expectedException = ExpectedException.none();
36
37   private CEQueueStatusImpl underTest = new CEQueueStatusImpl();
38
39   @Test
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);
47   }
48
49   @Test
50   public void initPendingCount_sets_value_of_pendingCount() {
51     underTest.initPendingCount(10);
52
53     assertThat(underTest.getPendingCount()).isEqualTo(10);
54   }
55
56   @Test
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");
60
61     underTest.initPendingCount(10);
62     underTest.initPendingCount(10);
63   }
64
65   @Test
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");
69
70     underTest.addReceived();
71   }
72
73   @Test
74   public void addReceived_sets_received_and_pending_counts_to_1_when_initPendingCount_has_not_been_called() {
75     underTest.initPendingCount(INITIAL_PENDING_COUNT);
76
77     underTest.addReceived();
78
79     assertThat(underTest.getReceivedCount()).isEqualTo(1);
80     assertThat(underTest.getPendingCount()).isEqualTo(INITIAL_PENDING_COUNT + 1);
81   }
82
83   @Test
84   public void addReceived_any_number_of_call_adds_1_per_call() {
85     underTest.initPendingCount(INITIAL_PENDING_COUNT);
86
87     int calls = new Random().nextInt(SOME_RANDOM_MAX);
88     for (int i = 0; i < calls; i++) {
89       underTest.addReceived();
90     }
91
92     assertThat(underTest.getReceivedCount()).isEqualTo(calls);
93     assertThat(underTest.getPendingCount()).isEqualTo(INITIAL_PENDING_COUNT + calls);
94   }
95
96   @Test
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");
100
101     underTest.addInProgress();
102   }
103
104   @Test
105   public void addInProgress_increases_InProgress_and_decreases_Pending_by_1_without_check_on_Pending() {
106     underTest.initPendingCount(INITIAL_PENDING_COUNT);
107
108     underTest.addInProgress();
109
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);
116   }
117
118   @Test
119   public void addInProgress_any_number_of_call_change_by_1_per_call() {
120     underTest.initPendingCount(INITIAL_PENDING_COUNT);
121
122     int calls = new Random().nextInt(SOME_RANDOM_MAX);
123     for (int i = 0; i < calls; i++) {
124       underTest.addInProgress();
125     }
126
127     assertThat(underTest.getInProgressCount()).isEqualTo(calls);
128     assertThat(underTest.getPendingCount()).isEqualTo(INITIAL_PENDING_COUNT - calls);
129     assertThat(underTest.getProcessingTime()).isEqualTo(0);
130   }
131
132   @Test
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");
136
137     underTest.addError(-1);
138   }
139
140   @Test
141   public void addError_increases_Error_and_decreases_InProgress_by_1_without_check_on_InProgress() {
142     underTest.addError(SOME_PROCESSING_TIME);
143
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);
150   }
151
152   @Test
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);
157     }
158
159     assertThat(underTest.getErrorCount()).isEqualTo(calls);
160     assertThat(underTest.getInProgressCount()).isEqualTo(-calls);
161     assertThat(underTest.getProcessingTime()).isEqualTo(calls);
162   }
163
164   @Test
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");
168
169     underTest.addSuccess(-1);
170   }
171
172   @Test
173   public void addSuccess_increases_Error_and_decreases_InProgress_by_1_without_check_on_InProgress() {
174     underTest.addSuccess(SOME_PROCESSING_TIME);
175
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);
182   }
183
184   @Test
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);
189     }
190
191     assertThat(underTest.getSuccessCount()).isEqualTo(calls);
192     assertThat(underTest.getInProgressCount()).isEqualTo(-calls);
193     assertThat(underTest.getProcessingTime()).isEqualTo(calls);
194   }
195 }