Change getUniqueJobParameters() to use random number generator that has a lower chance of repeating

Issue #821
This commit is contained in:
Saikat Bhadra
2020-04-25 17:40:48 -07:00
committed by Mahmoud Ben Hassine
parent 900a6738c4
commit 342554a720
2 changed files with 19 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.test;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
@@ -160,7 +161,7 @@ public class JobLauncherTestUtils {
*/
public JobParameters getUniqueJobParameters() {
Map<String, JobParameter> parameters = new HashMap<>();
parameters.put("random", new JobParameter((long) (Math.random() * JOB_PARAMETER_MAXIMUM)));
parameters.put("random", new JobParameter(new SecureRandom().nextLong()));
return new JobParameters(parameters);
}

View File

@@ -20,6 +20,7 @@ import org.junit.Test;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
@@ -35,7 +36,11 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import java.util.HashSet;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
/**
* @author mminella
@@ -53,6 +58,18 @@ public class JobLauncherTestUtilsTests {
assertEquals(ExitStatus.COMPLETED, execution.getExitStatus());
}
@Test
public void getUniqueJobParameters_doesNotRepeatJobParameters() {
ApplicationContext context = new AnnotationConfigApplicationContext(TestJobConfiguration.class);
JobLauncherTestUtils testUtils = context.getBean(JobLauncherTestUtils.class);
Set<JobParameters> jobParametersSeen = new HashSet<>();
for (int i = 0; i < 10_000; i++) {
JobParameters jobParameters = testUtils.getUniqueJobParameters();
assertFalse(jobParametersSeen.contains(jobParameters));
jobParametersSeen.add(jobParameters);
}
}
@Configuration
@EnableBatchProcessing
public static class TestJobConfiguration {