Skip to content

Commit 706add7

Browse files
committed
Fix unnecessary reads when chunk size exceeds item count
Resolves #5048
1 parent d6ce9f6 commit 706add7

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/ChunkOrientedStep.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ private Chunk<I> readChunk(StepContribution contribution) throws Exception {
482482
if (item != null) {
483483
chunk.add(item);
484484
}
485+
else {
486+
break;
487+
}
485488
}
486489
return chunk;
487490
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2025-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.step.item;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import org.springframework.batch.core.job.JobExecution;
21+
import org.springframework.batch.core.job.JobInstance;
22+
import org.springframework.batch.core.job.parameters.JobParameters;
23+
import org.springframework.batch.core.repository.JobRepository;
24+
import org.springframework.batch.core.repository.support.ResourcelessJobRepository;
25+
import org.springframework.batch.core.step.StepExecution;
26+
import org.springframework.batch.infrastructure.item.ItemReader;
27+
import org.springframework.batch.infrastructure.item.ItemWriter;
28+
29+
import static org.mockito.Mockito.mock;
30+
import static org.mockito.Mockito.times;
31+
import static org.mockito.Mockito.verify;
32+
import static org.mockito.Mockito.when;
33+
34+
/**
35+
* @author Mahmoud Ben Hassine
36+
*/
37+
public class ChunkOrientedStepTests {
38+
39+
@Test
40+
void testReadNoMoreThanAvailableItems() throws Exception {
41+
// given
42+
ItemReader<String> reader = mock();
43+
ItemWriter<String> writer = chunk -> {
44+
};
45+
JobRepository jobRepository = new ResourcelessJobRepository();
46+
when(reader.read()).thenReturn("1", "2", "3", "4", "5", null);
47+
ChunkOrientedStep<String, String> step = new ChunkOrientedStep<>("step", 10, reader, writer, jobRepository);
48+
step.afterPropertiesSet();
49+
JobInstance jobInstance = new JobInstance(1L, "job");
50+
JobExecution jobExecution = new JobExecution(1L, jobInstance, new JobParameters());
51+
StepExecution stepExecution = new StepExecution(1L, "step", jobExecution);
52+
53+
// when
54+
step.execute(stepExecution);
55+
56+
// then
57+
verify(reader, times(6)).read();
58+
}
59+
60+
}

0 commit comments

Comments
 (0)