Creado apartir del commit b888de6c92056de294456f581a56e25e734d4ab7 de develop
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user