2026-03-09 13:53:58 +00:00
|
|
|
package process
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
"dappco.re/go/core"
|
2026-03-09 13:53:58 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestPIDFile_Acquire_Good(t *testing.T) {
|
|
|
|
|
pidPath := core.JoinPath(t.TempDir(), "test.pid")
|
2026-03-09 13:53:58 +00:00
|
|
|
pid := NewPIDFile(pidPath)
|
|
|
|
|
err := pid.Acquire()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
data, err := os.ReadFile(pidPath)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.NotEmpty(t, data)
|
|
|
|
|
err = pid.Release()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
_, err = os.Stat(pidPath)
|
|
|
|
|
assert.True(t, os.IsNotExist(err))
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestPIDFile_AcquireStale_Good(t *testing.T) {
|
|
|
|
|
pidPath := core.JoinPath(t.TempDir(), "stale.pid")
|
2026-03-09 13:53:58 +00:00
|
|
|
require.NoError(t, os.WriteFile(pidPath, []byte("999999999"), 0644))
|
|
|
|
|
pid := NewPIDFile(pidPath)
|
|
|
|
|
err := pid.Acquire()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = pid.Release()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestPIDFile_CreateDirectory_Good(t *testing.T) {
|
|
|
|
|
pidPath := core.JoinPath(t.TempDir(), "subdir", "nested", "test.pid")
|
2026-03-09 13:53:58 +00:00
|
|
|
pid := NewPIDFile(pidPath)
|
|
|
|
|
err := pid.Acquire()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = pid.Release()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestPIDFile_Path_Good(t *testing.T) {
|
2026-03-09 13:53:58 +00:00
|
|
|
pid := NewPIDFile("/tmp/test.pid")
|
|
|
|
|
assert.Equal(t, "/tmp/test.pid", pid.Path())
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestReadPID_Missing_Bad(t *testing.T) {
|
2026-03-09 13:53:58 +00:00
|
|
|
pid, running := ReadPID("/nonexistent/path.pid")
|
|
|
|
|
assert.Equal(t, 0, pid)
|
|
|
|
|
assert.False(t, running)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestReadPID_Invalid_Bad(t *testing.T) {
|
|
|
|
|
path := core.JoinPath(t.TempDir(), "bad.pid")
|
2026-03-09 13:53:58 +00:00
|
|
|
require.NoError(t, os.WriteFile(path, []byte("notanumber"), 0644))
|
|
|
|
|
pid, running := ReadPID(path)
|
|
|
|
|
assert.Equal(t, 0, pid)
|
|
|
|
|
assert.False(t, running)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 00:45:36 +00:00
|
|
|
func TestReadPID_Stale_Bad(t *testing.T) {
|
|
|
|
|
path := core.JoinPath(t.TempDir(), "stale.pid")
|
2026-03-09 13:53:58 +00:00
|
|
|
require.NoError(t, os.WriteFile(path, []byte("999999999"), 0644))
|
|
|
|
|
pid, running := ReadPID(path)
|
|
|
|
|
assert.Equal(t, 999999999, pid)
|
|
|
|
|
assert.False(t, running)
|
|
|
|
|
}
|