-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add ItemStreamReaderProcessor interfaces #115
Merged
acktsap
merged 2 commits into
naver:main
from
imbyungjun:topic/add-reader-processor-interface
Nov 14, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
374 changes: 374 additions & 0 deletions
374
...avercorp/spring/batch/plus/step/adapter/ItemStreamFluxReaderProcessorIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,374 @@ | ||
/* | ||
* Spring Batch Plus | ||
* | ||
* Copyright 2022-present NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.spring.batch.plus.step.adapter; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.RepeatedTest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.batch.core.*; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.configuration.annotation.StepScope; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.builder.StepBuilder; | ||
import org.springframework.batch.item.ExecutionContext; | ||
import org.springframework.batch.item.ItemStreamWriter; | ||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.jdbc.datasource.DataSourceTransactionManager; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.transaction.TransactionManager; | ||
import reactor.core.publisher.Flux; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.UUID; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import static com.navercorp.spring.batch.plus.step.adapter.AdapterFactory.*; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SuppressWarnings("unchecked") | ||
class ItemStreamFluxReaderProcessorIntegrationTest { | ||
|
||
private static final int TEST_REPEAT_COUNT = 5; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger( | ||
ItemStreamFluxReaderProcessorIntegrationTest.class); | ||
|
||
private static int onOpenReadCallCount = 0; | ||
private static int readContextCallCount = 0; | ||
private static int onUpdateReadCallCount = 0; | ||
private static int onCloseReadCallCount = 0; | ||
|
||
private static int processCallCount = 0; | ||
|
||
private static int itemCount = 0; | ||
private static int chunkCount = 0; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
onOpenReadCallCount = 0; | ||
readContextCallCount = 0; | ||
onUpdateReadCallCount = 0; | ||
onCloseReadCallCount = 0; | ||
|
||
processCallCount = 0; | ||
|
||
itemCount = ThreadLocalRandom.current().nextInt(10, 100); | ||
chunkCount = ThreadLocalRandom.current().nextInt(1, 10); | ||
|
||
logger.debug("itemCount: {}, chunkCount: {}", itemCount, chunkCount); | ||
} | ||
|
||
@RepeatedTest(TEST_REPEAT_COUNT) | ||
void testReaderProcessor() throws Exception { | ||
// given | ||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); | ||
JobRepository jobRepository = context.getBean(JobRepository.class); | ||
ItemStreamFluxReaderProcessor<Integer, Integer> testTasklet = context.getBean( | ||
"testTasklet", | ||
ItemStreamFluxReaderProcessor.class); | ||
ItemStreamWriter<Integer> emptyItemStreamWriter = context.getBean( | ||
"emptyItemStreamWriter", | ||
ItemStreamWriter.class); | ||
Job job = new JobBuilder("testJob", jobRepository) | ||
.start( | ||
new StepBuilder("testStep", jobRepository) | ||
.<Integer, Integer>chunk(chunkCount, new ResourcelessTransactionManager()) | ||
.reader(itemStreamReader(testTasklet)) | ||
.processor(itemProcessor(testTasklet)) | ||
.writer(emptyItemStreamWriter) | ||
.build() | ||
) | ||
.build(); | ||
JobLauncher jobLauncher = context.getBean(JobLauncher.class); | ||
int beforeRepeatCount = ThreadLocalRandom.current().nextInt(0, 3); | ||
for (int i = 0; i < beforeRepeatCount; ++i) { | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addString(UUID.randomUUID().toString(), UUID.randomUUID().toString()) | ||
.toJobParameters(); | ||
jobLauncher.run(job, jobParameters); | ||
} | ||
logger.debug("beforeRepeatCount: {}", beforeRepeatCount); | ||
|
||
// when | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addString(UUID.randomUUID().toString(), UUID.randomUUID().toString()) | ||
.toJobParameters(); | ||
JobExecution jobExecution = jobLauncher.run(job, jobParameters); | ||
|
||
// then | ||
assertThat(jobExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED); | ||
assertThat(onOpenReadCallCount).isEqualTo(beforeRepeatCount + 1); | ||
assertThat(readContextCallCount).isEqualTo(beforeRepeatCount + 1); | ||
assertThat(onUpdateReadCallCount).isGreaterThanOrEqualTo(beforeRepeatCount + 1); | ||
assertThat(onCloseReadCallCount).isEqualTo(beforeRepeatCount + 1); | ||
} | ||
|
||
@RepeatedTest(TEST_REPEAT_COUNT) | ||
void testReaderProcessorWithSameTaskletShouldKeepContext() throws Exception { | ||
// given | ||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); | ||
JobRepository jobRepository = context.getBean(JobRepository.class); | ||
ItemStreamFluxReaderProcessor<Integer, Integer> testTasklet = context.getBean( | ||
"testTasklet", | ||
ItemStreamFluxReaderProcessor.class); | ||
ItemStreamWriter<Integer> emptyItemStreamWriter = context.getBean( | ||
"emptyItemStreamWriter", | ||
ItemStreamWriter.class); | ||
Job job = new JobBuilder("testJob", jobRepository) | ||
.start( | ||
new StepBuilder("testStep", jobRepository) | ||
.<Integer, Integer>chunk(chunkCount, new ResourcelessTransactionManager()) | ||
.reader(itemStreamReader(testTasklet)) | ||
.processor(itemProcessor(testTasklet)) | ||
.writer(emptyItemStreamWriter) | ||
.build() | ||
) | ||
.build(); | ||
JobLauncher jobLauncher = context.getBean(JobLauncher.class); | ||
int beforeRepeatCount = ThreadLocalRandom.current().nextInt(0, 3); | ||
for (int i = 0; i < beforeRepeatCount; ++i) { | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addString(UUID.randomUUID().toString(), UUID.randomUUID().toString()) | ||
.toJobParameters(); | ||
jobLauncher.run(job, jobParameters); | ||
} | ||
logger.debug("beforeRepeatCount: {}", beforeRepeatCount); | ||
|
||
// when | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addString(UUID.randomUUID().toString(), UUID.randomUUID().toString()) | ||
.toJobParameters(); | ||
JobExecution jobExecution = jobLauncher.run(job, jobParameters); | ||
|
||
// then | ||
assertThat(jobExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED); | ||
// it's not changed since it keeps 'count' in a bean | ||
assertThat(processCallCount).isEqualTo(itemCount); | ||
} | ||
|
||
@RepeatedTest(TEST_REPEAT_COUNT) | ||
void testStepScopeReaderProcessor() throws Exception { | ||
// given | ||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); | ||
JobRepository jobRepository = context.getBean(JobRepository.class); | ||
ItemStreamFluxReaderProcessor<Integer, Integer> testTasklet = context.getBean( | ||
"stepScopeTestTasklet", | ||
ItemStreamFluxReaderProcessor.class); | ||
ItemStreamWriter<Integer> emptyItemStreamWriter = context.getBean( | ||
"emptyItemStreamWriter", | ||
ItemStreamWriter.class); | ||
Job job = new JobBuilder("testJob", jobRepository) | ||
.start( | ||
new StepBuilder("testStep", jobRepository) | ||
.<Integer, Integer>chunk(chunkCount, new ResourcelessTransactionManager()) | ||
.reader(itemStreamReader(testTasklet)) | ||
.processor(itemProcessor(testTasklet)) | ||
.writer(emptyItemStreamWriter) | ||
.build() | ||
) | ||
.build(); | ||
JobLauncher jobLauncher = context.getBean(JobLauncher.class); | ||
int beforeRepeatCount = ThreadLocalRandom.current().nextInt(0, 3); | ||
for (int i = 0; i < beforeRepeatCount; ++i) { | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addString(UUID.randomUUID().toString(), UUID.randomUUID().toString()) | ||
.toJobParameters(); | ||
jobLauncher.run(job, jobParameters); | ||
} | ||
logger.debug("beforeRepeatCount: {}", beforeRepeatCount); | ||
|
||
// when | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addString(UUID.randomUUID().toString(), UUID.randomUUID().toString()) | ||
.toJobParameters(); | ||
JobExecution jobExecution = jobLauncher.run(job, jobParameters); | ||
|
||
// then | ||
assertThat(jobExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED); | ||
assertThat(onOpenReadCallCount).isEqualTo(beforeRepeatCount + 1); | ||
assertThat(readContextCallCount).isEqualTo(beforeRepeatCount + 1); | ||
assertThat(onUpdateReadCallCount).isGreaterThanOrEqualTo(beforeRepeatCount + 1); | ||
assertThat(onCloseReadCallCount).isEqualTo(beforeRepeatCount + 1); | ||
} | ||
|
||
@RepeatedTest(TEST_REPEAT_COUNT) | ||
void testStepScopeReaderProcessorWithSameTaskletShouldNotKeepCountContext() throws Exception { | ||
// given | ||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); | ||
JobRepository jobRepository = context.getBean(JobRepository.class); | ||
ItemStreamFluxReaderProcessor<Integer, Integer> testTasklet = context.getBean( | ||
"stepScopeTestTasklet", | ||
ItemStreamFluxReaderProcessor.class); | ||
ItemStreamWriter<Integer> emptyItemStreamWriter = context.getBean( | ||
"emptyItemStreamWriter", | ||
ItemStreamWriter.class); | ||
Job job = new JobBuilder("testJob", jobRepository) | ||
.start( | ||
new StepBuilder("testStep", jobRepository) | ||
.<Integer, Integer>chunk(chunkCount, new ResourcelessTransactionManager()) | ||
.reader(itemStreamReader(testTasklet)) | ||
.processor(itemProcessor(testTasklet)) | ||
.writer(emptyItemStreamWriter) | ||
.build() | ||
) | ||
.build(); | ||
JobLauncher jobLauncher = context.getBean(JobLauncher.class); | ||
int beforeRepeatCount = ThreadLocalRandom.current().nextInt(0, 3); | ||
for (int i = 0; i < beforeRepeatCount; ++i) { | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addString(UUID.randomUUID().toString(), UUID.randomUUID().toString()) | ||
.toJobParameters(); | ||
jobLauncher.run(job, jobParameters); | ||
} | ||
logger.debug("beforeRepeatCount: {}", beforeRepeatCount); | ||
|
||
// when | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addString(UUID.randomUUID().toString(), UUID.randomUUID().toString()) | ||
.toJobParameters(); | ||
JobExecution jobExecution = jobLauncher.run(job, jobParameters); | ||
|
||
// then | ||
assertThat(jobExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED); | ||
// 'count' field is isolated per job instances since it is step scoped. so count is 0 for all job instances | ||
assertThat(processCallCount).isEqualTo(beforeRepeatCount * itemCount + itemCount); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
@EnableBatchProcessing( | ||
dataSourceRef = "metadataDataSource", | ||
transactionManagerRef = "metadataTransactionManager" | ||
) | ||
static class TestConfiguration { | ||
|
||
@Bean | ||
TransactionManager metadataTransactionManager() { | ||
return new DataSourceTransactionManager(metadataDataSource()); | ||
} | ||
|
||
@Bean | ||
DataSource metadataDataSource() { | ||
return new EmbeddedDatabaseBuilder() | ||
.setType(EmbeddedDatabaseType.H2) | ||
.addScript("/org/springframework/batch/core/schema-h2.sql") | ||
.generateUniqueName(true) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
ItemStreamFluxReaderProcessor<Integer, Integer> testTasklet() { | ||
return new ItemStreamFluxReaderProcessor<>() { | ||
|
||
private int count = 0; | ||
|
||
@Override | ||
public void onOpenRead(@NonNull ExecutionContext executionContext) { | ||
++onOpenReadCallCount; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Flux<Integer> readFlux(@NonNull ExecutionContext executionContext) { | ||
++readContextCallCount; | ||
return Flux.generate(sink -> { | ||
if (count < itemCount) { | ||
sink.next(count); | ||
++count; | ||
} else { | ||
sink.complete(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onUpdateRead(@NonNull ExecutionContext executionContext) { | ||
++onUpdateReadCallCount; | ||
} | ||
|
||
@Override | ||
public void onCloseRead() { | ||
++onCloseReadCallCount; | ||
} | ||
|
||
@Override | ||
public Integer process(@NonNull Integer item) { | ||
++processCallCount; | ||
return item; | ||
} | ||
}; | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
ItemStreamFluxReaderProcessor<Integer, Integer> stepScopeTestTasklet() { | ||
return new ItemStreamFluxReaderProcessor<>() { | ||
|
||
private int count = 0; | ||
|
||
@Override | ||
public void onOpenRead(@NonNull ExecutionContext executionContext) { | ||
++onOpenReadCallCount; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Flux<Integer> readFlux(@NonNull ExecutionContext executionContext) { | ||
++readContextCallCount; | ||
return Flux.generate(sink -> { | ||
if (count < itemCount) { | ||
sink.next(count); | ||
++count; | ||
} else { | ||
sink.complete(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onUpdateRead(@NonNull ExecutionContext executionContext) { | ||
++onUpdateReadCallCount; | ||
} | ||
|
||
@Override | ||
public void onCloseRead() { | ||
++onCloseReadCallCount; | ||
} | ||
|
||
@Override | ||
public Integer process(@NonNull Integer item) { | ||
++processCallCount; | ||
return item; | ||
} | ||
}; | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
ItemStreamWriter<Integer> emptyItemStreamWriter() { | ||
return chunk -> { /* noop */ }; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SimpleStepBuilder requires both reader and writer, I added no operation writer in integration tests.