storetest

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package storetest provides test doubles for store.ObjectStore.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FaultHook

type FaultHook func(key string, callNum int) error

FaultHook decides whether a FaultStore call should fail. key is the object key (List passes its prefix instead), and callNum is the 1-indexed count of calls to that method so far, including the current one. A nil return lets the call proceed to the wrapped store.

func AlwaysFail

func AlwaysFail(err error) FaultHook

AlwaysFail returns a FaultHook that fails every call to that method.

func FailKeys

func FailKeys(err error, keys ...string) FaultHook

FailKeys returns a FaultHook that fails every call whose key is in keys, regardless of call count. For List, "key" is the queried prefix.

func FailNth

func FailNth(n int, err error) FaultHook

FailNth returns a FaultHook that fails only on the nth call (1-indexed) to that method, regardless of key. Use it to fail one call in the middle of a multi-step operation and assert the rest of the operation reacts correctly.

type FaultStore

type FaultStore struct {
	ObjectStore

	FailGet  FaultHook
	FailPut  FaultHook
	FailList FaultHook
	// contains filtered or unexported fields
}

FaultStore wraps an ObjectStore and lets tests inject failures into Get, Put, or List independently of what the backend itself would do — simulating a network blip, a permission error, or a partial outage. Each hook is optional; a nil hook means that method always passes through.

FaultStore is safe for concurrent use.

func (*FaultStore) Get

func (f *FaultStore) Get(ctx context.Context, key string) ([]byte, error)

func (*FaultStore) List

func (f *FaultStore) List(ctx context.Context, prefix string) ([]string, error)

func (*FaultStore) Put

func (f *FaultStore) Put(ctx context.Context, key string, data []byte) error

type ObjectStore

type ObjectStore interface {
	Put(ctx context.Context, key string, data []byte) error
	Get(ctx context.Context, key string) ([]byte, error)
	Exists(ctx context.Context, key string) (bool, error)
	Delete(ctx context.Context, key string) error
	List(ctx context.Context, prefix string) ([]string, error)
	Size(ctx context.Context, key string) (int64, error)
	TotalSize(ctx context.Context) (int64, error)
	Flush(ctx context.Context) error
}

ObjectStore mirrors the method set of store.ObjectStore. It is declared independently, rather than imported, so this package can also be used from pkg/store's own internal (package store) tests without an import cycle — any store.ObjectStore implementation satisfies this interface structurally.

Jump to

Keyboard shortcuts

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