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 22, 2024
1 parent 7180ae4 commit 9fa3167
Show file tree
Hide file tree
Showing 6 changed files with 47 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(500));
}

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(500));
}

@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(100));
}
}
37 changes: 37 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,37 @@
package test.support;

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

public final class SafeRandoms {

private static final Random random = new Random();

private SafeRandoms() {}

/**
* @param bound - The upper bound (exclusive). Must be positive.
* @return - the next pseudorandom, uniformly distributed int value between 1 and bound
* (exclusive) from this random number generator's sequence
*/
public static int nextInt(int bound) {
return nextInt(random, bound);
}

/**
* @param random - An existing instance of {@link Random} to be used.
* @param bound - The upper bound (exclusive). Must be positive.
* @return - the next pseudorandom, uniformly distributed int value between 1 and bound
* (exclusive) from this random number generator's sequence
*/
public static int nextInt(Random random, int bound) {
if (bound <= 0) {
throw new IllegalStateException("The upper bound should be positive number.");
}
int value = 0;
while (value == 0) {
value = Objects.requireNonNull(random).nextInt(bound);
}
return value;
}
}
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(random, 10));
} 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(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 9fa3167

Please sign in to comment.