Progmasoft Git

tazel

A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.

Clonegit clone https://git.progmasoft.com/tazel

Fix Codacy empty catch blocks and add daemon lifecycle tests

Address the two Codacy empty catch block findings in DaemonClient and
DaemonServer, and add StartRestartAndKillManageRealDaemonProcess to
cover real daemon process spawning, duplicate-start detection, restart,
and immediate kill.
Commit
0662a4e3bbdaa26aa118442b56aa7d85dd05e279
Author
Leitwolf11 ·
Parents
f1767bcd7567

Files · Download .tar.gz

Changes

 src/Progmasoft.Tazel/Daemon/DaemonClient.cs |  6 ++--
 src/Progmasoft.Tazel/Daemon/DaemonServer.cs |  7 ++---
 tests/Progmasoft.Tazel.Tests/DaemonTests.cs | 47 +++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/src/Progmasoft.Tazel/Daemon/DaemonClient.cs b/src/Progmasoft.Tazel/Daemon/DaemonClient.cs
index cfe9bd3..429048e 100644
--- a/src/Progmasoft.Tazel/Daemon/DaemonClient.cs
+++ b/src/Progmasoft.Tazel/Daemon/DaemonClient.cs
@@ -248,11 +248,9 @@ internal static class DaemonClient
                 File.Delete(path);
             }
         }
-        catch (IOException)
-        {
-        }
-        catch (UnauthorizedAccessException)
+        catch (Exception error) when (error is IOException or UnauthorizedAccessException)
         {
+            // Best-effort cleanup of stale socket or metadata file.
         }
     }
 }
diff --git a/src/Progmasoft.Tazel/Daemon/DaemonServer.cs b/src/Progmasoft.Tazel/Daemon/DaemonServer.cs
index 9550c34..442cb7a 100644
--- a/src/Progmasoft.Tazel/Daemon/DaemonServer.cs
+++ b/src/Progmasoft.Tazel/Daemon/DaemonServer.cs
@@ -62,6 +62,7 @@ internal sealed class DaemonServer(DaemonConfig config, Version tazelVersion)
             }
             catch (OperationCanceledException)
             {
+                // Expected when the daemon shuts down.
             }
 
             DeleteIfExists(config.SocketPath);
@@ -165,11 +166,9 @@ internal sealed class DaemonServer(DaemonConfig config, Version tazelVersion)
                 File.Delete(path);
             }
         }
-        catch (IOException)
-        {
-        }
-        catch (UnauthorizedAccessException)
+        catch (Exception error) when (error is IOException or UnauthorizedAccessException)
         {
+            // Best-effort cleanup of stale socket or metadata file.
         }
     }
 }
diff --git a/tests/Progmasoft.Tazel.Tests/DaemonTests.cs b/tests/Progmasoft.Tazel.Tests/DaemonTests.cs
index 2ade608..2e79e60 100644
--- a/tests/Progmasoft.Tazel.Tests/DaemonTests.cs
+++ b/tests/Progmasoft.Tazel.Tests/DaemonTests.cs
@@ -143,4 +143,51 @@ public sealed class DaemonTests
             }
         }
     }
+
+    [Fact]
+    public async Task StartRestartAndKillManageRealDaemonProcess()
+    {
+        var dir = Path.Combine(Path.GetTempPath(), $"tz-p-{Guid.NewGuid():N}"[..16]);
+        Directory.CreateDirectory(dir);
+        try
+        {
+            using var envDir = new EnvironmentVariable("TAZEL_DAEMON_DIR", dir);
+            using var envIdle = new EnvironmentVariable("TAZEL_DAEMON_IDLE_MINUTES", "5");
+            using var envHeap = new EnvironmentVariable("TAZEL_DAEMON_MAX_HEAP", "256m");
+            using var envMode = new EnvironmentVariable("TAZEL_DAEMON", "start");
+
+            var config = DaemonConfig.FromEnvironment(typeof(Program).Assembly.Location);
+            Assert.Equal(DaemonMode.Start, config.Mode);
+            Assert.Equal(5, config.IdleMinutes);
+            Assert.Equal(256L * 1024 * 1024, config.MaxHeapBytes);
+
+            var tazelExe = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location)!, OperatingSystem.IsWindows() ? "tazel.exe" : "tazel");
+            var runConfig = File.Exists(tazelExe)
+                ? DaemonConfig.FromEnvironment(tazelExe)
+                : DaemonConfig.FromEnvironment();
+
+            var started = await DaemonClient.StartAsync(runConfig, CancellationToken.None);
+            Assert.True(started.Ok, started.Message);
+            Assert.NotNull(started.Pid);
+
+            var again = await DaemonClient.StartAsync(runConfig, CancellationToken.None);
+            Assert.True(again.Ok, again.Message);
+            Assert.Contains("already running", again.Message, StringComparison.Ordinal);
+
+            var restarted = await DaemonClient.RestartAsync(runConfig, CancellationToken.None);
+            Assert.True(restarted.Ok, restarted.Message);
+            Assert.NotNull(restarted.Pid);
+
+            var killed = await DaemonClient.KillAsync(runConfig, CancellationToken.None);
+            Assert.True(killed.Ok, killed.Message);
+            Assert.Null(await DaemonClient.TryStatusAsync(runConfig, CancellationToken.None));
+        }
+        finally
+        {
+            if (Directory.Exists(dir))
+            {
+                Directory.Delete(dir, recursive: true);
+            }
+        }
+    }
 }