testkit

package module
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package testkit provides test helpers for testing lakta modules and runtimes. Intended for both framework-internal tests and framework consumers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get added in v0.4.0

func Get[T any](s *Slice) T

Get invokes T from the slice injector or calls t.Fatal. Free fn.

func WaitForAddr

func WaitForAddr(t *testing.T, m addrProvider) net.Addr

WaitForAddr polls until m.Addr() returns a non-nil value or times out after 2 seconds. Used in server module tests to wait for the listener to be bound before making connections.

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

func NewHarness(t *testing.T) *Harness

NewHarness creates a new test harness with an injector-backed context.

func WithProvider

func WithProvider[T any](h *Harness, provider func(do.Injector) (T, error)) *Harness

WithProvider registers an arbitrary DI provider in the harness.

func (*Harness) Ctx

func (h *Harness) Ctx() context.Context

Ctx returns the context with the injector embedded.

func (*Harness) Injector

func (h *Harness) Injector() do.Injector

Injector returns the DI injector.

func (*Harness) Notifier

func (h *Harness) Notifier() *ReloadNotifier

Notifier returns the shared reload notifier.

func (*Harness) WithData

func (h *Harness) WithData(data map[string]any) *Harness

WithData loads a map into koanf and provides *koanf.Koanf and config.ReloadNotifier in DI.

func (*Harness) WithKoanf

func (h *Harness) WithKoanf(k *koanf.Koanf) *Harness

WithKoanf provides a pre-built koanf and config.ReloadNotifier in DI.

type MapProvider

type MapProvider map[string]any

MapProvider implements koanf.Provider for loading configuration from a map.

func (MapProvider) Read

func (m MapProvider) Read() (map[string]any, error)

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.

func NewMockModule

func NewMockModule() *MockModule

NewMockModule creates a new MockModule.

func (*MockModule) Init

func (m *MockModule) Init(ctx context.Context) error

func (*MockModule) Shutdown

func (m *MockModule) Shutdown(ctx context.Context) error

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.

func (*MockSyncModule) Start

func (m *MockSyncModule) Start(ctx context.Context) error

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

func Mock[T any](s *Slice, v T) *Slice

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 NewSlice added in v0.4.0

func NewSlice(t *testing.T, modules ...lakta.Module) *Slice

NewSlice creates a Slice for the given modules under test.

func SliceProvide added in v0.4.0

func SliceProvide[T any](s *Slice, provider func(do.Injector) (T, error)) *Slice

SliceProvide registers an arbitrary provider into the slice injector (mirrors testkit.WithProvider). Free fn: Go has no generic methods.

func (*Slice) Ctx added in v0.4.0

func (s *Slice) Ctx() context.Context

Ctx returns the context carrying the slice injector.

func (*Slice) Injector added in v0.4.0

func (s *Slice) Injector() do.Injector

Injector returns the slice DI injector.

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

func (s *Slice) Provided() []string

Provided wraps injector.ListProvidedServices(), returning service names.

func (*Slice) Shutdown added in v0.4.0

func (s *Slice) Shutdown() error

Shutdown delegates to the underlying RuntimeHarness.Shutdown.

func (*Slice) Start added in v0.4.0

func (s *Slice) Start() *Slice

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

func (s *Slice) Validate() error

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) With added in v0.4.0

func (s *Slice) With(modules ...lakta.Module) *Slice

With appends more modules under test.

func (*Slice) WithConfig added in v0.4.0

func (s *Slice) WithConfig(data map[string]any) *Slice

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

func (s *Slice) WithTestLogger() *Slice

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().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL