-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_test.go
More file actions
197 lines (180 loc) · 5.23 KB
/
Copy pathruntime_test.go
File metadata and controls
197 lines (180 loc) · 5.23 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
package sango_test
import (
"context"
"encoding/binary"
"errors"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/n9te9/sango"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
func TestNew(t *testing.T) {
tests := []struct {
name string
adapter sango.Adapter
opts []sango.Option
instanceFn func(ctx context.Context) *sango.Instance
initializeWasm []byte
code []byte
want *sango.Runtime
wantErr error
}{
{
name: "success: sango.New() success and return sango runtime is pool size 1 and error is nil",
adapter: &fakeAdapter{
initializeError: nil,
evalError: nil,
evalResult: sango.Result{
Value: []byte(`example`),
Err: nil,
},
},
opts: []sango.Option{sango.WithPoolSize(1)},
initializeWasm: minimalWasm,
code: []byte("console.log(\"example\")"),
want: &sango.Runtime{},
wantErr: nil,
}, {
name: "success: sango.New() success and return sango runtime has WASI and error is nil",
adapter: &fakeAdapter{
initializeError: nil,
evalError: nil,
evalResult: sango.Result{
Value: []byte(`example`),
Err: nil,
},
},
opts: []sango.Option{sango.WithWASI()},
initializeWasm: minimalWasm,
code: []byte("console.log(\"example\")"),
want: &sango.Runtime{},
wantErr: nil,
}, {
name: "fail: sango.New() fail and return an error is failed to initialize code",
adapter: &fakeAdapter{
initializeError: nil,
evalError: nil,
evalResult: sango.Result{
Value: []byte(`example`),
Err: nil,
},
},
initializeWasm: []byte(``),
code: []byte("console.log(\"example\")"),
want: nil,
wantErr: errors.New("sango: compile module: invalid magic number"),
},
}
t.Parallel()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := sango.New(t.Context(), tt.initializeWasm, tt.adapter, tt.opts...)
if err != nil {
if tt.wantErr == nil {
t.Fatalf("error expected nil, but got %v\n", err)
}
if err.Error() != tt.wantErr.Error() {
t.Fatalf("error expected %v, but got %v\n", tt.wantErr, err)
}
}
if tt.wantErr != nil {
if err == nil {
t.Fatalf("error expected %v, but got nil\n", tt.wantErr)
}
}
if d := cmp.Diff(got, tt.want, cmpopts.IgnoreUnexported(sango.Runtime{})); d != "" {
t.Fatal(d)
}
})
}
}
func TestRuntime_CompilationCacheDir(t *testing.T) {
dir := t.TempDir()
adptr := &fakeAdapter{evalResult: sango.Result{Value: []byte(`ok`)}}
rt, err := sango.New(t.Context(), minimalWasm, adptr,
sango.WithPoolSize(0), sango.WithCompilationCacheDir(dir))
if err != nil {
t.Fatalf("New with cache dir failed: %v", err)
}
rt.Close(t.Context())
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("ReadDir failed: %v", err)
}
if len(entries) == 0 {
t.Fatalf("expected cache dir populated, got empty")
}
rt2, err := sango.New(t.Context(), minimalWasm, adptr,
sango.WithPoolSize(0), sango.WithCompilationCacheDir(dir))
if err != nil {
t.Fatalf("New reusing cache dir failed: %v", err)
}
rt2.Close(t.Context())
}
func TestRuntime_CompilationCache_Shared(t *testing.T) {
cache := wazero.NewCompilationCache()
defer cache.Close(t.Context())
adptr := &fakeAdapter{evalResult: sango.Result{Value: []byte(`ok`)}}
for i := 0; i < 3; i++ {
rt, err := sango.New(t.Context(), minimalWasm, adptr,
sango.WithPoolSize(0), sango.WithCompilationCache(cache))
if err != nil {
t.Fatalf("iteration %d: New failed: %v", i, err)
}
rt.Close(t.Context())
}
}
type memAdapter struct{}
func (memAdapter) ID() string { return "memiso" }
func (memAdapter) Initialize(ctx context.Context, mod api.Module) error { return nil }
func (memAdapter) Eval(ctx context.Context, mod api.Module, code []byte) (sango.Result, error) {
if len(code) < 1 {
return sango.Result{}, errors.New("memAdapter: empty code")
}
mem := mod.Memory()
switch code[0] {
case 'S':
size := mem.Size()
out := make([]byte, 4)
binary.BigEndian.PutUint32(out, size)
return sango.Result{Value: out}, nil
case 'G':
if len(code) < 5 {
return sango.Result{}, errors.New("memAdapter: G needs delta")
}
delta := binary.BigEndian.Uint32(code[1:5])
if _, ok := mem.Grow(delta); !ok {
return sango.Result{}, errors.New("memAdapter: grow failed")
}
return sango.Result{Value: []byte("ok")}, nil
}
if len(code) < 5 {
return sango.Result{}, errors.New("memAdapter: code too short")
}
off := binary.BigEndian.Uint32(code[1:5])
switch code[0] {
case 'W':
if !mem.Write(off, code[5:]) {
return sango.Result{}, errors.New("memAdapter: write out of range")
}
return sango.Result{Value: []byte("ok")}, nil
case 'R':
if len(code) < 9 {
return sango.Result{}, errors.New("memAdapter: R needs length")
}
n := binary.BigEndian.Uint32(code[5:9])
b, ok := mem.Read(off, n)
if !ok {
return sango.Result{}, errors.New("memAdapter: read out of range")
}
out := make([]byte, len(b))
copy(out, b)
return sango.Result{Value: out}, nil
default:
return sango.Result{}, errors.New("memAdapter: unknown op")
}
}