fix(lns): make lifecycle hooks nil-safe

This commit is contained in:
Virgil 2026-04-04 06:39:40 +00:00
parent c7438413ae
commit 28d8907c17
2 changed files with 23 additions and 3 deletions

10
lns.go
View file

@ -123,6 +123,10 @@ var (
} = (*Service)(nil)
)
func okResult() core.Result {
return core.Result{OK: true}
}
// serviceOptions holds configuration for the LNS service.
type serviceOptions struct{}
@ -510,13 +514,13 @@ func (s *Service) GetDecodeResource(raw []byte) (*Resource, error) {
// OnStartup satisfies core.Startable so LNS participates in Core lifecycle
// discovery without requiring any explicit setup.
func (s *Service) OnStartup(context.Context) core.Result {
return core.Result{OK: true}
return okResult()
}
// OnShutdown satisfies core.Stoppable so LNS participates in Core lifecycle
// discovery without requiring any explicit teardown.
func (s *Service) OnShutdown(context.Context) core.Result {
return core.Result{OK: true}
return okResult()
}
// HandleIPCEvents satisfies Core's IPC handler discovery.
@ -524,7 +528,7 @@ func (s *Service) OnShutdown(context.Context) core.Result {
// core.New(core.WithService(lns.Register))
// // LNS registers a no-op IPC handler so it can join the bus cleanly.
func (s *Service) HandleIPCEvents(*core.Core, core.Message) core.Result {
return core.Result{OK: true}
return okResult()
}
func canonicalizeName(name any) (string, bool) {

View file

@ -25,6 +25,16 @@ func TestServiceLifecycleHooks(t *testing.T) {
if got := svc.OnShutdown(nil); !got.OK {
t.Fatalf("OnShutdown returned %#v, want OK", got)
}
var nilSvc *Service
if got := nilSvc.OnStartup(nil); !got.OK {
t.Fatalf("nil OnStartup returned %#v, want OK", got)
}
if got := nilSvc.OnShutdown(nil); !got.OK {
t.Fatalf("nil OnShutdown returned %#v, want OK", got)
}
}
func TestServiceHandleIPCEvents(t *testing.T) {
@ -33,6 +43,12 @@ func TestServiceHandleIPCEvents(t *testing.T) {
if got := svc.HandleIPCEvents(nil, nil); !got.OK {
t.Fatalf("HandleIPCEvents returned %#v, want OK", got)
}
var nilSvc *Service
if got := nilSvc.HandleIPCEvents(nil, nil); !got.OK {
t.Fatalf("nil HandleIPCEvents returned %#v, want OK", got)
}
}
func TestServiceResolve(t *testing.T) {