Skip to content

Commit

Permalink
Fix NPE when manually flushing a batch
Browse files Browse the repository at this point in the history
When manually flushing a batch with a set size, we could potentially run into a NPE if the command queue is empty when flush() is called.
  • Loading branch information
luciopaiva committed Jul 12, 2023
1 parent 263e093 commit 6b05b7c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/java/io/lettuce/core/dynamic/SimpleBatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* </ul>
*
* @author Mark Paluch
* @author Lucio Paiva
*/
class SimpleBatcher implements Batcher {

Expand Down Expand Up @@ -148,12 +149,12 @@ private List<RedisCommand<Object, Object, Object>> prepareForceFlush() {

List<RedisCommand<Object, Object, Object>> batch = new ArrayList<>(Math.max(batchSize, 10));

do {
while (!queue.isEmpty()) {

RedisCommand<Object, Object, Object> poll = queue.poll();

assert poll != null;
batch.add(poll);
} while (!queue.isEmpty());
}

return batch;
}
Expand All @@ -166,7 +167,6 @@ private List<RedisCommand<Object, Object, Object>> prepareDefaultFlush(int consu

RedisCommand<Object, Object, Object> poll = queue.poll();

assert poll != null;
batch.add(poll);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

/**
* @author Mark Paluch
* @author Lucio Paiva
*/
@ExtendWith(LettuceExtension.class)
class RedisCommandsBatchingIntegrationTests extends TestSupport {
Expand Down Expand Up @@ -99,6 +100,16 @@ void selectiveBatchingShouldHandleErrors() {
}
}

@Test
void shouldNotCrashWhenFlushCalledWithEmptyQueue() {

RedisCommandFactory factory = new RedisCommandFactory(redis.getStatefulConnection());

SelectiveBatchingWithSize api = factory.getCommands(SelectiveBatchingWithSize.class);

api.flush();
}

@Test
void shouldExecuteBatchingSynchronously() {

Expand Down Expand Up @@ -215,4 +226,11 @@ static interface SelectiveBatching extends Commands, BatchExecutor {

}

@BatchSize(5)
interface SelectiveBatchingWithSize extends Commands, BatchExecutor {

void set(String key, String value);

}

}

0 comments on commit 6b05b7c

Please sign in to comment.