use of com.mongodb.event.ServerHeartbeatSucceededEvent in project mongo-java-driver by mongodb.
the class ServerDiscoveryAndMonitoringProseTests method testHeartbeatFrequency.
@Test
@SuppressWarnings("try")
public void testHeartbeatFrequency() throws InterruptedException {
assumeFalse(isServerlessTest());
CountDownLatch latch = new CountDownLatch(5);
MongoClientSettings settings = getMongoClientSettingsBuilder().applyToServerSettings(new Block<ServerSettings.Builder>() {
@Override
public void apply(final ServerSettings.Builder builder) {
builder.heartbeatFrequency(50, TimeUnit.MILLISECONDS);
builder.addServerMonitorListener(new ServerMonitorListener() {
@Override
public void serverHeartbeatSucceeded(final ServerHeartbeatSucceededEvent event) {
latch.countDown();
}
});
}
}).build();
try (MongoClient ignored = MongoClients.create(settings)) {
assertTrue("Took longer than expected to reach expected number of hearbeats", latch.await(500, TimeUnit.MILLISECONDS));
}
}
use of com.mongodb.event.ServerHeartbeatSucceededEvent 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");
}
}
use of com.mongodb.event.ServerHeartbeatSucceededEvent in project mongo-java-driver by mongodb.
the class AsynchronousClusterEventListenerTest method testEventsPublished.
@Test
public void testEventsPublished() throws InterruptedException {
AllClusterEventListener targetListener = new AllClusterEventListener();
ClusterId clusterId = new ClusterId();
ServerId serverId = new ServerId(clusterId, new ServerAddress());
ConnectionId connectionId = new ConnectionId(serverId);
AsynchronousClusterEventListener listener = AsynchronousClusterEventListener.startNew(clusterId, targetListener, targetListener, targetListener);
ClusterOpeningEvent clusterOpeningEvent = new ClusterOpeningEvent(clusterId);
listener.clusterOpening(clusterOpeningEvent);
assertEquals(clusterOpeningEvent, targetListener.take());
ClusterDescriptionChangedEvent clusterDescriptionChangedEvent = new ClusterDescriptionChangedEvent(clusterId, new ClusterDescription(ClusterConnectionMode.SINGLE, ClusterType.STANDALONE, Collections.emptyList()), new ClusterDescription(ClusterConnectionMode.SINGLE, ClusterType.STANDALONE, Collections.emptyList()));
listener.clusterDescriptionChanged(clusterDescriptionChangedEvent);
assertEquals(clusterDescriptionChangedEvent, targetListener.take());
ServerHeartbeatStartedEvent serverHeartbeatStartedEvent = new ServerHeartbeatStartedEvent(connectionId);
listener.serverHearbeatStarted(serverHeartbeatStartedEvent);
assertEquals(serverHeartbeatStartedEvent, targetListener.take());
ServerHeartbeatSucceededEvent serverHeartbeatSucceededEvent = new ServerHeartbeatSucceededEvent(connectionId, new BsonDocument(), 1, true);
listener.serverHeartbeatSucceeded(serverHeartbeatSucceededEvent);
assertEquals(serverHeartbeatSucceededEvent, targetListener.take());
ServerHeartbeatFailedEvent serverHeartbeatFailedEvent = new ServerHeartbeatFailedEvent(connectionId, 1, true, new IOException());
listener.serverHeartbeatFailed(serverHeartbeatFailedEvent);
assertEquals(serverHeartbeatFailedEvent, targetListener.take());
ServerOpeningEvent serverOpeningEvent = new ServerOpeningEvent(serverId);
listener.serverOpening(serverOpeningEvent);
assertEquals(serverOpeningEvent, targetListener.take());
ServerDescriptionChangedEvent serverDescriptionChangedEvent = new ServerDescriptionChangedEvent(serverId, ServerDescription.builder().address(new ServerAddress()).type(UNKNOWN).state(CONNECTING).build(), ServerDescription.builder().address(new ServerAddress()).type(STANDALONE).state(CONNECTED).build());
listener.serverDescriptionChanged(serverDescriptionChangedEvent);
assertEquals(serverDescriptionChangedEvent, targetListener.take());
ServerClosedEvent serverClosedEvent = new ServerClosedEvent(serverId);
listener.serverClosed(serverClosedEvent);
assertEquals(serverClosedEvent, targetListener.take());
ClusterClosedEvent clusterClosedEvent = new ClusterClosedEvent(clusterId);
listener.clusterClosed(clusterClosedEvent);
assertEquals(clusterClosedEvent, targetListener.take());
// The thread should die after publishing the ClusterClosedEvent
listener.getPublishingThread().join(5000);
}
Aggregations