Search in sources :

Example 6 with ConnectionDescription

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

the class DefaultConnectionPoolTest method newControllableConnectionFactory.

private static ControllableConnectionFactory newControllableConnectionFactory(final ExecutorService asyncOpenExecutor) {
    ControllableConnectionFactory.OpenDurationHandle openDurationHandle = new ControllableConnectionFactory.OpenDurationHandle();
    InternalConnectionFactory connectionFactory = (serverId, connectionGenerationSupplier) -> {
        InternalConnection connection = mock(InternalConnection.class, withSettings().stubOnly());
        when(connection.getGeneration()).thenReturn(connectionGenerationSupplier.getGeneration());
        when(connection.getDescription()).thenReturn(new ConnectionDescription(serverId));
        AtomicBoolean open = new AtomicBoolean(false);
        when(connection.opened()).thenAnswer(invocation -> open.get());
        Runnable doOpen = () -> {
            sleepMillis(openDurationHandle.getDurationAndCountDown().toMillis());
            if (ThreadLocalRandom.current().nextFloat() < 0.2) {
                // add a bit more randomness
                sleepMillis(ThreadLocalRandom.current().nextInt(7, 15));
            }
            open.set(true);
        };
        doAnswer(invocation -> {
            doOpen.run();
            return null;
        }).when(connection).open();
        doAnswer(invocation -> {
            SingleResultCallback<?> callback = invocation.getArgument(0, SingleResultCallback.class);
            asyncOpenExecutor.execute(() -> {
                doOpen.run();
                callback.onResult(null, null);
            });
            return null;
        }).when(connection).openAsync(any());
        return connection;
    };
    return new ControllableConnectionFactory(connectionFactory, openDurationHandle);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) MAX_VALUE(java.lang.Long.MAX_VALUE) TimeoutException(java.util.concurrent.TimeoutException) ClusterId(com.mongodb.connection.ClusterId) ConnectionDescription(com.mongodb.connection.ConnectionDescription) Future(java.util.concurrent.Future) Duration(java.time.Duration) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Tag(org.junit.jupiter.api.Tag) MongoServerUnavailableException(com.mongodb.MongoServerUnavailableException) MethodSource(org.junit.jupiter.params.provider.MethodSource) Collection(java.util.Collection) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Arguments(org.junit.jupiter.params.provider.Arguments) Executors(java.util.concurrent.Executors) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) EmptyProvider(com.mongodb.internal.inject.EmptyProvider) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Mockito.withSettings(org.mockito.Mockito.withSettings) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) MINUTES(java.util.concurrent.TimeUnit.MINUTES) SameObjectProvider(com.mongodb.internal.inject.SameObjectProvider) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MongoConnectionPoolClearedException(com.mongodb.MongoConnectionPoolClearedException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) SupplyingCallback(com.mongodb.client.syncadapter.SupplyingCallback) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ConnectionCreatedEvent(com.mongodb.event.ConnectionCreatedEvent) ExecutorService(java.util.concurrent.ExecutorService) ServerAddress(com.mongodb.ServerAddress) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ServerId(com.mongodb.connection.ServerId) Mockito.when(org.mockito.Mockito.when) Timeout(com.mongodb.internal.Timeout) ExecutionException(java.util.concurrent.ExecutionException) OptionalProvider(com.mongodb.internal.inject.OptionalProvider) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Condition(java.util.concurrent.locks.Condition) Lock(java.util.concurrent.locks.Lock) ConnectionId(com.mongodb.connection.ConnectionId) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ConnectionDescription(com.mongodb.connection.ConnectionDescription) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback)

Example 7 with ConnectionDescription

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

the class PlainAuthenticatorTest method setUp.

@Before
public void setUp() {
    String host = System.getProperty("org.mongodb.test.host");
    userName = System.getProperty("org.mongodb.test.userName");
    source = System.getProperty("org.mongod.test.source");
    password = System.getProperty("org.mongodb.test.password");
    internalConnection = new InternalStreamConnectionFactory(ClusterConnectionMode.SINGLE, streamFactory, null, null, null, Collections.<MongoCompressor>emptyList(), null, getServerApi()).create(new ServerId(new ClusterId(), new ServerAddress(host)));
    connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) ServerId(com.mongodb.connection.ServerId) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) Before(org.junit.Before)

Example 8 with ConnectionDescription

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

the class InternalStreamConnectionInitializer method createInitializationDescription.

private InternalConnectionInitializationDescription createInitializationDescription(final BsonDocument helloResult, final InternalConnection internalConnection, final long startTime) {
    ConnectionId connectionId = internalConnection.getDescription().getConnectionId();
    ConnectionDescription connectionDescription = createConnectionDescription(clusterConnectionMode, connectionId, helloResult);
    ServerDescription serverDescription = createServerDescription(internalConnection.getDescription().getServerAddress(), helloResult, System.nanoTime() - startTime);
    return new InternalConnectionInitializationDescription(connectionDescription, serverDescription);
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) DescriptionHelper.createConnectionDescription(com.mongodb.internal.connection.DescriptionHelper.createConnectionDescription) ConnectionId(com.mongodb.connection.ConnectionId) ServerDescription(com.mongodb.connection.ServerDescription) DescriptionHelper.createServerDescription(com.mongodb.internal.connection.DescriptionHelper.createServerDescription)

Example 9 with ConnectionDescription

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

the class DescriptionHelper method createConnectionDescription.

static ConnectionDescription createConnectionDescription(final ClusterConnectionMode clusterConnectionMode, final ConnectionId connectionId, final BsonDocument helloResult) {
    ConnectionDescription connectionDescription = new ConnectionDescription(connectionId, getMaxWireVersion(helloResult), getServerType(helloResult), getMaxWriteBatchSize(helloResult), getMaxBsonObjectSize(helloResult), getMaxMessageSizeBytes(helloResult), getCompressors(helloResult), helloResult.getArray("saslSupportedMechs", null));
    if (helloResult.containsKey("connectionId")) {
        ConnectionId newConnectionId = connectionDescription.getConnectionId().withServerValue(helloResult.getNumber("connectionId").intValue());
        connectionDescription = connectionDescription.withConnectionId(newConnectionId);
    }
    if (clusterConnectionMode == ClusterConnectionMode.LOAD_BALANCED) {
        ObjectId serviceId = getServiceId(helloResult);
        if (serviceId != null) {
            connectionDescription = connectionDescription.withServiceId(serviceId);
        } else {
            throw new MongoClientException("Driver attempted to initialize in load balancing mode, but the server does not support " + "this mode");
        }
    }
    return connectionDescription;
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) ConnectionId(com.mongodb.connection.ConnectionId) MongoClientException(com.mongodb.MongoClientException) ObjectId(org.bson.types.ObjectId)

Example 10 with ConnectionDescription

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

the class X509AuthenticatorNoUserNameTest method before.

@Before
public void before() {
    connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
    connectionDescriptionThreeTwo = new ConnectionDescription(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())), 4, ServerType.STANDALONE, 1000, 16000, 48000, Collections.<String>emptyList());
    connectionDescriptionThreeFour = new ConnectionDescription(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())), 5, ServerType.STANDALONE, 1000, 16000, 48000, Collections.<String>emptyList());
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) ConnectionId(com.mongodb.connection.ConnectionId) ServerId(com.mongodb.connection.ServerId) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) Before(org.junit.Before)

Aggregations

ConnectionDescription (com.mongodb.connection.ConnectionDescription)12 ServerAddress (com.mongodb.ServerAddress)6 ClusterId (com.mongodb.connection.ClusterId)6 ConnectionId (com.mongodb.connection.ConnectionId)6 ServerId (com.mongodb.connection.ServerId)6 Before (org.junit.Before)5 MongoException (com.mongodb.MongoException)2 WriteConcern (com.mongodb.WriteConcern)2 Assertions (com.mongodb.assertions.Assertions)2 BulkWriteResult (com.mongodb.bulk.BulkWriteResult)2 SingleResultCallback (com.mongodb.internal.async.SingleResultCallback)2 RetryState (com.mongodb.internal.async.function.RetryState)2 DescriptionHelper.createConnectionDescription (com.mongodb.internal.connection.DescriptionHelper.createConnectionDescription)2 MongoWriteConcernWithResponseException (com.mongodb.internal.connection.MongoWriteConcernWithResponseException)2 Span (brave.Span)1 ThreadLocalSpan (brave.propagation.ThreadLocalSpan)1 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 MongoClientException (com.mongodb.MongoClientException)1 MongoConnectionPoolClearedException (com.mongodb.MongoConnectionPoolClearedException)1 MongoCredential (com.mongodb.MongoCredential)1