Service is now a proper struct with OnStart/OnStop/OnReload lifecycle
functions — not a registry wrapping any. Packages create Service{} with
typed fields, same pattern as Command and Option.
Result drops generics — Value is any. The struct is the container,
Value is the generic. No more Result[T] ceremony.
Service(name, Service{}) to register, Service(name) to get — both
return Result. ServiceFactory returns Result not (any, error).
NewWithFactories/NewRuntime return Result.
232 tests, 77.8% coverage.
Co-Authored-By: Virgil <virgil@lethean.io>
51 lines
1,013 B
Go
51 lines
1,013 B
Go
package core_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
. "forge.lthn.ai/core/go/pkg/core"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestLock_Good(t *testing.T) {
|
|
c := New()
|
|
lock := c.Lock("test")
|
|
assert.NotNil(t, lock)
|
|
assert.NotNil(t, lock.Mu)
|
|
}
|
|
|
|
func TestLock_SameName_Good(t *testing.T) {
|
|
c := New()
|
|
l1 := c.Lock("shared")
|
|
l2 := c.Lock("shared")
|
|
assert.Equal(t, l1, l2)
|
|
}
|
|
|
|
func TestLock_DifferentName_Good(t *testing.T) {
|
|
c := New()
|
|
l1 := c.Lock("a")
|
|
l2 := c.Lock("b")
|
|
assert.NotEqual(t, l1, l2)
|
|
}
|
|
|
|
func TestLockEnable_Good(t *testing.T) {
|
|
c := New()
|
|
c.Service("early", Service{})
|
|
c.LockEnable()
|
|
c.LockApply()
|
|
|
|
r := c.Service("late", Service{})
|
|
assert.False(t, r.OK)
|
|
}
|
|
|
|
func TestStartables_Good(t *testing.T) {
|
|
c := New()
|
|
c.Service("s", Service{OnStart: func() Result { return Result{OK: true} }})
|
|
assert.Len(t, c.Startables(), 1)
|
|
}
|
|
|
|
func TestStoppables_Good(t *testing.T) {
|
|
c := New()
|
|
c.Service("s", Service{OnStop: func() Result { return Result{OK: true} }})
|
|
assert.Len(t, c.Stoppables(), 1)
|
|
}
|