diff --git a/dotnet/test/VectorData/MongoDB.ConformanceTests/MongoDB.ConformanceTests.csproj b/dotnet/test/VectorData/MongoDB.ConformanceTests/MongoDB.ConformanceTests.csproj index 733edea9325d..9f08e3a9a4eb 100644 --- a/dotnet/test/VectorData/MongoDB.ConformanceTests/MongoDB.ConformanceTests.csproj +++ b/dotnet/test/VectorData/MongoDB.ConformanceTests/MongoDB.ConformanceTests.csproj @@ -10,6 +10,9 @@ + + + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/dotnet/test/VectorData/MongoDB.ConformanceTests/Support/MongoTestEnvironment.cs b/dotnet/test/VectorData/MongoDB.ConformanceTests/Support/MongoTestEnvironment.cs new file mode 100644 index 000000000000..4dede246c6a1 --- /dev/null +++ b/dotnet/test/VectorData/MongoDB.ConformanceTests/Support/MongoTestEnvironment.cs @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft. All rights reserved. + +using Microsoft.Extensions.Configuration; + +namespace MongoDB.ConformanceTests.Support; + +#pragma warning disable CA1810 // Initialize all static fields when those fields are declared + +internal static class MongoTestEnvironment +{ + public static readonly string? ConnectionUrl; + + public static bool IsConnectionInfoDefined => ConnectionUrl is not null; + + static MongoTestEnvironment() + { + var configuration = new ConfigurationBuilder() + .AddJsonFile(path: "testsettings.json", optional: true) + .AddJsonFile(path: "testsettings.development.json", optional: true) + .AddEnvironmentVariables() + .Build(); + + var mongoSection = configuration.GetSection("MongoDB"); + ConnectionUrl = mongoSection["ConnectionURL"]; + } +} diff --git a/dotnet/test/VectorData/MongoDB.ConformanceTests/Support/MongoTestStore.cs b/dotnet/test/VectorData/MongoDB.ConformanceTests/Support/MongoTestStore.cs index 75dec3a6d10b..15bea231a726 100644 --- a/dotnet/test/VectorData/MongoDB.ConformanceTests/Support/MongoTestStore.cs +++ b/dotnet/test/VectorData/MongoDB.ConformanceTests/Support/MongoTestStore.cs @@ -13,9 +13,7 @@ internal sealed class MongoTestStore : TestStore { public static MongoTestStore Instance { get; } = new(); - private readonly MongoDbContainer _container = new MongoDbBuilder() - .WithImage("mongodb/mongodb-atlas-local:7.0.6") - .Build(); + private MongoDbContainer? _container; public MongoClient? _client { get; private set; } public IMongoDatabase? _database { get; private set; } @@ -32,21 +30,40 @@ private MongoTestStore() protected override async Task StartAsync() { + var clientSettings = MongoTestEnvironment.IsConnectionInfoDefined + ? MongoClientSettings.FromConnectionString(MongoTestEnvironment.ConnectionUrl) + : await this.StartMongoDbContainerAsync(); + + this._client = new MongoClient(clientSettings); + this._database = this._client.GetDatabase("VectorSearchTests"); + this.DefaultVectorStore = new MongoVectorStore(this._database); + } + + private async Task StartMongoDbContainerAsync() + { + this._container = new MongoDbBuilder() + .WithImage("mongodb/mongodb-atlas-local:7.0.6") + .Build(); + using CancellationTokenSource cts = new(); cts.CancelAfter(TimeSpan.FromSeconds(60)); await this._container.StartAsync(cts.Token); - this._client = new MongoClient(new MongoClientSettings + return new MongoClientSettings { Server = new MongoServerAddress(this._container.Hostname, this._container.GetMappedPublicPort(MongoDbBuilder.MongoDbPort)), DirectConnection = true, // ReadConcern = ReadConcern.Linearizable, // WriteConcern = WriteConcern.WMajority - }); - this._database = this._client.GetDatabase("VectorSearchTests"); - this.DefaultVectorStore = new MongoVectorStore(this._database); + }; } - protected override Task StopAsync() - => this._container.StopAsync(); + protected override async Task StopAsync() + { + if (this._container != null) + { + await this._container.StopAsync(); + this._container = null; + } + } } diff --git a/dotnet/test/VectorData/VectorData.ConformanceTests/Support/TestStore.cs b/dotnet/test/VectorData/VectorData.ConformanceTests/Support/TestStore.cs index 719153071515..5094cc215443 100644 --- a/dotnet/test/VectorData/VectorData.ConformanceTests/Support/TestStore.cs +++ b/dotnet/test/VectorData/VectorData.ConformanceTests/Support/TestStore.cs @@ -96,7 +96,7 @@ public virtual async Task WaitForDataAsync( var vector = dummyVector ?? new ReadOnlyMemory(Enumerable.Range(0, vectorSize ?? 3).Select(i => (float)i).ToArray()); - for (var i = 0; i < 20; i++) + for (var i = 0; i < 200; i++) { var results = collection.SearchAsync( vector,