Files
dev/go/module_test.go
7146aad392 go/frame: Perform names the thread and the action, after its snapshot is saved (#70)
A session layer could not claim an adapter's workspace action in its ledger,
because Perform was handed the Op alone: no id to claim it by, and no thread to
say whose loop gave the id. Each thread runs its own loop, so an id is unique
within a thread and not across them.

Perform now takes the thread and the dev.Action. Before the first action of a
step that touches a workspace, the thread's snapshot is saved, so a thread
restored from it is the loop waiting on that id and never mints the id again
for another action (dev.h's warning on dev_restore). A ledger that answers a
claimed id from its record then answers the action the id was claimed for.

module_test: dev.wasm exports dev_decide since bf85a70, so the export list has
ten calls.

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-11-22 05:26:24 -05:00

155 lines
4.6 KiB
Go

package dev
import (
"errors"
"fmt"
"maps"
"slices"
"strings"
"testing"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
// compiled is the embedded module as wazero reads it, for the tests that look
// at its shape rather than run it.
func compiled(t *testing.T) wazero.CompiledModule {
t.Helper()
rt := wazero.NewRuntime(t.Context())
t.Cleanup(func() { _ = rt.Close(t.Context()) })
mod, err := rt.CompileModule(t.Context(), module)
if err != nil {
t.Fatal(err)
}
return mod
}
// The import list is the proof that the core touches nothing: no file, no
// socket, no clock, no randomness. A module that grows it has given the loop
// an effect of its own.
func TestTheModuleImportsFourWASICallsAndNothingElse(t *testing.T) {
mod := compiled(t)
var imports []string
for _, f := range mod.ImportedFunctions() {
module, name, _ := f.Import()
imports = append(imports, module+"."+name)
}
slices.Sort(imports)
want := []string{
"wasi_snapshot_preview1.environ_get",
"wasi_snapshot_preview1.environ_sizes_get",
"wasi_snapshot_preview1.fd_write",
"wasi_snapshot_preview1.proc_exit",
}
if !slices.Equal(imports, want) {
t.Errorf("imports\n got %v\nwant %v", imports, want)
}
if n := len(mod.ImportedMemories()); n != 0 {
t.Errorf("the module imports %d memories", n)
}
}
// The exports are dev.h, lowered to wasm32. This package calls them by
// position, so a signature that moved is a call that reads the wrong argument.
func TestTheModuleExportsMemoryAndTheTenCalls(t *testing.T) {
mod := compiled(t)
want := map[string]string{
"dev_abi": "() (i32)",
"dev_new": "(i32 i32 i32) (i32)",
"dev_step": "(i64 i32 i32 i32) (i32)",
"dev_snapshot": "(i64 i32) (i32)",
"dev_restore": "(i32 i32 i32) (i32)",
"dev_decide": "(i32 i32 i32 i32 i32) (i32)",
"dev_drop": "(i64) ()",
// dev_free(dev_buf buf) takes twelve bytes by value, and the wasm32 C
// ABI passes a struct that size as a pointer to a copy of it.
"dev_free": "(i32) ()",
"dev_alloc": "(i32) (i32)",
"dev_release": "(i32 i32) ()",
}
got := map[string]string{}
for name, f := range mod.ExportedFunctions() {
got[name] = signature(f)
}
if !maps.Equal(got, want) {
t.Errorf("exported functions\n got %v\nwant %v", got, want)
}
memories := slices.Collect(maps.Keys(mod.ExportedMemories()))
if !slices.Equal(memories, []string{"memory"}) {
t.Errorf("exported memories %v, want [memory]", memories)
}
}
func signature(f api.FunctionDefinition) string {
names := func(types []api.ValueType) string {
var out []string
for _, t := range types {
out = append(out, api.ValueTypeName(t))
}
return "(" + strings.Join(out, " ") + ")"
}
return names(f.ParamTypes()) + " " + names(f.ResultTypes())
}
func TestTheModuleSpeaksThisPackagesABI(t *testing.T) {
c := core(t)
inst, err := c.module.Start(t.Context())
if err != nil {
t.Fatal(err)
}
defer inst.Close(t.Context())
out, err := inst.Call(t.Context(), "dev_abi")
if err != nil {
t.Fatal(err)
}
if abi := uint32(out[0]); abi != ABI {
t.Errorf("dev_abi() = %d, want %d", abi, ABI)
}
}
// A module that answers dev_abi with another version is refused at Open, not
// discovered one malformed step at a time.
func TestOpenRefusesAnotherABI(t *testing.T) {
for _, abi := range []byte{0, 2} {
// (module (func (export "dev_abi") (result i32) i32.const <abi>))
foreign := []byte{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x01, 0x05, 0x01, 0x60, 0x00, 0x01, 0x7f,
0x03, 0x02, 0x01, 0x00,
0x07, 0x0b, 0x01, 0x07, 'd', 'e', 'v', '_', 'a', 'b', 'i', 0x00, 0x00,
0x0a, 0x06, 0x01, 0x04, 0x00, 0x41, abi, 0x0b,
}
c, err := open(t.Context(), foreign)
if err == nil {
_ = c.Close(t.Context())
t.Fatalf("a module speaking ABI %d was opened", abi)
}
if want := fmt.Sprintf("speaks ABI %d", abi); !strings.Contains(err.Error(), want) {
t.Errorf("ABI %d refused for another reason: %v", abi, err)
}
}
}
// Every status dev.h names is an error errors.Is can match, and none is a
// bare number.
func TestEveryStatusIsASentinel(t *testing.T) {
codes := map[int32]error{
-1: ErrHandle, -2: ErrMalformed, -3: ErrPoison, -4: ErrPanic,
-5: ErrNull, -6: ErrBusy, -7: ErrFull,
}
if err := status(0, nil); err != nil {
t.Errorf("DEV_OK is %v", err)
}
for code, want := range codes {
if err := status(uint64(uint32(code)), nil); !errors.Is(err, want) {
t.Errorf("status %d is %v, want %v", code, err, want)
}
}
for _, code := range []int32{1, -8} {
if err := status(uint64(uint32(code)), nil); err == nil {
t.Errorf("status %d is not an error", code)
}
}
}