Documentation
¶
Overview ¶
Package testkit provides test helpers for testing lakta modules and runtimes. Intended for both framework-internal tests and framework consumers.
Index ¶
- func Get[T any](s *Slice) T
- func WaitForAddr(t *testing.T, m addrProvider) net.Addr
- type Harness
- type MapProvider
- type MockAsyncModule
- type MockModule
- type MockProviderModule
- type MockSyncModule
- type ReloadNotifier
- type RuntimeHarness
- type Slice
- func (s *Slice) Ctx() context.Context
- func (s *Slice) Injector() do.Injector
- func (s *Slice) Notifier() *ReloadNotifier
- func (s *Slice) Provided() []string
- func (s *Slice) Shutdown() error
- func (s *Slice) Start() *Slice
- func (s *Slice) Validate() error
- func (s *Slice) With(modules ...lakta.Module) *Slice
- func (s *Slice) WithConfig(data map[string]any) *Slice
- func (s *Slice) WithTestLogger() *Slice
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Harness ¶
type Harness struct {
// contains filtered or unexported fields
}
Harness provides a test context with a DI injector for testing lakta modules.
func NewHarness ¶
NewHarness creates a new test harness with an injector-backed context.
func WithProvider ¶
WithProvider registers an arbitrary DI provider in the harness.
func (*Harness) Notifier ¶
func (h *Harness) Notifier() *ReloadNotifier
Notifier returns the shared reload notifier.
type MapProvider ¶
MapProvider implements koanf.Provider for loading configuration from a map.
func (MapProvider) ReadBytes ¶
func (m MapProvider) ReadBytes() ([]byte, error)
type MockAsyncModule ¶
type MockAsyncModule struct {
MockModule
StartAsyncErr error
StartAsyncCalls atomic.Int32
OnStartAsync func(ctx context.Context) error
}
MockAsyncModule implements lakta.AsyncModule and tracks call counts.
func NewMockAsyncModule ¶
func NewMockAsyncModule() *MockAsyncModule
NewMockAsyncModule creates a new MockAsyncModule.
func (*MockAsyncModule) StartAsync ¶
func (m *MockAsyncModule) StartAsync(ctx context.Context) error
type MockModule ¶
type MockModule struct {
InitErr error
ShutdownErr error
InitCalls atomic.Int32
ShutdownCalls atomic.Int32
OnInit func(ctx context.Context) error
OnShutdown func(ctx context.Context) error
}
MockModule implements lakta.Module and tracks call counts.
type MockProviderModule ¶
type MockProviderModule struct {
MockModule
ProvidesTypes []reflect.Type
RequiredDeps []reflect.Type
OptionalDeps []reflect.Type
}
MockProviderModule implements lakta.Module, lakta.Provider, and lakta.Dependent with configurable type declarations for use in dependency graph tests.
func NewMockProviderModule ¶
func NewMockProviderModule() *MockProviderModule
NewMockProviderModule creates a new MockProviderModule.
func (*MockProviderModule) Dependencies ¶
func (m *MockProviderModule) Dependencies() ([]reflect.Type, []reflect.Type)
func (*MockProviderModule) Provides ¶
func (m *MockProviderModule) Provides() []reflect.Type
type MockSyncModule ¶
type MockSyncModule struct {
MockModule
StartErr error
StartCalls atomic.Int32
OnStart func(ctx context.Context) error
// BlockStart blocks Start until the channel is closed or ctx is cancelled.
BlockStart chan struct{}
}
MockSyncModule implements lakta.SyncModule and tracks call counts.
func NewMockSyncModule ¶
func NewMockSyncModule() *MockSyncModule
NewMockSyncModule creates a new MockSyncModule.
type ReloadNotifier ¶
type ReloadNotifier struct {
// contains filtered or unexported fields
}
ReloadNotifier is a test double for config.ReloadNotifier.
func (*ReloadNotifier) FireReload ¶
func (r *ReloadNotifier) FireReload(k *koanf.Koanf)
FireReload invokes all registered callbacks with the given koanf instance.
func (*ReloadNotifier) OnReload ¶
func (r *ReloadNotifier) OnReload(fn func(*koanf.Koanf))
OnReload implements config.ReloadNotifier.
func (*ReloadNotifier) OnValidate ¶ added in v0.2.0
func (r *ReloadNotifier) OnValidate(_ func(*koanf.Koanf) error)
OnValidate implements config.ReloadNotifier (no-op for the test double).
type RuntimeHarness ¶
type RuntimeHarness struct {
// contains filtered or unexported fields
}
RuntimeHarness starts a Runtime in a goroutine and provides a way to trigger graceful shutdown.
func NewRuntimeHarness ¶
func NewRuntimeHarness(t *testing.T, modules ...lakta.Module) *RuntimeHarness
NewRuntimeHarness creates a Runtime with the given modules and starts it immediately. t.Cleanup ensures Shutdown is called if the test does not call it explicitly.
func (*RuntimeHarness) Err ¶
func (h *RuntimeHarness) Err() error
Err returns the RunContext error without blocking (only valid after Shutdown).
func (*RuntimeHarness) Shutdown ¶
func (h *RuntimeHarness) Shutdown() error
Shutdown waits for RunContext to complete naturally, then cancels the context if it hasn't yet. Safe to call multiple times; only the first call has effect.
type Slice ¶ added in v0.4.0
type Slice struct {
// contains filtered or unexported fields
}
Slice boots a subset of modules with mocked collaborators, surfacing unmet declared dependencies as pre-boot diagnostics instead of a raw topo-sort error inside Init. It owns a pre-seeded injector so Mock/SliceProvide doubles survive into module Init (relies on RunContext adopting a ctx-supplied injector).
func Mock ¶ added in v0.4.0
Mock seeds a double for an unmet dependency of type T. Excluding a module and mocking its output are the same operation (do/v2 Provide panics on duplicates). Free fn: Go has no generic methods.
func SliceProvide ¶ added in v0.4.0
SliceProvide registers an arbitrary provider into the slice injector (mirrors testkit.WithProvider). Free fn: Go has no generic methods.
func (*Slice) Notifier ¶ added in v0.4.0
func (s *Slice) Notifier() *ReloadNotifier
Notifier returns the shared test ReloadNotifier (for FireReload in tests).
func (*Slice) Provided ¶ added in v0.4.0
Provided wraps injector.ListProvidedServices(), returning service names.
func (*Slice) Shutdown ¶ added in v0.4.0
Shutdown delegates to the underlying RuntimeHarness.Shutdown.
func (*Slice) Start ¶ added in v0.4.0
Start calls Validate (t.Fatal on error, before any module Init), then boots the runtime through the ctx-accepting RuntimeHarness entry using the slice's pre-seeded injector context.
func (*Slice) Validate ¶ added in v0.4.0
Validate runs the two pre-boot checks and returns the first failure: unmet declared deps (against every module's Provides() plus seeded mocks), cycles (delegated to Runtime.Validate, ignoring its unmet-dep verdict), and mock/module collisions (a seeded type a module-under-test also Provides()).
func (*Slice) WithConfig ¶ added in v0.4.0
WithConfig builds a koanf from data and registers *koanf.Koanf + config.ReloadNotifier (the testkit *ReloadNotifier) in the slice injector, reusing the MapProvider path from Harness.WithData.
func (*Slice) WithTestLogger ¶ added in v0.4.0
WithTestLogger registers a *slog.Logger bridging records to t.Log, so the runtime's do.Invoke[*slog.Logger] picks it up instead of slog.Default().