Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE when manually flushing a batch #2444

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am removing this assert because it doesn't help with production code. I searched for uses of assert throughout the code and found only this and another one a few lines below, which I'm also removing.

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code here in prepareDefaultFlush() is already checking if the queue is empty before poll()ing from it, so I didn't have to do anything.

}

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,9 @@ static interface SelectiveBatching extends Commands, BatchExecutor {

}

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

}

}