Skip to content

Commit 695ce52

Browse files
authored
Wait for unacknowledged write to complete (#958)
To avoid interference between tests, poll the database until the unacknowledged write is present in the collection. This is intended to avoid SnapshotUnavailable errors from being reported by the server. JAVA-4637
1 parent 592059e commit 695ce52

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

driver-legacy/src/test/functional/com/mongodb/MongoClientSessionSpecification.groovy

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616

1717
package com.mongodb
1818

19+
import com.mongodb.client.MongoCollection
20+
import com.mongodb.client.model.Filters
1921
import com.mongodb.event.CommandStartedEvent
2022
import com.mongodb.internal.connection.TestCommandListener
2123
import org.bson.BsonBinarySubType
2224
import org.bson.BsonDocument
2325
import org.bson.BsonInt32
2426
import org.bson.BsonTimestamp
2527
import org.bson.Document
28+
import org.bson.types.ObjectId
2629
import org.junit.Assert
2730
import spock.lang.IgnoreIf
2831
import util.spock.annotations.Slow
@@ -308,17 +311,18 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
308311
.addCommandListener(commandListener)
309312
def mongoClientURI = getMongoClientURI(optionsBuilder)
310313
def client = new MongoClient(mongoClientURI)
314+
def collection = client.getDatabase(getDatabaseName()).getCollection(getCollectionName())
315+
def id = new ObjectId()
311316

312317
when:
313-
client.getDatabase(getDatabaseName()).getCollection(getCollectionName())
314-
.withWriteConcern(WriteConcern.UNACKNOWLEDGED)
315-
.insertOne(new Document())
318+
collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED).insertOne(new Document('_id', id))
316319

317320
then:
318321
def insertEvent = commandListener.events.get(0) as CommandStartedEvent
319322
!insertEvent.command.containsKey('lsid')
320323

321324
cleanup:
325+
waitForInsertAcknowledgement(collection, id)
322326
client?.close()
323327
}
324328

@@ -358,4 +362,12 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
358362
cleanup:
359363
session.close()
360364
}
365+
366+
void waitForInsertAcknowledgement(MongoCollection<Document> collection, ObjectId id) {
367+
Document document = collection.find(Filters.eq(id)).first()
368+
while (document == null) {
369+
Thread.sleep(1)
370+
document = collection.find(Filters.eq(id)).first()
371+
}
372+
}
361373
}

driver-sync/src/test/functional/com/mongodb/client/MongoClientSessionSpecification.groovy

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ import com.mongodb.ReadConcern
2323
import com.mongodb.ReadPreference
2424
import com.mongodb.TransactionOptions
2525
import com.mongodb.WriteConcern
26+
import com.mongodb.client.model.Filters
2627
import com.mongodb.event.CommandStartedEvent
2728
import com.mongodb.internal.connection.TestCommandListener
2829
import org.bson.BsonBinarySubType
2930
import org.bson.BsonDocument
3031
import org.bson.BsonInt32
3132
import org.bson.BsonTimestamp
3233
import org.bson.Document
34+
import org.bson.types.ObjectId
3335
import org.junit.Assert
3436
import spock.lang.IgnoreIf
3537
import util.spock.annotations.Slow
@@ -293,17 +295,18 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
293295
def commandListener = new TestCommandListener()
294296
def settings = MongoClientSettings.builder(getMongoClientSettings()).commandListenerList([commandListener]).build()
295297
def client = MongoClients.create(settings)
298+
def collection = client.getDatabase(getDatabaseName()).getCollection(getCollectionName())
299+
def id = new ObjectId()
296300

297301
when:
298-
client.getDatabase(getDatabaseName()).getCollection(getCollectionName())
299-
.withWriteConcern(WriteConcern.UNACKNOWLEDGED)
300-
.insertOne(new Document())
302+
collection.withWriteConcern(WriteConcern.UNACKNOWLEDGED).insertOne(new Document('_id', id))
301303

302304
then:
303305
def insertEvent = commandListener.events.get(0) as CommandStartedEvent
304306
!insertEvent.command.containsKey('lsid')
305307

306308
cleanup:
309+
waitForInsertAcknowledgement(collection, id)
307310
client?.close()
308311
}
309312

@@ -344,4 +347,12 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
344347
cleanup:
345348
session.close()
346349
}
350+
351+
void waitForInsertAcknowledgement(MongoCollection<Document> collection, ObjectId id) {
352+
Document document = collection.find(Filters.eq(id)).first()
353+
while (document == null) {
354+
Thread.sleep(1)
355+
document = collection.find(Filters.eq(id)).first()
356+
}
357+
}
347358
}

0 commit comments

Comments
 (0)