21
21
import com .mongodb .MongoNamespace ;
22
22
import com .mongodb .ReadPreference ;
23
23
import com .mongodb .UnixServerAddress ;
24
- import com .mongodb .client .unified .UnifiedTestModifications .TestDef ;
25
- import com .mongodb .event .TestServerMonitorListener ;
26
- import com .mongodb .internal .logging .LogMessage ;
27
- import com .mongodb .logging .TestLoggingInterceptor ;
28
24
import com .mongodb .WriteConcern ;
29
25
import com .mongodb .client .ClientSession ;
30
26
import com .mongodb .client .MongoClient ;
31
27
import com .mongodb .client .MongoDatabase ;
32
28
import com .mongodb .client .gridfs .GridFSBucket ;
33
29
import com .mongodb .client .model .Filters ;
34
30
import com .mongodb .client .test .CollectionHelper ;
31
+ import com .mongodb .client .unified .UnifiedTestModifications .TestDef ;
35
32
import com .mongodb .client .vault .ClientEncryption ;
36
33
import com .mongodb .connection .ClusterDescription ;
37
34
import com .mongodb .connection .ClusterType ;
38
35
import com .mongodb .connection .ServerDescription ;
39
36
import com .mongodb .event .CommandEvent ;
40
37
import com .mongodb .event .CommandStartedEvent ;
38
+ import com .mongodb .event .TestServerMonitorListener ;
41
39
import com .mongodb .internal .connection .TestCommandListener ;
42
40
import com .mongodb .internal .connection .TestConnectionPoolListener ;
41
+ import com .mongodb .internal .logging .LogMessage ;
43
42
import com .mongodb .lang .NonNull ;
44
43
import com .mongodb .lang .Nullable ;
44
+ import com .mongodb .logging .TestLoggingInterceptor ;
45
45
import com .mongodb .test .AfterBeforeParameterResolver ;
46
46
import org .bson .BsonArray ;
47
47
import org .bson .BsonBoolean ;
57
57
import org .junit .jupiter .params .ParameterizedTest ;
58
58
import org .junit .jupiter .params .provider .Arguments ;
59
59
import org .junit .jupiter .params .provider .MethodSource ;
60
+ import org .opentest4j .AssertionFailedError ;
60
61
import org .opentest4j .TestAbortedException ;
61
62
62
63
import java .io .File ;
63
64
import java .io .IOException ;
64
65
import java .net .URISyntaxException ;
66
+ import java .text .MessageFormat ;
65
67
import java .util .ArrayList ;
66
68
import java .util .Collection ;
67
69
import java .util .Collections ;
70
+ import java .util .HashSet ;
68
71
import java .util .List ;
69
72
import java .util .Set ;
70
73
import java .util .concurrent .ExecutionException ;
81
84
import static com .mongodb .client .test .CollectionHelper .getCurrentClusterTime ;
82
85
import static com .mongodb .client .test .CollectionHelper .killAllSessions ;
83
86
import static com .mongodb .client .unified .RunOnRequirementsMatcher .runOnRequirementsMet ;
87
+ import static com .mongodb .client .unified .UnifiedTestModifications .doSkips ;
84
88
import static com .mongodb .client .unified .UnifiedTestModifications .testDef ;
85
89
import static java .util .Collections .singletonList ;
86
90
import static java .util .stream .Collectors .toList ;
91
95
import static org .junit .jupiter .api .Assertions .assertNull ;
92
96
import static org .junit .jupiter .api .Assertions .assertTrue ;
93
97
import static org .junit .jupiter .api .Assertions .fail ;
98
+ import static org .junit .jupiter .api .Assumptions .assumeFalse ;
94
99
import static org .junit .jupiter .api .Assumptions .assumeTrue ;
95
100
import static util .JsonPoweredTestHelper .getTestDocument ;
96
101
import static util .JsonPoweredTestHelper .getTestFiles ;
@@ -100,6 +105,9 @@ public abstract class UnifiedTest {
100
105
private static final Set <String > PRESTART_POOL_ASYNC_WORK_MANAGER_FILE_DESCRIPTIONS = Collections .singleton (
101
106
"wait queue timeout errors include details about checked out connections" );
102
107
108
+ public static final int ATTEMPTS = 3 ;
109
+ private static Set <String > completed = new HashSet <>();
110
+
103
111
@ Nullable
104
112
private String fileDescription ;
105
113
private String schemaVersion ;
@@ -154,32 +162,51 @@ public Entities getEntities() {
154
162
}
155
163
156
164
@ NonNull
157
- protected static Collection <Arguments > getTestData (final String directory ) throws URISyntaxException , IOException {
165
+ protected static Collection <Arguments > getTestData (final String directory , final boolean isReactive )
166
+ throws URISyntaxException , IOException {
158
167
List <Arguments > data = new ArrayList <>();
159
168
for (File file : getTestFiles ("/" + directory + "/" )) {
160
169
BsonDocument fileDocument = getTestDocument (file );
161
-
162
170
for (BsonValue cur : fileDocument .getArray ("tests" )) {
163
- data .add (UnifiedTest .createTestData (directory , fileDocument , cur .asDocument ()));
171
+
172
+ final BsonDocument testDocument = cur .asDocument ();
173
+ String testDescription = testDocument .getString ("description" ).getValue ();
174
+ String fileDescription = fileDocument .getString ("description" ).getValue ();
175
+ TestDef testDef = testDef (directory , fileDescription , testDescription , isReactive );
176
+ doSkips (testDef );
177
+
178
+ boolean forceFlaky = testDef .wasAssignedModifier (UnifiedTestModifications .Modifier .FORCE_FLAKY );
179
+ boolean retry = forceFlaky || testDef .wasAssignedModifier (UnifiedTestModifications .Modifier .RETRY );
180
+
181
+ int attempts = retry ? ATTEMPTS : 1 ;
182
+ if (forceFlaky ) {
183
+ attempts = 10 ;
184
+ }
185
+
186
+ for (int attempt = 1 ; attempt <= attempts ; attempt ++) {
187
+ String testName = !retry
188
+ ? MessageFormat .format ("{0}: {1}" , fileDescription , testDescription )
189
+ : MessageFormat .format (
190
+ "{0}: {1} ({2} of {3})" ,
191
+ fileDescription , testDescription , attempt , attempts );
192
+ data .add (Arguments .of (
193
+ testName ,
194
+ fileDescription ,
195
+ testDescription ,
196
+ directory ,
197
+ attempt ,
198
+ attempts * (forceFlaky ? -1 : 1 ),
199
+ fileDocument .getString ("schemaVersion" ).getValue (),
200
+ fileDocument .getArray ("runOnRequirements" , null ),
201
+ fileDocument .getArray ("createEntities" , new BsonArray ()),
202
+ fileDocument .getArray ("initialData" , new BsonArray ()),
203
+ testDocument ));
204
+ }
164
205
}
165
206
}
166
207
return data ;
167
208
}
168
209
169
- @ NonNull
170
- private static Arguments createTestData (
171
- final String directory , final BsonDocument fileDocument , final BsonDocument testDocument ) {
172
- return Arguments .of (
173
- fileDocument .getString ("description" ).getValue (),
174
- testDocument .getString ("description" ).getValue (),
175
- directory ,
176
- fileDocument .getString ("schemaVersion" ).getValue (),
177
- fileDocument .getArray ("runOnRequirements" , null ),
178
- fileDocument .getArray ("createEntities" , new BsonArray ()),
179
- fileDocument .getArray ("initialData" , new BsonArray ()),
180
- testDocument );
181
- }
182
-
183
210
protected BsonDocument getDefinition () {
184
211
return definition ;
185
212
}
@@ -192,9 +219,12 @@ protected BsonDocument getDefinition() {
192
219
193
220
@ BeforeEach
194
221
public void setUp (
222
+ final String testName ,
195
223
@ Nullable final String fileDescription ,
196
224
@ Nullable final String testDescription ,
197
225
@ Nullable final String directoryName ,
226
+ final int attemptNumber ,
227
+ final int totalAttempts ,
198
228
final String schemaVersion ,
199
229
@ Nullable final BsonArray runOnRequirements ,
200
230
final BsonArray entitiesArray ,
@@ -287,8 +317,9 @@ protected void postCleanUp(final TestDef testDef) {
287
317
}
288
318
289
319
/**
290
- * This method is called once per {@link #setUp(String, String, String, String, org.bson.BsonArray, org.bson.BsonArray, org.bson.BsonArray, org.bson.BsonDocument)},
291
- * unless {@link #setUp(String, String, String, String, org.bson.BsonArray, org.bson.BsonArray, org.bson.BsonArray, org.bson.BsonDocument)} fails unexpectedly.
320
+ * This method is called once per
321
+ * {@link #setUp(String, String, String, String, int, int, String, org.bson.BsonArray, org.bson.BsonArray, org.bson.BsonArray, org.bson.BsonDocument)}, unless
322
+ * {@link #setUp(String, String, String, String, int, int, String, org.bson.BsonArray, org.bson.BsonArray, org.bson.BsonArray, org.bson.BsonDocument)} fails unexpectedly.
292
323
*/
293
324
protected void skips (final String fileDescription , final String testDescription ) {
294
325
}
@@ -297,40 +328,58 @@ protected boolean isReactive() {
297
328
return false ;
298
329
}
299
330
300
- @ ParameterizedTest (name = "{0}: {1} " )
331
+ @ ParameterizedTest (name = "{0}" )
301
332
@ MethodSource ("data" )
302
333
public void shouldPassAllOutcomes (
334
+ final String testName ,
303
335
@ Nullable final String fileDescription ,
304
336
@ Nullable final String testDescription ,
305
337
@ Nullable final String directoryName ,
338
+ final int attemptNumber ,
339
+ final int totalAttempts ,
306
340
final String schemaVersion ,
307
341
@ Nullable final BsonArray runOnRequirements ,
308
342
final BsonArray entitiesArray ,
309
343
final BsonArray initialData ,
310
344
final BsonDocument definition ) {
311
- BsonArray operations = definition . getArray ( "operations" ) ;
312
- for ( int i = 0 ; i < operations . size (); i ++ ) {
313
- BsonValue cur = operations . get ( i );
314
- assertOperation ( rootContext , cur . asDocument (), i );
345
+ boolean forceFlaky = totalAttempts < 0 ;
346
+ if (! forceFlaky ) {
347
+ assumeFalse ( completed . contains ( testName ), "Skipping retryable test that succeeded" );
348
+ completed . add ( testName );
315
349
}
350
+ try {
351
+ BsonArray operations = definition .getArray ("operations" );
352
+ for (int i = 0 ; i < operations .size (); i ++) {
353
+ BsonValue cur = operations .get (i );
354
+ assertOperation (rootContext , cur .asDocument (), i );
355
+ }
316
356
317
- if (definition .containsKey ("outcome" )) {
318
- assertOutcome (rootContext );
319
- }
357
+ if (definition .containsKey ("outcome" )) {
358
+ assertOutcome (rootContext );
359
+ }
320
360
321
- if (definition .containsKey ("expectEvents" )) {
322
- compareEvents (rootContext , definition );
323
- }
361
+ if (definition .containsKey ("expectEvents" )) {
362
+ compareEvents (rootContext , definition );
363
+ }
324
364
325
- if (definition .containsKey ("expectLogMessages" )) {
326
- ArrayList <LogMatcher .Tweak > tweaks = new ArrayList <>(singletonList (
327
- // `LogMessage.Entry.Name.OPERATION` is not supported, therefore we skip matching its value
328
- LogMatcher .Tweak .skip (LogMessage .Entry .Name .OPERATION )));
329
- if (getMongoClientSettings ().getClusterSettings ()
330
- .getHosts ().stream ().anyMatch (serverAddress -> serverAddress instanceof UnixServerAddress )) {
331
- tweaks .add (LogMatcher .Tweak .skip (LogMessage .Entry .Name .SERVER_PORT ));
365
+ if (definition .containsKey ("expectLogMessages" )) {
366
+ ArrayList <LogMatcher .Tweak > tweaks = new ArrayList <>(singletonList (
367
+ // `LogMessage.Entry.Name.OPERATION` is not supported, therefore we skip matching its value
368
+ LogMatcher .Tweak .skip (LogMessage .Entry .Name .OPERATION )));
369
+ if (getMongoClientSettings ().getClusterSettings ()
370
+ .getHosts ().stream ().anyMatch (serverAddress -> serverAddress instanceof UnixServerAddress )) {
371
+ tweaks .add (LogMatcher .Tweak .skip (LogMessage .Entry .Name .SERVER_PORT ));
372
+ }
373
+ compareLogMessages (rootContext , definition , tweaks );
374
+ }
375
+ } catch (AssertionFailedError e ) {
376
+ completed .remove (testName );
377
+ boolean lastAttempt = attemptNumber == Math .abs (totalAttempts );
378
+ if (forceFlaky || lastAttempt ) {
379
+ throw e ;
380
+ } else {
381
+ assumeFalse (completed .contains (testName ), "Ignoring failure and retrying attempt " + attemptNumber );
332
382
}
333
- compareLogMessages (rootContext , definition , tweaks );
334
383
}
335
384
}
336
385
0 commit comments