8
8
import java .util .List ;
9
9
import java .util .Map ;
10
10
import java .util .concurrent .ConcurrentHashMap ;
11
+ import java .util .concurrent .CopyOnWriteArrayList ;
12
+ import java .util .concurrent .CountDownLatch ;
11
13
import java .util .concurrent .TimeUnit ;
12
14
import java .util .concurrent .atomic .AtomicReference ;
13
15
import java .util .function .BiFunction ;
@@ -651,9 +653,11 @@ void testInitialize(String clientType) {
651
653
652
654
@ ParameterizedTest (name = "{0} : {displayName} " )
653
655
@ 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 );
655
659
// Create a list to store received logging notifications
656
- List <McpSchema .LoggingMessageNotification > receivedNotifications = new ArrayList <>();
660
+ List <McpSchema .LoggingMessageNotification > receivedNotifications = new CopyOnWriteArrayList <>();
657
661
658
662
var clientBuilder = clientBuilders .get (clientType );
659
663
@@ -709,6 +713,7 @@ void testLoggingNotification(String clientType) {
709
713
// Create client with logging notification handler
710
714
var mcpClient = clientBuilder .loggingConsumer (notification -> {
711
715
receivedNotifications .add (notification );
716
+ latch .countDown ();
712
717
}).build ()) {
713
718
714
719
// Initialize client
@@ -724,31 +729,29 @@ void testLoggingNotification(String clientType) {
724
729
assertThat (result .content ().get (0 )).isInstanceOf (McpSchema .TextContent .class );
725
730
assertThat (((McpSchema .TextContent ) result .content ().get (0 )).text ()).isEqualTo ("Logging test completed" );
726
731
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 ();
729
734
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 );
732
737
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 ));
735
740
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" );
740
745
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" );
745
750
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" );
752
755
}
753
756
mcpServer .close ();
754
757
}
0 commit comments