- pkg/api/provider.go: remove banned os/syscall imports; delegate to
new process.KillPID and process.IsPIDAlive exported helpers
- service.go: rename `sr` → `startResult`; add KillPID/IsPIDAlive exports
- runner.go: rename `aggResult` → `aggregate` in all three RunXxx methods;
add usage-example comments on all exported functions
- process.go: replace prose doc-comments with usage-example comments
- buffer.go, registry.go, health.go: replace prose comments with examples
- buffer_test.go: rename TestRingBuffer_Basics_Good → TestBuffer_{Write,String,Reset}_{Good,Bad,Ugly}
- All test files: add missing _Bad and _Ugly variants for all functions
(daemon, health, pidfile, registry, runner, process, program, exec, pkg/api)
Co-Authored-By: Virgil <virgil@lethean.io>
190 lines
3.9 KiB
Go
190 lines
3.9 KiB
Go
package process
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"dappco.re/go/core"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestDaemon_Lifecycle_Good(t *testing.T) {
|
|
pidPath := core.JoinPath(t.TempDir(), "test.pid")
|
|
|
|
d := NewDaemon(DaemonOptions{
|
|
PIDFile: pidPath,
|
|
HealthAddr: "127.0.0.1:0",
|
|
ShutdownTimeout: 5 * time.Second,
|
|
})
|
|
|
|
err := d.Start()
|
|
require.NoError(t, err)
|
|
|
|
addr := d.HealthAddr()
|
|
require.NotEmpty(t, addr)
|
|
|
|
resp, err := http.Get("http://" + addr + "/health")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
_ = resp.Body.Close()
|
|
|
|
err = d.Stop()
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestDaemon_AlreadyRunning_Bad(t *testing.T) {
|
|
d := NewDaemon(DaemonOptions{
|
|
HealthAddr: "127.0.0.1:0",
|
|
})
|
|
|
|
err := d.Start()
|
|
require.NoError(t, err)
|
|
defer func() { _ = d.Stop() }()
|
|
|
|
err = d.Start()
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "already running")
|
|
}
|
|
|
|
func TestDaemon_RunUnstarted_Bad(t *testing.T) {
|
|
d := NewDaemon(DaemonOptions{})
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
err := d.Run(ctx)
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "not started")
|
|
}
|
|
|
|
func TestDaemon_SetReady_Good(t *testing.T) {
|
|
d := NewDaemon(DaemonOptions{
|
|
HealthAddr: "127.0.0.1:0",
|
|
})
|
|
|
|
err := d.Start()
|
|
require.NoError(t, err)
|
|
defer func() { _ = d.Stop() }()
|
|
|
|
addr := d.HealthAddr()
|
|
|
|
resp, _ := http.Get("http://" + addr + "/ready")
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
_ = resp.Body.Close()
|
|
|
|
d.SetReady(false)
|
|
|
|
resp, _ = http.Get("http://" + addr + "/ready")
|
|
assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)
|
|
_ = resp.Body.Close()
|
|
}
|
|
|
|
func TestDaemon_HealthAddrDisabled_Good(t *testing.T) {
|
|
d := NewDaemon(DaemonOptions{})
|
|
assert.Empty(t, d.HealthAddr())
|
|
}
|
|
|
|
func TestDaemon_DefaultTimeout_Good(t *testing.T) {
|
|
d := NewDaemon(DaemonOptions{})
|
|
assert.Equal(t, 30*time.Second, d.opts.ShutdownTimeout)
|
|
}
|
|
|
|
func TestDaemon_RunBlocking_Good(t *testing.T) {
|
|
d := NewDaemon(DaemonOptions{
|
|
HealthAddr: "127.0.0.1:0",
|
|
})
|
|
|
|
err := d.Start()
|
|
require.NoError(t, err)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
done <- d.Run(ctx)
|
|
}()
|
|
|
|
// Run should be blocking
|
|
select {
|
|
case <-done:
|
|
t.Fatal("Run should block until context is cancelled")
|
|
case <-time.After(50 * time.Millisecond):
|
|
// Expected — still blocking
|
|
}
|
|
|
|
cancel()
|
|
|
|
select {
|
|
case err := <-done:
|
|
assert.NoError(t, err)
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("Run should return after context cancellation")
|
|
}
|
|
}
|
|
|
|
func TestDaemon_StopIdempotent_Good(t *testing.T) {
|
|
d := NewDaemon(DaemonOptions{})
|
|
|
|
// Stop without Start should be a no-op
|
|
err := d.Stop()
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestDaemon_AutoRegister_Good(t *testing.T) {
|
|
dir := t.TempDir()
|
|
reg := NewRegistry(core.JoinPath(dir, "daemons"))
|
|
|
|
d := NewDaemon(DaemonOptions{
|
|
HealthAddr: "127.0.0.1:0",
|
|
Registry: reg,
|
|
RegistryEntry: DaemonEntry{
|
|
Code: "test-app",
|
|
Daemon: "serve",
|
|
},
|
|
})
|
|
|
|
err := d.Start()
|
|
require.NoError(t, err)
|
|
|
|
// Should be registered
|
|
entry, ok := reg.Get("test-app", "serve")
|
|
require.True(t, ok)
|
|
assert.Equal(t, os.Getpid(), entry.PID)
|
|
assert.NotEmpty(t, entry.Health)
|
|
|
|
// Stop should unregister
|
|
err = d.Stop()
|
|
require.NoError(t, err)
|
|
|
|
_, ok = reg.Get("test-app", "serve")
|
|
assert.False(t, ok)
|
|
}
|
|
|
|
func TestDaemon_Lifecycle_Ugly(t *testing.T) {
|
|
t.Run("stop called twice is safe", func(t *testing.T) {
|
|
d := NewDaemon(DaemonOptions{
|
|
HealthAddr: "127.0.0.1:0",
|
|
})
|
|
|
|
err := d.Start()
|
|
require.NoError(t, err)
|
|
|
|
err = d.Stop()
|
|
assert.NoError(t, err)
|
|
|
|
// Second stop should be a no-op
|
|
err = d.Stop()
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("set ready with no health server is a no-op", func(t *testing.T) {
|
|
d := NewDaemon(DaemonOptions{})
|
|
// Should not panic
|
|
d.SetReady(true)
|
|
d.SetReady(false)
|
|
})
|
|
}
|