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,28 @@ 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" ).isTrue ();
729
733
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 );
732
736
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 ));
735
739
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" );
740
744
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" );
745
749
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" );
752
754
}
753
755
mcpServer .close ();
754
756
}
0 commit comments