Formerly, we introduced `wazero.Namespace` to help avoid module name or import conflicts while still sharing the runtime's compilation cache. Now that we've introduced `CompilationCache` `wazero.Namespace` is no longer necessary. By removing it, we reduce the conceptual load on end users as well internal complexity. Since most users don't use namespace, the change isn't very impactful.
Users who are only trying to avoid module name conflict can generate a name like below instead of using multiple runtimes:
```go
moduleName := fmt.Sprintf("%d", atomic.AddUint64(&m.instanceCounter, 1))
module, err := runtime.InstantiateModule(ctx, compiled, config.WithName(moduleName))
```
For `HostModuleBuilder` users, we no longer take `Namespace` as the last parameter of `Instantiate` method:
```diff
// log to the console.
_, err := r.NewHostModuleBuilder("env").
NewFunctionBuilder().WithFunc(logString).Export("log").
- Instantiate(ctx, r)
+ Instantiate(ctx)
if err != nil {
log.Panicln(err)
}
```
The following is an example diff a use of namespace can use to keep compilation cache while also ensuring their modules don't conflict:
```diff
func useMultipleRuntimes(ctx context.Context, cache) {
- r := wazero.NewRuntime(ctx)
+ cache := wazero.NewCompilationCache()
for i := 0; i < N; i++ {
- // Create a new namespace to instantiate modules into.
- ns := r.NewNamespace(ctx) // Note: this is closed when the Runtime is
+ r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().WithCompilationCache(cache))
// Instantiate a new "env" module which exports a stateful function.
_, err := r.NewHostModuleBuilder("env").
```
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
288 lines
7.8 KiB
Go
288 lines
7.8 KiB
Go
package wasm
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/tetratelabs/wazero/internal/sys"
|
|
testfs "github.com/tetratelabs/wazero/internal/testing/fs"
|
|
"github.com/tetratelabs/wazero/internal/testing/require"
|
|
)
|
|
|
|
func TestCallContext_WithMemory(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
mod *CallContext
|
|
mem *MemoryInstance
|
|
expectSame bool
|
|
}{
|
|
{
|
|
name: "nil->nil: same",
|
|
mod: &CallContext{},
|
|
mem: nil,
|
|
expectSame: true,
|
|
},
|
|
{
|
|
name: "nil->mem: not same",
|
|
mod: &CallContext{},
|
|
mem: &MemoryInstance{},
|
|
expectSame: false,
|
|
},
|
|
{
|
|
name: "mem->nil: same",
|
|
mod: &CallContext{memory: &MemoryInstance{}},
|
|
mem: nil,
|
|
expectSame: true,
|
|
},
|
|
{
|
|
name: "mem1->mem2: not same",
|
|
mod: &CallContext{memory: &MemoryInstance{}},
|
|
mem: &MemoryInstance{},
|
|
expectSame: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tc := tt
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
mod2 := tc.mod.WithMemory(tc.mem)
|
|
if tc.expectSame {
|
|
require.Same(t, tc.mod, mod2)
|
|
} else {
|
|
require.NotSame(t, tc.mod, mod2)
|
|
require.Equal(t, tc.mem, mod2.memory)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCallContext_String(t *testing.T) {
|
|
s := newStore()
|
|
|
|
tests := []struct {
|
|
name, moduleName, expected string
|
|
}{
|
|
{
|
|
name: "empty",
|
|
moduleName: "",
|
|
expected: "Module[]",
|
|
},
|
|
{
|
|
name: "not empty",
|
|
moduleName: "math",
|
|
expected: "Module[math]",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tc := tt
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
// Ensure paths that can create the host module can see the name.
|
|
m, err := s.Instantiate(context.Background(), &Module{}, tc.moduleName, nil)
|
|
defer m.Close(testCtx) //nolint
|
|
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.expected, m.String())
|
|
require.Equal(t, tc.expected, s.Module(m.Name()).String())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCallContext_Close(t *testing.T) {
|
|
s := newStore()
|
|
|
|
tests := []struct {
|
|
name string
|
|
closer func(context.Context, *CallContext) error
|
|
expectedClosed uint64
|
|
}{
|
|
{
|
|
name: "Close()",
|
|
closer: func(ctx context.Context, callContext *CallContext) error {
|
|
return callContext.Close(ctx)
|
|
},
|
|
expectedClosed: uint64(1),
|
|
},
|
|
{
|
|
name: "CloseWithExitCode(255)",
|
|
closer: func(ctx context.Context, callContext *CallContext) error {
|
|
return callContext.CloseWithExitCode(ctx, 255)
|
|
},
|
|
expectedClosed: uint64(255)<<32 + 1,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tc := tt
|
|
t.Run(fmt.Sprintf("%s calls ns.CloseWithExitCode(module.name))", tc.name), func(t *testing.T) {
|
|
for _, ctx := range []context.Context{nil, testCtx} { // Ensure it doesn't crash on nil!
|
|
moduleName := t.Name()
|
|
m, err := s.Instantiate(ctx, &Module{}, moduleName, nil)
|
|
require.NoError(t, err)
|
|
|
|
// We use side effects to see if Close called ns.CloseWithExitCode (without repeating store_test.go).
|
|
// One side effect of ns.CloseWithExitCode is that the moduleName can no longer be looked up.
|
|
require.Equal(t, s.Module(moduleName), m)
|
|
|
|
// Closing should not err.
|
|
require.NoError(t, tc.closer(ctx, m))
|
|
|
|
require.Equal(t, tc.expectedClosed, *m.closed)
|
|
|
|
// Verify our intended side-effect
|
|
require.Nil(t, s.Module(moduleName))
|
|
|
|
// Verify no error closing again.
|
|
require.NoError(t, tc.closer(ctx, m))
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("calls Context.Close()", func(t *testing.T) {
|
|
sysCtx := sys.DefaultContext(testfs.FS{"foo": &testfs.File{}})
|
|
fsCtx := sysCtx.FS()
|
|
|
|
_, err := fsCtx.OpenFile("/foo", os.O_RDONLY, 0)
|
|
require.NoError(t, err)
|
|
|
|
m, err := s.Instantiate(context.Background(), &Module{}, t.Name(), sysCtx)
|
|
require.NoError(t, err)
|
|
|
|
// We use side effects to determine if Close in fact called Context.Close (without repeating sys_test.go).
|
|
// One side effect of Context.Close is that it clears the openedFiles map. Verify our base case.
|
|
_, ok := fsCtx.LookupFile(3)
|
|
require.True(t, ok, "sysCtx.openedFiles was empty")
|
|
|
|
// Closing should not err.
|
|
require.NoError(t, m.Close(testCtx))
|
|
|
|
// Verify our intended side-effect
|
|
_, ok = fsCtx.LookupFile(3)
|
|
require.False(t, ok, "expected no opened files")
|
|
|
|
// Verify no error closing again.
|
|
require.NoError(t, m.Close(testCtx))
|
|
})
|
|
|
|
t.Run("error closing", func(t *testing.T) {
|
|
// Right now, the only way to err closing the sys context is if a File.Close erred.
|
|
testFS := testfs.FS{"foo": &testfs.File{CloseErr: errors.New("error closing")}}
|
|
sysCtx := sys.DefaultContext(testFS)
|
|
fsCtx := sysCtx.FS()
|
|
|
|
_, err := fsCtx.OpenFile("/foo", os.O_RDONLY, 0)
|
|
require.NoError(t, err)
|
|
|
|
m, err := s.Instantiate(context.Background(), &Module{}, t.Name(), sysCtx)
|
|
require.NoError(t, err)
|
|
|
|
require.EqualError(t, m.Close(testCtx), "error closing")
|
|
|
|
// Verify our intended side-effect
|
|
_, ok := fsCtx.LookupFile(3)
|
|
require.False(t, ok, "expected no opened files")
|
|
})
|
|
}
|
|
|
|
func TestCallContext_CallDynamic(t *testing.T) {
|
|
s := newStore()
|
|
|
|
tests := []struct {
|
|
name string
|
|
closer func(context.Context, *CallContext) error
|
|
expectedClosed uint64
|
|
}{
|
|
{
|
|
name: "Close()",
|
|
closer: func(ctx context.Context, callContext *CallContext) error {
|
|
return callContext.Close(ctx)
|
|
},
|
|
expectedClosed: uint64(1),
|
|
},
|
|
{
|
|
name: "CloseWithExitCode(255)",
|
|
closer: func(ctx context.Context, callContext *CallContext) error {
|
|
return callContext.CloseWithExitCode(ctx, 255)
|
|
},
|
|
expectedClosed: uint64(255)<<32 + 1,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tc := tt
|
|
t.Run(fmt.Sprintf("%s calls ns.CloseWithExitCode(module.name))", tc.name), func(t *testing.T) {
|
|
for _, ctx := range []context.Context{nil, testCtx} { // Ensure it doesn't crash on nil!
|
|
moduleName := t.Name()
|
|
m, err := s.Instantiate(ctx, &Module{}, moduleName, nil)
|
|
require.NoError(t, err)
|
|
|
|
// We use side effects to see if Close called ns.CloseWithExitCode (without repeating store_test.go).
|
|
// One side effect of ns.CloseWithExitCode is that the moduleName can no longer be looked up.
|
|
require.Equal(t, s.Module(moduleName), m)
|
|
|
|
// Closing should not err.
|
|
require.NoError(t, tc.closer(ctx, m))
|
|
|
|
require.Equal(t, tc.expectedClosed, *m.closed)
|
|
|
|
// Verify our intended side-effect
|
|
require.Nil(t, s.Module(moduleName))
|
|
|
|
// Verify no error closing again.
|
|
require.NoError(t, tc.closer(ctx, m))
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("calls Context.Close()", func(t *testing.T) {
|
|
sysCtx := sys.DefaultContext(testfs.FS{"foo": &testfs.File{}})
|
|
fsCtx := sysCtx.FS()
|
|
|
|
_, err := fsCtx.OpenFile("/foo", os.O_RDONLY, 0)
|
|
require.NoError(t, err)
|
|
|
|
m, err := s.Instantiate(context.Background(), &Module{}, t.Name(), sysCtx)
|
|
require.NoError(t, err)
|
|
|
|
// We use side effects to determine if Close in fact called Context.Close (without repeating sys_test.go).
|
|
// One side effect of Context.Close is that it clears the openedFiles map. Verify our base case.
|
|
_, ok := fsCtx.LookupFile(3)
|
|
require.True(t, ok, "sysCtx.openedFiles was empty")
|
|
|
|
// Closing should not err.
|
|
require.NoError(t, m.Close(testCtx))
|
|
|
|
// Verify our intended side-effect
|
|
_, ok = fsCtx.LookupFile(3)
|
|
require.False(t, ok, "expected no opened files")
|
|
|
|
// Verify no error closing again.
|
|
require.NoError(t, m.Close(testCtx))
|
|
})
|
|
|
|
t.Run("error closing", func(t *testing.T) {
|
|
// Right now, the only way to err closing the sys context is if a File.Close erred.
|
|
testFS := testfs.FS{"foo": &testfs.File{CloseErr: errors.New("error closing")}}
|
|
sysCtx := sys.DefaultContext(testFS)
|
|
fsCtx := sysCtx.FS()
|
|
|
|
path := "/foo"
|
|
_, err := fsCtx.OpenFile(path, os.O_RDONLY, 0)
|
|
require.NoError(t, err)
|
|
|
|
m, err := s.Instantiate(context.Background(), &Module{}, t.Name(), sysCtx)
|
|
require.NoError(t, err)
|
|
|
|
require.EqualError(t, m.Close(testCtx), "error closing")
|
|
|
|
// Verify our intended side-effect
|
|
_, ok := fsCtx.LookupFile(3)
|
|
require.False(t, ok, "expected no opened files")
|
|
})
|
|
}
|