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 0a94832
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/main/java/io/lettuce/core/dynamic/SimpleBatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,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 +166,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 @@ -99,6 +99,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 +225,11 @@ static interface SelectiveBatching extends Commands, BatchExecutor {

}

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

void set(String key, String value);

}

}

0 comments on commit 0a94832

Please sign in to comment.