wasi: uses the shared compilation cache in tests (#2149)

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
This commit is contained in:
Takeshi Yoneda
2024-03-12 12:46:09 +09:00
committed by GitHub
parent 9f56676383
commit a05f23b0ff
3 changed files with 46 additions and 16 deletions

View File

@@ -8,6 +8,8 @@ import (
"path"
"testing"
"time"
"github.com/tetratelabs/wazero"
)
func TestMain(m *testing.M) {
@@ -18,6 +20,9 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}
// runtimeCfg is a shared runtime configuration for tests within this package to reduce the compilation time of the binary.
var runtimeCfg = wazero.NewRuntimeConfig().WithCompilationCache(wazero.NewCompilationCache())
// compileWasip1Wasm allows us to generate a binary with runtime.GOOS=wasip1
// and runtime.GOARCH=wasm. This intentionally does so on-demand, because the
// wasm is too big to check in.
@@ -32,8 +37,8 @@ func compileWasip1Wasm() ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
bin := path.Join(workdir, "wasi.wasm")
cmd := exec.CommandContext(ctx, "go", "build", "-o", bin, ".") //nolint:gosec
binPath := path.Join(workdir, "wasi.wasm")
cmd := exec.CommandContext(ctx, "go", "build", "-o", binPath, ".") //nolint:gosec
cmd.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm")
cmd.Dir = "testdata/go"
out, err := cmd.CombinedOutput()
@@ -41,5 +46,14 @@ func compileWasip1Wasm() ([]byte, error) {
return nil, fmt.Errorf("couldn't compile %s: %w", string(out), err)
}
return os.ReadFile(bin) //nolint:gosec
bin, err := os.ReadFile(binPath) //nolint:gosec
if err != nil {
return nil, err
}
// Proactively compile the binary to reduce the test time.
r := wazero.NewRuntimeWithConfig(context.Background(), runtimeCfg)
defer r.Close(context.Background())
_, err = r.CompileModule(context.Background(), bin)
return bin, err
}

View File

@@ -242,13 +242,16 @@ func compileAndRunWithPreStart(t *testing.T, ctx context.Context, config wazero.
// same for console and stderr as sometimes the stack trace is in one or the other.
var consoleBuf bytes.Buffer
r := wazero.NewRuntime(ctx)
r := wazero.NewRuntimeWithConfig(ctx, runtimeCfg)
defer r.Close(ctx)
_, err := wasi_snapshot_preview1.Instantiate(ctx, r)
require.NoError(t, err)
mod, err := r.InstantiateWithConfig(ctx, bin, config.
compiled, err := r.CompileModule(ctx, bin)
require.NoError(t, err)
mod, err := r.InstantiateModule(ctx, compiled, config.
WithStdout(&consoleBuf).
WithStderr(&consoleBuf).
WithStartFunctions()) // clear
@@ -515,12 +518,18 @@ func testStdin(t *testing.T, bin []byte) {
ch := make(chan struct{}, 1)
go func() {
defer close(ch)
r := wazero.NewRuntimeWithConfig(testCtx, runtimeCfg)
defer func() {
require.NoError(t, r.Close(testCtx))
}()
r := wazero.NewRuntime(testCtx)
defer r.Close(testCtx)
_, err := wasi_snapshot_preview1.Instantiate(testCtx, r)
require.NoError(t, err)
_, err = r.InstantiateWithConfig(testCtx, bin, moduleConfig)
compiled, err := r.CompileModule(testCtx, wasmGo)
require.NoError(t, err)
_, err = r.InstantiateModule(testCtx, compiled, moduleConfig) // clear
require.NoError(t, err)
}()
@@ -539,7 +548,7 @@ func testStdin(t *testing.T, bin []byte) {
func Test_LargeStdout(t *testing.T) {
if wasmGo != nil {
var buf bytes.Buffer
r := wazero.NewRuntime(testCtx)
r := wazero.NewRuntimeWithConfig(testCtx, runtimeCfg)
defer func() {
require.NoError(t, r.Close(testCtx))
}()
@@ -547,10 +556,12 @@ func Test_LargeStdout(t *testing.T) {
_, err := wasi_snapshot_preview1.Instantiate(testCtx, r)
require.NoError(t, err)
_, err = r.InstantiateWithConfig(testCtx, wasmGo,
wazero.NewModuleConfig().
WithArgs("wasi", "largestdout").
WithStdout(&buf))
compiled, err := r.CompileModule(testCtx, wasmGo)
require.NoError(t, err)
_, err = r.InstantiateModule(testCtx, compiled, wazero.NewModuleConfig().
WithArgs("wasi", "largestdout").
WithStdout(&buf)) // clear
require.NoError(t, err)
tempDir := t.TempDir()

View File

@@ -122,13 +122,18 @@ func Test_NonblockGo(t *testing.T) {
ch := make(chan string, 1)
go func() {
r := wazero.NewRuntime(testCtx)
defer r.Close(testCtx)
r := wazero.NewRuntimeWithConfig(testCtx, runtimeCfg)
defer func() {
require.NoError(t, r.Close(testCtx))
}()
_, err := wasi_snapshot_preview1.Instantiate(testCtx, r)
require.NoError(t, err)
mod, err := r.InstantiateWithConfig(testCtx, wasmGo, moduleConfig) // clear
compiled, err := r.CompileModule(testCtx, wasmGo)
require.NoError(t, err)
mod, err := r.InstantiateModule(testCtx, compiled, moduleConfig)
require.NoError(t, err)
_, err = mod.ExportedFunction("_start").Call(testCtx)