Search in sources :

Example 6 with ServerDescriptionChangedEvent

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

the class ServerDiscoveryAndMonitoringMonitoringTest method assertEvents.

private void assertEvents(final BsonArray events) {
    Iterator<ClusterDescriptionChangedEvent> clusterDescriptionChangedEventIterator = clusterListener.getClusterDescriptionChangedEvents().iterator();
    for (BsonValue eventValue : events) {
        BsonDocument eventDocument = eventValue.asDocument();
        if (eventDocument.containsKey("topology_opening_event")) {
            ClusterOpeningEvent event = clusterListener.getClusterOpeningEvent();
            assertNotNull("event", event);
            assertEquals("clusterId", getCluster().getClusterId(), event.getClusterId());
        } else if (eventDocument.containsKey("topology_description_changed_event")) {
            ClusterDescriptionChangedEvent event = clusterDescriptionChangedEventIterator.next();
            assertNotNull("event", event);
            assertEquals(getCluster().getClusterId(), event.getClusterId());
            BsonDocument topologyDescriptionChangedEventDocument = eventDocument.getDocument("topology_description_changed_event");
            assertEqualClusterDescriptions(createClusterDescriptionFromClusterDescriptionDocument(topologyDescriptionChangedEventDocument.getDocument("previousDescription")), event.getPreviousDescription());
            BsonDocument newDescription = topologyDescriptionChangedEventDocument.getDocument("newDescription");
            assertEqualClusterDescriptions(createClusterDescriptionFromClusterDescriptionDocument(newDescription), event.getNewDescription());
            if (newDescription.getString("topologyType").getValue().equals("Single")) {
                assertEquals(SingleServerCluster.class, getCluster().getClass());
            } else {
                assertEquals(MultiServerCluster.class, getCluster().getClass());
            }
        } else if (eventDocument.containsKey("server_opening_event")) {
            BsonDocument serverOpeningEventDocument = eventDocument.getDocument("server_opening_event");
            ServerAddress serverAddress = new ServerAddress(serverOpeningEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            ServerOpeningEvent event = serverListener.getServerOpeningEvent();
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
        } else if (eventDocument.containsKey("server_closed_event")) {
            BsonDocument serverClosedEventDocument = eventDocument.getDocument("server_closed_event");
            ServerAddress serverAddress = new ServerAddress(serverClosedEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            ServerClosedEvent event = serverListener.getServerClosedEvent();
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
        } else if (eventDocument.containsKey("server_description_changed_event")) {
            BsonDocument serverDescriptionChangedEventDocument = eventDocument.getDocument("server_description_changed_event");
            ServerAddress serverAddress = new ServerAddress(serverDescriptionChangedEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            assertEquals("serverDescriptionChangedEvents size", 1, serverListener.getServerDescriptionChangedEvents().size());
            ServerDescriptionChangedEvent event = serverListener.getServerDescriptionChangedEvents().get(0);
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
            assertEqualServerDescriptions(createServerDescriptionFromServerDescriptionDocument(serverDescriptionChangedEventDocument.getDocument("previousDescription")), event.getPreviousDescription());
            assertEqualServerDescriptions(createServerDescriptionFromServerDescriptionDocument(serverDescriptionChangedEventDocument.getDocument("newDescription")), event.getNewDescription());
        } else {
            throw new IllegalArgumentException("Unsupported event type: " + eventDocument.keySet().iterator().next());
        }
    }
    assertFalse(clusterDescriptionChangedEventIterator.hasNext());
}
Also used : ServerAddress(com.mongodb.ServerAddress) ServerClosedEvent(com.mongodb.event.ServerClosedEvent) ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ClusterDescriptionChangedEvent(com.mongodb.event.ClusterDescriptionChangedEvent) BsonDocument(org.bson.BsonDocument) ServerOpeningEvent(com.mongodb.event.ServerOpeningEvent) ClusterOpeningEvent(com.mongodb.event.ClusterOpeningEvent) BsonValue(org.bson.BsonValue)

Example 7 with ServerDescriptionChangedEvent

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

the class ServerDiscoveryAndMonitoringMonitoringTest method assertEvents.

private void assertEvents(final BsonArray events) {
    Iterator<ClusterDescriptionChangedEvent> clusterDescriptionChangedEventIterator = clusterListener.getClusterDescriptionChangedEvents().iterator();
    for (BsonValue eventValue : events) {
        BsonDocument eventDocument = eventValue.asDocument();
        if (eventDocument.containsKey("topology_opening_event")) {
            ClusterOpeningEvent event = clusterListener.getClusterOpeningEvent();
            assertNotNull("event", event);
            assertEquals("clusterId", getCluster().getClusterId(), event.getClusterId());
        } else if (eventDocument.containsKey("topology_description_changed_event")) {
            ClusterDescriptionChangedEvent event = clusterDescriptionChangedEventIterator.next();
            assertNotNull("event", event);
            assertEquals(getCluster().getClusterId(), event.getClusterId());
            BsonDocument topologyDescriptionChangedEventDocument = eventDocument.getDocument("topology_description_changed_event");
            assertEqualClusterDescriptions(createClusterDescriptionFromClusterDescriptionDocument(topologyDescriptionChangedEventDocument.getDocument("previousDescription")), event.getPreviousDescription());
            BsonDocument newDescription = topologyDescriptionChangedEventDocument.getDocument("newDescription");
            assertEqualClusterDescriptions(createClusterDescriptionFromClusterDescriptionDocument(newDescription), event.getNewDescription());
            if (newDescription.getString("topologyType").getValue().equals("Single") && isSingleServerClusterExpected()) {
                assertEquals(SingleServerCluster.class, getCluster().getClass());
            } else if (newDescription.getString("topologyType").getValue().equals("LoadBalanced")) {
                assertEquals(LoadBalancedCluster.class, getCluster().getClass());
            } else {
                assertEquals(MultiServerCluster.class, getCluster().getClass());
            }
        } else if (eventDocument.containsKey("server_opening_event")) {
            BsonDocument serverOpeningEventDocument = eventDocument.getDocument("server_opening_event");
            ServerAddress serverAddress = new ServerAddress(serverOpeningEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            ServerOpeningEvent event = serverListener.getServerOpeningEvent();
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
        } else if (eventDocument.containsKey("server_closed_event")) {
            BsonDocument serverClosedEventDocument = eventDocument.getDocument("server_closed_event");
            ServerAddress serverAddress = new ServerAddress(serverClosedEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            ServerClosedEvent event = serverListener.getServerClosedEvent();
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
        } else if (eventDocument.containsKey("server_description_changed_event")) {
            BsonDocument serverDescriptionChangedEventDocument = eventDocument.getDocument("server_description_changed_event");
            ServerAddress serverAddress = new ServerAddress(serverDescriptionChangedEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            assertEquals("serverDescriptionChangedEvents size", 1, serverListener.getServerDescriptionChangedEvents().size());
            ServerDescriptionChangedEvent event = serverListener.getServerDescriptionChangedEvents().get(0);
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
            assertEqualServerDescriptions(createServerDescriptionFromServerDescriptionDocument(serverDescriptionChangedEventDocument.getDocument("previousDescription")), event.getPreviousDescription());
            assertEqualServerDescriptions(createServerDescriptionFromServerDescriptionDocument(serverDescriptionChangedEventDocument.getDocument("newDescription")), event.getNewDescription());
        } else {
            throw new IllegalArgumentException("Unsupported event type: " + eventDocument.keySet().iterator().next());
        }
    }
    assertFalse(clusterDescriptionChangedEventIterator.hasNext());
}
Also used : ServerAddress(com.mongodb.ServerAddress) ServerClosedEvent(com.mongodb.event.ServerClosedEvent) ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ClusterDescriptionChangedEvent(com.mongodb.event.ClusterDescriptionChangedEvent) ServerId(com.mongodb.connection.ServerId) BsonDocument(org.bson.BsonDocument) ServerOpeningEvent(com.mongodb.event.ServerOpeningEvent) ClusterOpeningEvent(com.mongodb.event.ClusterOpeningEvent) BsonValue(org.bson.BsonValue)

Aggregations

ServerDescriptionChangedEvent (com.mongodb.event.ServerDescriptionChangedEvent)7 BsonDocument (org.bson.BsonDocument)4 ServerAddress (com.mongodb.ServerAddress)3 ClusterDescriptionChangedEvent (com.mongodb.event.ClusterDescriptionChangedEvent)3 ClusterOpeningEvent (com.mongodb.event.ClusterOpeningEvent)3 ServerClosedEvent (com.mongodb.event.ServerClosedEvent)3 ServerOpeningEvent (com.mongodb.event.ServerOpeningEvent)3 ServerDescription (com.mongodb.connection.ServerDescription)2 ServerId (com.mongodb.connection.ServerId)2 BsonValue (org.bson.BsonValue)2 Block (com.mongodb.Block)1 ClusterFixture.isServerlessTest (com.mongodb.ClusterFixture.isServerlessTest)1 MongoClientSettings (com.mongodb.MongoClientSettings)1 Fixture.getMongoClientSettingsBuilder (com.mongodb.client.Fixture.getMongoClientSettingsBuilder)1 ClusterDescription (com.mongodb.connection.ClusterDescription)1 ClusterId (com.mongodb.connection.ClusterId)1 ConnectionId (com.mongodb.connection.ConnectionId)1 ServerSettings (com.mongodb.connection.ServerSettings)1 ClusterClosedEvent (com.mongodb.event.ClusterClosedEvent)1 ServerHeartbeatFailedEvent (com.mongodb.event.ServerHeartbeatFailedEvent)1