package godaemon import ( "os" "path/filepath" "testing" ) // mockLogger is a simple mock logger for testing type mockLogger struct { debugLogs []any infoLogs []any errorLogs []any } func (m *mockLogger) Debug(v ...any) { m.debugLogs = append(m.debugLogs, v) } func (m *mockLogger) Info(v ...any) { m.infoLogs = append(m.infoLogs, v) } func (m *mockLogger) Error(v ...any) { m.errorLogs = append(m.errorLogs, v) } func TestIsMaster(t *testing.T) { // Clear the environment variable first os.Unsetenv(daemon_env_key) if !IsMaster() { t.Error("IsMaster() should return true when environment variable is not set") } } func TestIsDaemon(t *testing.T) { tests := []struct { name string envValue string expected bool }{ {"daemon process", daemon_process, true}, {"task process", daemon_task, false}, {"empty", "", false}, {"unknown value", "unknown", false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.envValue == "" { os.Unsetenv(daemon_env_key) } else { os.Setenv(daemon_env_key, tt.envValue) } defer os.Unsetenv(daemon_env_key) if got := IsDaemon(); got != tt.expected { t.Errorf("IsDaemon() = %v, want %v", got, tt.expected) } }) } } func TestIsDaemonTask(t *testing.T) { tests := []struct { name string envValue string expected bool }{ {"task process", daemon_task, true}, {"daemon process", daemon_process, false}, {"empty", "", false}, {"unknown value", "unknown", false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.envValue == "" { os.Unsetenv(daemon_env_key) } else { os.Setenv(daemon_env_key, tt.envValue) } defer os.Unsetenv(daemon_env_key) if got := IsDaemonTask(); got != tt.expected { t.Errorf("IsDaemonTask() = %v, want %v", got, tt.expected) } }) } } func TestNewGoDaemon(t *testing.T) { logger := &mockLogger{} startCalled := false stopCalled := false startFn := func(g *GoDaemon) { startCalled = true } stopFn := func(g *GoDaemon) { stopCalled = true } gd := NewGoDaemonWithLogger(startFn, stopFn, logger) if gd == nil { t.Fatal("NewGoDaemonWithLogger returned nil") } if gd.StartFn == nil || gd.StopFn == nil { t.Error("StartFn or StopFn is nil") } if gd.logger == nil { t.Error("logger is nil") } if gd.pidFile == "" || gd.taskPidFile == "" { t.Error("PID file paths are not set") } // Verify functions are properly assigned gd.StartFn(gd) if !startCalled { t.Error("StartFn was not called") } gd.StopFn(gd) if !stopCalled { t.Error("StopFn was not called") } } func TestGetPid(t *testing.T) { tmpDir := t.TempDir() pidFile := filepath.Join(tmpDir, "test.pid") gd := &GoDaemon{pidFile: pidFile} // Test when file doesn't exist if got := gd.GetPid(); got != 0 { t.Errorf("GetPid() = %v, want 0 when file doesn't exist", got) } // Test with invalid content os.WriteFile(pidFile, []byte("invalid"), 0644) if got := gd.GetPid(); got != 0 { t.Errorf("GetPid() = %v, want 0 for invalid content", got) } // Test with valid PID os.WriteFile(pidFile, []byte("12345"), 0644) if got := gd.GetPid(); got != 12345 { t.Errorf("GetPid() = %v, want 12345", got) } // Test with whitespace os.WriteFile(pidFile, []byte(" 67890 \n"), 0644) if got := gd.GetPid(); got != 67890 { t.Errorf("GetPid() = %v, want 67890 with whitespace", got) } } func TestGetTaskPid(t *testing.T) { tmpDir := t.TempDir() taskPidFile := filepath.Join(tmpDir, "test.task.pid") gd := &GoDaemon{taskPidFile: taskPidFile} // Test when file doesn't exist if got := gd.GetTaskPid(); got != 0 { t.Errorf("GetTaskPid() = %v, want 0 when file doesn't exist", got) } // Test with valid PID os.WriteFile(taskPidFile, []byte("54321"), 0644) if got := gd.GetTaskPid(); got != 54321 { t.Errorf("GetTaskPid() = %v, want 54321", got) } } func TestCleanupPIDFiles(t *testing.T) { tmpDir := t.TempDir() pidFile := filepath.Join(tmpDir, "test.pid") taskPidFile := filepath.Join(tmpDir, "test.task.pid") gd := &GoDaemon{pidFile: pidFile, taskPidFile: taskPidFile} // Create PID files os.WriteFile(pidFile, []byte("12345"), 0644) os.WriteFile(taskPidFile, []byte("67890"), 0644) // Verify files exist if _, err := os.Stat(pidFile); os.IsNotExist(err) { t.Error("pidFile should exist before cleanup") } if _, err := os.Stat(taskPidFile); os.IsNotExist(err) { t.Error("taskPidFile should exist before cleanup") } // Cleanup gd.cleanupPIDFiles() // Verify files are removed if _, err := os.Stat(pidFile); !os.IsNotExist(err) { t.Error("pidFile should be removed after cleanup") } if _, err := os.Stat(taskPidFile); !os.IsNotExist(err) { t.Error("taskPidFile should be removed after cleanup") } } func TestDefaultLogger(t *testing.T) { logger := &defaultLogger{} // These should not panic logger.Debug("debug", "message", 123) logger.Info("info", "message") logger.Error("error", "message") } func TestLoggerInterface(t *testing.T) { // Verify that defaultLogger implements Logger interface var _ Logger = &defaultLogger{} var _ Logger = &mockLogger{} }