memory

package
v0.14.7 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package memory holds the in-memory adapters: a full implementation used by the test suite, and a template for custom adapters.

Index

Constants

View Source
const PublishedKept = 10_000

PublishedKept is how many of the latest published events a memory Bus keeps for Published: enough for any test, bounded for a dev server that runs for days.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus struct {
	// contains filtered or unexported fields
}

Bus is a synchronous in-memory bus. Publishes are delivered to subscribers in order, on the publisher's goroutine.

func NewBus

func NewBus() *Bus

NewBus makes an empty Bus.

func (*Bus) Publish

func (b *Bus) Publish(_ context.Context, threadID string, event ports.AgentEvent) error

func (*Bus) Published

func (b *Bus) Published() []ports.AgentEvent

Published is the latest PublishedKept events published, in order.

func (*Bus) Subscribe

func (b *Bus) Subscribe(_ context.Context, threadID string, handler func(ports.AgentEvent)) (func() error, error)

func (*Bus) Subscribers

func (b *Bus) Subscribers(threadID string) int

Subscribers counts live subscriptions on a thread, so a test can prove a stream cleaned up after itself.

type Fetcher added in v0.5.0

type Fetcher struct {
	// contains filtered or unexported fields
}

Fetcher is a page reader for tests: it serves pages from a map by URL, and keeps every URL it was asked for. An unknown URL fails like a 404.

func NewFetcher added in v0.5.0

func NewFetcher(pages map[string]Page, name string) *Fetcher

NewFetcher serves pages.

func (*Fetcher) Fetch added in v0.5.0

func (*Fetcher) Fetched added in v0.5.0

func (f *Fetcher) Fetched() []string

Fetched are the URLs asked for so far.

func (*Fetcher) Name added in v0.5.0

func (f *Fetcher) Name() string

type Kv

type Kv struct {
	// contains filtered or unexported fields
}

Kv is an in-memory Kv for tests and local prototyping. Expiry is enforced lazily on read; Incr holds the lock, so concurrent callers can never collide on the same counter (§3.4).

func NewKv

func NewKv() *Kv

NewKv makes an empty Kv.

func (*Kv) Del

func (k *Kv) Del(_ context.Context, key string) error

func (*Kv) DelIfValue added in v0.3.0

func (k *Kv) DelIfValue(_ context.Context, key, expected string) (bool, error)

func (*Kv) Get

func (k *Kv) Get(_ context.Context, key string) (string, bool, error)

func (*Kv) Incr

func (k *Kv) Incr(_ context.Context, key string) (int64, error)

func (*Kv) IncrWithExpiry added in v0.3.0

func (k *Kv) IncrWithExpiry(_ context.Context, key string, ttl time.Duration) (int64, error)

func (*Kv) Set

func (k *Kv) Set(_ context.Context, key, value string, opts ports.SetOptions) (bool, error)

func (*Kv) SetIfValue added in v0.3.0

func (k *Kv) SetIfValue(_ context.Context, key, expected, value string, ttl time.Duration) (bool, error)

type Page added in v0.5.0

type Page struct {
	Title   string
	Content string
}

Page is one page a memory Fetcher serves.

type Queue

type Queue struct {

	// MaxDepth refuses a fresh dispatch past this many waiting jobs; zero is
	// unbounded.
	MaxDepth int
	// contains filtered or unexported fields
}

Queue is an in-memory queue with a Drain helper: tests process jobs exactly like the worker would. It honours the port's keys (a duplicate key is refused, Cancel drops it) and an optional depth cap, so the engine's behaviour around both can be tested without a database.

func NewQueue

func NewQueue() *Queue

NewQueue makes an empty Queue.

func (*Queue) Cancel added in v0.3.0

func (q *Queue) Cancel(_ context.Context, key string) error

Cancel drops every waiting job under key.

func (*Queue) Delays

func (q *Queue) Delays() []time.Duration

Delays are the delivery delays requested per enqueue, index-aligned with Items.

func (*Queue) Drain

func (q *Queue) Drain(ctx context.Context, handler func(ctx context.Context, job ports.RunJob) error) (int, error)

Drain hands every waiting job to the handler, in order, including jobs the handler enqueues. Returns how many were handled.

func (*Queue) Enqueue

func (q *Queue) Enqueue(_ context.Context, job ports.RunJob, opts *ports.EnqueueOptions) error

func (*Queue) Find added in v0.3.0

func (q *Queue) Find(_ context.Context, runID string) (*ports.QueuedJob, error)

Find returns the earliest waiting job for a run, or nil.

func (*Queue) Items

func (q *Queue) Items() []ports.RunJob

Items are the jobs waiting, oldest first.

func (*Queue) Keys added in v0.3.0

func (q *Queue) Keys() []string

Keys are the dedupe keys per enqueue, index-aligned with Items.

func (*Queue) Len

func (q *Queue) Len() int

Len is how many jobs wait.

func (*Queue) Priorities added in v0.3.0

func (q *Queue) Priorities() []int

Priorities are the priorities per enqueue, index-aligned with Items.

func (*Queue) Shift

func (q *Queue) Shift() (ports.RunJob, bool)

Shift takes the oldest job, or false.

func (*Queue) Stats added in v0.3.0

func (q *Queue) Stats(_ context.Context) (ports.QueueStats, error)

Stats counts what waits.

type RunStreams added in v0.4.0

type RunStreams struct {
	// contains filtered or unexported fields
}

RunStreams is an in-memory RunStreams for tests and local prototyping. The offset is the item's 1-based position. Expiry is checked on every call, and an Open drops every expired stream, so a dev server that runs for days holds only live and recent streams.

func NewRunStreams added in v0.4.0

func NewRunStreams() *RunStreams

NewRunStreams makes an empty RunStreams.

func (*RunStreams) Append added in v0.4.0

func (r *RunStreams) Append(_ context.Context, streamID string, events []ports.StreamEvent) ([]string, error)

func (*RunStreams) Close added in v0.4.0

func (r *RunStreams) Close(_ context.Context, streamID string, end ports.StreamEnd, grace time.Duration) error

func (*RunStreams) Delete added in v0.4.0

func (r *RunStreams) Delete(_ context.Context, streamID string) error

func (*RunStreams) Len added in v0.4.0

func (r *RunStreams) Len() int

Len is how many streams are held now, expired ones included until the next sweep. Lets a test prove a stream was deleted.

func (*RunStreams) Open added in v0.4.0

func (r *RunStreams) Open(_ context.Context, streamID string, meta ports.StreamMeta, ttl time.Duration) error

func (*RunStreams) Read added in v0.4.0

func (r *RunStreams) Read(ctx context.Context, streamID, after string) iter.Seq2[ports.StreamItem, error]

func (*RunStreams) Snapshot added in v0.4.0

func (r *RunStreams) Snapshot(_ context.Context, streamID, after string) (*ports.StreamSnapshot, error)

type Sandbox added in v0.5.0

type Sandbox struct {
	// contains filtered or unexported fields
}

Sandbox makes sandboxes that live in memory, for tests: files are kept, commands go to the run function you give (nil fails every command with 127). It records what was made, pushed back and destroyed, and End makes one go away as a timed-out sandbox would.

func NewSandbox added in v0.5.0

func NewSandbox(run SandboxRun, name string) *Sandbox

NewSandbox returns a memory sandbox provider. Name defaults to "memory".

func (*Sandbox) Commands added in v0.5.0

func (s *Sandbox) Commands() []SandboxCommand

Commands is every command run, in order.

func (*Sandbox) Connect added in v0.5.0

func (s *Sandbox) Connect(_ context.Context, id string) (ports.Sandbox, error)

Connect implements ports.SandboxProvider.

func (*Sandbox) Create added in v0.5.0

Create implements ports.SandboxProvider.

func (*Sandbox) Created added in v0.5.0

func (s *Sandbox) Created() []ports.CreateSandboxOptions

Created is every Create call's options, in order.

func (*Sandbox) Destroyed added in v0.5.0

func (s *Sandbox) Destroyed() []string

Destroyed is the ids destroyed, in order.

func (*Sandbox) End added in v0.5.0

func (s *Sandbox) End(id string)

End makes the sandbox go away on its own, as one past its time does.

func (*Sandbox) Live added in v0.5.0

func (s *Sandbox) Live() []string

Live is the ids of the sandboxes still there.

func (*Sandbox) Name added in v0.5.0

func (s *Sandbox) Name() string

Name implements ports.SandboxProvider.

func (*Sandbox) Timeouts added in v0.5.0

func (s *Sandbox) Timeouts() []SandboxTimeout

Timeouts is every SetTimeout call, in order.

type SandboxCommand added in v0.5.0

type SandboxCommand struct {
	SandboxID string
	Command   string
	Options   ports.RunCommandOptions
}

SandboxCommand is one command a memory Sandbox was asked to run.

type SandboxRun added in v0.5.0

type SandboxRun func(command string, opts ports.RunCommandOptions, sandbox ports.Sandbox) (ports.CommandResult, error)

SandboxRun answers one command for a memory Sandbox.

type SandboxTimeout added in v0.5.0

type SandboxTimeout struct {
	SandboxID string
	Timeout   time.Duration
}

SandboxTimeout is one SetTimeout call a memory Sandbox saw.

type Search struct {
	// contains filtered or unexported fields
}

Search is a search engine for tests: it answers from a fixed list, or from a function of the query, and keeps every query it was asked.

func NewSearch added in v0.5.0

func NewSearch(hits []ports.SearchHit, name string) *Search

NewSearch answers every query with hits.

func NewSearchFunc added in v0.5.0

func NewSearchFunc(fn func(query string, opts ports.SearchOptions) ([]ports.SearchHit, error), name string) *Search

NewSearchFunc answers with fn.

func (*Search) Name added in v0.5.0

func (s *Search) Name() string

func (*Search) Queries added in v0.5.0

func (s *Search) Queries() []SearchQuery

Queries are the queries asked so far.

func (*Search) Search added in v0.5.0

func (s *Search) Search(_ context.Context, query string, opts ports.SearchOptions) ([]ports.SearchHit, error)

type SearchQuery added in v0.5.0

type SearchQuery struct {
	Query   string
	Options ports.SearchOptions
}

SearchQuery is one query a memory Search was asked.

type Storage

type Storage struct {

	// LastContext is the StorageContext of the most recent call, so a test
	// can prove the run state reached the adapter (§2.10).
	LastContext ports.StorageContext
	// Contexts records every StorageContext seen, in order.
	Contexts []ports.StorageContext
	// contains filtered or unexported fields
}

Storage is a full in-memory Storage: tests, demos, and a template for custom adapters.

func NewStorage

func NewStorage() *Storage

NewStorage makes an empty Storage.

func (*Storage) Events

func (s *Storage) Events() ports.EventStore

func (*Storage) MessageRows

func (s *Storage) MessageRows(threadID string) []ports.MessageDTO

MessageRows is the raw message list for a thread, for tests.

func (*Storage) Messages

func (s *Storage) Messages() ports.MessageStore

func (*Storage) Threads

func (s *Storage) Threads() ports.ThreadStore

func (*Storage) Usage

func (s *Storage) Usage() ports.UsageStore

func (*Storage) UsageRows

func (s *Storage) UsageRows() []struct {
	ThreadID string
	ports.NewUsage
}

UsageRows is every recorded usage segment, for tests.

Jump to

Keyboard shortcuts

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