Skip to content

Commit d94b4db

Browse files
author
Divjot Arora
committed
allow server monitoring to be disabled for tests
1 parent 2410df0 commit d94b4db

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

x/mongo/driver/topology/rtt_monitor.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,15 @@ func (r *rttMonitor) disconnect() {
5959
conn = r.conn
6060
r.Unlock()
6161

62-
if conn == nil {
63-
return
62+
if conn != nil {
63+
// If the connection exists, we need to wait for it to be connected. We can ignore the error from conn.wait().
64+
// If the connection wasn't successfully opened, its state was set back to disconnected, so calling conn.close()
65+
// will be a noop.
66+
conn.closeConnectContext()
67+
_ = conn.wait()
68+
_ = conn.close()
6469
}
6570

66-
// If the connection exists, we need to wait for it to be connected. We can ignore the error from conn.wait(). If
67-
// the connection wasn't successfully opened, its state was set back to disconnected, so calling conn.close() will
68-
// be a noop.
69-
conn.closeConnectContext()
70-
_ = conn.wait()
71-
_ = conn.close()
72-
7371
r.closeWg.Wait()
7472
}
7573

x/mongo/driver/topology/sdam_spec_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,21 @@ func setUpTopology(t *testing.T, uri string) *Topology {
137137
cs, err := connstring.ParseAndValidate(uri)
138138
assert.Nil(t, err, "Parse error: %v", err)
139139

140-
topo, err := New(WithConnString(func(connstring.ConnString) connstring.ConnString {
141-
return cs
142-
}))
140+
// Disable server monitoring because the hosts in the SDAM spec tests don't actually exist, so the server monitor
141+
// can race with the test and mark the server Unknown when it fails to connect, which causes tests to fail.
142+
serverOpts := []ServerOption{
143+
withMonitoringDisabled(func(bool) bool {
144+
return true
145+
}),
146+
}
147+
topo, err := New(
148+
WithConnString(func(connstring.ConnString) connstring.ConnString {
149+
return cs
150+
}),
151+
WithServerOptions(func(opts ...ServerOption) []ServerOption {
152+
return append(opts, serverOpts...)
153+
}),
154+
)
143155
assert.Nil(t, err, "topology.New error: %v", err)
144156

145157
// add servers to topology without starting heartbeat goroutines

x/mongo/driver/topology/server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,12 @@ func (s *Server) Connect(updateCallback updateTopologyCallback) error {
191191
}
192192
s.desc.Store(description.NewDefaultServer(s.address))
193193
s.updateTopologyCallback.Store(updateCallback)
194-
s.rttMonitor.connect()
195-
s.closewg.Add(1)
196-
go s.update()
194+
195+
if !s.cfg.monitoringDisabled {
196+
s.rttMonitor.connect()
197+
s.closewg.Add(1)
198+
go s.update()
199+
}
197200
return s.pool.connect()
198201
}
199202

x/mongo/driver/topology/server_options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type serverConfig struct {
2929
poolMonitor *event.PoolMonitor
3030
connectionPoolMaxIdleTime time.Duration
3131
registry *bsoncodec.Registry
32+
monitoringDisabled bool
3233
}
3334

3435
func newServerConfig(opts ...ServerOption) (*serverConfig, error) {
@@ -52,6 +53,13 @@ func newServerConfig(opts ...ServerOption) (*serverConfig, error) {
5253
// ServerOption configures a server.
5354
type ServerOption func(*serverConfig) error
5455

56+
func withMonitoringDisabled(fn func(bool) bool) ServerOption {
57+
return func(cfg *serverConfig) error {
58+
cfg.monitoringDisabled = fn(cfg.monitoringDisabled)
59+
return nil
60+
}
61+
}
62+
5563
// WithConnectionOptions configures the server's connections.
5664
func WithConnectionOptions(fn func(...ConnectionOption) []ConnectionOption) ServerOption {
5765
return func(cfg *serverConfig) error {

0 commit comments

Comments
 (0)