Help us shape the future of Go! Take the 2026 Go Developer Survey

resourcetest

package
v1.9.3 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package resourcetest provides fakes for testing resource-aware code: a deterministic clock, a lease-based reclaimer, and a scripted estimator.

It mirrors artifact/artifacttest, so a test that needs both staging and admission fakes reaches for the same shapes in both packages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

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

Clock is a manually advanced time source for resource.WithClock.

func NewClock

func NewClock(t time.Time) *Clock

NewClock starts a clock at t.

func (*Clock) Advance

func (c *Clock) Advance(d time.Duration)

Advance moves the clock forward.

func (*Clock) Now

func (c *Clock) Now() time.Time

Now returns the current fake time.

type FakeEstimator

type FakeEstimator struct {
	Out   resource.Set
	Err   error
	Calls int
	Last  resource.Request
}

FakeEstimator returns a scripted Set and records what it was asked.

func (*FakeEstimator) Estimate

Estimate returns the scripted result.

type FakeReclaimer

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

FakeReclaimer stands in for a component like the artifact cache: it holds real resource.Manager leases for one key and frees capacity by releasing them, never by crediting a private counter.

This matters because resource.Manager's invariant is used == Σ (held of every live lease); a Reclaimer that does not hold a lease has nothing to release, so under the real contract it can never actually return units to the ledger. A counter-based fake would make Manager.Acquire spin on a predicate that never becomes true once the counter is spent, and a test built on it would hang or pass without exercising reclamation at all. FakeReclaimer is built the way countingReclaimer in resource/manager_test.go is: it acquires the pool it represents as real leases up front and releases whole leases to satisfy Reclaim, so a test asserting against it is exercising the same admission and release path a live reclaimer would.

func NewFakeReclaimer

func NewFakeReclaimer(m resource.Manager, key string, leaseSize int64, count int) (*FakeReclaimer, error)

NewFakeReclaimer builds a reclaimer for key by acquiring count leases of leaseSize units each from m, standing in for count evictable entries (an artifact cache would hold one lease per cached object). The pool Reclaim can free totals count*leaseSize units.

Splitting the pool into count leases rather than one big one is what lets Reclaim satisfy a partial request the way a real evictor does: releasing whole entries until the shortfall is covered, rather than only being able to free everything or nothing.

It returns an error rather than taking a *testing.T, because a construction failure here is a caller setup mistake (asking for more than the manager's capacity), not a test assertion — the caller decides whether that is fatal.

func (*FakeReclaimer) Available

func (r *FakeReclaimer) Available(key string) int64

Available reports how much this reclaimer could still free without blocking: the sum of what its live leases hold. A key it was not built for is never available through it.

func (*FakeReclaimer) Calls

func (r *FakeReclaimer) Calls() int

Calls reports how many times Reclaim was called.

func (*FakeReclaimer) Reclaim

func (r *FakeReclaimer) Reclaim(_ context.Context, key string, need int64) (int64, error)

Reclaim releases whole held leases until at least need units of key have been freed, returning the total actually released. Releasing the lease is what returns the units to the manager; the return value is only the "something changed, re-check" signal Manager.Acquire uses, exactly as resource.Reclaimer documents.

A key this reclaimer was not built for frees nothing: it owns no leases in that key, so there is nothing honest to release.

func (*FakeReclaimer) Refill

func (r *FakeReclaimer) Refill(n int64) error

Refill acquires one more lease of n units from the manager and adds it to the pool Reclaim can release. It fails if the manager has no room to grant it: refill goes through the same admission check any other acquisition does, rather than pretending capacity that is not there.

func (*FakeReclaimer) SetError

func (r *FakeReclaimer) SetError(err error)

SetError makes every subsequent Reclaim fail with err instead of releasing leases.

Jump to

Keyboard shortcuts

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