Skip to content

Commit b7b9c07

Browse files
committed
JAVA-2815: Add transaction tests for runCommand
1 parent ff71a1d commit b7b9c07

File tree

14 files changed

+386
-14
lines changed

14 files changed

+386
-14
lines changed

driver-async/src/main/com/mongodb/async/client/MongoDatabaseImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb.async.client;
1818

1919
import com.mongodb.Function;
20+
import com.mongodb.MongoClientException;
2021
import com.mongodb.MongoNamespace;
2122
import com.mongodb.ReadConcern;
2223
import com.mongodb.ReadPreference;
@@ -220,6 +221,9 @@ private <TResult> void executeCommand(@Nullable final ClientSession clientSessio
220221
final SingleResultCallback<TResult> callback) {
221222
notNull("command", command);
222223
notNull("readPreference", readPreference);
224+
if (clientSession != null && clientSession.hasActiveTransaction() && !readPreference.equals(ReadPreference.primary())) {
225+
throw new MongoClientException("Read preference in a transaction must be primary");
226+
}
223227
executor.execute(new CommandReadOperation<TResult>(getName(), toBsonDocument(command), codecRegistry.get(resultClass)),
224228
readPreference, readConcern, clientSession, callback);
225229
}

driver-async/src/test/functional/com/mongodb/async/client/CrudTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void setUp() {
6262
super.setUp();
6363
collection = Fixture.initializeCollection(new MongoNamespace(getDefaultDatabaseName(), getClass().getName()))
6464
.withDocumentClass(BsonDocument.class);
65-
helper = new JsonPoweredCrudTestHelper(description, collection);
65+
helper = new JsonPoweredCrudTestHelper(description, getDefaultDatabase(), collection);
6666
new MongoOperation<Void>() {
6767
@Override
6868
public void execute() {

driver-async/src/test/functional/com/mongodb/async/client/JsonPoweredCrudTestHelper.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@
6868

6969
public class JsonPoweredCrudTestHelper {
7070
private final String description;
71+
private final MongoDatabase database;
7172
private final MongoCollection<BsonDocument> baseCollection;
7273

73-
public JsonPoweredCrudTestHelper(final String description, final MongoCollection<BsonDocument> collection) {
74+
public JsonPoweredCrudTestHelper(final String description, final MongoDatabase database,
75+
final MongoCollection<BsonDocument> collection) {
7476
this.description = description;
77+
this.database = database;
7578
this.baseCollection = collection;
7679
}
7780

@@ -649,6 +652,34 @@ BsonDocument toResult(@Nullable final BsonValue results) {
649652
return new BsonDocument("result", results != null ? results : BsonNull.VALUE);
650653
}
651654

655+
BsonDocument getRunCommandResult(final BsonDocument arguments, @Nullable final ClientSession clientSession) {
656+
BsonDocument command = arguments.getDocument("command");
657+
ReadPreference readPreference = arguments.containsKey("readPreference") ? getReadPreference(arguments) : null;
658+
659+
FutureResultCallback<BsonDocument> futureResultCallback = new FutureResultCallback<BsonDocument>();
660+
661+
if (clientSession == null) {
662+
if (readPreference == null) {
663+
database.runCommand(command, BsonDocument.class, futureResultCallback);
664+
} else {
665+
database.runCommand(command, readPreference, BsonDocument.class, futureResultCallback);
666+
}
667+
} else {
668+
if (readPreference == null) {
669+
database.runCommand(clientSession, command, BsonDocument.class, futureResultCallback);
670+
} else {
671+
database.runCommand(clientSession, command, readPreference, BsonDocument.class, futureResultCallback);
672+
}
673+
}
674+
BsonDocument response = futureResult(futureResultCallback);
675+
response.remove("ok");
676+
response.remove("operationTime");
677+
response.remove("opTime");
678+
response.remove("electionId");
679+
response.remove("$clusterTime");
680+
return toResult(response);
681+
}
682+
652683
BsonDocument getAggregateResult(final BsonDocument arguments, @Nullable final ClientSession clientSession) {
653684
List<BsonDocument> pipeline = new ArrayList<BsonDocument>();
654685
for (BsonValue stage : arguments.getArray("pipeline")) {

driver-async/src/test/functional/com/mongodb/async/client/RetryableWritesTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ public void setUp() {
127127
collectionHelper.drop();
128128
collectionHelper.insertDocuments(documents);
129129

130-
collection = mongoClient.getDatabase(databaseName).getCollection(collectionName, BsonDocument.class);
131-
helper = new JsonPoweredCrudTestHelper(description, collection);
130+
MongoDatabase database = mongoClient.getDatabase(databaseName);
131+
collection = database.getCollection(collectionName, BsonDocument.class);
132+
helper = new JsonPoweredCrudTestHelper(description, database, collection);
132133
setFailPoint();
133134
}
134135

driver-async/src/test/functional/com/mongodb/async/client/TransactionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void apply(final SocketSettings.Builder builder) {
138138
.build());
139139

140140
MongoDatabase database = mongoClient.getDatabase(databaseName);
141-
helper = new JsonPoweredCrudTestHelper(description, database.getCollection(collectionName, BsonDocument.class));
141+
helper = new JsonPoweredCrudTestHelper(description, database, database.getCollection(collectionName, BsonDocument.class));
142142

143143
ClientSession sessionZero = createSession("session0");
144144
ClientSession sessionOne = createSession("session1");

driver-core/src/test/resources/transactions/README.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ For each YAML file, for each element in ``tests``:
9898
- Enter a "try" block or your programming language's closest equivalent.
9999
- If ``name`` is "startTransaction", "commitTransaction", or
100100
"abortTransaction", call the named method on ``session0`` or
101-
``session1``, depending on the "session" argument.
101+
``session1``, depending on the "session" argument.
102+
- If ``name`` is "runCommand", call the runCommand method on the database
103+
specified in the test. Pass the argument named "command" to the runCommand
104+
method. Pass ``session0`` or ``session1`` to the runCommand method, depending
105+
on which session's name is in the arguments list. If ``arguments``
106+
contains no "session", pass no explicit session to the method. If ``arguments``
107+
includes "readPreference", also pass the read preference to the runCommand
108+
method.
102109
- Otherwise, ``name`` refers to a CRUD method, such as ``insertOne``.
103110
Execute the named method on the "transactions-tests" database on the "test"
104111
collection, passing the arguments listed. Pass ``session0`` or ``session1``

0 commit comments

Comments
 (0)