@@ -542,20 +542,30 @@ func TestSessionsProse(t *testing.T) {
542
542
},
543
543
}
544
544
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
+
545
562
opts := options .Client ().
546
563
ApplyURI (mtest .ClusterURI ()).
547
564
SetHosts ([]string {mtest .ClusterConnString ().Hosts [0 ]}).
548
565
SetDirect (true ).
549
566
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 )
559
569
560
570
client , err := mongo .Connect (opts )
561
571
require .NoError (mt , err , "expected no error connecting to client, got: %v" , err )
@@ -564,12 +574,8 @@ func TestSessionsProse(t *testing.T) {
564
574
require .NoError (mt , err , "expected no error disconnecting client, got: %v" , err )
565
575
}()
566
576
567
- res , err := client .Database ("admin" ).RunCommand (context .Background (), bson.D {
568
- {"ping" , 1 },
569
- }).Raw ()
577
+ err = client .Ping (context .Background (), readpref .Primary ())
570
578
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" )
573
579
574
580
_ , err = mt .Client .Database ("test" ).Collection ("test" ).InsertOne (context .Background (), bson.D {{"advance" , "$clusterTime" }})
575
581
require .NoError (mt , err , "expected no error inserting document, got: %v" , err )
@@ -578,13 +584,17 @@ func TestSessionsProse(t *testing.T) {
578
584
<- heartbeatStarted
579
585
<- heartbeatSucceeded
580
586
581
- res , err = client .Database ("admin" ).RunCommand (context .Background (), bson.D {
582
- {"ping" , 1 },
583
- }).Raw ()
587
+ err = client .Ping (context .Background (), readpref .Primary ())
584
588
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" )
586
593
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 )
588
598
})
589
599
}
590
600
0 commit comments