Search in sources :

Example 1 with SameObjectProvider

use of com.mongodb.internal.inject.SameObjectProvider in project mongo-java-driver by mongodb.

the class AbstractConnectionPoolTest method setUp.

@Before
public void setUp() {
    assumeFalse(skipTest);
    ConnectionPoolSettings.Builder settingsBuilder = ConnectionPoolSettings.builder();
    BsonDocument poolOptions = definition.getDocument("poolOptions", new BsonDocument());
    if (poolOptions.containsKey("maxPoolSize")) {
        settingsBuilder.maxSize(poolOptions.getNumber("maxPoolSize").intValue());
    }
    if (poolOptions.containsKey("minPoolSize")) {
        settingsBuilder.minSize(poolOptions.getNumber("minPoolSize").intValue());
    }
    if (poolOptions.containsKey("maxIdleTimeMS")) {
        settingsBuilder.maxConnectionIdleTime(poolOptions.getNumber("maxIdleTimeMS").intValue(), TimeUnit.MILLISECONDS);
    }
    if (poolOptions.containsKey("waitQueueTimeoutMS")) {
        settingsBuilder.maxWaitTime(poolOptions.getNumber("waitQueueTimeoutMS").intValue(), TimeUnit.MILLISECONDS);
    }
    if (poolOptions.containsKey("backgroundThreadIntervalMS")) {
        long intervalMillis = poolOptions.getNumber("backgroundThreadIntervalMS").longValue();
        assertFalse(intervalMillis == 0);
        if (intervalMillis < 0) {
            settingsBuilder.maintenanceInitialDelay(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
        } else {
            /* Using frequency/period instead of an interval as required by the specification is incorrect, for example,
                 * because it opens up a possibility to run the background thread non-stop if runs are as long as or longer than the period.
                 * Nevertheless, I am reusing what we already have in the driver instead of clogging up the implementation. */
            settingsBuilder.maintenanceFrequency(poolOptions.getNumber("backgroundThreadIntervalMS").longValue(), TimeUnit.MILLISECONDS);
        }
    }
    if (poolOptions.containsKey("maxConnecting")) {
        settingsBuilder.maxConnecting(poolOptions.getInt32("maxConnecting").intValue());
    }
    listener = new TestConnectionPoolListener();
    settingsBuilder.addConnectionPoolListener(listener);
    settings = settingsBuilder.build();
    InternalConnectionPoolSettings internalSettings = InternalConnectionPoolSettings.builder().prestartAsyncWorkManager(PRESTART_POOL_ASYNC_WORK_MANAGER_FILE_NAMES.contains(fileName)).build();
    Style style = Style.of(definition.getString("style").getValue());
    switch(style) {
        case UNIT:
            {
                ServerId serverId = new ServerId(new ClusterId(), new ServerAddress("host1"));
                pool = new DefaultConnectionPool(serverId, new TestInternalConnectionFactory(), settings, internalSettings, SameObjectProvider.initialized(mock(SdamServerDescriptionManager.class)));
                break;
            }
        case INTEGRATION:
            {
                ServerId serverId = new ServerId(new ClusterId(), ClusterFixture.getPrimary());
                ClusterConnectionMode connectionMode = ClusterConnectionMode.MULTIPLE;
                SameObjectProvider<SdamServerDescriptionManager> sdamProvider = SameObjectProvider.uninitialized();
                pool = new ConnectionIdAdjustingConnectionPool(new DefaultConnectionPool(serverId, new InternalStreamConnectionFactory(connectionMode, createStreamFactory(SocketSettings.builder().build(), ClusterFixture.getSslSettings()), ClusterFixture.getCredentialWithCache(), poolOptions.getString("appName", new BsonString(fileName + ": " + description)).getValue(), MongoDriverInformation.builder().build(), Collections.emptyList(), new TestCommandListener(), ClusterFixture.getServerApi()), settings, internalSettings, sdamProvider));
                sdamProvider.initialize(new DefaultSdamServerDescriptionManager(mockedCluster(), serverId, mock(ServerListener.class), mock(ServerMonitor.class), pool, connectionMode));
                setFailPoint();
                break;
            }
        default:
            {
                throw new AssertionError(format("Style %s is not implemented", style));
            }
    }
    if (internalSettings.isPrestartAsyncWorkManager()) {
        waitForPoolAsyncWorkManagerStart();
    }
}
Also used : ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) SameObjectProvider(com.mongodb.internal.inject.SameObjectProvider) ClusterConnectionMode(com.mongodb.connection.ClusterConnectionMode) ServerId(com.mongodb.connection.ServerId) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings) ServerListener(com.mongodb.event.ServerListener) Before(org.junit.Before)

Aggregations

ServerAddress (com.mongodb.ServerAddress)1 ClusterConnectionMode (com.mongodb.connection.ClusterConnectionMode)1 ClusterId (com.mongodb.connection.ClusterId)1 ConnectionPoolSettings (com.mongodb.connection.ConnectionPoolSettings)1 ServerId (com.mongodb.connection.ServerId)1 ServerListener (com.mongodb.event.ServerListener)1 SameObjectProvider (com.mongodb.internal.inject.SameObjectProvider)1 BsonDocument (org.bson.BsonDocument)1 BsonString (org.bson.BsonString)1 Before (org.junit.Before)1