forked from wapc/wapc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine_test.go
184 lines (159 loc) · 4.73 KB
/
engine_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package wapc_test
import (
"context"
"os"
"testing"
"time"
"github.com/wapc/wapc-go"
"github.com/wapc/wapc-go/engines/wasmer"
"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(),
wazero.Engine(),
}
func TestGuests(t *testing.T) {
lang := map[string]string{
"assemblyscript": "as/hello.wasm",
"go": "go/hello.wasm",
"rust": "rust/hello.wasm",
}
for _, engine := range engines {
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("testdata/" + 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) {
for _, engine := range engines {
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) {
for _, engine := range engines {
t.Run(engine.Name(), func(t *testing.T) {
host := wapc.NoOpHostCallHandler
// Read .wasm file
guest, err := os.ReadFile("testdata/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")
}
})
})
}
}