88import java .util .List ;
99import java .util .Map ;
1010import java .util .concurrent .ConcurrentHashMap ;
11+ import java .util .concurrent .CopyOnWriteArrayList ;
12+ import java .util .concurrent .CountDownLatch ;
1113import java .util .concurrent .TimeUnit ;
1214import java .util .concurrent .atomic .AtomicReference ;
1315import java .util .function .BiFunction ;
@@ -651,9 +653,11 @@ void testInitialize(String clientType) {
651653
652654 @ ParameterizedTest (name = "{0} : {displayName} " )
653655 @ ValueSource (strings = { "httpclient" , "webflux" })
654- void testLoggingNotification (String clientType ) {
656+ void testLoggingNotification (String clientType ) throws InterruptedException {
657+ int expectedNotificationsCount = 3 ;
658+ CountDownLatch latch = new CountDownLatch (expectedNotificationsCount );
655659 // Create a list to store received logging notifications
656- List <McpSchema .LoggingMessageNotification > receivedNotifications = new ArrayList <>();
660+ List <McpSchema .LoggingMessageNotification > receivedNotifications = new CopyOnWriteArrayList <>();
657661
658662 var clientBuilder = clientBuilders .get (clientType );
659663
@@ -709,6 +713,7 @@ void testLoggingNotification(String clientType) {
709713 // Create client with logging notification handler
710714 var mcpClient = clientBuilder .loggingConsumer (notification -> {
711715 receivedNotifications .add (notification );
716+ latch .countDown ();
712717 }).build ()) {
713718
714719 // Initialize client
@@ -724,31 +729,28 @@ void testLoggingNotification(String clientType) {
724729 assertThat (result .content ().get (0 )).isInstanceOf (McpSchema .TextContent .class );
725730 assertThat (((McpSchema .TextContent ) result .content ().get (0 )).text ()).isEqualTo ("Logging test completed" );
726731
727- // Wait for notifications to be processed
728- await ().atMost (Duration .ofSeconds (5 )).untilAsserted (() -> {
732+ assertThat (latch .await (5 , TimeUnit .SECONDS )).as ("Should receive notifications in reasonable time" ).isTrue ();
729733
730- // Should have received 3 notifications (1 NOTICE and 2 ERROR)
731- assertThat (receivedNotifications ).hasSize (3 );
734+ // Should have received 3 notifications (1 NOTICE and 2 ERROR)
735+ assertThat (receivedNotifications ).hasSize (expectedNotificationsCount );
732736
733- Map <String , McpSchema .LoggingMessageNotification > notificationMap = receivedNotifications .stream ()
734- .collect (Collectors .toMap (n -> n .data (), n -> n ));
737+ Map <String , McpSchema .LoggingMessageNotification > notificationMap = receivedNotifications .stream ()
738+ .collect (Collectors .toMap (n -> n .data (), n -> n ));
735739
736- // First notification should be NOTICE level
737- assertThat (notificationMap .get ("Notice message" ).level ()).isEqualTo (McpSchema .LoggingLevel .NOTICE );
738- assertThat (notificationMap .get ("Notice message" ).logger ()).isEqualTo ("test-logger" );
739- assertThat (notificationMap .get ("Notice message" ).data ()).isEqualTo ("Notice message" );
740+ // First notification should be NOTICE level
741+ assertThat (notificationMap .get ("Notice message" ).level ()).isEqualTo (McpSchema .LoggingLevel .NOTICE );
742+ assertThat (notificationMap .get ("Notice message" ).logger ()).isEqualTo ("test-logger" );
743+ assertThat (notificationMap .get ("Notice message" ).data ()).isEqualTo ("Notice message" );
740744
741- // Second notification should be ERROR level
742- assertThat (notificationMap .get ("Error message" ).level ()).isEqualTo (McpSchema .LoggingLevel .ERROR );
743- assertThat (notificationMap .get ("Error message" ).logger ()).isEqualTo ("test-logger" );
744- assertThat (notificationMap .get ("Error message" ).data ()).isEqualTo ("Error message" );
745+ // Second notification should be ERROR level
746+ assertThat (notificationMap .get ("Error message" ).level ()).isEqualTo (McpSchema .LoggingLevel .ERROR );
747+ assertThat (notificationMap .get ("Error message" ).logger ()).isEqualTo ("test-logger" );
748+ assertThat (notificationMap .get ("Error message" ).data ()).isEqualTo ("Error message" );
745749
746- // Third notification should be ERROR level
747- assertThat (notificationMap .get ("Another error message" ).level ())
748- .isEqualTo (McpSchema .LoggingLevel .ERROR );
749- assertThat (notificationMap .get ("Another error message" ).logger ()).isEqualTo ("test-logger" );
750- assertThat (notificationMap .get ("Another error message" ).data ()).isEqualTo ("Another error message" );
751- });
750+ // Third notification should be ERROR level
751+ assertThat (notificationMap .get ("Another error message" ).level ()).isEqualTo (McpSchema .LoggingLevel .ERROR );
752+ assertThat (notificationMap .get ("Another error message" ).logger ()).isEqualTo ("test-logger" );
753+ assertThat (notificationMap .get ("Another error message" ).data ()).isEqualTo ("Another error message" );
752754 }
753755 mcpServer .close ();
754756 }
0 commit comments