]> source.dussan.org Git - sonarqube.git/blob
3d7f4c5e422c97272e354a7e5a89691817438e53
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
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 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;
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     expectISEForIllegalCallToInitPendingCount();
59
60     underTest.initPendingCount(10);
61     underTest.initPendingCount(10);
62   }
63
64   @Test
65   public void initPendingCount_throws_ISE_if_called_after_getPendingCount() {
66     expectISEForIllegalCallToInitPendingCount();
67
68     underTest.getPendingCount();
69     underTest.initPendingCount(10);
70   }
71
72   @Test
73   public void initPendingCount_throws_ISE_if_called_after_addReceived() {
74     expectISEForIllegalCallToInitPendingCount();
75
76     underTest.addReceived();
77     underTest.initPendingCount(10);
78   }
79
80   @Test
81   public void initPendingCount_throws_ISE_if_called_after_addInProgress() {
82     expectISEForIllegalCallToInitPendingCount();
83
84     underTest.addInProgress();
85     underTest.initPendingCount(10);
86   }
87
88   private void expectISEForIllegalCallToInitPendingCount() {
89     expectedException.expect(IllegalStateException.class);
90     expectedException.expectMessage(ISE_initPendingCount_CALL_MSG);
91   }
92
93   @Test
94   public void addReceived_sets_received_and_pending_counts_to_1_when_initPendingCount_has_not_been_called() {
95     underTest.addReceived();
96
97     assertThat(underTest.getReceivedCount()).isEqualTo(1);
98     assertThat(underTest.getPendingCount()).isEqualTo(1);
99   }
100
101   @Test
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();
106     }
107
108     assertThat(underTest.getReceivedCount()).isEqualTo(calls);
109     assertThat(underTest.getPendingCount()).isEqualTo(calls);
110   }
111
112   @Test
113   public void addInProgress_increases_InProgress_and_decreases_Pending_by_1_without_check_on_Pending() {
114     underTest.addInProgress();
115
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);
122   }
123
124   @Test
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();
129     }
130
131     assertThat(underTest.getInProgressCount()).isEqualTo(calls);
132     assertThat(underTest.getPendingCount()).isEqualTo(-calls);
133     assertThat(underTest.getProcessingTime()).isEqualTo(0);
134   }
135
136   @Test
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");
140
141     underTest.addError(-1);
142   }
143
144   @Test
145   public void addError_increases_Error_and_decreases_InProgress_by_1_without_check_on_InProgress() {
146     underTest.addError(SOME_PROCESSING_TIME);
147
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);
154   }
155
156   @Test
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);
161     }
162
163     assertThat(underTest.getErrorCount()).isEqualTo(calls);
164     assertThat(underTest.getInProgressCount()).isEqualTo(-calls);
165     assertThat(underTest.getProcessingTime()).isEqualTo(calls);
166   }
167
168   @Test
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");
172
173     underTest.addSuccess(-1);
174   }
175
176   @Test
177   public void addSuccess_increases_Error_and_decreases_InProgress_by_1_without_check_on_InProgress() {
178     underTest.addSuccess(SOME_PROCESSING_TIME);
179
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);
186   }
187
188   @Test
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);
193     }
194
195     assertThat(underTest.getSuccessCount()).isEqualTo(calls);
196     assertThat(underTest.getInProgressCount()).isEqualTo(-calls);
197     assertThat(underTest.getProcessingTime()).isEqualTo(calls);
198   }
199 }