I am using a MongoDB 3.4.4 instance from official docker image in my staging environment machine. I use MongoDB.Driver NuGet package 2.5.0 for connect from code. It's works fine when I connect from deployed application (running in docker container from aspnetcore:2.0.3 base image). I have a some set of integration tests for my application. In application project and tests project connection with Mongo creation is same and as see below:
services.AddSingleton<IMongoClient>(s =>
{
var config = s.GetService<ISettings>();
var logger = s.GetService<ILogger<IMongoClient>>();
var settings = MongoClientSettings.FromUrl(new MongoUrl("mongodb://dev:pass@192.168.0.163/authDb?connect=direct"));
settings.MaxConnectionIdleTime = TimeSpan.FromSeconds(5);
settings.MaxConnectionPoolSize = 1000;
settings.MinConnectionPoolSize = 30;
if (config.ActiveLogMongoQueries)
{
settings.ClusterConfigurator = (cb) =>
{
cb.Subscribe<CommandStartedEvent>(
(e) =>
{
if (!IsServiceRequest(e.CommandName))
{
logger.LogInformation($"Mongo.Query DataBaseNameSpace: {e.DatabaseNamespace}. CommandName: {e.CommandName}. Command: {e.Command.ToString()}");
}
});
};
}
return new MongoClient(settings);
});
services.AddTransient(s =>
{
var config = s.GetService<ISettings>();
var dbName = "myDbName";
var mongoConnectionString = new MongoUrl(config.MongoConnectionString);
if (!string.IsNullOrEmpty(mongoConnectionString.DatabaseName))
{
dbName = mongoConnectionString.DatabaseName;
}
return s.GetService<IMongoClient>().GetDatabase(dbName);
});
This also works fine when I run integration tests from Visual studio IDE test runner. But when I try run test project automatic using gitlab CI, I have ConnectionTimeoutExeption:
System.AggregateException : One or more errors occurred. (A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Direct", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "192.168.0.163:27017" }", EndPoint: "192.168.0.163:27017", State: "Disconnected", Type: "Unknown" }] }.) (The following constructor parameters did not have matching fixture data: MongoDatabaseFixture fixture)
---- System.TimeoutException : A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Direct", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "192.168.0.163:27017" }", EndPoint: "192.168.0.163:27017", State: "Disconnected", Type: "Unknown" }] }.
---- The following constructor parameters did not have matching fixture data: MongoDatabaseFixture fixture
Stack Trace:
----- Inner Stack Trace #1 (System.TimeoutException) -----
at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChanged(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Clusters.Cluster.SelectServer(IServerSelector selector, CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.AreSessionsSupportedAfterServerSelection(CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.AreSessionsSupported(CancellationToken cancellationToken)
at MongoDB.Driver.OperationExecutor.StartImplicitSession(CancellationToken cancellationToken)
at MongoDB.Driver.MongoDatabaseImpl.UsingImplicitSession(Action`1 func, CancellationToken cancellationToken)
at MongoDB.Driver.MongoDatabaseImpl.DropCollection(String name, CancellationToken cancellationToken)
at IntegrationTests.MongoDatabaseFixture.DropPreviousTestData() in /builds/project/project-main/project.IntegrationTests/MongoDatabaseFixture.cs:line 40
----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----
My test stage in gitlab-ci.yml looks like this:
integrationtests:
image: microsoft/aspnetcore-build:1.0-2.0
stage: integrationtests
script:
- dotnet test -c Release ./$PROJECT_NAME.IntegrationTests/$PROJECT_NAME.IntegrationTests.csproj
tags:
- docker
only:
- schedules
Host machine 192.168.0.163 OS is Ubuntu and it also contains some other docker containers like clickhouse and no problems with connect to them while test runs on gitlab runner.
I try update MongoDb.Driver to latest version 2.7.2 and it solves problem. But for some reasons better for me not to update driver version. I think, if code throws no exceptions when its works from runned application then same code also works fine from tests? Any ideas to avoid this problem without update MongoDb library and why it happens? I also see this post Connection times out after upgrading MongoDB.Driver from 2.7.0 to 2.7.1
But if the problem is with the driver, why does the application works correctly with old Mongo.Driver library? Big thanks!
Updated: Today i try reproduse steps (if I put it correctly) , executed by gitlab runner. I build same image locally and run it with docker on my PC. I use following .Dockerfile for build:
FROM microsoft/aspnetcore-build:1.0-2.0 as base
WORKDIR /app
EXPOSE 80
FROM base AS final
WORKDIR /Test
COPY ./Test /Test
ENTRYPOINT /bin/bash
Where "Test" is my solution folder. Next, i go into container using the following command:
docker run -it 2da12ce6cb7d
(name assigned by docker after build cmd) and try execute
- dotnet test -c Release ./ProjectName.IntegrationTests/ProjectName.IntegrationTests.csproj
and got these exception again. So, because my runned application (where connection is ok) used microsoft/aspnetcore:2.0.3 docker image as base the problem is probably in it.