@@ -50,7 +50,7 @@ func (r *mockResolver) LookupSRV(service, proto, name string) (string, []*net.SR
50
50
if r .lookupTimeout {
51
51
return "" , nil , & net.DNSError {IsTimeout : true }
52
52
}
53
- str , addresses , err := net .LookupSRV ("mongodb" , "tcp" , name )
53
+ str , addresses , err := net .LookupSRV (service , proto , name )
54
54
if err != nil {
55
55
return str , addresses , err
56
56
}
@@ -153,12 +153,6 @@ func TestPollingSRVRecordsSpec(t *testing.T) {
153
153
154
154
require .True (t , tt .heartbeatTime == topo .pollHeartbeatTime .Load ().(bool ), "Not polling on correct intervals" )
155
155
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
- }
162
156
_ = topo .Disconnect (context .Background ())
163
157
})
164
158
}
@@ -204,12 +198,6 @@ func TestPollSRVRecords(t *testing.T) {
204
198
actualHosts := topo .Description ().Servers
205
199
expectedHosts := []string {"localhost.test.build.10gen.cc:27017" , "localhost.test.build.10gen.cc:27018" }
206
200
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
- }
213
201
_ = topo .Disconnect (context .Background ())
214
202
215
203
})
@@ -238,12 +226,6 @@ func TestPollSRVRecords(t *testing.T) {
238
226
require .False (t , topo .pollHeartbeatTime .Load ().(bool ))
239
227
expectedHosts := []string {"localhost.test.build.10gen.cc:27017" , "localhost.test.build.10gen.cc:27018" , "localhost.test.build.10gen.cc:27020" }
240
228
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
- }
247
229
_ = topo .Disconnect (context .Background ())
248
230
249
231
})
@@ -273,12 +255,6 @@ func TestPollSRVRecords(t *testing.T) {
273
255
require .False (t , topo .pollHeartbeatTime .Load ().(bool ))
274
256
expectedHosts := []string {"localhost.test.build.10gen.cc:27017" , "localhost.test.build.10gen.cc:27018" }
275
257
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
- }
282
258
_ = topo .Disconnect (context .Background ())
283
259
})
284
260
}
@@ -380,12 +356,6 @@ func TestPollSRVRecordsMaxHosts(t *testing.T) {
380
356
"localhost.test.build.10gen.cc:27020" ,
381
357
}
382
358
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
- }
389
359
})
390
360
t .Run ("SRVMaxHosts is number of hosts" , func (t * testing.T ) {
391
361
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) {
399
369
"localhost.test.build.10gen.cc:27020" ,
400
370
}
401
371
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
- }
408
372
})
409
373
t .Run ("SRVMaxHosts is less than number of hosts" , func (t * testing.T ) {
410
374
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) {
421
385
assert .True (t , ok , "topology server list did not contain expected host %v" , expectedHost )
422
386
})
423
387
}
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