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,29 @@ 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" )
733+ . isTrue ();
729734
730- // Should have received 3 notifications (1 NOTICE and 2 ERROR)
731- assertThat (receivedNotifications ).hasSize (3 );
735+ // Should have received 3 notifications (1 NOTICE and 2 ERROR)
736+ assertThat (receivedNotifications ).hasSize (expectedNotificationsCount );
732737
733- Map <String , McpSchema .LoggingMessageNotification > notificationMap = receivedNotifications .stream ()
734- .collect (Collectors .toMap (n -> n .data (), n -> n ));
738+ Map <String , McpSchema .LoggingMessageNotification > notificationMap = receivedNotifications .stream ()
739+ .collect (Collectors .toMap (n -> n .data (), n -> n ));
735740
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" );
741+ // First notification should be NOTICE level
742+ assertThat (notificationMap .get ("Notice message" ).level ()).isEqualTo (McpSchema .LoggingLevel .NOTICE );
743+ assertThat (notificationMap .get ("Notice message" ).logger ()).isEqualTo ("test-logger" );
744+ assertThat (notificationMap .get ("Notice message" ).data ()).isEqualTo ("Notice message" );
740745
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" );
746+ // Second notification should be ERROR level
747+ assertThat (notificationMap .get ("Error message" ).level ()).isEqualTo (McpSchema .LoggingLevel .ERROR );
748+ assertThat (notificationMap .get ("Error message" ).logger ()).isEqualTo ("test-logger" );
749+ assertThat (notificationMap .get ("Error message" ).data ()).isEqualTo ("Error message" );
745750
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- });
751+ // Third notification should be ERROR level
752+ assertThat (notificationMap .get ("Another error message" ).level ()).isEqualTo (McpSchema .LoggingLevel .ERROR );
753+ assertThat (notificationMap .get ("Another error message" ).logger ()).isEqualTo ("test-logger" );
754+ assertThat (notificationMap .get ("Another error message" ).data ()).isEqualTo ("Another error message" );
752755 }
753756 mcpServer .close ();
754757 }
0 commit comments