tests

package
v0.1.2 Latest Latest
Warning

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

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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SharedMemoryContext added in v0.0.4

func SharedMemoryContext(ctx context.Context, shm *SharedMemory) context.Context

SharedMemoryContext stores shm on ctx for constructor injection.

Types

type Record

type Record struct {
	Key   string
	Value string
}

Record is a rich value type returned to PHP. Its exported fields are read from PHP via property access (`$rec->key`, `$rec->value`), matched case-insensitively.

type SharedMemory added in v0.0.4

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

SharedMemory is a host-owned in-memory key/value and counter store exposed to route PHP scripts. It behaves like request-shared SHM: each request gets a new VM, but all VMs can use the same Go value through `new SharedMemory`.

Example
root, err := fs.Sub(fixturesFS, "fixtures/route")
if err != nil {
	fmt.Println(err)
	return
}

shm := NewSharedMemory()
mux := http.NewServeMux()
_, err = route.NewService(root, mux, route.WithRuntimeFunc(func(rt *runner.Runtime) {
	rt.SetContext(SharedMemoryContext(rt.Context(), shm))
	rt.RegisterConstructor("SharedMemory", NewSharedMemoryBinding)
}))
if err != nil {
	fmt.Println(err)
	return
}

post := httptest.NewRequest(http.MethodPost, "/kv/color", strings.NewReader("value=blue"))
post.Header.Set("Content-Type", "application/x-www-form-urlencoded")
postRR := httptest.NewRecorder()
mux.ServeHTTP(postRR, post)
fmt.Println(postRR.Body.String())

getRR := httptest.NewRecorder()
mux.ServeHTTP(getRR, httptest.NewRequest(http.MethodGet, "/kv/color", nil))
fmt.Println(getRR.Body.String())

statsRR := httptest.NewRecorder()
mux.ServeHTTP(statsRR, httptest.NewRequest(http.MethodGet, "/stats/requests", nil))
fmt.Println(statsRR.Body.String())
Output:
ok
blue
2

func NewSharedMemory added in v0.0.4

func NewSharedMemory() *SharedMemory

NewSharedMemory returns an empty shared-memory store.

func NewSharedMemoryBinding added in v0.0.4

func NewSharedMemoryBinding(ctx context.Context) (*SharedMemory, error)

NewSharedMemoryBinding returns the constructor registered for PHP `new SharedMemory`.

func (*SharedMemory) Count added in v0.0.4

func (m *SharedMemory) Count(_ context.Context, key string) string

Count returns a named counter as a string for simple PHP output checks.

func (*SharedMemory) Get added in v0.0.4

func (m *SharedMemory) Get(_ context.Context, key string) string

Get reads a string value. Missing keys return an empty string.

func (*SharedMemory) Incr added in v0.0.4

func (m *SharedMemory) Incr(_ context.Context, key string) int64

Incr increments and returns a named counter.

func (*SharedMemory) Set added in v0.0.4

func (m *SharedMemory) Set(_ context.Context, key, value string)

Set writes a string value.

type Storage

type Storage interface {
	Set(ctx context.Context, key, value string)
	Get(ctx context.Context, key string) (Record, error)
	All(ctx context.Context) ([]Record, error)
	Len() int64
	Tenant() string
}

Storage is a host-provided key/value capability whose context parameters are supplied automatically when PHP invokes its methods.

Example
ctx := context.WithValue(context.Background(), tenantKey, "acme")
storage, err := NewStorage(ctx)
if err != nil {
	fmt.Println(err)
	return
}

storage.Set(ctx, "color", "blue")
record, err := storage.Get(ctx, "color")
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(storage.Tenant())
fmt.Println(record.Key, record.Value)
fmt.Println(storage.Len())
Output:
acme
color blue
1

func NewFailStorage

func NewFailStorage(ctx context.Context) (Storage, error)

NewFailStorage is a constructor that always fails, used to exercise the thrown-error path of `new`.

func NewStorage

func NewStorage(ctx context.Context) (Storage, error)

NewStorage is the constructor registered for `new Storage`. Its first parameter is a context.Context, filled in automatically by the runner, so PHP calls `new Storage` with no arguments.

Jump to

Keyboard shortcuts

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