Search in sources :

Example 1 with TIMEOUT_DURATION

use of com.mongodb.ClusterFixture.TIMEOUT_DURATION in project mongo-java-driver by mongodb.

the class ConnectionsSurvivePrimaryStepDownProseTest method setUp.

@Before
public void setUp() {
    assumeTrue(isDiscoverableReplicaSet() && serverVersionAtLeast(4, 0));
    connectionPoolListener = new TestConnectionPoolListener();
    MongoClientSettings settings = MongoClientSettings.builder(getMongoClientSettings()).retryWrites(false).applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(connectionPoolListener)).build();
    collectionHelper = new CollectionHelper<>(new DocumentCodec(), new MongoNamespace(getDefaultDatabaseName(), COLLECTION_NAME));
    client = MongoClients.create(settings);
    MongoDatabase database = client.getDatabase(getDefaultDatabaseName());
    collection = client.getDatabase(getDefaultDatabaseName()).getCollection(COLLECTION_NAME);
    Mono.from(collection.withWriteConcern(WriteConcern.MAJORITY).drop()).block(TIMEOUT_DURATION);
    Mono.from(database.withWriteConcern(WriteConcern.MAJORITY).createCollection(COLLECTION_NAME)).block(TIMEOUT_DURATION);
}
Also used : Document(org.bson.Document) ClusterFixture.isDiscoverableReplicaSet(com.mongodb.ClusterFixture.isDiscoverableReplicaSet) Assume.assumeFalse(org.junit.Assume.assumeFalse) Fixture.getMongoClientSettings(com.mongodb.reactivestreams.client.Fixture.getMongoClientSettings) MongoClients(com.mongodb.reactivestreams.client.MongoClients) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) MongoClient(com.mongodb.reactivestreams.client.MongoClient) TIMEOUT_DURATION(com.mongodb.ClusterFixture.TIMEOUT_DURATION) Collections.singletonList(java.util.Collections.singletonList) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Arrays.asList(java.util.Arrays.asList) After(org.junit.After) DocumentCodec(org.bson.codecs.DocumentCodec) Assert.fail(org.junit.Assert.fail) MongoNotPrimaryException(com.mongodb.MongoNotPrimaryException) Before(org.junit.Before) CollectionHelper(com.mongodb.client.test.CollectionHelper) MongoNamespace(com.mongodb.MongoNamespace) MongoException(com.mongodb.MongoException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ConnectionPoolClearedEvent(com.mongodb.event.ConnectionPoolClearedEvent) ClusterFixture.getDefaultDatabaseName(com.mongodb.ClusterFixture.getDefaultDatabaseName) List(java.util.List) ClusterFixture.serverVersionAtLeast(com.mongodb.ClusterFixture.serverVersionAtLeast) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) Assume.assumeTrue(org.junit.Assume.assumeTrue) MongoClientSettings(com.mongodb.MongoClientSettings) WriteConcern(com.mongodb.WriteConcern) Assert.assertEquals(org.junit.Assert.assertEquals) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) DocumentCodec(org.bson.codecs.DocumentCodec) Fixture.getMongoClientSettings(com.mongodb.reactivestreams.client.Fixture.getMongoClientSettings) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Before(org.junit.Before)

Example 2 with TIMEOUT_DURATION

use of com.mongodb.ClusterFixture.TIMEOUT_DURATION in project mongo-java-driver by mongodb.

the class BatchCursorFluxTest method ensureExists.

/**
 * This method ensures that the server considers the specified {@code collection} existing for the purposes of, e.g.,
 * {@link MongoCollection#drop()}, instead of replying with
 * {@code "errmsg": "ns not found", "code": 26, "codeName": "NamespaceNotFound"}.
 *
 * @return {@code operationTime} starting at which the {@code collection} is guaranteed to exist.
 */
private static BsonTimestamp ensureExists(final MongoClient client, final MongoCollection<Document> collection) {
    BsonValue insertedId = Mono.from(collection.insertOne(Document.parse("{}"))).map(InsertOneResult::getInsertedId).block(TIMEOUT_DURATION);
    BsonArray deleteStatements = new BsonArray();
    deleteStatements.add(new BsonDocument().append("q", new BsonDocument().append("_id", insertedId)).append("limit", new BsonInt32(1)));
    Publisher<Document> deletePublisher = client.getDatabase(collection.getNamespace().getDatabaseName()).runCommand(new BsonDocument().append("delete", new BsonString(collection.getNamespace().getCollectionName())).append("deletes", deleteStatements));
    BsonTimestamp operationTime = Mono.from(deletePublisher).map(doc -> doc.get("operationTime", BsonTimestamp.class)).block(TIMEOUT_DURATION);
    assertNotNull(operationTime);
    return operationTime;
}
Also used : Document(org.bson.Document) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Mock(org.mockito.Mock) MongoClients(com.mongodb.reactivestreams.client.MongoClients) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) BsonString(org.bson.BsonString) MongoClient(com.mongodb.reactivestreams.client.MongoClient) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) TIMEOUT_DURATION(com.mongodb.ClusterFixture.TIMEOUT_DURATION) Fixture.getMongoClientBuilderFromConnectionString(com.mongodb.reactivestreams.client.Fixture.getMongoClientBuilderFromConnectionString) Collections.singletonList(java.util.Collections.singletonList) Fixture.drop(com.mongodb.reactivestreams.client.Fixture.drop) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BsonTimestamp(org.bson.BsonTimestamp) Arrays.asList(java.util.Arrays.asList) BsonArray(org.bson.BsonArray) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Fixture.isReplicaSet(com.mongodb.reactivestreams.client.Fixture.isReplicaSet) Assertions.assertAll(org.junit.jupiter.api.Assertions.assertAll) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CommandEvent(com.mongodb.event.CommandEvent) TestSubscriber(com.mongodb.reactivestreams.client.TestSubscriber) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) InsertOneResult(com.mongodb.client.result.InsertOneResult) Assertions.assertIterableEquals(org.junit.jupiter.api.Assertions.assertIterableEquals) Collections.emptyList(java.util.Collections.emptyList) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) Fixture.serverVersionAtLeast(com.mongodb.reactivestreams.client.Fixture.serverVersionAtLeast) Mockito.when(org.mockito.Mockito.when) ClusterFixture.getDefaultDatabaseName(com.mongodb.ClusterFixture.getDefaultDatabaseName) Collectors(java.util.stream.Collectors) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) FindPublisher(com.mongodb.reactivestreams.client.FindPublisher) MongoClientSettings(com.mongodb.MongoClientSettings) BsonInt32(org.bson.BsonInt32) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonDocument(org.bson.BsonDocument) BsonTimestamp(org.bson.BsonTimestamp) BsonValue(org.bson.BsonValue)

Aggregations

TIMEOUT_DURATION (com.mongodb.ClusterFixture.TIMEOUT_DURATION)2 ClusterFixture.getDefaultDatabaseName (com.mongodb.ClusterFixture.getDefaultDatabaseName)2 MongoClientSettings (com.mongodb.MongoClientSettings)2 MongoClient (com.mongodb.reactivestreams.client.MongoClient)2 MongoClients (com.mongodb.reactivestreams.client.MongoClients)2 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)2 Arrays.asList (java.util.Arrays.asList)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 Document (org.bson.Document)2 ClusterFixture.isDiscoverableReplicaSet (com.mongodb.ClusterFixture.isDiscoverableReplicaSet)1 ClusterFixture.serverVersionAtLeast (com.mongodb.ClusterFixture.serverVersionAtLeast)1 MongoException (com.mongodb.MongoException)1 MongoNamespace (com.mongodb.MongoNamespace)1 MongoNotPrimaryException (com.mongodb.MongoNotPrimaryException)1 WriteConcern (com.mongodb.WriteConcern)1 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)1 InsertOneResult (com.mongodb.client.result.InsertOneResult)1 CollectionHelper (com.mongodb.client.test.CollectionHelper)1 CommandEvent (com.mongodb.event.CommandEvent)1