Skip to content

Commit

Permalink
CSHARP-4870: CreateCluster throws NullReferenceException when not set…
Browse files Browse the repository at this point in the history
…ting LoggingSettings (#1236)
  • Loading branch information
BorisDog authored Dec 15, 2023
1 parent 136ee95 commit f41c21e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/MongoDB.Driver.Core/Core/Clusters/ClusterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ private LoadBalancedCluster CreateLoadBalancedCluster(ClusterSettings setting)

private void ProcessClusterEnvironment(ClusterSettings settings)
{
if (_loggerFactory == null)
{
return;
}

foreach (var (host, _) in settings.EndPoints.Select(EndPointHelper.GetHostAndPort))
{
if (LogIfCosmosDB(host) || LogIfDocumentDB(host))
Expand All @@ -129,7 +134,7 @@ bool LogIfExternalEnvironment(string host, string environment, string documentat
if (suffixes.Any(s => host.EndsWith(s, StringComparison.InvariantCultureIgnoreCase)))
{
var logger = _loggerFactory.CreateLogger<LogCategories.Client>();
logger.LogInformation("You appear to be connected to a {environment} cluster. For more information regarding feature compatibility and support please visit {url}", environment, documentationUrl);
logger?.LogInformation("You appear to be connected to a {environment} cluster. For more information regarding feature compatibility and support please visit {url}", environment, documentationUrl);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public ClusterFactoryTests(ITestOutputHelper output) : base(output)
{
}

[Fact]
public void ClusterFactory_should_create_cluster_when_loggerfactory_is_not_set()
{
const string connectionString = "mongodb://a.MONGO.COSMOS.AZURE.COM:19555";
var subject = CreateSubject(connectionString, null);
var cluster = subject.CreateCluster();

cluster.Should().NotBeNull();
}

[Theory]
[InlineData("mongodb://a.MONGO.COSMOS.AZURE.COM:19555", ExpectedCosmosDBMessage)]
[InlineData("mongodb://a.MONGO.COSMOS.AZURE.COM:19555", ExpectedCosmosDBMessage)]
Expand All @@ -59,7 +69,7 @@ public ClusterFactoryTests(ITestOutputHelper output) : base(output)
[InlineData("mongodb://a.mongo.cosmos.azure.com:19554,b.docdb-elastic.amazonaws.com:27017,c.mongo.cosmos.azure.com:19555/", ExpectedCosmosDBMessage)]
public void ClusterFactory_should_log_if_external_environment_is_detected(string connectionString, string expectedMessage)
{
var subject = CreateSubject(connectionString);
var subject = CreateSubject(connectionString, LoggerFactory);
_ = subject.CreateCluster();

var logs = GetLogs();
Expand All @@ -77,22 +87,22 @@ public void ClusterFactory_should_log_if_external_environment_is_detected(string
[InlineData("mongodb+srv://a.docdb-elastic.amazonaws.com.tld/")]
public void ClusterFactory_should_not_log_if_no_external_environment_is_detected(string connectionString)
{
var subject = CreateSubject(connectionString);
var subject = CreateSubject(connectionString, LoggerFactory);
_ = subject.CreateCluster();

var logs = GetLogs();
logs.Length.Should().Be(0);
}

private ClusterFactory CreateSubject(string connectionString)
private ClusterFactory CreateSubject(string connectionString, ILoggerFactory loggerFactory)
{
var parsedConnectionString = new ConnectionString(connectionString);

var eventSubscriberMock = Mock.Of<IEventSubscriber>();
var serverFactoryMock = Mock.Of<IClusterableServerFactory>();

var clusterSettings = new ClusterSettings(endPoints: Optional.Enumerable(parsedConnectionString.Hosts));
var clusterFactory = new ClusterFactory(clusterSettings, serverFactoryMock, eventSubscriberMock, LoggerFactory);
var clusterFactory = new ClusterFactory(clusterSettings, serverFactoryMock, eventSubscriberMock, loggerFactory);

return clusterFactory;
}
Expand Down

0 comments on commit f41c21e

Please sign in to comment.