Skip to content

Commit a1c052a

Browse files
committed
Reuse OperationContext.
1 parent 9f047b7 commit a1c052a

4 files changed

Lines changed: 19 additions & 13 deletions

File tree

driver-core/src/test/functional/com/mongodb/connection/ConnectionSpecification.groovy

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb.connection
1818

1919
import com.mongodb.OperationFunctionalSpecification
20+
import com.mongodb.internal.connection.OperationContext
2021
import com.mongodb.internal.operation.CommandReadOperation
2122
import org.bson.BsonDocument
2223
import org.bson.BsonInt32
@@ -32,8 +33,9 @@ class ConnectionSpecification extends OperationFunctionalSpecification {
3233

3334
def 'should have id'() {
3435
when:
35-
def source = getBinding().getReadConnectionSource(createOperationContext())
36-
def connection = source.getConnection(createOperationContext())
36+
def operationContext = createOperationContext()
37+
def source = getBinding().getReadConnectionSource(operationContext)
38+
def connection = source.getConnection(operationContext)
3739

3840
then:
3941
connection.getDescription().getConnectionId() != null
@@ -45,13 +47,14 @@ class ConnectionSpecification extends OperationFunctionalSpecification {
4547

4648
def 'should have description'() {
4749
when:
48-
def commandResult = getHelloResult()
50+
def operationContext = createOperationContext()
51+
def commandResult = getHelloResult(operationContext)
4952
def expectedMaxMessageSize = commandResult.getNumber('maxMessageSizeBytes',
5053
new BsonInt32(getDefaultMaxMessageSize())).intValue()
5154
def expectedMaxBatchCount = commandResult.getNumber('maxWriteBatchSize',
5255
new BsonInt32(getDefaultMaxWriteBatchSize())).intValue()
53-
def source = getBinding().getReadConnectionSource(createOperationContext())
54-
def connection = source.getConnection(createOperationContext())
56+
def source = getBinding().getReadConnectionSource(operationContext)
57+
def connection = source.getConnection(operationContext)
5558

5659
then:
5760
connection.description.serverAddress == source.getServerDescription().getAddress()
@@ -64,8 +67,8 @@ class ConnectionSpecification extends OperationFunctionalSpecification {
6467
connection?.release()
6568
source?.release()
6669
}
67-
private static BsonDocument getHelloResult() {
70+
private static BsonDocument getHelloResult(OperationContext operationContext) {
6871
new CommandReadOperation<BsonDocument>('admin', new BsonDocument(LEGACY_HELLO, new BsonInt32(1)),
69-
new BsonDocumentCodec()).execute(getBinding(), createOperationContext())
72+
new BsonDocumentCodec()).execute(getBinding(), operationContext)
7073
}
7174
}

driver-core/src/test/unit/com/mongodb/internal/connection/AbstractServerDiscoveryAndMonitoringTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ protected void applyResponse(final BsonArray response) {
8282
}
8383

8484
protected void applyApplicationError(final BsonDocument applicationError) {
85-
Timeout serverSelectionTimeout = ClusterFixture.createOperationContext().getTimeoutContext().computeServerSelectionTimeout();
85+
OperationContext operationContext = ClusterFixture.createOperationContext();
86+
Timeout serverSelectionTimeout = operationContext.getTimeoutContext().computeServerSelectionTimeout();
8687
ServerAddress serverAddress = new ServerAddress(applicationError.getString("address").getValue());
8788
TimeoutContext timeoutContext = new TimeoutContext(TIMEOUT_SETTINGS);
8889
int errorGeneration = applicationError.getNumber("generation",
@@ -98,7 +99,7 @@ protected void applyApplicationError(final BsonDocument applicationError) {
9899
switch (type) {
99100
case "command":
100101
exception = getCommandFailureException(applicationError.getDocument("response"), serverAddress,
101-
ClusterFixture.createOperationContext().getTimeoutContext());
102+
operationContext.getTimeoutContext());
102103
break;
103104
case "network":
104105
exception = new MongoSocketReadException("Read error", serverAddress, new IOException());

driver-core/src/test/unit/com/mongodb/internal/connection/MultiServerClusterSpecification.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ class MultiServerClusterSpecification extends Specification {
9393
def cluster = new MultiServerCluster(CLUSTER_ID, ClusterSettings.builder().hosts(Arrays.asList(firstServer)).mode(MULTIPLE).build(),
9494
factory, CLIENT_METADATA)
9595
cluster.close()
96+
def operationContext = createOperationContext()
9697

9798
when:
9899
cluster.getServersSnapshot(
99-
createOperationContext().getTimeoutContext().computeServerSelectionTimeout(),
100-
createOperationContext().getTimeoutContext())
100+
operationContext.getTimeoutContext().computeServerSelectionTimeout(),
101+
operationContext.getTimeoutContext())
101102

102103
then:
103104
thrown(IllegalStateException)

driver-core/src/test/unit/com/mongodb/internal/connection/ServerDiscoveryAndMonitoringTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,10 @@ private void assertServer(final String serverName, final BsonDocument expectedSe
154154

155155
if (expectedServerDescriptionDocument.isDocument("pool")) {
156156
int expectedGeneration = expectedServerDescriptionDocument.getDocument("pool").getNumber("generation").intValue();
157-
Timeout serverSelectionTimeout = ClusterFixture.createOperationContext().getTimeoutContext().computeServerSelectionTimeout();
157+
OperationContext operationContext = ClusterFixture.createOperationContext();
158+
Timeout serverSelectionTimeout = operationContext.getTimeoutContext().computeServerSelectionTimeout();
158159
DefaultServer server = (DefaultServer) getCluster()
159-
.getServersSnapshot(serverSelectionTimeout, ClusterFixture.createOperationContext().getTimeoutContext())
160+
.getServersSnapshot(serverSelectionTimeout, operationContext.getTimeoutContext())
160161
.getServer(new ServerAddress(serverName));
161162
assertEquals(expectedGeneration, server.getConnectionPool().getGeneration());
162163
}

0 commit comments

Comments
 (0)