Browse Source

SONAR-7183 add CeQueueStatus.addReceived(long)

tags/5.4-M11
Sébastien Lesaint 8 years ago
parent
commit
9e5c942745

+ 18
- 0
server/sonar-server/src/main/java/org/sonar/server/computation/monitoring/CEQueueStatus.java View 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.

+ 9
- 0
server/sonar-server/src/main/java/org/sonar/server/computation/monitoring/CEQueueStatusImpl.java View 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");

+ 50
- 1
server/sonar-server/src/test/java/org/sonar/server/computation/monitoring/CEQueueStatusImplTest.java View 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);

+ 7
- 2
server/sonar-server/src/test/java/org/sonar/server/computation/monitoring/ComputeEngineQueueMonitorTest.java View 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");
}
}
}

Loading…
Cancel
Save