5
5
"fmt"
6
6
"github.com/stretchr/testify/assert"
7
7
"net/http"
8
+ "sync"
8
9
"testing"
9
10
"time"
10
11
@@ -460,16 +461,19 @@ func TestSSEMCPClient(t *testing.T) {
460
461
t .Fatalf ("Failed to create client: %v" , err )
461
462
}
462
463
464
+ mu := sync.Mutex {}
463
465
notificationNum := 0
464
466
var messageNotification * mcp.JSONRPCNotification
465
467
progressNotifications := make ([]* mcp.JSONRPCNotification , 0 )
466
468
client .OnNotification (func (notification mcp.JSONRPCNotification ) {
469
+ mu .Lock ()
467
470
if notification .Method == string (mcp .MethodNotificationMessage ) {
468
471
messageNotification = & notification
469
472
} else if notification .Method == string (mcp .MethodNotificationProgress ) {
470
473
progressNotifications = append (progressNotifications , & notification )
471
474
}
472
475
notificationNum += 1
476
+ mu .Unlock ()
473
477
})
474
478
defer client .Close ()
475
479
@@ -516,6 +520,7 @@ func TestSSEMCPClient(t *testing.T) {
516
520
517
521
time .Sleep (time .Millisecond * 200 )
518
522
523
+ mu .Lock ()
519
524
assert .Equal (t , notificationNum , 3 )
520
525
assert .NotNil (t , messageNotification )
521
526
assert .Equal (t , messageNotification .Method , string (mcp .MethodNotificationMessage ))
@@ -537,6 +542,7 @@ func TestSSEMCPClient(t *testing.T) {
537
542
assert .EqualValues (t , 100 , progressNotifications [1 ].Params .AdditionalFields ["progress" ])
538
543
assert .Equal (t , "progress_token" , progressNotifications [1 ].Params .AdditionalFields ["progressToken" ])
539
544
assert .EqualValues (t , 100 , progressNotifications [1 ].Params .AdditionalFields ["total" ])
545
+ mu .Lock ()
540
546
})
541
547
542
548
t .Run ("Ensure the server does not send notifications" , func (t * testing.T ) {
@@ -545,9 +551,12 @@ func TestSSEMCPClient(t *testing.T) {
545
551
t .Fatalf ("Failed to create client: %v" , err )
546
552
}
547
553
554
+ mu := sync.Mutex {}
548
555
notifications := make ([]* mcp.JSONRPCNotification , 0 )
549
556
client .OnNotification (func (notification mcp.JSONRPCNotification ) {
557
+ mu .Lock ()
550
558
notifications = append (notifications , & notification )
559
+ mu .Unlock ()
551
560
})
552
561
defer client .Close ()
553
562
@@ -585,7 +594,9 @@ func TestSSEMCPClient(t *testing.T) {
585
594
_ , _ = client .CallTool (ctx , request )
586
595
time .Sleep (time .Millisecond * 200 )
587
596
597
+ mu .Lock ()
588
598
assert .Len (t , notifications , 0 )
599
+ mu .Unlock ()
589
600
})
590
601
591
602
t .Run ("GetPrompt for testing log and progress notification" , func (t * testing.T ) {
@@ -594,17 +605,19 @@ func TestSSEMCPClient(t *testing.T) {
594
605
t .Fatalf ("Failed to create client: %v" , err )
595
606
}
596
607
608
+ mu := sync.Mutex {}
597
609
var messageNotification * mcp.JSONRPCNotification
598
610
progressNotifications := make ([]* mcp.JSONRPCNotification , 0 )
599
611
notificationNum := 0
600
612
client .OnNotification (func (notification mcp.JSONRPCNotification ) {
601
- println ( notification . Method )
613
+ mu . Lock ( )
602
614
if notification .Method == string (mcp .MethodNotificationMessage ) {
603
615
messageNotification = & notification
604
616
} else if notification .Method == string (mcp .MethodNotificationProgress ) {
605
617
progressNotifications = append (progressNotifications , & notification )
606
618
}
607
619
notificationNum += 1
620
+ mu .Unlock ()
608
621
})
609
622
defer client .Close ()
610
623
@@ -645,6 +658,7 @@ func TestSSEMCPClient(t *testing.T) {
645
658
if err != nil {
646
659
t .Fatalf ("GetPrompt failed: %v" , err )
647
660
}
661
+ mu .Lock ()
648
662
assert .NotNil (t , result )
649
663
assert .Len (t , result .Messages , 1 )
650
664
assert .Equal (t , result .Messages [0 ].Role , mcp .RoleAssistant )
@@ -675,6 +689,7 @@ func TestSSEMCPClient(t *testing.T) {
675
689
assert .EqualValues (t , 100 , progressNotifications [1 ].Params .AdditionalFields ["progress" ])
676
690
assert .Equal (t , "progress_token" , progressNotifications [1 ].Params .AdditionalFields ["progressToken" ])
677
691
assert .EqualValues (t , 100 , progressNotifications [1 ].Params .AdditionalFields ["total" ])
692
+ mu .Unlock ()
678
693
})
679
694
680
695
t .Run ("GetResource for testing log and progress notification" , func (t * testing.T ) {
@@ -683,17 +698,19 @@ func TestSSEMCPClient(t *testing.T) {
683
698
t .Fatalf ("Failed to create client: %v" , err )
684
699
}
685
700
701
+ mu := sync.Mutex {}
686
702
var messageNotification * mcp.JSONRPCNotification
687
703
progressNotifications := make ([]* mcp.JSONRPCNotification , 0 )
688
704
notificationNum := 0
689
705
client .OnNotification (func (notification mcp.JSONRPCNotification ) {
690
- println ( notification . Method )
706
+ mu . Lock ( )
691
707
if notification .Method == string (mcp .MethodNotificationMessage ) {
692
708
messageNotification = & notification
693
709
} else if notification .Method == string (mcp .MethodNotificationProgress ) {
694
710
progressNotifications = append (progressNotifications , & notification )
695
711
}
696
712
notificationNum += 1
713
+ mu .Unlock ()
697
714
})
698
715
defer client .Close ()
699
716
@@ -735,6 +752,7 @@ func TestSSEMCPClient(t *testing.T) {
735
752
t .Fatalf ("ReadResource failed: %v" , err )
736
753
}
737
754
755
+ mu .Lock ()
738
756
assert .NotNil (t , result )
739
757
assert .Len (t , result .Contents , 1 )
740
758
assert .Equal (t , result .Contents [0 ].(mcp.TextResourceContents ).URI , "resource://testresource" )
@@ -763,5 +781,6 @@ func TestSSEMCPClient(t *testing.T) {
763
781
assert .EqualValues (t , 100 , progressNotifications [1 ].Params .AdditionalFields ["progress" ])
764
782
assert .Equal (t , "progress_token" , progressNotifications [1 ].Params .AdditionalFields ["progressToken" ])
765
783
assert .EqualValues (t , 100 , progressNotifications [1 ].Params .AdditionalFields ["total" ])
784
+ mu .Unlock ()
766
785
})
767
786
}
0 commit comments