64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
using adas_core.Infrastructure.Utils;
|
|
using Mongo2Go;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
|
|
namespace adas_core.Test.Repositories;
|
|
|
|
[SetUpFixture]
|
|
[Category("Integration")]
|
|
public class IntegrationDb
|
|
{
|
|
private const int TimeoutInSeconds = 60; // Set the desired timeout in seconds.
|
|
public static MongoDbRunner? Runner { get; private set; }
|
|
|
|
private static MongoClient? Client { get; set; }
|
|
|
|
public static IMongoDatabase Database { get; private set; } = null!;
|
|
public static string DatabaseName { get; } = "IntegrationTestDb";
|
|
|
|
[OneTimeSetUp]
|
|
public void InitIntegrationTests()
|
|
{
|
|
StartMongoDbRunner().Wait();
|
|
MongoDbHostBuilderExtension.ConfigureMongoDbConventions();
|
|
MongoDbHostBuilderExtension.ConfigureRegisterMapClass();
|
|
Client = new MongoClient(Runner?.ConnectionString);
|
|
Database = Client.GetDatabase(DatabaseName);
|
|
}
|
|
|
|
private static async Task StartMongoDbRunner()
|
|
{
|
|
Runner = MongoDbRunner.Start();
|
|
|
|
// Wait for the MongoDB server to become available or timeout.
|
|
var startTime = DateTime.UtcNow;
|
|
while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(TimeoutInSeconds))
|
|
try
|
|
{
|
|
var testClient = new MongoClient(Runner.ConnectionString);
|
|
var adminDb = testClient.GetDatabase("admin");
|
|
await adminDb.RunCommandAsync((Command<BsonDocument>)"{ping:1}");
|
|
return; // MongoDB server is available, continue.
|
|
}
|
|
catch
|
|
{
|
|
// Retry after a short delay.
|
|
await Task.Delay(500);
|
|
}
|
|
|
|
// Timeout reached, dispose the runner and throw an exception.
|
|
Runner.Dispose();
|
|
throw new TimeoutException("Timeout while starting MongoDB server.");
|
|
}
|
|
|
|
[OneTimeTearDown]
|
|
public void TeardownIntegrationTests()
|
|
{
|
|
Client?.Dispose();
|
|
Runner?.Dispose();
|
|
//_runner = null;
|
|
//_client = null;
|
|
//_fakeDb = null;
|
|
}
|
|
} |