Skip to content

Commit ad5fd8a

Browse files
authored
GODRIVER-2188 Add prose test for SRV polling with a custom service name (#782)
1 parent e9c269e commit ad5fd8a

File tree

1 file changed

+51
-37
lines changed

1 file changed

+51
-37
lines changed

x/mongo/driver/topology/polling_srv_records_test.go

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (r *mockResolver) LookupSRV(service, proto, name string) (string, []*net.SR
5050
if r.lookupTimeout {
5151
return "", nil, &net.DNSError{IsTimeout: true}
5252
}
53-
str, addresses, err := net.LookupSRV("mongodb", "tcp", name)
53+
str, addresses, err := net.LookupSRV(service, proto, name)
5454
if err != nil {
5555
return str, addresses, err
5656
}
@@ -153,12 +153,6 @@ func TestPollingSRVRecordsSpec(t *testing.T) {
153153

154154
require.True(t, tt.heartbeatTime == topo.pollHeartbeatTime.Load().(bool), "Not polling on correct intervals")
155155
compareHosts(t, desc.Servers, tt.expectedHosts)
156-
for _, e := range tt.expectedHosts {
157-
addr := address.Address(e).Canonicalize()
158-
if _, ok := topo.servers[addr]; !ok {
159-
t.Errorf("Topology server list did not contain expected value %v", e)
160-
}
161-
}
162156
_ = topo.Disconnect(context.Background())
163157
})
164158
}
@@ -204,12 +198,6 @@ func TestPollSRVRecords(t *testing.T) {
204198
actualHosts := topo.Description().Servers
205199
expectedHosts := []string{"localhost.test.build.10gen.cc:27017", "localhost.test.build.10gen.cc:27018"}
206200
compareHosts(t, actualHosts, expectedHosts)
207-
for _, e := range expectedHosts {
208-
addr := address.Address(e).Canonicalize()
209-
if _, ok := topo.servers[addr]; !ok {
210-
t.Errorf("Topology server list did not contain expected value %v", e)
211-
}
212-
}
213201
_ = topo.Disconnect(context.Background())
214202

215203
})
@@ -238,12 +226,6 @@ func TestPollSRVRecords(t *testing.T) {
238226
require.False(t, topo.pollHeartbeatTime.Load().(bool))
239227
expectedHosts := []string{"localhost.test.build.10gen.cc:27017", "localhost.test.build.10gen.cc:27018", "localhost.test.build.10gen.cc:27020"}
240228
compareHosts(t, desc.Servers, expectedHosts)
241-
for _, e := range expectedHosts {
242-
addr := address.Address(e).Canonicalize()
243-
if _, ok := topo.servers[addr]; !ok {
244-
t.Errorf("Topology server list did not contain expected value %v", e)
245-
}
246-
}
247229
_ = topo.Disconnect(context.Background())
248230

249231
})
@@ -273,12 +255,6 @@ func TestPollSRVRecords(t *testing.T) {
273255
require.False(t, topo.pollHeartbeatTime.Load().(bool))
274256
expectedHosts := []string{"localhost.test.build.10gen.cc:27017", "localhost.test.build.10gen.cc:27018"}
275257
compareHosts(t, desc.Servers, expectedHosts)
276-
for _, e := range expectedHosts {
277-
addr := address.Address(e).Canonicalize()
278-
if _, ok := topo.servers[addr]; !ok {
279-
t.Errorf("Topology server list did not contain expected value %v", e)
280-
}
281-
}
282258
_ = topo.Disconnect(context.Background())
283259
})
284260
}
@@ -380,12 +356,6 @@ func TestPollSRVRecordsMaxHosts(t *testing.T) {
380356
"localhost.test.build.10gen.cc:27020",
381357
}
382358
compareHosts(t, actualHosts, expectedHosts)
383-
384-
for _, e := range expectedHosts {
385-
addr := address.Address(e).Canonicalize()
386-
_, ok := topo.servers[addr]
387-
assert.True(t, ok, "topology server list did not contain expected host %v", e)
388-
}
389359
})
390360
t.Run("SRVMaxHosts is number of hosts", func(t *testing.T) {
391361
recordsToAdd := []*net.SRV{{"localhost.test.build.10gen.cc.", 27019, 0, 0}, {"localhost.test.build.10gen.cc.", 27020, 0, 0}}
@@ -399,12 +369,6 @@ func TestPollSRVRecordsMaxHosts(t *testing.T) {
399369
"localhost.test.build.10gen.cc:27020",
400370
}
401371
compareHosts(t, actualHosts, expectedHosts)
402-
403-
for _, e := range expectedHosts {
404-
addr := address.Address(e).Canonicalize()
405-
_, ok := topo.servers[addr]
406-
assert.True(t, ok, "topology server list did not contain expected host %v", e)
407-
}
408372
})
409373
t.Run("SRVMaxHosts is less than number of hosts", func(t *testing.T) {
410374
recordsToAdd := []*net.SRV{{"localhost.test.build.10gen.cc.", 27019, 0, 0}, {"localhost.test.build.10gen.cc.", 27020, 0, 0}}
@@ -421,3 +385,53 @@ func TestPollSRVRecordsMaxHosts(t *testing.T) {
421385
assert.True(t, ok, "topology server list did not contain expected host %v", expectedHost)
422386
})
423387
}
388+
389+
func TestPollSRVRecordsServiceName(t *testing.T) {
390+
if testing.Short() {
391+
t.Skip("skipping integration test in short mode")
392+
}
393+
394+
// simulateSRVPoll creates a topology with srvServiceName, mocks the DNS changes described by
395+
// recordsToAdd and recordsToRemove, and returns the topology.
396+
simulateSRVPoll := func(srvServiceName string, recordsToAdd []*net.SRV, recordsToRemove []*net.SRV) (*Topology, func(ctx context.Context) error) {
397+
t.Helper()
398+
399+
cs, err := connstring.ParseAndValidate("mongodb+srv://test22.test.build.10gen.cc/?heartbeatFrequencyMS=100&srvServiceName=customname")
400+
assert.Nil(t, err, "ParseAndValidate error: %v", err)
401+
topo, err := New(
402+
WithConnString(func(connstring.ConnString) connstring.ConnString { return cs }),
403+
WithURI(func(string) string { return cs.Original }),
404+
WithSRVServiceName(func(string) string { return srvServiceName }),
405+
)
406+
assert.Nil(t, err, "error during topology creation: %v", err)
407+
408+
mockRes := newMockResolver(recordsToAdd, recordsToRemove, false, false)
409+
topo.dnsResolver = &dns.Resolver{mockRes.LookupSRV, mockRes.LookupTXT}
410+
topo.rescanSRVInterval = time.Millisecond * 5
411+
err = topo.Connect()
412+
assert.Nil(t, err, "Connect error: %v", err)
413+
414+
// Wait for description to update.
415+
sub, err := topo.Subscribe()
416+
assert.Nil(t, err, "Subscribe error: %v", err)
417+
for atomic.LoadInt32(&mockRes.ranLookup) < 2 {
418+
<-sub.Updates
419+
}
420+
421+
return topo, topo.Disconnect
422+
}
423+
424+
t.Run("SRVServiceName is customname", func(t *testing.T) {
425+
recordsToAdd := []*net.SRV{{"localhost.test.build.10gen.cc.", 27019, 0, 0}, {"localhost.test.build.10gen.cc.", 27020, 0, 0}}
426+
recordsToRemove := []*net.SRV{{"localhost.test.build.10gen.cc.", 27017, 0, 0}, {"localhost.test.build.10gen.cc.", 27018, 0, 0}}
427+
topo, disconnect := simulateSRVPoll("customname", recordsToAdd, recordsToRemove)
428+
defer func() { _ = disconnect(context.Background()) }()
429+
430+
actualHosts := topo.Description().Servers
431+
expectedHosts := []string{
432+
"localhost.test.build.10gen.cc:27019",
433+
"localhost.test.build.10gen.cc:27020",
434+
}
435+
compareHosts(t, actualHosts, expectedHosts)
436+
})
437+
}

0 commit comments

Comments
 (0)