Files

90 lines
2.7 KiB
C#

using System.Diagnostics;
namespace Tests;
[SetUpFixture]
public class TestSetup
{
private const string ServerUrl = "http://localhost:5256";
private Process? _serverProcess;
[OneTimeSetUp]
public async Task BeforeAllTests()
{
Console.WriteLine("[DEBUG_LOG] Starting Cloud...");
// Find solution root
var currentDir = AppContext.BaseDirectory;
while (currentDir != null && !File.Exists(Path.Combine(currentDir, "Chrono.sln")))
currentDir = Path.GetDirectoryName(currentDir);
if (currentDir == null) throw new Exception("Could not find Chrono.sln to determine project path.");
var projectPath = Path.Combine(currentDir, "Cloud", "Cloud.csproj");
var startInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"run --project \"{projectPath}\" --no-build",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = currentDir
};
_serverProcess = Process.Start(startInfo) ?? throw new Exception("Failed to start server process.");
_serverProcess.OutputDataReceived += (s, e) =>
{
if (e.Data != null) Console.WriteLine($"[SERVER OUT] {e.Data}");
};
_serverProcess.ErrorDataReceived += (s, e) =>
{
if (e.Data != null) Console.WriteLine($"[SERVER ERR] {e.Data}");
};
_serverProcess.BeginOutputReadLine();
_serverProcess.BeginErrorReadLine();
// Wait for the server to be ready
using var client = new HttpClient();
var sw = Stopwatch.StartNew();
var timeout = TimeSpan.FromSeconds(30);
var isReady = false;
while (sw.Elapsed < timeout)
try
{
var response = await client.GetAsync(ServerUrl);
if (response.IsSuccessStatusCode)
{
isReady = true;
break;
}
}
catch
{
// Wait and retry
await Task.Delay(1000);
}
if (!isReady)
{
_serverProcess.Kill(true);
throw new Exception($"Cloud did not start within {timeout.TotalSeconds} seconds.");
}
Console.WriteLine("[DEBUG_LOG] Cloud is ready.");
}
[OneTimeTearDown]
public void AfterAllTests()
{
if (_serverProcess != null && !_serverProcess.HasExited)
{
Console.WriteLine("[DEBUG_LOG] Stopping Cloud...");
_serverProcess.Kill(true);
_serverProcess.Dispose();
}
}
}