/**
* 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
*
*/
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.
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");
}
@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();
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);