|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using Coder.Desktop.App.Services; |
| 4 | + |
| 5 | +namespace Coder.Desktop.Tests.App.Services; |
| 6 | + |
| 7 | +[TestFixture] |
| 8 | +public class MutagenControllerTest |
| 9 | +{ |
| 10 | + [OneTimeSetUp] |
| 11 | + public async Task DownloadMutagen() |
| 12 | + { |
| 13 | + var ct = new CancellationTokenSource(TimeSpan.FromSeconds(60)).Token; |
| 14 | + var scriptDirectory = Path.GetFullPath(Path.Combine(TestContext.CurrentContext.TestDirectory, |
| 15 | + "..", "..", "..", "..", "scripts")); |
| 16 | + var process = new Process(); |
| 17 | + process.StartInfo.FileName = "powershell.exe"; |
| 18 | + process.StartInfo.UseShellExecute = false; |
| 19 | + process.StartInfo.Arguments = $"-ExecutionPolicy Bypass -File Get-Mutagen.ps1 -arch {_arch}"; |
| 20 | + process.StartInfo.RedirectStandardError = true; |
| 21 | + process.StartInfo.RedirectStandardOutput = true; |
| 22 | + process.StartInfo.WorkingDirectory = scriptDirectory; |
| 23 | + process.Start(); |
| 24 | + var output = await process.StandardOutput.ReadToEndAsync(ct); |
| 25 | + TestContext.Out.Write(output); |
| 26 | + var error = await process.StandardError.ReadToEndAsync(ct); |
| 27 | + TestContext.Error.Write(error); |
| 28 | + Assert.That(process.ExitCode, Is.EqualTo(0)); |
| 29 | + _mutagenBinaryPath = Path.Combine(scriptDirectory, "files", $"mutagen-windows-{_arch}.exe"); |
| 30 | + Assert.That(File.Exists(_mutagenBinaryPath)); |
| 31 | + } |
| 32 | + |
| 33 | + [SetUp] |
| 34 | + public void CreateTempDir() |
| 35 | + { |
| 36 | + _tempDirectory = Directory.CreateTempSubdirectory(GetType().Name); |
| 37 | + TestContext.Out.WriteLine($"temp directory: {_tempDirectory}"); |
| 38 | + } |
| 39 | + |
| 40 | + private readonly string _arch = RuntimeInformation.ProcessArchitecture switch |
| 41 | + { |
| 42 | + Architecture.X64 => "x64", |
| 43 | + Architecture.Arm64 => "arm64", |
| 44 | + // We only support amd64 and arm64 on Windows currently. |
| 45 | + _ => throw new PlatformNotSupportedException( |
| 46 | + $"Unsupported architecture '{RuntimeInformation.ProcessArchitecture}'. Coder only supports x64 and arm64."), |
| 47 | + }; |
| 48 | + |
| 49 | + private string _mutagenBinaryPath; |
| 50 | + private DirectoryInfo _tempDirectory; |
| 51 | + |
| 52 | + [Test(Description = "Shut down daemon when no sessions")] |
| 53 | + [CancelAfter(30_000)] |
| 54 | + public async Task ShutdownNoSessions(CancellationToken ct) |
| 55 | + { |
| 56 | + // NUnit runs each test in a temporary directory |
| 57 | + var dataDirectory = _tempDirectory.FullName; |
| 58 | + await using var controller = new MutagenController(_mutagenBinaryPath, dataDirectory); |
| 59 | + await controller.Initialize(ct); |
| 60 | + |
| 61 | + // log file tells us the daemon was started. |
| 62 | + var logPath = Path.Combine(dataDirectory, "daemon.log"); |
| 63 | + Assert.That(File.Exists(logPath)); |
| 64 | + |
| 65 | + var lockPath = Path.Combine(dataDirectory, "daemon", "daemon.lock"); |
| 66 | + // If we can lock the daemon.lock file, it means the daemon has stopped. |
| 67 | + while (true) |
| 68 | + { |
| 69 | + ct.ThrowIfCancellationRequested(); |
| 70 | + try |
| 71 | + { |
| 72 | + await using var lockFile = new FileStream(lockPath, FileMode.Open, FileAccess.Write, FileShare.None); |
| 73 | + } |
| 74 | + catch (IOException e) |
| 75 | + { |
| 76 | + TestContext.Out.WriteLine($"Didn't get lock (will retry): {e.Message}"); |
| 77 | + await Task.Delay(100, ct); |
| 78 | + } |
| 79 | + |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + [Test(Description = "Daemon is restarted when we create a session")] |
| 85 | + [CancelAfter(30_000)] |
| 86 | + public async Task CreateRestartsDaemon(CancellationToken ct) |
| 87 | + { |
| 88 | + // NUnit runs each test in a temporary directory |
| 89 | + var dataDirectory = _tempDirectory.FullName; |
| 90 | + await using (var controller = new MutagenController(_mutagenBinaryPath, dataDirectory)) |
| 91 | + { |
| 92 | + await controller.Initialize(ct); |
| 93 | + await controller.CreateSyncSession(new SyncSession(), ct); |
| 94 | + } |
| 95 | + |
| 96 | + var logPath = Path.Combine(dataDirectory, "daemon.log"); |
| 97 | + Assert.That(File.Exists(logPath)); |
| 98 | + var logLines = File.ReadAllLines(logPath); |
| 99 | + |
| 100 | + // Here we're going to use the log to verify the daemon was started 2 times. |
| 101 | + // slightly brittle, but unlikely this log line will change. |
| 102 | + Assert.That(logLines.Count(s => s.Contains("[sync] Session manager initialized")), Is.EqualTo(2)); |
| 103 | + } |
| 104 | + |
| 105 | + [Test(Description = "Controller kills orphaned daemon")] |
| 106 | + [CancelAfter(30_000)] |
| 107 | + public async Task Orphaned(CancellationToken ct) |
| 108 | + { |
| 109 | + // NUnit runs each test in a temporary directory |
| 110 | + var dataDirectory = _tempDirectory.FullName; |
| 111 | + MutagenController? controller1 = null; |
| 112 | + MutagenController? controller2 = null; |
| 113 | + try |
| 114 | + { |
| 115 | + controller1 = new MutagenController(_mutagenBinaryPath, dataDirectory); |
| 116 | + await controller1.Initialize(ct); |
| 117 | + await controller1.CreateSyncSession(new SyncSession(), ct); |
| 118 | + |
| 119 | + controller2 = new MutagenController(_mutagenBinaryPath, dataDirectory); |
| 120 | + await controller2.Initialize(ct); |
| 121 | + } |
| 122 | + finally |
| 123 | + { |
| 124 | + if (controller1 != null) await controller1.DisposeAsync(); |
| 125 | + if (controller2 != null) await controller2.DisposeAsync(); |
| 126 | + } |
| 127 | + |
| 128 | + var logPath = Path.Combine(dataDirectory, "daemon.log"); |
| 129 | + Assert.That(File.Exists(logPath)); |
| 130 | + var logLines = File.ReadAllLines(logPath); |
| 131 | + |
| 132 | + // Here we're going to use the log to verify the daemon was started 3 times. |
| 133 | + // slightly brittle, but unlikely this log line will change. |
| 134 | + Assert.That(logLines.Count(s => s.Contains("[sync] Session manager initialized")), Is.EqualTo(3)); |
| 135 | + } |
| 136 | + |
| 137 | + // TODO: Add more tests once we actually implement creating sessions on the daemon |
| 138 | +} |
0 commit comments