@@ -17,6 +17,7 @@ import (
1717 "time"
1818
1919 "go.mongodb.org/mongo-driver/v2/bson"
20+ "go.mongodb.org/mongo-driver/v2/event"
2021 "go.mongodb.org/mongo-driver/v2/internal/assert"
2122 "go.mongodb.org/mongo-driver/v2/internal/integration/mtest"
2223 "go.mongodb.org/mongo-driver/v2/internal/mongoutil"
@@ -597,7 +598,40 @@ func TestSessionsProse(t *testing.T) {
597598 */
598599
599600 mt .Run ("ping test" , func (mt * mtest.T ) {
600- err := mt .Client .Ping (context .Background (), readpref .Primary ())
601+ serverMonitor := & event.ServerMonitor {
602+ ServerHeartbeatStarted : func (e * event.ServerHeartbeatStartedEvent ) {
603+ fmt .Println ("Server heartbeat started:" , e .ConnectionID )
604+ },
605+ ServerHeartbeatSucceeded : func (e * event.ServerHeartbeatSucceededEvent ) {
606+ fmt .Println ("Server heartbeat succeeded:" , e .ConnectionID , e .Duration , e .Reply )
607+ },
608+ }
609+
610+ commandMonitor := & event.CommandMonitor {
611+ Started : func (_ context.Context , cse * event.CommandStartedEvent ) {
612+ fmt .Println ("Command started:" , cse .CommandName , cse .Command )
613+ },
614+ Succeeded : func (_ context.Context , cse * event.CommandSucceededEvent ) {
615+ fmt .Println ("Command succeeded:" , cse .CommandName , cse .Reply )
616+ },
617+ }
618+
619+ opts := options .Client ().
620+ ApplyURI (mtest .ClusterURI ()).
621+ // SetHosts([]string{mtest.ClusterConnString().Hosts[0]}).
622+ // SetDirect(true).
623+ SetHeartbeatInterval (500 * time .Millisecond ). // Minimum interval
624+ SetServerMonitor (serverMonitor ).
625+ SetMonitor (commandMonitor )
626+
627+ client , err := mongo .Connect (opts )
628+ require .NoError (mt , err , "expected no error connecting to client, got: %v" , err )
629+ defer func () {
630+ err = client .Disconnect (context .Background ())
631+ require .NoError (mt , err , "expected no error disconnecting client, got: %v" , err )
632+ }()
633+
634+ err = client .Ping (context .Background (), readpref .Primary ())
601635 require .NoError (mt , err , "expected no error, got: %v" , err )
602636 })
603637}
0 commit comments