Skip to content

Commit

Permalink
GEODE-8643: Fix IllegalArgumentException in ParallelGatewaySenderQueu… (
Browse files Browse the repository at this point in the history
apache#5653)

* GEODE-8643: Fix IllegalArgumentException in ParallelGatewaySenderQueue for negative sleep value

* GEODE-8643: Small corrections after review
  • Loading branch information
albertogpz authored Nov 19, 2020
1 parent 57db39c commit 475dce8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ public List peek(int batchSize, int timeToWait) throws InterruptedException, Cac
// Sleep a bit before trying again.
long currentTime = System.currentTimeMillis();
try {
Thread.sleep(getTimeToSleep(end - currentTime));
Thread.sleep(calculateTimeToSleep(end - currentTime));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
Expand Down Expand Up @@ -1393,7 +1393,11 @@ private boolean areAllTransactionsCompleteInBatch(Map incompleteTransactions) {
return (incompleteTransactions.size() == 0);
}

private long getTimeToSleep(long timeToWait) {
@VisibleForTesting
static long calculateTimeToSleep(long timeToWait) {
if (timeToWait <= 0) {
return 0;
}
// Get the minimum of 50 and 5% of the time to wait (which by default is 1000 ms)
long timeToSleep = Math.min(50L, ((long) (timeToWait * 0.05)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,26 @@ public void peekDoesNotGetExtraEventsWhenMustGroupTransactionEventsAndNotAllEven
assertEquals(3, peekedAfter.size());
}

@Test
public void testCalculateTimeToSleepNegativeInputReturnsZero() {
assertEquals(0L, ParallelGatewaySenderQueue.calculateTimeToSleep(-3));
}

@Test
public void testCalculateTimeToSleepZeroInputReturnsZero() {
assertEquals(0L, ParallelGatewaySenderQueue.calculateTimeToSleep(0));
}

@Test
public void testCalculateTimeToSleepInputGreaterThanOneThousand() {
assertEquals(50L, ParallelGatewaySenderQueue.calculateTimeToSleep(1002));
}

@Test
public void testCalculateTimeToSleepInputSmallerThanOneThousand() {
assertEquals(2L, ParallelGatewaySenderQueue.calculateTimeToSleep(40));
}

private GatewaySenderEventImpl createGatewaySenderEventImpl(int transactionId,
boolean isLastEventInTransaction) {
GatewaySenderEventImpl event = mock(GatewaySenderEventImpl.class);
Expand Down

0 comments on commit 475dce8

Please sign in to comment.