Skip to content

Commit 2cb087d

Browse files
committed
Hide the implementation details under a subpackage.
1 parent d189d17 commit 2cb087d

23 files changed

Lines changed: 228 additions & 188 deletions

randomizedtesting-jupiter/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ test {
1111
excludeTags 'nested-integration-test'
1212
}
1313
jvmArgs("-Dnet.bytebuddy.safe=true")
14-
}
14+
}

randomizedtesting-jupiter/src/main/java/com/carrotsearch/randomizedtesting/jupiter/DetectThreadLeaks.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.carrotsearch.randomizedtesting.jupiter;
22

3+
import com.carrotsearch.randomizedtesting.jupiter.internals.DetectThreadLeaksExtension;
34
import java.lang.annotation.Documented;
45
import java.lang.annotation.ElementType;
56
import java.lang.annotation.Inherited;

randomizedtesting-jupiter/src/main/java/com/carrotsearch/randomizedtesting/jupiter/FixSeed.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.carrotsearch.randomizedtesting.jupiter;
22

3+
import com.carrotsearch.randomizedtesting.jupiter.internals.RandomizedContextExtension;
34
import java.lang.annotation.Documented;
45
import java.lang.annotation.ElementType;
56
import java.lang.annotation.Retention;
@@ -12,13 +13,12 @@
1213
* to use a constant seed (for reproducing a problem or other reasons).
1314
*
1415
* <p>Note that seed fixing is always possible by setting {@link
15-
* com.carrotsearch.randomizedtesting.jupiter.RandomizedContextSupplier.SysProps#TESTS_SEED} system
16-
* property, this is just convenience.
16+
* RandomizedContextExtension.SysProps#TESTS_SEED} system property, this is just convenience.
1717
*/
1818
@Target({ElementType.TYPE, ElementType.METHOD})
1919
@Documented
2020
@Retention(RetentionPolicy.RUNTIME)
21-
@ExtendWith(RandomizedContextSupplier.class)
21+
@ExtendWith(RandomizedContextExtension.class)
2222
public @interface FixSeed {
2323
String value();
2424
}

randomizedtesting-jupiter/src/main/java/com/carrotsearch/randomizedtesting/jupiter/Hashing.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/** Static hashing utilities. */
44
public final class Hashing {
55
/** Bit mixer for {@code long} values. */
6-
public static long mix64(long k) {
6+
public static long hash(long k) {
77
k ^= k >>> 33;
88
k *= 0xff51afd7ed558ccdL;
99
k ^= k >>> 33;
@@ -13,12 +13,12 @@ public static long mix64(long k) {
1313
}
1414

1515
/** String hash function redistributing over a {@code long}. */
16-
public static long longHash(String v) {
16+
public static long hash(String v) {
1717
long h = 0;
1818
int length = v.length();
1919
for (int i = 0; i < length; i++) {
2020
h = 31 * h + v.charAt(i);
2121
}
22-
return mix64(h);
22+
return hash(h);
2323
}
2424
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.carrotsearch.randomizedtesting.jupiter;
22

3+
import com.carrotsearch.randomizedtesting.jupiter.internals.RandomizedContextExtension;
4+
import com.carrotsearch.randomizedtesting.jupiter.internals.RandomizedContextImpl;
35
import java.lang.annotation.Documented;
46
import java.lang.annotation.ElementType;
57
import java.lang.annotation.Retention;
@@ -8,12 +10,12 @@
810
import org.junit.jupiter.api.extension.ExtendWith;
911

1012
/**
11-
* This annotation should be placed on classes that want access to {@link RandomizedContext}. This
12-
* extension injects {@link RandomizedContext} parameter type automatically into test methods and
13-
* lifecycle hooks.
13+
* This annotation should be placed on classes that want access to {@link RandomizedContextImpl}.
14+
* This extension injects {@link RandomizedContextImpl} parameter type automatically into test
15+
* methods and lifecycle hooks.
1416
*/
1517
@Target({ElementType.TYPE})
1618
@Documented
1719
@Retention(RetentionPolicy.RUNTIME)
18-
@ExtendWith(RandomizedContextSupplier.class)
20+
@ExtendWith(RandomizedContextExtension.class)
1921
public @interface Randomized {}
Lines changed: 4 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,11 @@
11
package com.carrotsearch.randomizedtesting.jupiter;
22

3-
import java.io.Closeable;
4-
import java.io.IOException;
5-
import java.util.ArrayList;
6-
import java.util.Collections;
7-
import java.util.Locale;
8-
import java.util.Objects;
93
import java.util.Random;
10-
import org.junit.jupiter.api.extension.ExtensionContext;
114

12-
public final class RandomizedContext implements Closeable {
13-
private final RandomizedContext parent;
14-
private final Seed seed;
15-
final String contextId;
5+
public interface RandomizedContext {
6+
SeedChain getSeedChain();
167

17-
private final SeedChain remainingSeedChain;
8+
Seed getRootSeed();
189

19-
private final Random random;
20-
private final RandomFactory randomFactory;
21-
22-
RandomizedContext(
23-
String contextId,
24-
RandomizedContext parent,
25-
RandomFactory randomFactory,
26-
Seed seed,
27-
SeedChain remainingSeedChain) {
28-
this.contextId = contextId;
29-
this.parent = parent;
30-
this.remainingSeedChain = remainingSeedChain;
31-
this.randomFactory = randomFactory;
32-
33-
assert !seed.isUnspecified();
34-
this.seed = seed;
35-
this.random = randomFactory.apply(seed.value());
36-
}
37-
38-
@Override
39-
public String toString() {
40-
return "Randomized context [" + ("seedChain=" + getSeedChain() + ",") + "]";
41-
}
42-
43-
public SeedChain getSeedChain() {
44-
ArrayList<Seed> seeds = new ArrayList<>();
45-
for (RandomizedContext c = this; c != null; c = c.getParent()) {
46-
seeds.add(c.seed);
47-
}
48-
Collections.reverse(seeds);
49-
return new SeedChain(seeds);
50-
}
51-
52-
/**
53-
* @return Returns the root seed (randomization source).
54-
* @see RandomizedContextSupplier.SysProps#TESTS_SEED
55-
*/
56-
public Seed getRootSeed() {
57-
return getSeedChain().seeds().getFirst();
58-
}
59-
60-
private RandomizedContext getParent() {
61-
return parent;
62-
}
63-
64-
public Random getRandom() {
65-
return random;
66-
}
67-
68-
RandomizedContext deriveNew(ExtensionContext extensionContext) {
69-
// sanity check.
70-
{
71-
var id = extensionContext.getUniqueId();
72-
for (var ctx = this; ctx != null; ctx = ctx.getParent()) {
73-
if (Objects.equals(ctx.contextId, id)) {
74-
throw new RuntimeException(
75-
"deriveNew on a context that is already present in the parent chain: " + id);
76-
}
77-
}
78-
}
79-
80-
SeedChain seedChain;
81-
var annotationSeed = extensionContext.getElement().map(e -> e.getAnnotation(FixSeed.class));
82-
if (annotationSeed.isPresent()) {
83-
seedChain = SeedChain.parse(annotationSeed.get().value());
84-
for (var seed : seedChain.seeds()) {
85-
if (seed.isUnspecified()) {
86-
throw new RuntimeException(
87-
String.format(
88-
Locale.ROOT,
89-
"@%s annotatoin must declare concrete seeds or seed chains on: %s",
90-
FixSeed.class.getName(),
91-
extensionContext.getElement().get()));
92-
}
93-
}
94-
} else {
95-
seedChain = this.remainingSeedChain;
96-
}
97-
98-
var firstAndRest = seedChain.pop();
99-
var nextSeed = firstAndRest.first();
100-
var remainingChain = firstAndRest.rest();
101-
if (nextSeed.isUnspecified()) {
102-
nextSeed = new Seed(this.seed.value() ^ Hashing.longHash(extensionContext.getUniqueId()));
103-
}
104-
105-
return new RandomizedContext(
106-
extensionContext.getUniqueId(), this, randomFactory, nextSeed, remainingChain);
107-
}
108-
109-
@Override
110-
public void close() throws IOException {
111-
if (random instanceof Closeable c) {
112-
c.close();
113-
}
114-
}
10+
Random getRandom();
11511
}

randomizedtesting-jupiter/src/main/java/com/carrotsearch/randomizedtesting/jupiter/Seed.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
/** A single randomization seed (typically part of a larger {@link SeedChain}). */
44
public record Seed(long value) {
5+
public static final Seed UNSPECIFIED = new Seed(0);
56
private static final char[] HEX = "0123456789ABCDEF".toCharArray();
6-
static final Seed UNSPECIFIED = new Seed(0);
77

88
@Override
99
public String toString() {

randomizedtesting-jupiter/src/main/java/com/carrotsearch/randomizedtesting/jupiter/SeedChain.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
* contexts.
1212
*/
1313
public record SeedChain(List<Seed> seeds) {
14-
private static final SeedChain EMPTY = new SeedChain(List.of());
15-
1614
public static SeedChain parse(String chain) {
1715
return new SeedChain(
1816
Stream.of(chain.replaceAll("[\\[\\]]", "").split("[:]"))
@@ -38,16 +36,4 @@ public String toString() {
3836
.collect(Collectors.joining(":"))
3937
+ "]";
4038
}
41-
42-
record FirstAndRest(Seed first, SeedChain rest) {}
43-
44-
FirstAndRest pop() {
45-
if (seeds.isEmpty()) {
46-
return new FirstAndRest(Seed.UNSPECIFIED, SeedChain.EMPTY);
47-
}
48-
49-
var first = seeds.iterator().next();
50-
var rest = new SeedChain(seeds.subList(1, seeds.size()));
51-
return new FirstAndRest(first, rest);
52-
}
5339
}

randomizedtesting-jupiter/src/main/java/com/carrotsearch/randomizedtesting/jupiter/AssertingRandom.java renamed to randomizedtesting-jupiter/src/main/java/com/carrotsearch/randomizedtesting/jupiter/internals/AssertingRandom.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.carrotsearch.randomizedtesting.jupiter;
1+
package com.carrotsearch.randomizedtesting.jupiter.internals;
22

33
import java.io.Closeable;
44
import java.util.Locale;
@@ -9,7 +9,7 @@
99
* A {@link Random} with a delegate, preventing {@link Random#setSeed(long)} and locked to only be
1010
* usable by a single {@link Thread}.
1111
*/
12-
final class AssertingRandom extends Random implements Closeable {
12+
public final class AssertingRandom extends Random implements Closeable {
1313
private final Random delegate;
1414
private final Thread ownerRef;
1515
private final String ownerName;

randomizedtesting-jupiter/src/main/java/com/carrotsearch/randomizedtesting/jupiter/DetectThreadLeaksExtension.java renamed to randomizedtesting-jupiter/src/main/java/com/carrotsearch/randomizedtesting/jupiter/internals/DetectThreadLeaksExtension.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package com.carrotsearch.randomizedtesting.jupiter;
1+
package com.carrotsearch.randomizedtesting.jupiter.internals;
22

3+
import com.carrotsearch.randomizedtesting.jupiter.DetectThreadLeaks;
34
import java.time.Duration;
45
import java.util.ArrayList;
56
import java.util.HashSet;

0 commit comments

Comments
 (0)