-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
25 changed files
with
500 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.