Skip to content

Commit 5704a4d

Browse files
committed
Ensure that all tests that write to system.profile collection drop it before and after the test.
Otherwise it creates ordering dependencies between the tests.
1 parent b36ca02 commit 5704a4d

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

driver-async/src/test/functional/com/mongodb/async/client/MongoClientsSpecification.groovy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,25 @@ class MongoClientsSpecification extends FunctionalSpecification {
206206
def client = MongoClients.create(getMongoClientBuilderFromConnectionString().applicationName(appName).build(), driverInfo)
207207
def database = client.getDatabase(getDatabaseName())
208208
def collection = database.getCollection(getCollectionName())
209+
210+
def profileCollection = database.getCollection('system.profile')
211+
run(profileCollection.&drop)
209212
run(database.&runCommand, new Document('profile', 2))
210213

211214
when:
212215
run(collection.&count)
213216

214217
then:
215-
Document profileDocument = run(database.getCollection('system.profile').find().&first)
218+
Document profileDocument = run(profileCollection.find().&first)
216219
profileDocument.get('appName') == appName
217220

218221
cleanup:
219222
if (database != null) {
220223
run(database.&runCommand, new Document('profile', 0))
221224
}
225+
if (profileCollection != null) {
226+
run(profileCollection.&drop)
227+
}
222228
client?.close()
223229
}
224230
}

driver/src/test/functional/com/mongodb/DBCursorTest.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -400,29 +400,34 @@ public void testSettingACommentInsertsCommentIntoProfileCollectionWhenProfilingI
400400
assumeThat(isSharded(), is(false));
401401

402402
// given
403-
database.command(new BasicDBObject("profile", 2));
404403
String expectedComment = "test comment";
405404

406-
// when
407-
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
408-
.comment(expectedComment);
409-
while (cursor.hasNext()) {
410-
cursor.next();
411-
}
412-
413-
// then
414405
DBCollection profileCollection = database.getCollection("system.profile");
415-
assertEquals(1, profileCollection.count());
406+
profileCollection.drop();
407+
408+
database.command(new BasicDBObject("profile", 2));
416409

417-
DBObject profileDocument = profileCollection.findOne();
418-
if (serverVersionAtLeast(asList(3, 1, 7))) {
419-
assertEquals(expectedComment, ((DBObject) profileDocument.get("query")).get("comment"));
420-
} else {
421-
assertEquals(expectedComment, ((DBObject) profileDocument.get("query")).get("$comment"));
410+
try {
411+
// when
412+
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
413+
.comment(expectedComment);
414+
while (cursor.hasNext()) {
415+
cursor.next();
416+
}
417+
418+
// then
419+
assertEquals(1, profileCollection.count());
420+
421+
DBObject profileDocument = profileCollection.findOne();
422+
if (serverVersionAtLeast(asList(3, 1, 7))) {
423+
assertEquals(expectedComment, ((DBObject) profileDocument.get("query")).get("comment"));
424+
} else {
425+
assertEquals(expectedComment, ((DBObject) profileDocument.get("query")).get("$comment"));
426+
}
427+
} finally {
428+
database.command(new BasicDBObject("profile", 0));
429+
profileCollection.drop();
422430
}
423-
// finally
424-
database.command(new BasicDBObject("profile", 0));
425-
profileCollection.drop();
426431
}
427432

428433
@Test

driver/src/test/functional/com/mongodb/MongoClientsSpecification.groovy

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,22 @@ class MongoClientsSpecification extends FunctionalSpecification {
3434
def client = new MongoClient(getMongoClientURI(MongoClientOptions.builder().applicationName(appName)), driverInfo)
3535
def database = client.getDatabase(getDatabaseName())
3636
def collection = database.getCollection(getCollectionName())
37+
38+
def profileCollection = database.getCollection('system.profile')
39+
profileCollection.drop()
40+
3741
database.runCommand(new Document('profile', 2))
3842

3943
when:
4044
collection.count()
4145

4246
then:
43-
Document profileDocument = database.getCollection('system.profile').find().first()
47+
Document profileDocument = profileCollection.find().first()
4448
profileDocument.get('appName') == appName
4549

4650
cleanup:
47-
if (database != null) {
48-
database.runCommand(new Document('profile', 0))
49-
}
51+
database?.runCommand(new Document('profile', 0))
52+
profileCollection?.drop()
5053
client?.close()
5154
}
5255

0 commit comments

Comments
 (0)