69 lines
2.3 KiB
C#
Executable File
69 lines
2.3 KiB
C#
Executable File
using audit_logs.Utils;
|
|
|
|
namespace audit_test.Repository;
|
|
using Mongo2Go;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
|
|
[SetUpFixture]
|
|
[Category("Integration")]
|
|
public class IntegrationDb
|
|
{
|
|
public static MongoDbRunner Runner { get { return _runner; } }
|
|
public static MongoClient Client { get { return _client; } }
|
|
public static IMongoDatabase Database { get; private set; } = null!;
|
|
|
|
private static MongoDbRunner _runner = null!;
|
|
private static MongoClient _client = null!;
|
|
|
|
private const int TimeoutInSeconds = 60; // Set the desired timeout in seconds.
|
|
public static string DatabaseName { get; private set; } = "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()
|
|
{
|
|
_runner?.Dispose();
|
|
_runner = null;
|
|
_client = null;
|
|
Database = null;
|
|
|
|
}
|
|
|
|
} |