Search in sources :

Example 6 with ServerId

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

the class DefaultTestClusterableServerFactory method create.

@Override
public ClusterableServer create(final Cluster cluster, final ServerAddress serverAddress) {
    ServerId serverId = new ServerId(cluster.getClusterId(), serverAddress);
    if (clusterConnectionMode == ClusterConnectionMode.LOAD_BALANCED) {
        return new LoadBalancedServer(serverId, new TestConnectionPool(), new TestConnectionFactory(), serverListenerFactory.create(serverAddress), cluster.getClock());
    } else {
        SameObjectProvider<SdamServerDescriptionManager> sdamProvider = SameObjectProvider.uninitialized();
        TestServerMonitor serverMonitor = new TestServerMonitor(sdamProvider);
        serverAddressToServerMonitorMap.put(serverAddress, serverMonitor);
        ConnectionPool connectionPool = new TestConnectionPool();
        ServerListener serverListener = serverListenerFactory.create(serverAddress);
        SdamServerDescriptionManager sdam = new DefaultSdamServerDescriptionManager(cluster, serverId, serverListener, serverMonitor, connectionPool, clusterConnectionMode);
        sdamProvider.initialize(sdam);
        serverMonitor.start();
        return new DefaultServer(serverId, clusterConnectionMode, connectionPool, new TestConnectionFactory(), serverMonitor, sdam, serverListener, null, cluster.getClock(), true);
    }
}
Also used : ServerId(com.mongodb.connection.ServerId) ServerListener(com.mongodb.event.ServerListener)

Example 7 with ServerId

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

the class PlainAuthenticatorUnitTest method before.

@Before
public void before() {
    connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
    connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
    credential = MongoCredential.createPlainCredential("user", "$external", "pencil".toCharArray());
    subject = new PlainAuthenticator(new MongoCredentialWithCache(credential), ClusterConnectionMode.MULTIPLE, getServerApi());
}
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 ServerId

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

the class NativeAuthenticatorUnitTest method before.

@Before
public void before() {
    connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
    connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
    MongoCredential credential = MongoCredential.createCredential("\u53f0\u5317", "database", "Ta\u0301ibe\u030Ci".toCharArray());
    subject = new NativeAuthenticator(new MongoCredentialWithCache(credential), ClusterConnectionMode.MULTIPLE, getServerApi());
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) ServerId(com.mongodb.connection.ServerId) ClusterId(com.mongodb.connection.ClusterId) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) Before(org.junit.Before)

Example 9 with ServerId

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

the class X509AuthenticatorUnitTest method before.

@Before
public void before() {
    connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
    connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
    credential = MongoCredential.createMongoX509Credential("CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US");
    subject = new X509Authenticator(new MongoCredentialWithCache(credential), ClusterConnectionMode.MULTIPLE, getServerApi());
}
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 10 with ServerId

use of com.mongodb.connection.ServerId 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)

Aggregations

ServerId (com.mongodb.connection.ServerId)13 ServerAddress (com.mongodb.ServerAddress)9 ClusterId (com.mongodb.connection.ClusterId)8 ConnectionDescription (com.mongodb.connection.ConnectionDescription)7 Before (org.junit.Before)6 ConnectionId (com.mongodb.connection.ConnectionId)4 ConnectionPoolSettings (com.mongodb.connection.ConnectionPoolSettings)3 ServerListener (com.mongodb.event.ServerListener)3 MongoConnectionPoolClearedException (com.mongodb.MongoConnectionPoolClearedException)2 MongoServerUnavailableException (com.mongodb.MongoServerUnavailableException)2 ClusterConnectionMode (com.mongodb.connection.ClusterConnectionMode)2 ClusterDescriptionChangedEvent (com.mongodb.event.ClusterDescriptionChangedEvent)2 ClusterOpeningEvent (com.mongodb.event.ClusterOpeningEvent)2 ConnectionCreatedEvent (com.mongodb.event.ConnectionCreatedEvent)2 BsonDocument (org.bson.BsonDocument)2 MongoCredential (com.mongodb.MongoCredential)1 MongoException (com.mongodb.MongoException)1 MongoInterruptedException (com.mongodb.MongoInterruptedException)1 MongoTimeoutException (com.mongodb.MongoTimeoutException)1 RequestContext (com.mongodb.RequestContext)1