Skip to content

Commit

Permalink
Revamping Tests (#9)
Browse files Browse the repository at this point in the history
* Adding a test GH action

* Making linters happy

* Renaming go tests

* Fixing branch configuration

* Adding some sub-tests

* Updating Tests

* Little better coverage

* Tidy Go mod

* Updating Makefile and adding rust

* Cleaning up comments a bit

* s/an/and/

* Updated to TinyGo v0.20.0

Co-authored-by: Phil Kedy <[email protected]>
  • Loading branch information
madflojo and pkedy authored Nov 11, 2021
1 parent ec5ea10 commit 57b2e30
Show file tree
Hide file tree
Showing 25 changed files with 500 additions and 166 deletions.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Coverage
.coverage/

# No Swapfiles
*.swp

# Rust build Directory
target/

# npm Modules
node_modules

# Vendor
vendor

# Mac stuff
.DS_Store
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Makefile to build and execute tests

tests:
@echo "Executing Go tests"
mkdir -p .coverage
go test -v -covermode=count -coverprofile=.coverage/coverage.out ./...
go tool cover -html=.coverage/coverage.out -o .coverage/coverage.html

build-wasm: build-as build-go build-rust

build-as:
$(MAKE) -C testdata/as build

build-go:
$(MAKE) -C testdata/go build

build-rust:
$(MAKE) -C testdata/rust build
5 changes: 5 additions & 0 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ func (i *Instance) MemorySize() uint32 {

// Invoke calls `operation` with `payload` on the module and returns a byte slice payload.
func (i *Instance) Invoke(ctx context.Context, operation string, payload []byte) ([]byte, error) {
// Make sure instance isn't closed to avoid panics
if i.inst == nil {
return nil, fmt.Errorf("error invoking guest with closed instance")
}

context := invokeContext{
ctx: ctx,
operation: operation,
Expand Down
173 changes: 132 additions & 41 deletions module_test.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,153 @@
package wapc_test
package wapc

import (
"context"
"io/ioutil"
"strings"
"testing"
"time"
)

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
func TestGuests(t *testing.T) {
lang := map[string]string{
"assemblyscript": "as/hello.wasm",
"go": "go/hello.wasm",
"rust": "rust/hello.wasm",
}

"github.com/wapc/wapc-go"
)
for l, p := range lang {
t.Run("Module testing with "+l+" Guest", func(t *testing.T) {
// Read .wasm file
b, err := ioutil.ReadFile("testdata/" + p)
if err != nil {
t.Errorf("Unable to open test file - %s", err)
}

// Use these later
callbackCh := make(chan struct{}, 2)
payload := []byte("Testing")

// Create new module with a callback function
m, err := New(b, func(context.Context, string, string, string, []byte) ([]byte, error) {
callbackCh <- struct{}{}
return []byte(""), nil
})
if err != nil {
t.Errorf("Error creating module - %s", err)
}
defer m.Close()

// Set loggers and writers
m.SetLogger(Println)
m.SetWriter(Print)

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

t.Run("Call Successful Function", func(t *testing.T) {
// Call echo function
r, err := i.Invoke(context.Background(), "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(context.Background(), "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(context.Background(), "404", payload)
if err == nil {
t.Errorf("Expected error when calling unregistered function, got nil")
}
})

})
}
}

func TestModuleBadBytes(t *testing.T) {
b := []byte("Do not do this at home kids")
_, err := New(b, NoOpHostCallHandler)
if err == nil {
t.Errorf("Expected error when creating module with invalid wasm, got nil")
}
}

func TestModule(t *testing.T) {
ctx := context.Background()
code, err := ioutil.ReadFile("testdata/hello.wasm")
require.NoError(t, err)
// Read .wasm file
b, err := ioutil.ReadFile("testdata/as/hello.wasm")
if err != nil {
t.Errorf("Unable to open test file - %s", err)
}

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

consoleLog := func(msg string) {
assert.Equal(t, "logging something", msg)
consoleLogInvoked = true
// Create new module with a NoOpCallback function
m, err := New(b, NoOpHostCallHandler)
if err != nil {
t.Errorf("Error creating module - %s", err)
}
defer m.Close()

hostCall := func(ctx context.Context, binding, namespace, operation string, payload []byte) ([]byte, error) {
assert.Equal(t, "myBinding", binding)
assert.Equal(t, "sample", namespace)
assert.Equal(t, "hello", operation)
assert.Equal(t, "Simon", string(payload))
hostCallInvoked = true
return []byte("test"), nil
// Set loggers and writers
m.SetLogger(Println)
m.SetWriter(Print)

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

module, err := wapc.New(code, hostCall)
module.SetLogger(consoleLog)
require.NoError(t, err)
defer module.Close()
t.Run("Check MemorySize", func(t *testing.T) {
_ = i.MemorySize()
})

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

result, err := instance.Invoke(ctx, "hello", []byte("waPC"))
require.NoError(t, err)
// Verify payload is returned
if len(r) != len(payload) {
t.Errorf("Unexpected response message, got %s, expected %s", r, payload)
}
})

assert.Equal(t, "Hello, waPC", string(result))
assert.True(t, consoleLogInvoked)
assert.True(t, hostCallInvoked)
i.Close()

_, err = instance.Invoke(ctx, "error", []byte("waPC"))
require.Error(t, err)
t.Run("Call Function with Closed Instance", func(t *testing.T) {
// Call echo function
_, err := i.Invoke(context.Background(), "echo", payload)
if err == nil {
t.Errorf("Expected error when calling wasm module with closed instance")
}
})

msg := err.Error()
index := strings.IndexByte(msg, ';')
if index != -1 {
msg = msg[:index]
}
assert.Equal(t, "error occurred", msg)
}
34 changes: 0 additions & 34 deletions package-lock.json

This file was deleted.

16 changes: 0 additions & 16 deletions package.json

This file was deleted.

Loading

0 comments on commit 57b2e30

Please sign in to comment.