Search in sources :

Example 1 with Cluster

use of com.mongodb.internal.connection.Cluster in project mongo-java-driver by mongodb.

the class MongoClientImplTest method testStartSession.

@Test
void testStartSession() {
    ServerDescription serverDescription = ServerDescription.builder().address(new ServerAddress()).state(ServerConnectionState.CONNECTED).maxWireVersion(8).build();
    MongoClientImpl mongoClient = createMongoClient();
    Cluster cluster = mongoClient.getCluster();
    when(cluster.getCurrentDescription()).thenReturn(new ClusterDescription(ClusterConnectionMode.SINGLE, ClusterType.STANDALONE, singletonList(serverDescription)));
    ServerSessionPool serverSessionPool = mock(ServerSessionPool.class);
    ClientSessionHelper clientSessionHelper = new ClientSessionHelper(mongoClient, serverSessionPool);
    assertAll("Start Session Tests", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> mongoClient.startSession(null))), () -> {
        Mono<ClientSession> expected = clientSessionHelper.createClientSessionMono(ClientSessionOptions.builder().build(), OPERATION_EXECUTOR);
        assertPublisherIsTheSameAs(expected, mongoClient.startSession(), "Default");
    }, () -> {
        ClientSessionOptions options = ClientSessionOptions.builder().causallyConsistent(true).defaultTransactionOptions(TransactionOptions.builder().readConcern(ReadConcern.LINEARIZABLE).build()).build();
        Mono<ClientSession> expected = clientSessionHelper.createClientSessionMono(options, OPERATION_EXECUTOR);
        assertPublisherIsTheSameAs(expected, mongoClient.startSession(options), "with options");
    });
}
Also used : ServerSessionPool(com.mongodb.internal.session.ServerSessionPool) ServerDescription(com.mongodb.connection.ServerDescription) ClientSession(com.mongodb.reactivestreams.client.ClientSession) ServerAddress(com.mongodb.ServerAddress) Cluster(com.mongodb.internal.connection.Cluster) ClusterDescription(com.mongodb.connection.ClusterDescription) ClientSessionOptions(com.mongodb.ClientSessionOptions) Test(org.junit.jupiter.api.Test)

Example 2 with Cluster

use of com.mongodb.internal.connection.Cluster in project mongo-java-driver by mongodb.

the class ClusterFixture method getConnectionString.

public static synchronized ConnectionString getConnectionString() {
    if (connectionString != null) {
        return connectionString;
    }
    ConnectionString mongoURIProperty = getConnectionStringFromSystemProperty(MONGODB_URI_SYSTEM_PROPERTY_NAME);
    if (mongoURIProperty != null) {
        return mongoURIProperty;
    }
    // Figure out what the connection string should be
    Cluster cluster = createCluster(new ConnectionString(DEFAULT_URI), new SocketStreamFactory(SocketSettings.builder().build(), SslSettings.builder().build()));
    try {
        BsonDocument helloResult = new CommandReadOperation<BsonDocument>("admin", new BsonDocument(LEGACY_HELLO, new BsonInt32(1)), new BsonDocumentCodec()).execute(new ClusterBinding(cluster, ReadPreference.nearest(), ReadConcern.DEFAULT, getServerApi(), IgnorableRequestContext.INSTANCE));
        if (helloResult.containsKey("setName")) {
            connectionString = new ConnectionString(DEFAULT_URI + "/?replicaSet=" + helloResult.getString("setName").getValue());
        } else {
            connectionString = new ConnectionString(DEFAULT_URI);
            ClusterFixture.cluster = cluster;
        }
        return connectionString;
    } finally {
        if (ClusterFixture.cluster == null) {
            cluster.close();
        }
    }
}
Also used : BsonInt32(org.bson.BsonInt32) SocketStreamFactory(com.mongodb.connection.SocketStreamFactory) BsonDocument(org.bson.BsonDocument) Cluster(com.mongodb.internal.connection.Cluster) ClusterBinding(com.mongodb.internal.binding.ClusterBinding) AsyncClusterBinding(com.mongodb.internal.binding.AsyncClusterBinding) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Aggregations

Cluster (com.mongodb.internal.connection.Cluster)2 ClientSessionOptions (com.mongodb.ClientSessionOptions)1 ServerAddress (com.mongodb.ServerAddress)1 ClusterDescription (com.mongodb.connection.ClusterDescription)1 ServerDescription (com.mongodb.connection.ServerDescription)1 SocketStreamFactory (com.mongodb.connection.SocketStreamFactory)1 AsyncClusterBinding (com.mongodb.internal.binding.AsyncClusterBinding)1 ClusterBinding (com.mongodb.internal.binding.ClusterBinding)1 ServerSessionPool (com.mongodb.internal.session.ServerSessionPool)1 ClientSession (com.mongodb.reactivestreams.client.ClientSession)1 BsonDocument (org.bson.BsonDocument)1 BsonInt32 (org.bson.BsonInt32)1 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)1 Test (org.junit.jupiter.api.Test)1