-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Reuse ConnectionSource to avoid extra server selection. #1813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 12 commits
9bf8a1d
c5ba7f5
b125e5a
835d556
ccd52af
4d1da60
b0e6100
830b009
50d0df3
8befb71
15152d4
467ae63
3a0e72e
0d2de36
c7d1f8f
c340ab0
320ad4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.mongodb.reactivestreams.client; | ||
|
|
||
| import com.mongodb.MongoClientSettings; | ||
| import com.mongodb.client.AbstractChangeSteamFunctionalTest; | ||
| import com.mongodb.client.MongoClient; | ||
| import com.mongodb.reactivestreams.client.syncadapter.SyncMongoClient; | ||
|
|
||
| public class ChangeStreamFunctionalTest extends AbstractChangeSteamFunctionalTest { | ||
| @Override | ||
| protected MongoClient createMongoClient(final MongoClientSettings mongoClientSettings) { | ||
| return new SyncMongoClient(mongoClientSettings); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.mongodb.client; | ||
|
|
||
| import com.mongodb.ClusterFixture; | ||
| import com.mongodb.MongoClientSettings; | ||
| import com.mongodb.MongoNamespace; | ||
| import com.mongodb.client.model.changestream.ChangeStreamDocument; | ||
| import com.mongodb.client.test.CollectionHelper; | ||
| import org.bson.BsonDocument; | ||
| import org.bson.BsonTimestamp; | ||
| import org.bson.Document; | ||
| import org.bson.codecs.BsonDocumentCodec; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static com.mongodb.client.Fixture.getDefaultDatabaseName; | ||
| import static java.lang.String.format; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assumptions.assumeTrue; | ||
|
|
||
| /** | ||
| * The {@link ChangeStreamProseTest}, which is defined only for sync driver, should be migrated to this class. | ||
| * Once this done, this class should be renamed to ChangeStreamProseTest. | ||
| */ | ||
| public abstract class AbstractChangeSteamFunctionalTest { | ||
|
|
||
| private static final String FAIL_COMMAND_NAME = "failCommand"; | ||
| private static final MongoNamespace NAMESPACE = new MongoNamespace(getDefaultDatabaseName(), "test"); | ||
| private final CollectionHelper<BsonDocument> collectionHelper = new CollectionHelper<>(new BsonDocumentCodec(), NAMESPACE); | ||
|
|
||
| protected abstract MongoClient createMongoClient(MongoClientSettings mongoClientSettings); | ||
|
|
||
| @Test | ||
| public void shouldDoOneServerSelectionForResumeAttempt() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to have failed a couple of times in the tests:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, All tests pass now. |
||
| //given | ||
| assumeTrue(ClusterFixture.isDiscoverableReplicaSet()); | ||
| AtomicInteger serverSelectionCounter = new AtomicInteger(); | ||
| BsonTimestamp startTime = new BsonTimestamp((int) Instant.now().getEpochSecond(), 0); | ||
| try (MongoClient mongoClient = createMongoClient(Fixture.getMongoClientSettingsBuilder() | ||
| .applyToClusterSettings(builder -> builder.serverSelector(clusterDescription -> { | ||
| serverSelectionCounter.incrementAndGet(); | ||
| return clusterDescription.getServerDescriptions(); | ||
| })).build())) { | ||
|
|
||
| MongoCollection<Document> collection = mongoClient | ||
| .getDatabase(NAMESPACE.getDatabaseName()) | ||
| .getCollection(NAMESPACE.getCollectionName()); | ||
|
|
||
| collectionHelper.runAdminCommand("{" | ||
| + " configureFailPoint: \"" + FAIL_COMMAND_NAME + "\"," | ||
| + " mode: {" | ||
| + " times: 1" | ||
| + " }," | ||
| + " data: {" | ||
| + " failCommands: ['getMore']," | ||
| + " errorCode: 10107," | ||
| + " errorLabels: ['ResumableChangeStreamError']" | ||
| + " }" | ||
| + "}"); | ||
| // We insert document here, because async cursor performs aggregate and getMore right after we call cursor() | ||
| collection.insertOne(Document.parse("{ x: 1 }")); | ||
| serverSelectionCounter.set(0); | ||
|
|
||
| try (MongoChangeStreamCursor<ChangeStreamDocument<Document>> cursor = collection.watch() | ||
| .batchSize(0) | ||
| .startAtOperationTime(startTime) | ||
| .cursor()) { | ||
|
|
||
| //when | ||
| ChangeStreamDocument<Document> changeStreamDocument = cursor.next(); | ||
| //then | ||
| assertNotNull(changeStreamDocument); | ||
| int actualCountOfServerSelections = serverSelectionCounter.get(); | ||
| assertEquals(2, actualCountOfServerSelections, | ||
| format("Expected 2 server selections (initial aggregate command + resume attempt aggregate command), but there were %s", | ||
| actualCountOfServerSelections)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() throws InterruptedException { | ||
| ClusterFixture.disableFailPoint(FAIL_COMMAND_NAME); | ||
| collectionHelper.drop(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.