Skip to content

Commit

Permalink
Streamline random generation
Browse files Browse the repository at this point in the history
* Add edit checks to ensure that when using Random
it always gives a non zero value
  • Loading branch information
krmahadevan committed Mar 30, 2024
1 parent bb2d8d2 commit e5218ab
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package test.dataprovider.issue3081;

import java.security.SecureRandom;
import java.util.Collections;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import test.support.SafeRandoms;

public class TestClassSample {
private static final Set<Long> logs = ConcurrentHashMap.newKeySet();
private static final Random random = new SecureRandom();

public static Set<Long> getLogs() {
return Collections.unmodifiableSet(logs);
Expand All @@ -30,7 +28,7 @@ public static Object[] parallelDpStrings() {
@Test(dataProvider = "parallelDpStrings")
public void testStrings(String ignored) throws InterruptedException {
print();
TimeUnit.MILLISECONDS.sleep(random.nextInt(500));
TimeUnit.MILLISECONDS.sleep(SafeRandoms.nextInt(200, 300));
}

private static void print() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package test.dataprovider.issue3081;

import java.security.SecureRandom;
import java.util.Collections;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import test.support.SafeRandoms;

public class TestClassWithPrioritiesSample {
private static final Set<Long> logs = ConcurrentHashMap.newKeySet();
private static final Random random = new SecureRandom();

public static Set<Long> getLogs() {
return Collections.unmodifiableSet(logs);
Expand All @@ -30,7 +28,7 @@ public static Object[] parallelDpStrings() {
@Test(dataProvider = "parallelDpStrings", priority = 1)
public void testStrings(String ignored) throws InterruptedException {
print();
TimeUnit.MILLISECONDS.sleep(random.nextInt(500));
TimeUnit.MILLISECONDS.sleep(SafeRandoms.nextInt(200, 300));
}

@Test(priority = 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import test.support.SafeRandoms;

public class SampleTestClass {

Expand All @@ -18,12 +18,10 @@ public class SampleTestClass {
static final String BARNEY = "Barney";

private final String instance;
private final Random random;

@Factory(dataProvider = "dp")
public SampleTestClass(String instance) {
this.instance = instance;
random = new Random();
}

@DataProvider
Expand All @@ -50,6 +48,6 @@ public String toString() {
private void printer() throws InterruptedException {
ITestResult result = Reporter.getCurrentTestResult();
result.setAttribute(THREAD_ID, Thread.currentThread().getId());
TimeUnit.MILLISECONDS.sleep(10 * random.nextInt(100));
TimeUnit.MILLISECONDS.sleep(10L * SafeRandoms.nextInt(30, 70));
}
}
39 changes: 39 additions & 0 deletions testng-core/src/test/java/test/support/SafeRandoms.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test.support;

import static org.assertj.core.util.Preconditions.checkArgument;

import java.util.Objects;
import java.util.Random;

public final class SafeRandoms {

private static final Random random = new Random();

private SafeRandoms() {}

/**
* @param delta - Represents a constant that should be added to the generated random value. This
* will ensure that there are no zero values generated.
* @param upperBound - The upper bound (exclusive). Must be positive.
* @return - A random number which is a summation of the delta and the actual random value that is
* lesser than the upperBound value.
*/
public static int nextInt(int delta, int upperBound) {
return nextInt(delta, upperBound, random);
}

/**
* @param delta - Represents a constant that should be added to the generated random value. This
* will ensure that there are no zero values generated.
* @param upperBound - The upper bound (exclusive). Must be positive.
* @param random - An existing instance of {@link Random} to be used.
* @return - A random number which is a summation of the delta and the actual random value that is
* lesser than the upperBound value.
*/
public static int nextInt(int delta, int upperBound, Random random) {
checkArgument(delta >= 0, "Delta should be non-zero");
checkArgument(upperBound >= 0, "Upper bound should be non-zero");
checkArgument(delta < upperBound, "Delta should be less than Upper bound");
return delta + Objects.requireNonNull(random).nextInt(upperBound);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Random;
import org.testng.annotations.Test;
import test.support.SafeRandoms;

@Test
public class TrueParallelSampleTest extends BaseThreadTest {
Expand All @@ -10,7 +11,7 @@ public class TrueParallelSampleTest extends BaseThreadTest {
private void log(String s) {
logString(s);
try {
Thread.sleep(random.nextInt(10));
Thread.sleep(SafeRandoms.nextInt(3, 7, random));
} catch (InterruptedException ex) {
Thread.yield();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package test.thread.issue188;

import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import test.support.SafeRandoms;

public class Issue188TestSample {
public static final Map<Long, Set<String>> timestamps = new ConcurrentHashMap<>();
private static final Random random = new Random();

@BeforeMethod
public void logTime(ITestResult itr) {
Expand All @@ -37,17 +36,9 @@ public void anotherSampleTest() {

private void sleepSilently() {
try {
TimeUnit.MILLISECONDS.sleep(500 * random());
TimeUnit.MILLISECONDS.sleep(500L * SafeRandoms.nextInt(1, 10));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

private static long random() {
int value = random.nextInt(10);
if (value == 0) {
return 1;
}
return value;
}
}

0 comments on commit e5218ab

Please sign in to comment.