Skip to content

Commit c50d9f9

Browse files
CSHARP-2642: Add srv connection strings to atlas connectivity tests. Fix the issue with initial host resolving for replica set.
1 parent d7b7031 commit c50d9f9

File tree

6 files changed

+23
-49
lines changed

6 files changed

+23
-49
lines changed

evergreen/evergreen.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ functions:
225225
working_dir: mongo-csharp-driver
226226
script: |
227227
# DO NOT ECHO WITH XTRACE (which PREPARE_SHELL does)
228-
ATLAS_FREE="${ATLAS_FREE}" ATLAS_REPLICA="${ATLAS_REPLICA}" ATLAS_SHARDED="${ATLAS_SHARDED}" ATLAS_TLS11="${ATLAS_TLS11}" ATLAS_TLS12="${ATLAS_TLS12}" evergreen/run-atlas-connectivity-tests.sh
228+
ATLAS_FREE="${ATLAS_FREE}" ATLAS_FREE_SRV="${ATLAS_FREE_SRV}" ATLAS_REPLICA="${ATLAS_REPLICA}" ATLAS_REPLICA_SRV="${ATLAS_REPLICA_SRV}" ATLAS_SHARDED="${ATLAS_SHARDED}" ATLAS_SHARDED_SRV="${ATLAS_SHARDED_SRV}" ATLAS_TLS11="${ATLAS_TLS11}" ATLAS_TLS11_SRV="${ATLAS_TLS11_SRV}" ATLAS_TLS12="${ATLAS_TLS12}" ATLAS_TLS12_SRV="${ATLAS_TLS12_SRV}" evergreen/run-atlas-connectivity-tests.sh
229229
230230
run-plain-auth-tests:
231231
- command: shell.exec

src/MongoDB.Driver.Core/Core/Clusters/DnsMonitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private void Monitor()
161161
{
162162
while (true)
163163
{
164-
if (_cluster.ShouldDnsMonitorStop())
164+
if (_processDnsResultHasEverBeenCalled && _cluster.ShouldDnsMonitorStop())
165165
{
166166
return;
167167
}

src/MongoDB.Driver.Core/Core/Clusters/MultiServerCluster.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,17 +536,15 @@ void IDnsMonitoringCluster.ProcessDnsResults(List<DnsEndPoint> dnsEndPoints)
536536
return;
537537
}
538538

539+
// Assuming that before this method one from the following conditions has been validated:
540+
// 1. The cluster type is Unknown or Sharded.
541+
// 2. This method has been called the first time.
542+
// Otherwise, the below code should not be called.
539543
var newServers = new List<IClusterableServer>();
540544
lock (_updateClusterDescriptionLock)
541545
{
542546
var oldClusterDescription = Description;
543547

544-
var clusterType = oldClusterDescription.Type;
545-
if (clusterType != ClusterType.Unknown && clusterType != ClusterType.Sharded)
546-
{
547-
return;
548-
}
549-
550548
var newClusterDescription = oldClusterDescription;
551549
var currentEndPoints = oldClusterDescription.Servers.Select(serverDescription => serverDescription.EndPoint).ToList();
552550

tests/AtlasConnectivity.Tests/ConnectivityTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
using MongoDB.Bson;
1919
using MongoDB.Driver;
2020
using MongoDB.Driver.TestHelpers;
21-
using MongoDB.Driver.Tests;
2221
using Xunit;
2322

2423
namespace AtlasConnectivity.Tests
@@ -27,10 +26,15 @@ public class ConnectivityTests
2726
{
2827
[Theory]
2928
[InlineData("ATLAS_FREE")]
29+
[InlineData("ATLAS_FREE_SRV")]
3030
[InlineData("ATLAS_REPLICA")]
31+
[InlineData("ATLAS_REPLICA_SRV")]
3132
[InlineData("ATLAS_SHARDED")]
33+
[InlineData("ATLAS_SHARDED_SRV")]
3234
[InlineData("ATLAS_TLS11")]
35+
[InlineData("ATLAS_TLS11_SRV")]
3336
[InlineData("ATLAS_TLS12")]
37+
[InlineData("ATLAS_TLS12_SRV")]
3438
public void Connection_to_Atlas_should_work(string environmentVariableName)
3539
{
3640
var connectionString = Environment.GetEnvironmentVariable(environmentVariableName);

tests/MongoDB.Driver.Core.Tests/Core/Clusters/DnsMonitorTests.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,19 +331,23 @@ public void IsValidHost_should_return_expected_result(string lookupDomainName, s
331331
result.Should().Be(expectedResult);
332332
}
333333

334-
[Theory]
335-
[InlineData(true, false)]
336-
[InlineData(false, true)]
337-
public void Monitor_should_return_when_ShouldDnsMonitorStop_returns_true(bool firstValue, bool secondValue)
334+
[Fact]
335+
public void Monitor_should_return_when_ShouldDnsMonitorStop_returns_true()
338336
{
337+
var mockDnsResolver = new Mock<IDnsResolver>();
338+
var srvRecords = CreateSrvRecords(new []{ "oneserver.test.com" });
339+
mockDnsResolver
340+
.Setup(m => m.ResolveSrvRecords(It.IsAny<string>(), It.IsAny<CancellationToken>()))
341+
.Returns(srvRecords);
339342
var mockCluster = new Mock<IDnsMonitoringCluster>();
340343
mockCluster
341-
.SetupSequence(m => m.ShouldDnsMonitorStop())
342-
.Returns(firstValue)
343-
.Returns(secondValue);
344-
var subject = CreateSubject(cluster: mockCluster.Object);
344+
.Setup(m => m.ShouldDnsMonitorStop())
345+
.Returns(true);
346+
var subject = CreateSubject(cluster: mockCluster.Object, dnsResolver: mockDnsResolver.Object);
345347

346348
subject.Monitor();
349+
mockDnsResolver.Verify(c => c.ResolveSrvRecords(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Once);
350+
mockCluster.Verify(c => c.ShouldDnsMonitorStop(), Times.Once);
347351
}
348352

349353
[Fact]
@@ -354,7 +358,6 @@ public void Monitor_should_call_ProcessDnsException_when_an_exception_is_thrown_
354358
var exception = new Exception();
355359
mockCluster
356360
.SetupSequence(m => m.ShouldDnsMonitorStop())
357-
.Returns(false)
358361
.Returns(true);
359362
mockDnsResolver
360363
.Setup(m => m.ResolveSrvRecords(It.IsAny<string>(), It.IsAny<CancellationToken>()))
@@ -374,7 +377,6 @@ public void Monitor_should_call_ProcessDnsResults_with_expected_endPoints(string
374377
var mockCluster = new Mock<IDnsMonitoringCluster>();
375378
mockCluster
376379
.SetupSequence(x => x.ShouldDnsMonitorStop())
377-
.Returns(false)
378380
.Returns(true);
379381
List<DnsEndPoint> actualEndPoints = null;
380382
var cts = new CancellationTokenSource();
@@ -405,7 +407,6 @@ public void Monitor_should_not_call_ProcessDnsResults_when_there_are_no_valid_ho
405407
var mockCluster = new Mock<IDnsMonitoringCluster>();
406408
mockCluster
407409
.SetupSequence(x => x.ShouldDnsMonitorStop())
408-
.Returns(false)
409410
.Returns(true);
410411
var cts = new CancellationTokenSource();
411412
var mockDnsResolver = new Mock<IDnsResolver>();
@@ -442,11 +443,9 @@ public void Monitor_should_throw_when_cancellation_is_requested()
442443
var mockCluster = new Mock<IDnsMonitoringCluster>();
443444
mockCluster
444445
.SetupSequence(x => x.ShouldDnsMonitorStop())
445-
.Returns(false)
446446
.Returns(() => { cts.Cancel(); return false; });
447447
var lookupDomainName = "a.b.com";
448448
var subject = CreateSubject(cluster: mockCluster.Object, lookupDomainName: lookupDomainName, cancellationToken: cts.Token);
449-
450449
var exception = Record.Exception(() => subject.Monitor());
451450

452451
exception.Should().BeOfType<OperationCanceledException>();

tests/MongoDB.Driver.Core.Tests/Core/Clusters/MultiServerClusterTests.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using System.Threading;
2121
using FluentAssertions;
2222
using MongoDB.Bson.TestHelpers;
23-
using MongoDB.Driver.Core.Clusters;
2423
using MongoDB.Driver.Core.Configuration;
2524
using MongoDB.Driver.Core.Events;
2625
using MongoDB.Driver.Core.Misc;
@@ -29,8 +28,6 @@
2928
using Moq;
3029
using Xunit;
3130
using MongoDB.Bson;
32-
using System.Reflection;
33-
using MongoDB.Driver.Core.Async;
3431
using MongoDB.Driver.Core.Tests.Core.Clusters;
3532
using System.Threading.Tasks;
3633

@@ -451,30 +448,6 @@ public void ProcessDnsResults_should_ignore_empty_end_points_list()
451448
}
452449
}
453450

454-
[Theory]
455-
[InlineData(ServerType.Standalone, ServerType.Standalone, ClusterType.Standalone)]
456-
[InlineData(ServerType.ReplicaSetPrimary, ServerType.ReplicaSetSecondary, ClusterType.ReplicaSet)]
457-
public void ProcessDnsResults_should_do_nothing_if_cluster_type_is_not_unknown_or_sharded(ServerType serverType1, ServerType serverType2, ClusterType clusterType)
458-
{
459-
var settings = new ClusterSettings(scheme: ConnectionStringScheme.MongoDBPlusSrv, endPoints: new[] { new DnsEndPoint("a.b.com", 53) });
460-
var mockDnsMonitorFactory = CreateMockDnsMonitorFactory();
461-
using (var subject = CreateSubject(settings: settings, dnsMonitorFactory: mockDnsMonitorFactory.Object))
462-
{
463-
subject.Initialize();
464-
PublishDnsResults(subject, _firstEndPoint);
465-
PublishDescription(subject, _firstEndPoint, serverType1);
466-
subject.Description.Type.Should().Be(clusterType);
467-
var expectedEndPoints = clusterType == ClusterType.Standalone ? new[] { _firstEndPoint } : new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint };
468-
subject.Description.Servers.Select(s => s.EndPoint).Should().Equal(expectedEndPoints);
469-
var originalDescription = subject.Description;
470-
var endPointThatShouldBeIgnored = new DnsEndPoint("localhost", 27020);
471-
472-
PublishDnsResults(subject, endPointThatShouldBeIgnored);
473-
474-
subject.Description.Should().BeSameAs(originalDescription);
475-
}
476-
}
477-
478451
[Fact]
479452
public void ProcessDnsResults_should_add_missing_servers()
480453
{

0 commit comments

Comments
 (0)