Skip to content

Commit 9002b1e

Browse files
jyeminstIncMale
authored andcommitted
Use getMore command in tests, when available
In 5.1+, the legacy wire protocol is no longer supported, which includew OP_GET_MORE Although this message type will not be invoked in 5.1+ under normal usage, there are integration tests in driver-core that use it directly, and those have been modified so that they use the getMore command instead, when available. JAVA-4228
1 parent 587d574 commit 9002b1e

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

driver-core/src/test/functional/com/mongodb/internal/operation/AsyncQueryBatchCursorFunctionalSpecification.groovy

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import com.mongodb.MongoException
2121
import com.mongodb.MongoTimeoutException
2222
import com.mongodb.OperationFunctionalSpecification
2323
import com.mongodb.ReadPreference
24-
import com.mongodb.ServerCursor
2524
import com.mongodb.WriteConcern
2625
import com.mongodb.async.FutureResultCallback
2726
import com.mongodb.client.model.CreateCollectionOptions
@@ -61,6 +60,7 @@ import static com.mongodb.ClusterFixture.serverVersionLessThan
6160
import static com.mongodb.internal.connection.ServerHelper.waitForLastRelease
6261
import static com.mongodb.internal.connection.ServerHelper.waitForRelease
6362
import static com.mongodb.internal.operation.OperationHelper.cursorDocumentToQueryResult
63+
import static com.mongodb.internal.operation.QueryOperationHelper.makeAdditionalGetMoreCall
6464
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotTwo
6565
import static java.util.Arrays.asList
6666
import static java.util.Collections.singletonList
@@ -357,7 +357,7 @@ class AsyncQueryBatchCursorFunctionalSpecification extends OperationFunctionalSp
357357
while (connection.getCount() > 1) {
358358
Thread.sleep(5)
359359
}
360-
makeAdditionalGetMoreCall(firstBatch.cursor, connection)
360+
makeAdditionalGetMoreCall(getNamespace(), firstBatch.cursor, connection as Connection)
361361

362362
then:
363363
thrown(MongoCursorNotFoundException)
@@ -447,8 +447,4 @@ class AsyncQueryBatchCursorFunctionalSpecification extends OperationFunctionalSp
447447
futureResultCallback.get();
448448
}
449449
}
450-
451-
private void makeAdditionalGetMoreCall(ServerCursor serverCursor, Connection connection) {
452-
connection.getMore(getNamespace(), serverCursor.getId(), 1, new DocumentCodec())
453-
}
454450
}

driver-core/src/test/functional/com/mongodb/internal/operation/QueryBatchCursorFunctionalSpecification.groovy

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import static com.mongodb.ClusterFixture.isSharded
5252
import static com.mongodb.ClusterFixture.serverVersionAtLeast
5353
import static com.mongodb.ClusterFixture.serverVersionLessThan
5454
import static com.mongodb.internal.operation.OperationHelper.cursorDocumentToQueryResult
55+
import static com.mongodb.internal.operation.QueryOperationHelper.makeAdditionalGetMoreCall
5556
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotTwo
5657
import static java.util.Arrays.asList
5758
import static java.util.Collections.singletonList
@@ -363,7 +364,7 @@ class QueryBatchCursorFunctionalSpecification extends OperationFunctionalSpecifi
363364
cursor = new QueryBatchCursor<Document>(firstBatch, 5, 0, 0, new DocumentCodec(), connectionSource, connection)
364365

365366
when:
366-
makeAdditionalGetMoreCall(firstBatch.cursor, connection)
367+
makeAdditionalGetMoreCall(getNamespace(), firstBatch.cursor, connection)
367368

368369
then:
369370
thrown(MongoCursorNotFoundException)
@@ -386,7 +387,7 @@ class QueryBatchCursorFunctionalSpecification extends OperationFunctionalSpecifi
386387

387388
Thread.sleep(1000) //Note: waiting for some time for killCursor operation to be performed on a server.
388389
when:
389-
makeAdditionalGetMoreCall(serverCursor)
390+
makeAdditionalGetMoreCall(getNamespace(), serverCursor, connectionSource)
390391

391392
then:
392393
thrown(MongoCursorNotFoundException)
@@ -608,17 +609,4 @@ class QueryBatchCursorFunctionalSpecification extends OperationFunctionalSpecifi
608609
connection.release();
609610
}
610611
}
611-
612-
private void makeAdditionalGetMoreCall(ServerCursor serverCursor) {
613-
def connection = connectionSource.getConnection()
614-
try {
615-
makeAdditionalGetMoreCall(serverCursor, connection)
616-
} finally {
617-
connection.release()
618-
}
619-
}
620-
621-
private void makeAdditionalGetMoreCall(ServerCursor serverCursor, Connection connection) {
622-
connection.getMore(getNamespace(), serverCursor.getId(), 1, new DocumentCodec())
623-
}
624612
}

driver-core/src/test/functional/com/mongodb/internal/operation/QueryOperationHelper.groovy

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,24 @@
1616

1717
package com.mongodb.internal.operation
1818

19+
import com.mongodb.MongoCommandException
20+
import com.mongodb.MongoCursorNotFoundException
21+
import com.mongodb.MongoNamespace
22+
import com.mongodb.MongoQueryException
23+
import com.mongodb.ReadPreference
24+
import com.mongodb.ServerCursor
25+
import com.mongodb.internal.binding.ConnectionSource
26+
import com.mongodb.internal.connection.Connection
27+
import com.mongodb.internal.connection.NoOpSessionContext
28+
import com.mongodb.internal.validator.NoOpFieldNameValidator
1929
import org.bson.BsonDocument
30+
import org.bson.BsonInt64
31+
import org.bson.BsonString
32+
import org.bson.codecs.BsonDocumentCodec
33+
import org.bson.codecs.DocumentCodec
34+
35+
import static com.mongodb.ClusterFixture.getServerApi
36+
import static com.mongodb.ClusterFixture.serverVersionLessThan
2037

2138
class QueryOperationHelper {
2239
static BsonDocument sanitizeExplainResult(BsonDocument document) {
@@ -44,4 +61,34 @@ class QueryOperationHelper {
4461
return getKeyPattern(new BsonDocument('queryPlanner', winningPlan.getArray('shards')[0].asDocument()))
4562
}
4663
}
64+
65+
static void makeAdditionalGetMoreCall(MongoNamespace namespace, ServerCursor serverCursor,
66+
ConnectionSource connectionSource) {
67+
def connection = connectionSource.getConnection()
68+
try {
69+
makeAdditionalGetMoreCall(namespace, serverCursor, connection)
70+
} finally {
71+
connection.release()
72+
}
73+
}
74+
75+
static void makeAdditionalGetMoreCall(MongoNamespace namespace, ServerCursor serverCursor, Connection connection) {
76+
if (serverVersionLessThan(3, 6)) {
77+
connection.getMore(namespace, serverCursor.getId(), 1, new DocumentCodec())
78+
} else {
79+
try {
80+
connection.command(namespace.databaseName,
81+
new BsonDocument('getMore', new BsonInt64(serverCursor.getId()))
82+
.append('collection', new BsonString(namespace.getCollectionName())),
83+
new NoOpFieldNameValidator(), ReadPreference.primary(),
84+
new BsonDocumentCodec(), new NoOpSessionContext(), getServerApi())
85+
} catch (MongoCommandException e) {
86+
if (e.getErrorCode() == 43) {
87+
throw new MongoCursorNotFoundException(serverCursor.getId(), serverCursor.getAddress())
88+
} else {
89+
throw new MongoQueryException(e)
90+
}
91+
}
92+
}
93+
}
4794
}

0 commit comments

Comments
 (0)