19
19
20
20
package org .elasticsearch .test ;
21
21
22
+ import com .carrotsearch .randomizedtesting .RandomizedTest ;
23
+ import com .carrotsearch .randomizedtesting .SeedUtils ;
24
+
22
25
import org .apache .lucene .index .IndexReader ;
23
26
import org .apache .lucene .util .Accountable ;
24
27
import org .elasticsearch .Version ;
84
87
import java .util .HashMap ;
85
88
import java .util .List ;
86
89
import java .util .Map ;
90
+ import java .util .concurrent .Callable ;
87
91
import java .util .concurrent .ExecutionException ;
88
92
import java .util .function .Function ;
89
93
import java .util .stream .Stream ;
@@ -124,14 +128,12 @@ public abstract class AbstractBuilderTestCase extends ESTestCase {
124
128
ALIAS_TO_CONCRETE_FIELD_NAME .put (GEO_POINT_ALIAS_FIELD_NAME , GEO_POINT_FIELD_NAME );
125
129
}
126
130
127
- protected static Version indexVersionCreated ;
128
-
129
131
private static ServiceHolder serviceHolder ;
130
132
private static ServiceHolder serviceHolderWithNoType ;
131
133
private static int queryNameId = 0 ;
132
134
private static Settings nodeSettings ;
133
135
private static Index index ;
134
- private static Index indexWithNoType ;
136
+ private static long nowInMillis ;
135
137
136
138
protected static Index getIndex () {
137
139
return index ;
@@ -152,7 +154,7 @@ public static void beforeClass() {
152
154
.build ();
153
155
154
156
index = new Index (randomAlphaOfLengthBetween (1 , 10 ), randomAlphaOfLength (10 ));
155
- indexWithNoType = new Index ( randomAlphaOfLengthBetween ( 1 , 10 ), randomAlphaOfLength ( 10 ) );
157
+ nowInMillis = randomNonNegativeLong ( );
156
158
}
157
159
158
160
@ Override
@@ -173,15 +175,19 @@ protected static String createUniqueRandomName() {
173
175
return queryName ;
174
176
}
175
177
176
- protected Settings indexSettings () {
178
+ protected Settings createTestIndexSettings () {
177
179
// we have to prefer CURRENT since with the range of versions we support it's rather unlikely to get the current actually.
178
- indexVersionCreated = randomBoolean () ? Version .CURRENT
180
+ Version indexVersionCreated = randomBoolean () ? Version .CURRENT
179
181
: VersionUtils .randomVersionBetween (random (), Version .V_6_0_0 , Version .CURRENT );
180
182
return Settings .builder ()
181
183
.put (IndexMetaData .SETTING_VERSION_CREATED , indexVersionCreated )
182
184
.build ();
183
185
}
184
186
187
+ protected static IndexSettings indexSettings () {
188
+ return serviceHolder .idxSettings ;
189
+ }
190
+
185
191
protected static String expectedFieldName (String builderFieldName ) {
186
192
return ALIAS_TO_CONCRETE_FIELD_NAME .getOrDefault (builderFieldName , builderFieldName );
187
193
}
@@ -195,14 +201,24 @@ public static void afterClass() throws Exception {
195
201
}
196
202
197
203
@ Before
198
- public void beforeTest () throws IOException {
204
+ public void beforeTest () throws Exception {
199
205
if (serviceHolder == null ) {
200
- serviceHolder = new ServiceHolder (nodeSettings , indexSettings (), getPlugins (), this , true );
206
+ assert serviceHolderWithNoType == null ;
207
+ // we initialize the serviceHolder and serviceHolderWithNoType just once, but need some
208
+ // calls to the randomness source during its setup. In order to not mix these calls with
209
+ // the randomness source that is later used in the test method, we use the master seed during
210
+ // this setup
211
+ long masterSeed = SeedUtils .parseSeed (RandomizedTest .getContext ().getRunnerSeedAsString ());
212
+ RandomizedTest .getContext ().runWithPrivateRandomness (masterSeed , (Callable <Void >) () -> {
213
+ serviceHolder = new ServiceHolder (nodeSettings , createTestIndexSettings (), getPlugins (), nowInMillis ,
214
+ AbstractBuilderTestCase .this , true );
215
+ serviceHolderWithNoType = new ServiceHolder (nodeSettings , createTestIndexSettings (), getPlugins (), nowInMillis ,
216
+ AbstractBuilderTestCase .this , false );
217
+ return null ;
218
+ });
201
219
}
220
+
202
221
serviceHolder .clientInvocationHandler .delegate = this ;
203
- if (serviceHolderWithNoType == null ) {
204
- serviceHolderWithNoType = new ServiceHolder (nodeSettings , indexSettings (), getPlugins (), this , false );
205
- }
206
222
serviceHolderWithNoType .clientInvocationHandler .delegate = this ;
207
223
}
208
224
@@ -305,13 +321,15 @@ private static class ServiceHolder implements Closeable {
305
321
private final BitsetFilterCache bitsetFilterCache ;
306
322
private final ScriptService scriptService ;
307
323
private final Client client ;
308
- private final long nowInMillis = randomNonNegativeLong () ;
324
+ private final long nowInMillis ;
309
325
310
326
ServiceHolder (Settings nodeSettings ,
311
327
Settings indexSettings ,
312
328
Collection <Class <? extends Plugin >> plugins ,
329
+ long nowInMillis ,
313
330
AbstractBuilderTestCase testCase ,
314
331
boolean registerType ) throws IOException {
332
+ this .nowInMillis = nowInMillis ;
315
333
Environment env = InternalSettingsPreparer .prepareEnvironment (nodeSettings );
316
334
PluginsService pluginsService ;
317
335
pluginsService = new PluginsService (nodeSettings , null , env .modulesFile (), env .pluginsFile (), plugins );
0 commit comments