From 9e5c942745ae76aab235e9c7aad3f8b65103e593 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 28 Jan 2016 16:20:33 +0100 Subject: [PATCH] SONAR-7183 add CeQueueStatus.addReceived(long) --- .../computation/monitoring/CEQueueStatus.java | 18 +++++++ .../monitoring/CEQueueStatusImpl.java | 9 ++++ .../monitoring/CEQueueStatusImplTest.java | 51 ++++++++++++++++++- .../ComputeEngineQueueMonitorTest.java | 9 +++- 4 files changed, 84 insertions(+), 3 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/monitoring/CEQueueStatus.java b/server/sonar-server/src/main/java/org/sonar/server/computation/monitoring/CEQueueStatus.java index 0b534335464..0dcab1aa0f2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/monitoring/CEQueueStatus.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/monitoring/CEQueueStatus.java @@ -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. + *

+ * 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. + *

* * @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. diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/monitoring/CEQueueStatusImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/monitoring/CEQueueStatusImpl.java index 25102d3a8cf..e786e5997eb 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/monitoring/CEQueueStatusImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/monitoring/CEQueueStatusImpl.java @@ -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"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/monitoring/CEQueueStatusImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/monitoring/CEQueueStatusImplTest.java index 15b69ad344a..b1d82c150b4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/monitoring/CEQueueStatusImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/monitoring/CEQueueStatusImplTest.java @@ -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); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/monitoring/ComputeEngineQueueMonitorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/monitoring/ComputeEngineQueueMonitorTest.java index 1587ab5f044..58739595d89 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/monitoring/ComputeEngineQueueMonitorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/monitoring/ComputeEngineQueueMonitorTest.java @@ -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"); + } } } -- 2.39.5