@@ -542,20 +542,30 @@ func TestSessionsProse(t *testing.T) {
542542 },
543543 }
544544
545+ var pingStartedCommands []bson.Raw
546+ var pingSucceededCommands []bson.Raw
547+ commandMonitor := & event.CommandMonitor {
548+ Started : func (_ context.Context , cse * event.CommandStartedEvent ) {
549+ fmt .Println ("Command started:" , cse .CommandName , cse .Command )
550+ if cse .CommandName == "ping" {
551+ pingStartedCommands = append (pingStartedCommands , cse .Command )
552+ }
553+ },
554+ Succeeded : func (_ context.Context , cse * event.CommandSucceededEvent ) {
555+ fmt .Println ("Command succeeded:" , cse .CommandName , cse .Reply )
556+ if cse .CommandName == "ping" {
557+ pingSucceededCommands = append (pingSucceededCommands , cse .Reply )
558+ }
559+ },
560+ }
561+
545562 opts := options .Client ().
546563 ApplyURI (mtest .ClusterURI ()).
547564 SetHosts ([]string {mtest .ClusterConnString ().Hosts [0 ]}).
548565 SetDirect (true ).
549566 SetHeartbeatInterval (500 * time .Millisecond ). // Minimum interval
550- SetMonitor (& event.CommandMonitor {
551- Started : func (_ context.Context , cse * event.CommandStartedEvent ) {
552- fmt .Println ("Command started:" , cse .CommandName , cse .Command )
553- },
554- Succeeded : func (_ context.Context , cse * event.CommandSucceededEvent ) {
555- fmt .Println ("Command succeeded:" , cse .CommandName , cse .Reply )
556- },
557- }).
558- SetServerMonitor (serverMonitor )
567+ SetServerMonitor (serverMonitor ).
568+ SetMonitor (commandMonitor )
559569
560570 client , err := mongo .Connect (opts )
561571 require .NoError (mt , err , "expected no error connecting to client, got: %v" , err )
@@ -564,12 +574,8 @@ func TestSessionsProse(t *testing.T) {
564574 require .NoError (mt , err , "expected no error disconnecting client, got: %v" , err )
565575 }()
566576
567- res , err := client .Database ("admin" ).RunCommand (context .Background (), bson.D {
568- {"ping" , 1 },
569- }).Raw ()
577+ err = client .Ping (context .Background (), readpref .Primary ())
570578 require .NoError (mt , err , "expected no error, got: %v" , err )
571- replyClusterTime , err := res .LookupErr ("$clusterTime" )
572- require .NoError (mt , err , "$clusterTime not found in response" )
573579
574580 _ , err = mt .Client .Database ("test" ).Collection ("test" ).InsertOne (context .Background (), bson.D {{"advance" , "$clusterTime" }})
575581 require .NoError (mt , err , "expected no error inserting document, got: %v" , err )
@@ -578,13 +584,17 @@ func TestSessionsProse(t *testing.T) {
578584 <- heartbeatStarted
579585 <- heartbeatSucceeded
580586
581- res , err = client .Database ("admin" ).RunCommand (context .Background (), bson.D {
582- {"ping" , 1 },
583- }).Raw ()
587+ err = client .Ping (context .Background (), readpref .Primary ())
584588 require .NoError (mt , err , "expected no error, got: %v" , err )
585- replyClusterTimeNew , err := res .LookupErr ("$clusterTime" )
589+
590+ require .Len (mt , pingStartedCommands , 2 , "expected 2 pings started, got: %v" , len (pingStartedCommands ))
591+ require .Len (mt , pingSucceededCommands , 2 , "expected 2 pings succeeded, got: %v" , len (pingSucceededCommands ))
592+ initialClusterTime , err := pingStartedCommands [0 ].LookupErr ("$clusterTime" )
586593 require .NoError (mt , err , "$clusterTime not found in response" )
587- mt .Fatalf ("$clusterTime: %v %v" , replyClusterTime , replyClusterTimeNew )
594+ currentClusterTime , err := pingStartedCommands [1 ].LookupErr ("$clusterTime" )
595+ require .NoError (mt , err , "$clusterTime not found in command" )
596+ assert .Equal (mt , initialClusterTime , currentClusterTime , "expected same cluster time, got %v and %v" , initialClusterTime , currentClusterTime )
597+ mt .Fatalf ("$clusterTime: %v %v" , initialClusterTime , currentClusterTime )
588598 })
589599}
590600
0 commit comments