Search in sources :

Example 1 with ServerAddressSelector

use of com.mongodb.internal.selector.ServerAddressSelector in project mongo-java-driver by mongodb.

the class ServerHelper method waitForLastRelease.

public static void waitForLastRelease(final ServerAddress address, final Cluster cluster) {
    ConcurrentPool<UsageTrackingInternalConnection> pool = connectionPool(cluster.selectServer(new ServerAddressSelector(address)).getServer());
    long startTime = System.currentTimeMillis();
    while (pool.getInUseCount() > 0) {
        try {
            sleep(10);
            if (System.currentTimeMillis() > startTime + ClusterFixture.TIMEOUT * 1000) {
                throw new MongoTimeoutException("Timed out waiting for pool in use count to drop to 0.  Now at: " + pool.getInUseCount());
            }
        } catch (InterruptedException e) {
            throw new MongoInterruptedException("Interrupted", e);
        }
    }
}
Also used : ServerAddressSelector(com.mongodb.internal.selector.ServerAddressSelector) MongoInterruptedException(com.mongodb.MongoInterruptedException) MongoTimeoutException(com.mongodb.MongoTimeoutException) MongoInterruptedException(com.mongodb.MongoInterruptedException)

Example 2 with ServerAddressSelector

use of com.mongodb.internal.selector.ServerAddressSelector in project mongo-java-driver by mongodb.

the class SingleServerClusterTest method shouldSuccessfullyQueryASecondaryWithPrimaryReadPreference.

@Test
public void shouldSuccessfullyQueryASecondaryWithPrimaryReadPreference() {
    // given
    ServerAddress secondary = getSecondary();
    setUpCluster(secondary);
    String collectionName = getClass().getName();
    Connection connection = cluster.selectServer(new ServerAddressSelector(secondary)).getServer().getConnection();
    // when
    BsonDocument result = connection.command(getDefaultDatabaseName(), new BsonDocument("count", new BsonString(collectionName)), new NoOpFieldNameValidator(), ReadPreference.primary(), new BsonDocumentCodec(), NoOpSessionContext.INSTANCE, getServerApi(), IgnorableRequestContext.INSTANCE);
    // then
    assertEquals(new BsonDouble(1.0).intValue(), result.getNumber("ok").intValue());
}
Also used : ServerAddressSelector(com.mongodb.internal.selector.ServerAddressSelector) NoOpFieldNameValidator(com.mongodb.internal.validator.NoOpFieldNameValidator) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ServerAddress(com.mongodb.ServerAddress) BsonDouble(org.bson.BsonDouble) BsonString(org.bson.BsonString) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Test(org.junit.Test)

Aggregations

ServerAddressSelector (com.mongodb.internal.selector.ServerAddressSelector)2 MongoInterruptedException (com.mongodb.MongoInterruptedException)1 MongoTimeoutException (com.mongodb.MongoTimeoutException)1 ServerAddress (com.mongodb.ServerAddress)1 NoOpFieldNameValidator (com.mongodb.internal.validator.NoOpFieldNameValidator)1 BsonDocument (org.bson.BsonDocument)1 BsonDouble (org.bson.BsonDouble)1 BsonString (org.bson.BsonString)1 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)1 Test (org.junit.Test)1