hst/instance: define instance state
All checks were successful
Test / Create distribution (push) Successful in 34s
Test / Sandbox (push) Successful in 2m13s
Test / Hakurei (push) Successful in 3m6s
Test / Hpkg (push) Successful in 4m2s
Test / Sandbox (race detector) (push) Successful in 4m5s
Test / Hakurei (race detector) (push) Successful in 4m51s
Test / Flake checks (push) Successful in 1m30s

This is now part of the hst API. This change also improves identifier generation and serialisation.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2025-10-23 22:51:10 +09:00
parent 0fd357e7f6
commit dd94818f20
19 changed files with 261 additions and 227 deletions

View File

@@ -61,7 +61,7 @@ func tryFd(msg message.Msg, name string) io.ReadCloser {
}
}
func tryShort(msg message.Msg, name string) (config *hst.Config, entry *state.State) {
func tryShort(msg message.Msg, name string) (config *hst.Config, entry *hst.State) {
likePrefix := false
if len(name) <= 32 {
likePrefix = true

View File

@@ -41,7 +41,7 @@ func printShowSystem(output io.Writer, short, flagJSON bool) {
// printShowInstance writes a representation of [state.State] or [hst.Config] to output.
func printShowInstance(
output io.Writer, now time.Time,
instance *state.State, config *hst.Config,
instance *hst.State, config *hst.Config,
short, flagJSON bool) (valid bool) {
valid = true
@@ -168,7 +168,7 @@ func printShowInstance(
// printPs writes a representation of active instances to output.
func printPs(output io.Writer, now time.Time, s state.Store, short, flagJSON bool) {
var entries state.Entries
var entries map[hst.ID]*hst.State
if e, err := state.Join(s); err != nil {
log.Fatalf("cannot join store: %v", err)
} else {
@@ -179,7 +179,7 @@ func printPs(output io.Writer, now time.Time, s state.Store, short, flagJSON boo
}
if !short && flagJSON {
es := make(map[string]*state.State, len(entries))
es := make(map[string]*hst.State, len(entries))
for id, instance := range entries {
es[id.String()] = instance
}
@@ -249,7 +249,7 @@ func printPs(output io.Writer, now time.Time, s state.Store, short, flagJSON boo
// expandedStateEntry stores [state.State] alongside a string representation of its [state.ID].
type expandedStateEntry struct {
s string
*state.State
*hst.State
}
// newPrinter returns a configured, wrapped [tabwriter.Writer].

View File

@@ -10,13 +10,13 @@ import (
)
var (
testID = state.ID{
testID = hst.ID{
0x8e, 0x2c, 0x76, 0xb0,
0x66, 0xda, 0xbe, 0x57,
0x4c, 0xf0, 0x73, 0xbd,
0xb4, 0x6e, 0xb5, 0xc1,
}
testState = &state.State{
testState = &hst.State{
ID: testID,
PID: 0xDEADBEEF,
Config: hst.Template(),
@@ -31,7 +31,7 @@ func TestPrintShowInstance(t *testing.T) {
testCases := []struct {
name string
instance *state.State
instance *hst.State
config *hst.Config
short, json bool
want string
@@ -185,24 +185,7 @@ App
{"json nil", nil, nil, false, true, `null
`, true},
{"json instance", testState, nil, false, true, `{
"instance": [
142,
44,
118,
176,
102,
218,
190,
87,
76,
240,
115,
189,
180,
110,
181,
193
],
"instance": "8e2c76b066dabe574cf073bdb46eb5c1",
"pid": 3735928559,
"config": {
"id": "org.chromium.Chromium",
@@ -530,43 +513,26 @@ func TestPrintPs(t *testing.T) {
testCases := []struct {
name string
entries state.Entries
entries map[hst.ID]*hst.State
short, json bool
want string
}{
{"no entries", make(state.Entries), false, false, " Instance PID Application Uptime\n"},
{"no entries short", make(state.Entries), true, false, ""},
{"nil instance", state.Entries{testID: nil}, false, false, " Instance PID Application Uptime\n"},
{"state corruption", state.Entries{state.ID{}: testState}, false, false, " Instance PID Application Uptime\n"},
{"no entries", make(map[hst.ID]*hst.State), false, false, " Instance PID Application Uptime\n"},
{"no entries short", make(map[hst.ID]*hst.State), true, false, ""},
{"nil instance", map[hst.ID]*hst.State{testID: nil}, false, false, " Instance PID Application Uptime\n"},
{"state corruption", map[hst.ID]*hst.State{hst.ID{}: testState}, false, false, " Instance PID Application Uptime\n"},
{"valid pd", state.Entries{testID: &state.State{ID: testID, PID: 1 << 8, Config: new(hst.Config), Time: testAppTime}}, false, false, ` Instance PID Application Uptime
{"valid pd", map[hst.ID]*hst.State{testID: {ID: testID, PID: 1 << 8, Config: new(hst.Config), Time: testAppTime}}, false, false, ` Instance PID Application Uptime
8e2c76b0 256 0 (app.hakurei.8e2c76b0) 1h2m32s
`},
{"valid", state.Entries{testID: testState}, false, false, ` Instance PID Application Uptime
{"valid", map[hst.ID]*hst.State{testID: testState}, false, false, ` Instance PID Application Uptime
8e2c76b0 3735928559 9 (org.chromium.Chromium) 1h2m32s
`},
{"valid short", state.Entries{testID: testState}, true, false, "8e2c76b0\n"},
{"valid json", state.Entries{testID: testState}, false, true, `{
{"valid short", map[hst.ID]*hst.State{testID: testState}, true, false, "8e2c76b0\n"},
{"valid json", map[hst.ID]*hst.State{testID: testState}, false, true, `{
"8e2c76b066dabe574cf073bdb46eb5c1": {
"instance": [
142,
44,
118,
176,
102,
218,
190,
87,
76,
240,
115,
189,
180,
110,
181,
193
],
"instance": "8e2c76b066dabe574cf073bdb46eb5c1",
"pid": 3735928559,
"config": {
"id": "org.chromium.Chromium",
@@ -721,7 +687,7 @@ func TestPrintPs(t *testing.T) {
}
}
`},
{"valid short json", state.Entries{testID: testState}, true, true, `["8e2c76b066dabe574cf073bdb46eb5c1"]
{"valid short json", map[hst.ID]*hst.State{testID: testState}, true, true, `["8e2c76b066dabe574cf073bdb46eb5c1"]
`},
}
@@ -741,9 +707,9 @@ func TestPrintPs(t *testing.T) {
}
// stubStore implements [state.Store] and returns test samples via [state.Joiner].
type stubStore state.Entries
type stubStore map[hst.ID]*hst.State
func (s stubStore) Join() (state.Entries, error) { return state.Entries(s), nil }
func (s stubStore) Join() (map[hst.ID]*hst.State, error) { return s, nil }
func (s stubStore) Do(int, func(c state.Cursor)) (bool, error) { panic("unreachable") }
func (s stubStore) List() ([]int, error) { panic("unreachable") }
func (s stubStore) Close() error { return nil }