Skip to content

Commit

Permalink
Moving engine implementations to separate modules.
Browse files Browse the repository at this point in the history
Handling `sys.ExitError` returned by `_start` in wazero engine.
  • Loading branch information
pkedy committed Jan 21, 2025
1 parent 17eb7d5 commit c08ffb6
Show file tree
Hide file tree
Showing 34 changed files with 843 additions and 320 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
go-version: '1.23.5'
- uses: acifani/setup-tinygo@v2
with:
tinygo-version: '0.34.0'
tinygo-version: '0.35.0'

- name: Set up Rust
uses: actions-rs/toolchain@v1
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
tests:
@echo "Executing Go tests"
mkdir -p .coverage
go test -v -covermode=count -coverprofile=.coverage/coverage.out ./...
go test -v -covermode=count -coverprofile=.coverage/coverage.out \
./engines/wasmer/... \
./engines/wasmtime/... \
./engines/wazero/...
go tool cover -html=.coverage/coverage.out -o .coverage/coverage.html

build-wasm: build-as build-example build-go build-rust
Expand Down
3 changes: 1 addition & 2 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"time"

"github.com/wapc/wapc-go"
"github.com/wapc/wapc-go/engines/wasmtime"
"github.com/wapc/wapc-go/engines/wazero"
)

var ctx = context.Background()

var engines = []wapc.Engine{
// wasmer.Engine(),
wasmtime.Engine(),
// wasmtime.Engine(),
wazero.Engine(),
}

Expand Down
172 changes: 172 additions & 0 deletions engines/tests/engine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package tests

import (
"context"
"os"
"path/filepath"
"testing"
"time"

"github.com/wapc/wapc-go"
)

var ctx = context.Background()

const TESTDATA_DIR = "../../testdata"

func TestGuest(t *testing.T, engine wapc.Engine) {
lang := map[string]string{
"assemblyscript": "as/hello.wasm",
"go": "go/hello.wasm",
"rust": "rust/hello.wasm",
}
t.Run(engine.Name(), func(t *testing.T) {
for l, p := range lang {
t.Run("Module testing with "+l+" Guest", func(t *testing.T) {
// Read .wasm file
guest, err := os.ReadFile(filepath.Join(TESTDATA_DIR, p))
if err != nil {
t.Errorf("Unable to open test file - %s", err)
}

// Use these later
callbackCh := make(chan struct{}, 2)
host := func(context.Context, string, string, string, []byte) ([]byte, error) {
callbackCh <- struct{}{}
return []byte(""), nil
}

payload := []byte("Testing")

// Create new module with a callback function
m, err := engine.New(ctx, host, guest, &wapc.ModuleConfig{
Logger: wapc.PrintlnLogger,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
if err != nil {
t.Errorf("Error creating module - %s", err)
}
defer m.Close(ctx)

// Instantiate Module
i, err := m.Instantiate(ctx)
if err != nil {
t.Errorf("Error instantiating module - %s", err)
}
defer i.Close(ctx)

t.Run("Call Successful Function", func(t *testing.T) {
// Call echo function
r, err := i.Invoke(ctx, "echo", payload)
if err != nil {
t.Errorf("Unexpected error when calling wasm module - %s", err)
}

// Verify payload is returned
if len(r) != len(payload) {
t.Errorf("Unexpected response message, got %s, expected %s", r, payload)
}

// Verify if callback is called
select {
case <-time.After(5 * time.Second):
t.Errorf("Timeout waiting for callback execution")
case <-callbackCh:
return
}
})

t.Run("Call Failing Function", func(t *testing.T) {
// Call nope function
_, err := i.Invoke(ctx, "nope", payload)
if err == nil {
t.Errorf("Expected error when calling failing function, got nil")
}
})

t.Run("Call Unregistered Function", func(t *testing.T) {
_, err := i.Invoke(ctx, "404", payload)
if err == nil {
t.Errorf("Expected error when calling unregistered function, got nil")
}
})

})
}
})
}

func TestModuleBadBytes(t *testing.T, engine wapc.Engine) {
t.Run(engine.Name(), func(t *testing.T) {
host := wapc.NoOpHostCallHandler
guest := []byte("Do not do this at home kids")
_, err := engine.New(ctx, host, guest, &wapc.ModuleConfig{})
if err == nil {
t.Errorf("Expected error when creating module with invalid wasm, got nil")
}
})
}

func TestModule(t *testing.T, engine wapc.Engine) {
t.Run(engine.Name(), func(t *testing.T) {
host := wapc.NoOpHostCallHandler
// Read .wasm file
guest, err := os.ReadFile(filepath.Join(TESTDATA_DIR, "as/hello.wasm"))
if err != nil {
t.Errorf("Unable to open test file - %s", err)
}

// Use these later
payload := []byte("Testing")

// Create new module with a NoOpCallback function
m, err := engine.New(ctx, host, guest, &wapc.ModuleConfig{
Logger: wapc.PrintlnLogger,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
if err != nil {
t.Errorf("Error creating module - %s", err)
}
defer m.Close(ctx)

// Instantiate Module
i, err := m.Instantiate(ctx)
if err != nil {
t.Errorf("Error instantiating module - %s", err)
}
defer i.Close(ctx)

t.Run("Check MemorySize", func(t *testing.T) {
// Verify implementations didn't mistake size in bytes for page count.
expectedMemorySize := uint32(65536) // 1 page
if i.MemorySize() != expectedMemorySize {
t.Errorf("Unexpected memory size, got %d, expected %d", i.MemorySize(), expectedMemorySize)
}
})

t.Run("Call Function", func(t *testing.T) {
// Call echo function
r, err := i.Invoke(ctx, "echo", payload)
if err != nil {
t.Errorf("Unexpected error when calling wasm module - %s", err)
}

// Verify payload is returned
if len(r) != len(payload) {
t.Errorf("Unexpected response message, got %s, expected %s", r, payload)
}
})

i.Close(ctx)

t.Run("Call Function with Closed Instance", func(t *testing.T) {
// Call echo function
_, err := i.Invoke(ctx, "echo", payload)
if err == nil {
t.Errorf("Expected error when calling wasm module with closed instance")
}
})
})
}
98 changes: 98 additions & 0 deletions engines/tests/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package tests

import (
"context"
"os"
"path/filepath"
"testing"
"time"

"github.com/wapc/wapc-go"
)

func TestGuestsWithPool(t *testing.T, engine wapc.Engine) {
lang := map[string]string{
"assemblyscript": "as/hello.wasm",
"go": "go/hello.wasm",
"rust": "rust/hello.wasm",
}

t.Run(engine.Name(), func(t *testing.T) {
for l, p := range lang {
t.Run("Module testing with "+l+" Guest", func(t *testing.T) {
// Read .wasm file
guest, err := os.ReadFile(filepath.Join(TESTDATA_DIR, p))
if err != nil {
t.Errorf("Unable to open test file - %s", err)
}

// Use these later
callbackCh := make(chan struct{}, 2)
host := func(context.Context, string, string, string, []byte) ([]byte, error) {
callbackCh <- struct{}{}
return []byte(""), nil
}
payload := []byte("Testing")

// Create new module with a callback function
m, err := engine.New(ctx, host, guest, &wapc.ModuleConfig{})
if err != nil {
t.Errorf("Error creating module - %s", err)
}
defer m.Close(ctx)

p, err := wapc.NewPool(ctx, m, 10)
if err != nil {
t.Errorf("Error creating module pool - %s", err)
}
defer p.Close(ctx)

i, err := p.Get(10 * time.Second)
if err != nil {
t.Errorf("Error unable to fetch instance from pool - %s", err)
}

t.Run("Call Successful Function", func(t *testing.T) {
// Call echo function
r, err := i.Invoke(ctx, "echo", payload)
if err != nil {
t.Errorf("Unexpected error when calling wasm module - %s", err)
}

// Verify payload is returned
if len(r) != len(payload) {
t.Errorf("Unexpected response message, got %s, expected %s", r, payload)
}

// Verify if callback is called
select {
case <-time.After(5 * time.Second):
t.Errorf("Timeout waiting for callback execution")
case <-callbackCh:
return
}
})

t.Run("Call Failing Function", func(t *testing.T) {
// Call nope function
_, err := i.Invoke(ctx, "nope", payload)
if err == nil {
t.Errorf("Expected error when calling failing function, got nil")
}
})

t.Run("Call Unregistered Function", func(t *testing.T) {
_, err := i.Invoke(ctx, "404", payload)
if err == nil {
t.Errorf("Expected error when calling unregistered function, got nil")
}
})

err = p.Return(i)
if err != nil {
t.Errorf("Unexpected error when returning instance to pool - %s", err)
}
})
}
})
}
24 changes: 24 additions & 0 deletions engines/wasmer/engine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package wasmer_test

// import (
// "testing"

// "github.com/wapc/wapc-go/engines/tests"
// "github.com/wapc/wapc-go/engines/wasmer"
// )

// func TestGuest(t *testing.T) {
// tests.TestGuest(t, wasmer.Engine())
// }

// func TestModuleBadBytes(t *testing.T) {
// tests.TestModuleBadBytes(t, wasmer.Engine())
// }

// func TestModule(t *testing.T) {
// tests.TestModule(t, wasmer.Engine())
// }

// func TestGuestsWithPool(t *testing.T) {
// tests.TestGuestsWithPool(t, wasmer.Engine())
// }
14 changes: 14 additions & 0 deletions engines/wasmer/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/wapc/wapc-go/engines/wasmer

go 1.23.0

toolchain go1.23.4

require (
github.com/wapc/wapc-go v0.0.0
github.com/wasmerio/wasmer-go v1.0.4
)

require github.com/Workiva/go-datastructures v1.0.53 // indirect

replace github.com/wapc/wapc-go => ../..
38 changes: 38 additions & 0 deletions engines/wasmer/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
github.com/Workiva/go-datastructures v1.0.53 h1:J6Y/52yX10Xc5JjXmGtWoSSxs3mZnGSaq37xZZh7Yig=
github.com/Workiva/go-datastructures v1.0.53/go.mod h1:1yZL+zfsztete+ePzZz/Zb1/t5BnDuE2Ya2MMGhzP6A=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tinylib/msgp v1.1.5/go.mod h1:eQsjooMTnV42mHu917E26IogZ2930nFyBQdofk10Udg=
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31/go.mod h1:onvgF043R+lC5RZ8IT9rBXDaEDnpnw/Cl+HFiw+v/7Q=
github.com/wasmerio/wasmer-go v1.0.4 h1:MnqHoOGfiQ8MMq2RF6wyCeebKOe84G88h5yv+vmxJgs=
github.com/wasmerio/wasmer-go v1.0.4/go.mod h1:0gzVdSfg6pysA6QVp6iVRPTagC6Wq9pOE8J86WKb2Fk=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit c08ffb6

Please sign in to comment.