Search in sources :

Example 1 with ClusterFixture

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

the class ServerDiscoveryAndMonitoringProseTests method testConnectionPoolManagement.

/**
 * See
 * <a href="https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring-tests.rst#connection-pool-management">Connection Pool Management</a>.
 */
@Test
@Ignore
@SuppressWarnings("try")
public void testConnectionPoolManagement() throws InterruptedException {
    assumeTrue(serverVersionAtLeast(4, 3));
    assumeFalse(isServerlessTest());
    BlockingQueue<Object> events = new LinkedBlockingQueue<>();
    ServerMonitorListener serverMonitorListener = new ServerMonitorListener() {

        @Override
        public void serverHeartbeatSucceeded(final ServerHeartbeatSucceededEvent event) {
            put(events, event);
        }

        @Override
        public void serverHeartbeatFailed(final ServerHeartbeatFailedEvent event) {
            put(events, event);
        }
    };
    ConnectionPoolListener connectionPoolListener = new ConnectionPoolListener() {

        @Override
        public void connectionPoolReady(final ConnectionPoolReadyEvent event) {
            put(events, event);
        }

        @Override
        public void connectionPoolCleared(final ConnectionPoolClearedEvent event) {
            put(events, event);
        }
    };
    String appName = "SDAMPoolManagementTest";
    MongoClientSettings clientSettings = getMongoClientSettingsBuilder().applicationName(appName).applyToClusterSettings(ClusterFixture::setDirectConnection).applyToServerSettings(builder -> builder.heartbeatFrequency(100, TimeUnit.MILLISECONDS).addServerMonitorListener(serverMonitorListener)).applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(connectionPoolListener)).build();
    try (MongoClient unused = MongoClients.create(clientSettings)) {
        /* Note that ServerHeartbeatSucceededEvent type is sometimes allowed but never required.
             * This is because DefaultServerMonitor does not send such events in situations when a server check happens as part
             * of a connection handshake. */
        assertPoll(events, ServerHeartbeatSucceededEvent.class, singleton(ConnectionPoolReadyEvent.class));
        configureFailPoint(new BsonDocument().append("configureFailPoint", new BsonString("failCommand")).append("mode", new BsonDocument().append("times", new BsonInt32(2))).append("data", new BsonDocument().append("failCommands", new BsonArray(asList(new BsonString("isMaster"), new BsonString("hello")))).append("errorCode", new BsonInt32(1234)).append("appName", new BsonString(appName))));
        assertPoll(events, ServerHeartbeatSucceededEvent.class, new HashSet<>(asList(ServerHeartbeatFailedEvent.class, ConnectionPoolClearedEvent.class)));
        assertPoll(events, null, new HashSet<>(asList(ServerHeartbeatSucceededEvent.class, ConnectionPoolReadyEvent.class)));
    } finally {
        disableFailPoint("failCommand");
    }
}
Also used : Document(org.bson.Document) ServerListener(com.mongodb.event.ServerListener) Fixture.getMongoClientSettingsBuilder(com.mongodb.client.Fixture.getMongoClientSettingsBuilder) Collections.singleton(java.util.Collections.singleton) Arrays.asList(java.util.Arrays.asList) BsonArray(org.bson.BsonArray) ClusterFixture.isStandalone(com.mongodb.ClusterFixture.isStandalone) Assert.fail(org.junit.Assert.fail) ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ServerHeartbeatSucceededEvent(com.mongodb.event.ServerHeartbeatSucceededEvent) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) BsonNull(org.bson.BsonNull) Collections.synchronizedList(java.util.Collections.synchronizedList) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ConnectionPoolClearedEvent(com.mongodb.event.ConnectionPoolClearedEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) String.format(java.lang.String.format) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Block(com.mongodb.Block) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) ClusterFixture.configureFailPoint(com.mongodb.ClusterFixture.configureFailPoint) Assume.assumeTrue(org.junit.Assume.assumeTrue) MongoClientSettings(com.mongodb.MongoClientSettings) Fixture.getDefaultDatabaseName(com.mongodb.client.Fixture.getDefaultDatabaseName) BsonDocument.parse(org.bson.BsonDocument.parse) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) Assume.assumeFalse(org.junit.Assume.assumeFalse) BsonString(org.bson.BsonString) ServerSettings(com.mongodb.connection.ServerSettings) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterFixture(com.mongodb.ClusterFixture) ServerHeartbeatFailedEvent(com.mongodb.event.ServerHeartbeatFailedEvent) Logger(com.mongodb.diagnostics.logging.Logger) MongoInterruptedException(com.mongodb.MongoInterruptedException) ConnectionPoolListener(com.mongodb.event.ConnectionPoolListener) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Timeout(com.mongodb.internal.Timeout) TimeUnit(java.util.concurrent.TimeUnit) ConnectionPoolReadyEvent(com.mongodb.event.ConnectionPoolReadyEvent) ServerMonitorListener(com.mongodb.event.ServerMonitorListener) Ignore(org.junit.Ignore) Loggers(com.mongodb.diagnostics.logging.Loggers) ClusterFixture.serverVersionAtLeast(com.mongodb.ClusterFixture.serverVersionAtLeast) Nullable(com.mongodb.lang.Nullable) BsonInt32(org.bson.BsonInt32) ClusterFixture.disableFailPoint(com.mongodb.ClusterFixture.disableFailPoint) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) ServerHeartbeatFailedEvent(com.mongodb.event.ServerHeartbeatFailedEvent) BsonString(org.bson.BsonString) MongoClientSettings(com.mongodb.MongoClientSettings) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ServerHeartbeatSucceededEvent(com.mongodb.event.ServerHeartbeatSucceededEvent) ServerMonitorListener(com.mongodb.event.ServerMonitorListener) BsonInt32(org.bson.BsonInt32) ConnectionPoolClearedEvent(com.mongodb.event.ConnectionPoolClearedEvent) BsonDocument(org.bson.BsonDocument) ConnectionPoolListener(com.mongodb.event.ConnectionPoolListener) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ConnectionPoolReadyEvent(com.mongodb.event.ConnectionPoolReadyEvent) Ignore(org.junit.Ignore) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) Test(org.junit.Test)

Example 2 with ClusterFixture

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

the class ServerDiscoveryAndMonitoringProseTests method monitorsSleepAtLeastMinHeartbeatFreqencyMSBetweenChecks.

/**
 * See
 * <a href="https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring-tests.rst#monitors-sleep-at-least-minheartbeatfreqencyms-between-checks">
 * Monitors sleep at least minHeartbeatFreqencyMS between checks</a>.
 */
@Test
@SuppressWarnings("try")
public void monitorsSleepAtLeastMinHeartbeatFreqencyMSBetweenChecks() {
    assumeTrue(serverVersionAtLeast(4, 3));
    assumeFalse(isServerlessTest());
    long defaultMinHeartbeatIntervalMillis = MongoClientSettings.builder().build().getServerSettings().getMinHeartbeatFrequency(MILLISECONDS);
    assertEquals(500, defaultMinHeartbeatIntervalMillis);
    String appName = "SDAMMinHeartbeatFrequencyTest";
    MongoClientSettings clientSettings = getMongoClientSettingsBuilder().applicationName(appName).applyToClusterSettings(ClusterFixture::setDirectConnection).applyToClusterSettings(builder -> builder.serverSelectionTimeout(5000, MILLISECONDS)).applyToServerSettings(builder -> builder.minHeartbeatFrequency(defaultMinHeartbeatIntervalMillis, MILLISECONDS)).build();
    BsonDocument configureFailPoint = new BsonDocument().append("configureFailPoint", new BsonString("failCommand")).append("mode", new BsonDocument().append("times", new BsonInt32(5))).append("data", new BsonDocument().append("failCommands", new BsonArray(asList(new BsonString("hello"), new BsonString("isMaster")))).append("errorCode", new BsonInt32(1234)).append("appName", new BsonString(appName)));
    try (FailPoint ignored = FailPoint.enable(configureFailPoint, clientSettings.getClusterSettings().getHosts().get(0));
        MongoClient client = MongoClients.create(clientSettings)) {
        long startNanos = System.nanoTime();
        client.getDatabase(getDefaultDatabaseName()).runCommand(new BsonDocument("ping", BsonNull.VALUE));
        long durationMillis = NANOSECONDS.toMillis(System.nanoTime() - startNanos);
        String msg = durationMillis + " ms";
        assertTrue(msg, durationMillis >= 2000);
        assertTrue(msg, durationMillis <= 3500);
    }
}
Also used : Document(org.bson.Document) ServerListener(com.mongodb.event.ServerListener) Fixture.getMongoClientSettingsBuilder(com.mongodb.client.Fixture.getMongoClientSettingsBuilder) Collections.singleton(java.util.Collections.singleton) Arrays.asList(java.util.Arrays.asList) BsonArray(org.bson.BsonArray) ClusterFixture.isStandalone(com.mongodb.ClusterFixture.isStandalone) Assert.fail(org.junit.Assert.fail) ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ServerHeartbeatSucceededEvent(com.mongodb.event.ServerHeartbeatSucceededEvent) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) BsonNull(org.bson.BsonNull) Collections.synchronizedList(java.util.Collections.synchronizedList) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ConnectionPoolClearedEvent(com.mongodb.event.ConnectionPoolClearedEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) String.format(java.lang.String.format) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Block(com.mongodb.Block) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) ClusterFixture.configureFailPoint(com.mongodb.ClusterFixture.configureFailPoint) Assume.assumeTrue(org.junit.Assume.assumeTrue) MongoClientSettings(com.mongodb.MongoClientSettings) Fixture.getDefaultDatabaseName(com.mongodb.client.Fixture.getDefaultDatabaseName) BsonDocument.parse(org.bson.BsonDocument.parse) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) Assume.assumeFalse(org.junit.Assume.assumeFalse) BsonString(org.bson.BsonString) ServerSettings(com.mongodb.connection.ServerSettings) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterFixture(com.mongodb.ClusterFixture) ServerHeartbeatFailedEvent(com.mongodb.event.ServerHeartbeatFailedEvent) Logger(com.mongodb.diagnostics.logging.Logger) MongoInterruptedException(com.mongodb.MongoInterruptedException) ConnectionPoolListener(com.mongodb.event.ConnectionPoolListener) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Timeout(com.mongodb.internal.Timeout) TimeUnit(java.util.concurrent.TimeUnit) ConnectionPoolReadyEvent(com.mongodb.event.ConnectionPoolReadyEvent) ServerMonitorListener(com.mongodb.event.ServerMonitorListener) Ignore(org.junit.Ignore) Loggers(com.mongodb.diagnostics.logging.Loggers) ClusterFixture.serverVersionAtLeast(com.mongodb.ClusterFixture.serverVersionAtLeast) Nullable(com.mongodb.lang.Nullable) BsonInt32(org.bson.BsonInt32) ClusterFixture.disableFailPoint(com.mongodb.ClusterFixture.disableFailPoint) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) MongoClientSettings(com.mongodb.MongoClientSettings) ClusterFixture.configureFailPoint(com.mongodb.ClusterFixture.configureFailPoint) ClusterFixture.disableFailPoint(com.mongodb.ClusterFixture.disableFailPoint) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) Test(org.junit.Test)

Aggregations

Block (com.mongodb.Block)2 ClusterFixture (com.mongodb.ClusterFixture)2 ClusterFixture.configureFailPoint (com.mongodb.ClusterFixture.configureFailPoint)2 ClusterFixture.disableFailPoint (com.mongodb.ClusterFixture.disableFailPoint)2 ClusterFixture.isServerlessTest (com.mongodb.ClusterFixture.isServerlessTest)2 ClusterFixture.isStandalone (com.mongodb.ClusterFixture.isStandalone)2 ClusterFixture.serverVersionAtLeast (com.mongodb.ClusterFixture.serverVersionAtLeast)2 MongoClientSettings (com.mongodb.MongoClientSettings)2 MongoInterruptedException (com.mongodb.MongoInterruptedException)2 Fixture.getDefaultDatabaseName (com.mongodb.client.Fixture.getDefaultDatabaseName)2 Fixture.getMongoClientSettingsBuilder (com.mongodb.client.Fixture.getMongoClientSettingsBuilder)2 ServerSettings (com.mongodb.connection.ServerSettings)2 Logger (com.mongodb.diagnostics.logging.Logger)2 Loggers (com.mongodb.diagnostics.logging.Loggers)2 ConnectionPoolClearedEvent (com.mongodb.event.ConnectionPoolClearedEvent)2 ConnectionPoolListener (com.mongodb.event.ConnectionPoolListener)2 ConnectionPoolReadyEvent (com.mongodb.event.ConnectionPoolReadyEvent)2 ServerDescriptionChangedEvent (com.mongodb.event.ServerDescriptionChangedEvent)2 ServerHeartbeatFailedEvent (com.mongodb.event.ServerHeartbeatFailedEvent)2 ServerHeartbeatSucceededEvent (com.mongodb.event.ServerHeartbeatSucceededEvent)2