@@ -17,7 +17,6 @@ import (
1717 "time"
1818
1919 "go.mongodb.org/mongo-driver/v2/bson"
20- "go.mongodb.org/mongo-driver/v2/event"
2120 "go.mongodb.org/mongo-driver/v2/internal/assert"
2221 "go.mongodb.org/mongo-driver/v2/internal/integration/mtest"
2322 "go.mongodb.org/mongo-driver/v2/internal/mongoutil"
@@ -597,42 +596,20 @@ func TestSessionsProse(t *testing.T) {
597596 })
598597 */
599598
600- mt .Run ("ping test" , func (mt * mtest.T ) {
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 ())
635- require .NoError (mt , err , "expected no error, got: %v" , err )
599+ firstServerAddr := mtest .GlobalTopology ().Description ().Servers [0 ].Addr
600+ directConnectionOpts := options .Client ().
601+ ApplyURI (fmt .Sprintf ("mongodb://%s" , firstServerAddr )).
602+ SetHeartbeatInterval (500 * time .Millisecond ). // Minimum interval
603+ SetReadPreference (readpref .Primary ()).
604+ SetDirect (true )
605+ pingOpts := mtest .NewOptions ().
606+ ClientOptions (directConnectionOpts ).
607+ CreateCollection (false ).
608+ Topologies (mtest .ReplicaSet ) // Read preference isn't sent to standalones so we can test on replica sets.
609+ mt .RunOpts ("ping test" , pingOpts , func (mt * mtest.T ) {
610+ err := mt .Client .Ping (context .Background (), readpref .Primary ())
611+ assert .NoError (mt , err , "expected no error, got: %v" , err )
612+ mt .Fatal (mtest .ClusterURI (), mtest .ClusterConnString ().Hosts , mtest .GlobalTopology ().Description ().Servers )
636613 })
637614}
638615
0 commit comments