]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7183 add CeQueueStatus.addReceived(long)
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 28 Jan 2016 15:20:33 +0000 (16:20 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 29 Jan 2016 15:05:28 +0000 (16:05 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/monitoring/CEQueueStatus.java
server/sonar-server/src/main/java/org/sonar/server/computation/monitoring/CEQueueStatusImpl.java
server/sonar-server/src/test/java/org/sonar/server/computation/monitoring/CEQueueStatusImplTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/monitoring/ComputeEngineQueueMonitorTest.java

index 0b534335464198f33026ee7d685c8c938b60ab86..0dcab1aa0f243f27e562d45dad4ac3c605ef6961 100644 (file)
@@ -35,6 +35,10 @@ public interface CEQueueStatus {
 
   /**
    * Adds 1 to the count of received batch reports and 1 to the count of batch reports waiting for processing.
+   * <p>
+   * Calling this method is equivalent to calling {@link #addReceived(long)} with {@code 1} as argument but will
+   * trigger no parameter check. So, it can be faster.
+   * </p>
    *
    * @return the new count of received batch reports
    *
@@ -45,6 +49,20 @@ public interface CEQueueStatus {
    */
   long addReceived();
 
+  /**
+   * Adds {@code numberOfReceived} to the count of received batch reports and {@code numberOfReceived} to the count of
+   * batch reports waiting for processing.
+   *
+   * @return the new count of received batch reports
+   *
+   * @see #getReceivedCount()
+   * @see #getPendingCount()
+   *
+   * @throws IllegalStateException if {@link #initPendingCount(long)} has not been called yet
+   * @throws IllegalArgumentException if {@code numberOfReceived} is less or equal to 0
+   */
+  long addReceived(long numberOfReceived);
+
   /**
    * Adds 1 to the count of batch reports under processing and removes 1 from the count of batch reports waiting for
    * processing.
index 25102d3a8cf0adde5b0f507fe6daafcdc46eeabd..e786e5997eba018faac812df855e5bef6cb7f56f 100644 (file)
@@ -51,6 +51,15 @@ public class CEQueueStatusImpl implements CEQueueStatus {
     return received.incrementAndGet();
   }
 
+  @Override
+  public long addReceived(long numberOfReceived) {
+    ensurePendingInitialized("addReceived");
+    checkArgument(numberOfReceived > 0, "numberOfReceived must be > 0");
+
+    pending.addAndGet(numberOfReceived);
+    return received.addAndGet(numberOfReceived);
+  }
+
   @Override
   public long addInProgress() {
     ensurePendingInitialized("addInProgress");
index 15b69ad344a34aee497ce2ad1b8e434a69e4bf27..b1d82c150b42e7999eb70d7771b881e0b5bd728f 100644 (file)
@@ -71,7 +71,7 @@ public class CEQueueStatusImplTest {
   }
 
   @Test
-  public void addReceived_sets_received_and_pending_counts_to_1_when_initPendingCount_has_not_been_called() {
+  public void addReceived_sets_received_to_1_and_pending_counts_to_initial_value_plus_1() {
     underTest.initPendingCount(INITIAL_PENDING_COUNT);
 
     underTest.addReceived();
@@ -93,6 +93,55 @@ public class CEQueueStatusImplTest {
     assertThat(underTest.getPendingCount()).isEqualTo(INITIAL_PENDING_COUNT + calls);
   }
 
+  @Test
+  public void addReceived_with_argument_throws_IAE_if_parameter_is_0() {
+    underTest.initPendingCount(INITIAL_PENDING_COUNT);
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("numberOfReceived must be > 0");
+
+    underTest.addReceived(0);
+  }
+
+  @Test
+  public void addReceived_with_argument_throws_IAE_if_parameter_less_than_0() {
+    underTest.initPendingCount(INITIAL_PENDING_COUNT);
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("numberOfReceived must be > 0");
+
+    underTest.addReceived(-1 * (new Random().nextInt(SOME_RANDOM_MAX)));
+  }
+
+  @Test
+  public void addReceived_with_argument_sets_received_to_value_and_pending_counts_to_initial_value_plus_value() {
+    long value = 123;
+    underTest.initPendingCount(INITIAL_PENDING_COUNT);
+
+    underTest.addReceived(value);
+
+    assertThat(underTest.getReceivedCount()).isEqualTo(value);
+    assertThat(underTest.getPendingCount()).isEqualTo(INITIAL_PENDING_COUNT + value);
+  }
+
+  @Test
+  public void addReceived_with_argument_any_number_of_call_adds_some_number_per_call() {
+    underTest.initPendingCount(INITIAL_PENDING_COUNT);
+
+    Random random = new Random();
+    int calls = random.nextInt(SOME_RANDOM_MAX);
+    long added = 0;
+    for (int i = 0; i < calls; i++) {
+      // adding 1 to random number because value can not be 0
+      long numberOfReceived = random.nextInt(SOME_RANDOM_MAX) + 1;
+      underTest.addReceived(numberOfReceived);
+      added += numberOfReceived;
+    }
+
+    assertThat(underTest.getReceivedCount()).isEqualTo(added);
+    assertThat(underTest.getPendingCount()).isEqualTo(INITIAL_PENDING_COUNT + added);
+  }
+
   @Test
   public void addInProgress_throws_ISE_if_called_before_initPendingCount() {
     expectedException.expect(IllegalStateException.class);
index 1587ab5f044ab0b7acf19b3d2feb622537e27be2..58739595d89cb7124a3be6863634ee8aadda1a6f 100644 (file)
@@ -73,8 +73,9 @@ public class ComputeEngineQueueMonitorTest {
       return methodNotImplemented();
     }
 
-    private long methodNotImplemented() {
-      throw new UnsupportedOperationException("Not Implemented");
+    @Override
+    public long addReceived(long numberOfReceived) {
+      return methodNotImplemented();
     }
 
     @Override
@@ -126,5 +127,9 @@ public class ComputeEngineQueueMonitorTest {
     public long getProcessingTime() {
       return PROCESSING_TIME;
     }
+
+    private long methodNotImplemented() {
+      throw new UnsupportedOperationException("Not Implemented");
+    }
   }
 }