Pulse/internal/config/watcher_fsnotify_test.go
rcourtman ed78509f92 Fix flaky tests and improve coverage across alerts, api, and config packages
- Fix deadlock and race conditions in internal/alerts
- Add comprehensive error path tests for internal/config
- Fix 401 handling in internal/api
- Fix Docker Swarm task filtering test logic
2026-01-03 18:36:17 +00:00

60 lines
1.3 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/fsnotify/fsnotify"
"github.com/stretchr/testify/require"
)
// TestHandleEvents tests handleEvents with mock channels
func TestHandleEvents(t *testing.T) {
tempDir := t.TempDir()
envPath := filepath.Join(tempDir, ".env")
t.Setenv("PULSE_AUTH_CONFIG_DIR", tempDir)
require.NoError(t, os.WriteFile(envPath, []byte(`PULSE_AUTH_USER="initial"`), 0644))
cfg := &Config{}
cw, err := NewConfigWatcher(cfg)
require.NoError(t, err)
// Override hash check
cw.lastEnvHash = "dummy"
events := make(chan fsnotify.Event)
errors := make(chan error)
go cw.handleEvents(events, errors)
defer cw.Stop()
// 1. Inject Write event
require.NoError(t, os.WriteFile(envPath, []byte(`PULSE_AUTH_USER="handled"`), 0644))
events <- fsnotify.Event{
Name: envPath,
Op: fsnotify.Write,
}
require.Eventually(t, func() bool {
Mu.RLock()
defer Mu.RUnlock()
return cfg.AuthUser == "handled"
}, 2*time.Second, 100*time.Millisecond)
// 2. Inject Error
// Just ensure it doesn't panic and logs it (can't easily check log here without hook)
errors <- parseError("test err")
time.Sleep(100 * time.Millisecond)
}
func parseError(s string) error {
return &testError{s}
}
type testError struct{ s string }
func (e *testError) Error() string { return e.s }