Skip to content

Commit

Permalink
Merge pull request elastic#17394 from camilojd/refactor/replace-getra…
Browse files Browse the repository at this point in the history
…ndom

Refactor: replace all ocurrences of ESTestCase.getRandom() with LuceneTestCase.random()
  • Loading branch information
nik9000 committed Mar 30, 2016
2 parents 8180145 + 7be11a3 commit 1c16d63
Show file tree
Hide file tree
Showing 81 changed files with 188 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testRandom() throws IOException {
final int numDocs = randomIntBetween(10, 200);
final Document doc = new Document();
final Directory dir = newDirectory();
final RandomIndexWriter w = new RandomIndexWriter(getRandom(), dir);
final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
for (int i = 0; i < numDocs; ++i) {
w.addDocument(doc);
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/java/org/elasticsearch/ESExceptionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ public void testWriteThrowable() throws IOException {
}
assertArrayEquals(e.getStackTrace(), ex.getStackTrace());
assertTrue(e.getStackTrace().length > 1);
ElasticsearchAssertions.assertVersionSerializable(VersionUtils.randomVersion(getRandom()), t);
ElasticsearchAssertions.assertVersionSerializable(VersionUtils.randomVersion(getRandom()), ex);
ElasticsearchAssertions.assertVersionSerializable(VersionUtils.randomVersion(getRandom()), e);
ElasticsearchAssertions.assertVersionSerializable(VersionUtils.randomVersion(random()), t);
ElasticsearchAssertions.assertVersionSerializable(VersionUtils.randomVersion(random()), ex);
ElasticsearchAssertions.assertVersionSerializable(VersionUtils.randomVersion(random()), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void testBulkProcessorWaitOnClose() throws Exception {
//let's make sure that the bulk action limit trips, one single execution will index all the documents
.setConcurrentRequests(randomIntBetween(0, 1)).setBulkActions(numDocs)
.setFlushInterval(TimeValue.timeValueHours(24)).setBulkSize(new ByteSizeValue(randomIntBetween(1, 10),
RandomPicks.randomFrom(getRandom(), ByteSizeUnit.values())))
RandomPicks.randomFrom(random(), ByteSizeUnit.values())))
.build();

MultiGetRequestBuilder multiGetRequestBuilder = indexDocs(client(), processor, numDocs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class BulkShardRequestTests extends ESTestCase {
public void testToString() {
String index = randomSimpleString(getRandom(), 10);
String index = randomSimpleString(random(), 10);
int count = between(1, 100);
BulkShardRequest r = new BulkShardRequest(null, new ShardId(index, "ignored", 0), false, new BulkItemRequest[count]);
assertEquals("BulkShardRequest to [" + index + "] containing [" + count + "] requests", r.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testAnalyzerTokensAfterUpgrade() throws IOException, ExecutionExcept
// cause differences when the random string generated contains these complex characters. To mitigate
// the problem, we skip any strings containing these characters.
// TODO: only skip strings containing complex chars when comparing against ES <= 1.3.x
input = TestUtil.randomAnalysisString(getRandom(), 100, false);
input = TestUtil.randomAnalysisString(random(), 100, false);
matcher = complexUnicodeChars.matcher(input);
} while (matcher.find());

Expand Down Expand Up @@ -104,7 +104,7 @@ public void testAnalyzerTokensAfterUpgrade() throws IOException, ExecutionExcept
}

private String randomAnalyzer() {
PreBuiltAnalyzers preBuiltAnalyzers = RandomPicks.randomFrom(getRandom(), PreBuiltAnalyzers.values());
PreBuiltAnalyzers preBuiltAnalyzers = RandomPicks.randomFrom(random(), PreBuiltAnalyzers.values());
return preBuiltAnalyzers.name().toLowerCase(Locale.ROOT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public void testIndexRollingUpgrade() throws Exception {
IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs];
String[] indexForDoc = new String[docs.length];
for (int i = 0; i < numDocs; i++) {
docs[i] = client().prepareIndex(indexForDoc[i] = RandomPicks.randomFrom(getRandom(), indices), "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i), "num_int", randomInt(), "num_double", randomDouble());
docs[i] = client().prepareIndex(indexForDoc[i] = RandomPicks.randomFrom(random(), indices), "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i), "num_int", randomInt(), "num_double", randomDouble());
}
indexRandom(true, docs);
for (String index : indices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static class TestIteration implements Closeable {

TestIteration() {
threadPool = new ThreadPool("transport-client-nodes-service-tests");
transport = new FailAndRetryMockTransport<TestResponse>(getRandom()) {
transport = new FailAndRetryMockTransport<TestResponse>(random()) {
@Override
public List<String> getLocalAddresses() {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public MetaData.Builder remove(MetaData.Builder builder, String name) {
public IndexMetaData randomCreate(String name) {
IndexMetaData.Builder builder = IndexMetaData.builder(name);
Settings.Builder settingsBuilder = Settings.builder();
setRandomIndexSettings(getRandom(), settingsBuilder);
setRandomIndexSettings(random(), settingsBuilder);
settingsBuilder.put(randomSettings(Settings.EMPTY)).put(IndexMetaData.SETTING_VERSION_CREATED, randomVersion(random()));
builder.settings(settingsBuilder);
builder.numberOfShards(randomIntBetween(1, 10)).numberOfReplicas(randomInt(10));
Expand Down Expand Up @@ -672,6 +672,6 @@ public ClusterState.Custom randomChange(ClusterState.Custom part) {
* Generates a random name that starts with the given prefix
*/
private String randomName(String prefix) {
return prefix + Strings.randomBase64UUID(getRandom());
return prefix + Strings.randomBase64UUID(random());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void testReasonOrdinalOrder() {
}

public void testSerialization() throws Exception {
UnassignedInfo meta = new UnassignedInfo(RandomPicks.randomFrom(getRandom(), UnassignedInfo.Reason.values()), randomBoolean() ? randomAsciiOfLength(4) : null);
UnassignedInfo meta = new UnassignedInfo(RandomPicks.randomFrom(random(), UnassignedInfo.Reason.values()), randomBoolean() ? randomAsciiOfLength(4) : null);
BytesStreamOutput out = new BytesStreamOutput();
meta.writeTo(out);
out.close();
Expand Down Expand Up @@ -273,7 +273,7 @@ public void testUnassignedDelayedOnlyOnNodeLeft() throws Exception {
public void testUnassignedDelayOnlyNodeLeftNonNodeLeftReason() throws Exception {
EnumSet<UnassignedInfo.Reason> reasons = EnumSet.allOf(UnassignedInfo.Reason.class);
reasons.remove(UnassignedInfo.Reason.NODE_LEFT);
UnassignedInfo unassignedInfo = new UnassignedInfo(RandomPicks.randomFrom(getRandom(), reasons), null);
UnassignedInfo unassignedInfo = new UnassignedInfo(RandomPicks.randomFrom(random(), reasons), null);
long delay = unassignedInfo.updateDelay(unassignedInfo.getUnassignedTimeInNanos() + 1, // add 1 tick delay
Settings.builder().put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), "10h").build(), Settings.EMPTY);
assertThat(delay, equalTo(0L));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public void testPersistedSettings() {
public void testNoRebalanceOnPrimaryOverload() {
Settings.Builder settings = settingsBuilder();
AllocationService strategy = new AllocationService(settings.build(), randomAllocationDeciders(settings.build(),
new ClusterSettings(Settings.Builder.EMPTY_SETTINGS, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), getRandom()),
new ClusterSettings(Settings.Builder.EMPTY_SETTINGS, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), random()),
NoopGatewayAllocator.INSTANCE, new ShardsAllocator() {

public Map<DiscoveryNode, Float> weighShard(RoutingAllocation allocation, ShardRouting shard) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class RandomAllocationDeciderTests extends ESAllocationTestCase {
* already allocated on a node and balances the cluster to gain optimal
* balance.*/
public void testRandomDecisions() {
RandomAllocationDecider randomAllocationDecider = new RandomAllocationDecider(getRandom());
RandomAllocationDecider randomAllocationDecider = new RandomAllocationDecider(random());
AllocationService strategy = new AllocationService(settingsBuilder().build(), new AllocationDeciders(Settings.EMPTY,
new HashSet<>(Arrays.asList(new SameShardAllocationDecider(Settings.EMPTY), new ReplicaAfterPrimaryActiveAllocationDecider(Settings.EMPTY),
randomAllocationDecider))), NoopGatewayAllocator.INSTANCE, new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ public void testIndexEnableNone() {

public void testEnableClusterBalance() {
final boolean useClusterSetting = randomBoolean();
final Rebalance allowedOnes = RandomPicks.randomFrom(getRandom(), EnumSet.of(Rebalance.PRIMARIES, Rebalance.REPLICAS, Rebalance.ALL));
final Rebalance allowedOnes = RandomPicks.randomFrom(random(), EnumSet.of(Rebalance.PRIMARIES, Rebalance.REPLICAS, Rebalance.ALL));
Settings build = settingsBuilder()
.put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), useClusterSetting ? Rebalance.NONE: RandomPicks.randomFrom(getRandom(), Rebalance.values())) // index settings override cluster settings
.put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), useClusterSetting ? Rebalance.NONE: RandomPicks.randomFrom(random(), Rebalance.values())) // index settings override cluster settings
.put(ConcurrentRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_CLUSTER_CONCURRENT_REBALANCE_SETTING.getKey(), 3)
.put(ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_OUTGOING_RECOVERIES_SETTING.getKey(), 10)
.build();
ClusterSettings clusterSettings = new ClusterSettings(build, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
AllocationService strategy = createAllocationService(build, clusterSettings, getRandom());
AllocationService strategy = createAllocationService(build, clusterSettings, random());
Settings indexSettings = useClusterSetting ? Settings.EMPTY : settingsBuilder().put(EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), Rebalance.NONE).build();

logger.info("Building initial routing table");
Expand Down Expand Up @@ -260,11 +260,11 @@ public void testEnableClusterBalance() {
public void testEnableClusterBalanceNoReplicas() {
final boolean useClusterSetting = randomBoolean();
Settings build = settingsBuilder()
.put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), useClusterSetting ? Rebalance.NONE: RandomPicks.randomFrom(getRandom(), Rebalance.values())) // index settings override cluster settings
.put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), useClusterSetting ? Rebalance.NONE: RandomPicks.randomFrom(random(), Rebalance.values())) // index settings override cluster settings
.put(ConcurrentRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_CLUSTER_CONCURRENT_REBALANCE_SETTING.getKey(), 3)
.build();
ClusterSettings clusterSettings = new ClusterSettings(build, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
AllocationService strategy = createAllocationService(build, clusterSettings, getRandom());
AllocationService strategy = createAllocationService(build, clusterSettings, random());
Settings indexSettings = useClusterSetting ? Settings.EMPTY : settingsBuilder().put(EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), Rebalance.NONE).build();

logger.info("Building initial routing table");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public void testEquals() {
final int len = randomIntBetween(0, randomBoolean() ? 10: 100000);
final int offset1 = randomInt(5);
final byte[] array1 = new byte[offset1 + len + randomInt(5)];
getRandom().nextBytes(array1);
random().nextBytes(array1);
final int offset2 = randomInt(offset1);
final byte[] array2 = Arrays.copyOfRange(array1, offset1 - offset2, array1.length);

final BytesArray b1 = new BytesArray(array1, offset1, len);
final BytesArray b2 = new BytesArray(array2, offset2, len);
assertTrue(BytesReference.Helper.bytesEqual(b1, b2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void testStreamInput() throws IOException {

// buffer for bulk reads
byte[] origBuf = new byte[length];
getRandom().nextBytes(origBuf);
random().nextBytes(origBuf);
byte[] targetBuf = Arrays.copyOf(origBuf, origBuf.length);

// bulk-read 0 bytes: must not modify buffer
Expand Down Expand Up @@ -172,7 +172,7 @@ public void testStreamInputBulkReadWithOffset() throws IOException {
byte[] pbrBytesWithOffset = Arrays.copyOfRange(pbr.toBytes(), offset, length);
// randomized target buffer to ensure no stale slots
byte[] targetBytes = new byte[pbrBytesWithOffset.length];
getRandom().nextBytes(targetBytes);
random().nextBytes(targetBytes);

// bulk-read all
si.readFully(targetBytes);
Expand Down Expand Up @@ -574,7 +574,7 @@ private BytesReference getRandomizedPagedBytesReference(int length) {
ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(length, bigarrays);
try {
for (int i = 0; i < length; i++) {
out.writeByte((byte) getRandom().nextInt(1 << 8));
out.writeByte((byte) random().nextInt(1 << 8));
}
} catch (IOException e) {
fail("should not happen " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected AbstractCompressedStreamTestCase(Compressor compressor) {
}

public void testRandom() throws IOException {
Random r = getRandom();
Random r = random();
for (int i = 0; i < 10; i++) {
byte bytes[] = new byte[TestUtil.nextInt(r, 1, 100000)];
r.nextBytes(bytes);
Expand All @@ -55,7 +55,7 @@ public void testRandom() throws IOException {
}

public void testRandomThreads() throws Exception {
final Random r = getRandom();
final Random r = random();
int threadCount = TestUtil.nextInt(r, 2, 6);
Thread[] threads = new Thread[threadCount];
final CountDownLatch startingGun = new CountDownLatch(1);
Expand Down Expand Up @@ -86,7 +86,7 @@ public void run() {
}

public void testLineDocs() throws IOException {
Random r = getRandom();
Random r = random();
LineFileDocs lineFileDocs = new LineFileDocs(r);
for (int i = 0; i < 10; i++) {
int numDocs = TestUtil.nextInt(r, 1, 200);
Expand All @@ -101,7 +101,7 @@ public void testLineDocs() throws IOException {
}

public void testLineDocsThreads() throws Exception {
final Random r = getRandom();
final Random r = random();
int threadCount = TestUtil.nextInt(r, 2, 6);
Thread[] threads = new Thread[threadCount];
final CountDownLatch startingGun = new CountDownLatch(1);
Expand Down Expand Up @@ -138,7 +138,7 @@ public void run() {
}

public void testRepetitionsL() throws IOException {
Random r = getRandom();
Random r = random();
for (int i = 0; i < 10; i++) {
int numLongs = TestUtil.nextInt(r, 1, 10000);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Expand All @@ -161,7 +161,7 @@ public void testRepetitionsL() throws IOException {
}

public void testRepetitionsLThreads() throws Exception {
final Random r = getRandom();
final Random r = random();
int threadCount = TestUtil.nextInt(r, 2, 6);
Thread[] threads = new Thread[threadCount];
final CountDownLatch startingGun = new CountDownLatch(1);
Expand Down Expand Up @@ -206,7 +206,7 @@ public void run() {
}

public void testRepetitionsI() throws IOException {
Random r = getRandom();
Random r = random();
for (int i = 0; i < 10; i++) {
int numInts = TestUtil.nextInt(r, 1, 20000);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Expand All @@ -225,7 +225,7 @@ public void testRepetitionsI() throws IOException {
}

public void testRepetitionsIThreads() throws Exception {
final Random r = getRandom();
final Random r = random();
int threadCount = TestUtil.nextInt(r, 2, 6);
Thread[] threads = new Thread[threadCount];
final CountDownLatch startingGun = new CountDownLatch(1);
Expand Down Expand Up @@ -266,7 +266,7 @@ public void run() {
}

public void testRepetitionsS() throws IOException {
Random r = getRandom();
Random r = random();
for (int i = 0; i < 10; i++) {
int numShorts = TestUtil.nextInt(r, 1, 40000);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Expand All @@ -283,7 +283,7 @@ public void testRepetitionsS() throws IOException {
}

public void testMixed() throws IOException {
Random r = getRandom();
Random r = random();
LineFileDocs lineFileDocs = new LineFileDocs(r);
for (int i = 0; i < 2; ++i) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Expand Down Expand Up @@ -349,7 +349,7 @@ private void addBytes(Random r, ByteArrayOutputStream bos) throws IOException {
}

public void testRepetitionsSThreads() throws Exception {
final Random r = getRandom();
final Random r = random();
int threadCount = TestUtil.nextInt(r, 2, 6);
Thread[] threads = new Thread[threadCount];
final CountDownLatch startingGun = new CountDownLatch(1);
Expand Down Expand Up @@ -396,8 +396,8 @@ private void doTest(byte bytes[]) throws IOException {
OutputStreamStreamOutput rawOs = new OutputStreamStreamOutput(bos);
StreamOutput os = c.streamOutput(rawOs);

Random r = getRandom();
int bufferSize = r.nextBoolean() ? 65535 : TestUtil.nextInt(getRandom(), 1, 70000);
Random r = random();
int bufferSize = r.nextBoolean() ? 65535 : TestUtil.nextInt(random(), 1, 70000);
int prepadding = r.nextInt(70000);
int postpadding = r.nextInt(70000);
byte buffer[] = new byte[prepadding + bufferSize + postpadding];
Expand All @@ -417,7 +417,7 @@ private void doTest(byte bytes[]) throws IOException {
StreamInput in = c.streamInput(compressedIn);

// randomize constants again
bufferSize = r.nextBoolean() ? 65535 : TestUtil.nextInt(getRandom(), 1, 70000);
bufferSize = r.nextBoolean() ? 65535 : TestUtil.nextInt(random(), 1, 70000);
prepadding = r.nextInt(70000);
postpadding = r.nextInt(70000);
buffer = new byte[prepadding + bufferSize + postpadding];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void testRandom() throws IOException {
Compressor defaultCompressor = CompressorFactory.defaultCompressor();
try {
CompressorFactory.setDefaultCompressor(compressor);
Random r = getRandom();
Random r = random();
for (int i = 0; i < 1000; i++) {
String string = TestUtil.randomUnicodeString(r, 10000);
// hack to make it detected as YAML
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static EnvelopeBuilder mutate(EnvelopeBuilder original) throws IOException {
}

static EnvelopeBuilder createRandomShape() {
Rectangle box = RandomShapeGenerator.xRandomRectangle(getRandom(), RandomShapeGenerator.xRandomPoint(getRandom()));
Rectangle box = RandomShapeGenerator.xRandomRectangle(random(), RandomShapeGenerator.xRandomPoint(random()));
EnvelopeBuilder envelope = new EnvelopeBuilder(new Coordinate(box.getMinX(), box.getMaxY()),
new Coordinate(box.getMaxX(), box.getMinY()));
return envelope;
Expand Down
Loading

0 comments on commit 1c16d63

Please sign in to comment.