Skip to content

Commit 4f8609b

Browse files
HongYeseulfmbenhassine
authored andcommitted
Generate unique IDs for StepExecution and JobExecution in MetaDataInstanceFactory to prevent collisions
Resolves #5181 Signed-off-by: HongYeseul <yeseul.dev@gmail.com>
1 parent cc06132 commit 4f8609b

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

spring-batch-test/src/main/java/org/springframework/batch/test/MetaDataInstanceFactory.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.batch.test;
1717

1818
import java.util.Collection;
19+
import java.util.concurrent.atomic.AtomicLong;
1920

2021
import org.springframework.batch.core.job.JobExecution;
2122
import org.springframework.batch.core.job.JobInstance;
@@ -58,6 +59,16 @@ public class MetaDataInstanceFactory {
5859
*/
5960
public static final long DEFAULT_STEP_EXECUTION_ID = 1234L;
6061

62+
/**
63+
* Atomic counter for generating unique job execution IDs in tests
64+
*/
65+
private static final AtomicLong jobExecutionIdCounter = new AtomicLong(DEFAULT_JOB_EXECUTION_ID);
66+
67+
/**
68+
* Atomic counter for generating unique step execution IDs in tests
69+
*/
70+
private static final AtomicLong stepExecutionIdCounter = new AtomicLong(DEFAULT_STEP_EXECUTION_ID);
71+
6172
/**
6273
* Create a {@link JobInstance} with the parameters provided.
6374
* @param jobName the name of the job
@@ -114,7 +125,7 @@ public static JobExecution createJobExecution(String jobName, Long instanceId, L
114125
* @return a {@link JobExecution}
115126
*/
116127
public static JobExecution createJobExecution(String jobName, Long instanceId, Long executionId,
117-
JobParameters jobParameters) {
128+
JobParameters jobParameters) {
118129
return new JobExecution(executionId, createJobInstance(jobName, instanceId), jobParameters);
119130
}
120131

@@ -170,6 +181,8 @@ public static JobExecution createJobExecutionWithStepExecutions(Long executionId
170181
/**
171182
* Create a {@link StepExecution} and all its parent entities with default values, but
172183
* using the {@link ExecutionContext} and {@link JobParameters} provided.
184+
* Each invocation generates unique IDs to avoid collision in
185+
* {@link org.springframework.batch.core.scope.context.StepSynchronizationManager}.
173186
* @param jobParameters come {@link JobParameters}
174187
* @param executionContext some {@link ExecutionContext}
175188
* @return a {@link StepExecution} with the execution context provided
@@ -183,13 +196,17 @@ public static StepExecution createStepExecution(JobParameters jobParameters, Exe
183196
/**
184197
* Create a {@link StepExecution} and all its parent entities with default values, but
185198
* using the {@link JobParameters} provided.
199+
* Each invocation generates unique IDs to avoid collision in
200+
* {@link org.springframework.batch.core.scope.context.StepSynchronizationManager}.
186201
* @param jobParameters some {@link JobParameters}
187202
* @return a {@link StepExecution} with the job parameters provided
188203
*/
189204
public static StepExecution createStepExecution(JobParameters jobParameters) {
190-
JobExecution jobExecution = createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID,
191-
DEFAULT_JOB_EXECUTION_ID, jobParameters);
192-
StepExecution stepExecution = createStepExecution(jobExecution, DEFAULT_STEP_NAME, DEFAULT_STEP_EXECUTION_ID);
205+
Long jobExecutionId = jobExecutionIdCounter.incrementAndGet();
206+
Long stepExecutionId = stepExecutionIdCounter.incrementAndGet();
207+
JobExecution jobExecution = createJobExecution(DEFAULT_JOB_NAME, DEFAULT_JOB_INSTANCE_ID, jobExecutionId,
208+
jobParameters);
209+
StepExecution stepExecution = createStepExecution(jobExecution, DEFAULT_STEP_NAME, stepExecutionId);
193210
jobExecution.addStepExecution(stepExecution);
194211
return stepExecution;
195212
}

spring-batch-test/src/test/java/org/springframework/batch/test/MetaDataInstanceFactoryTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.batch.test;
1717

1818
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1920
import static org.junit.jupiter.api.Assertions.assertNotNull;
2021

2122
import java.util.List;
@@ -106,4 +107,18 @@ void testCreateJobExecutionWithStepExecutions() {
106107
assertNotNull(MetaDataInstanceFactory.createJobExecutionWithStepExecutions(executionId, List.of(stepName)));
107108
}
108109

110+
@Test
111+
void testCreateStepExecutionWithJobParametersShouldGenerateUniqueIds() {
112+
JobParameters params1 = new JobParametersBuilder().addString("key", "value1").toJobParameters();
113+
JobParameters params2 = new JobParametersBuilder().addString("key", "value2").toJobParameters();
114+
115+
StepExecution step1 = MetaDataInstanceFactory.createStepExecution(params1);
116+
StepExecution step2 = MetaDataInstanceFactory.createStepExecution(params2);
117+
118+
assertNotEquals(step1.getId(), step2.getId());
119+
assertNotEquals(step1.getJobExecutionId(), step2.getJobExecutionId());
120+
121+
assertNotEquals(step1, step2);
122+
}
123+
109124
}

0 commit comments

Comments
 (0)