|
| 1 | +package com.carrotsearch.randomizedtesting.jupiter; |
| 2 | + |
| 3 | +import java.io.Closeable; |
| 4 | +import java.util.Locale; |
| 5 | +import java.util.Objects; |
| 6 | +import java.util.Random; |
| 7 | + |
| 8 | +/** |
| 9 | + * A {@link Random} with a delegate, preventing {@link Random#setSeed(long)} and locked to only be |
| 10 | + * usable by a single {@link Thread}. |
| 11 | + */ |
| 12 | +final class AssertingRandom extends Random implements Closeable { |
| 13 | + private final Random delegate; |
| 14 | + private final Thread ownerRef; |
| 15 | + private final String ownerName; |
| 16 | + private final StackTraceElement[] allocationStack; |
| 17 | + |
| 18 | + /** |
| 19 | + * Track out-of-context use of this {@link Random} instance. This introduces memory barriers and |
| 20 | + * scheduling side effects but there's no other way to do it in any other way and sharing randoms |
| 21 | + * across threads or test cases is very bad and worth tracking. |
| 22 | + */ |
| 23 | + private volatile boolean valid = true; |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates an instance to be used by <code>owner</code> thread and delegating to <code>delegate |
| 27 | + * </code> until {@link #close()}ed. |
| 28 | + */ |
| 29 | + public AssertingRandom(Thread owner, Random delegate) { |
| 30 | + // Must be here, the only Random constructor. Has side effects on setSeed, see below. |
| 31 | + super(0); |
| 32 | + |
| 33 | + this.delegate = delegate; |
| 34 | + this.ownerRef = Objects.requireNonNull(owner); |
| 35 | + this.ownerName = owner.toString(); |
| 36 | + this.allocationStack = Thread.currentThread().getStackTrace(); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected int next(int bits) { |
| 41 | + throw new RuntimeException("Shouldn't be reachable."); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean nextBoolean() { |
| 46 | + checkValid(); |
| 47 | + return delegate.nextBoolean(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void nextBytes(byte[] bytes) { |
| 52 | + checkValid(); |
| 53 | + delegate.nextBytes(bytes); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public double nextDouble() { |
| 58 | + checkValid(); |
| 59 | + return delegate.nextDouble(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public float nextFloat() { |
| 64 | + checkValid(); |
| 65 | + return delegate.nextFloat(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public double nextGaussian() { |
| 70 | + checkValid(); |
| 71 | + return delegate.nextGaussian(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public int nextInt() { |
| 76 | + checkValid(); |
| 77 | + return delegate.nextInt(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public int nextInt(int n) { |
| 82 | + checkValid(); |
| 83 | + return delegate.nextInt(n); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public long nextLong() { |
| 88 | + checkValid(); |
| 89 | + return delegate.nextLong(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void setSeed(long seed) { |
| 94 | + // This is an interesting case of observing uninitialized object from an instance method |
| 95 | + // (this method is called from the superclass constructor). |
| 96 | + if (seed == 0 && delegate == null) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + throw noSetSeed(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public String toString() { |
| 105 | + checkValid(); |
| 106 | + return delegate.toString(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public boolean equals(Object obj) { |
| 111 | + checkValid(); |
| 112 | + return delegate.equals(obj); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public int hashCode() { |
| 117 | + checkValid(); |
| 118 | + return delegate.hashCode(); |
| 119 | + } |
| 120 | + |
| 121 | + /** This object will no longer be usable after this method is called. */ |
| 122 | + public void close() { |
| 123 | + this.valid = false; |
| 124 | + } |
| 125 | + |
| 126 | + private static final class StackTraceHolder extends Throwable { |
| 127 | + public StackTraceHolder(String message) { |
| 128 | + super(message); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /* */ |
| 133 | + private void checkValid() { |
| 134 | + if (!valid) { |
| 135 | + throw new RuntimeException( |
| 136 | + "This Random instance has been invalidated and " |
| 137 | + + "is probably used out of its allowed context (test or suite)."); |
| 138 | + } |
| 139 | + |
| 140 | + if (Thread.currentThread() != ownerRef) { |
| 141 | + Throwable allocationEx = |
| 142 | + new StackTraceHolder( |
| 143 | + "Original allocation stack for this Random (" + "allocated by " + ownerName + ")"); |
| 144 | + allocationEx.setStackTrace(allocationStack); |
| 145 | + throw new RuntimeException( |
| 146 | + String.format( |
| 147 | + Locale.ROOT, |
| 148 | + "This Random instance is tied to thread %s, can't access it from thread: %s " |
| 149 | + + "(Random instances must not be shared). Allocation stack is included as a nested exception.", |
| 150 | + ownerName, |
| 151 | + Thread.currentThread()), |
| 152 | + allocationEx); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + protected Object clone() throws CloneNotSupportedException { |
| 158 | + checkValid(); |
| 159 | + throw new CloneNotSupportedException("Don't clone test Randoms."); |
| 160 | + } |
| 161 | + |
| 162 | + static RuntimeException noSetSeed() { |
| 163 | + return new RuntimeException( |
| 164 | + "Changing the seed of Random instances is forbidden, it breaks repeatability" |
| 165 | + + " of tests. If you need a mutable instance of Random, create a new (local) instance," |
| 166 | + + " preferably with the initial seed acquired from this Random instance."); |
| 167 | + } |
| 168 | +} |
0 commit comments