forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isolate_test.go
157 lines (128 loc) · 3.3 KB
/
isolate_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
// Copyright 2019 Roger Chapman and the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package v8go_test
import (
"encoding/json"
"fmt"
"math/rand"
"runtime"
"strings"
"testing"
"time"
"rogchap.com/v8go"
)
func TestIsolateTermination(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
ctx, _ := v8go.NewContext(iso)
// ctx2, _ := v8go.NewContext(iso)
err := make(chan error, 1)
go func() {
_, e := ctx.RunScript(`while (true) { }`, "forever.js")
err <- e
}()
go func() {
// [RC] find a better way to know when a script has started execution
time.Sleep(time.Millisecond)
iso.TerminateExecution()
}()
if e := <-err; e == nil || !strings.HasPrefix(e.Error(), "ExecutionTerminated") {
t.Errorf("unexpected error: %v", e)
}
}
func TestGetHeapStatistics(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
v8go.NewContext(iso)
v8go.NewContext(iso)
hs := iso.GetHeapStatistics()
if hs.NumberOfNativeContexts != 3 {
t.Error("expect NumberOfNativeContexts return 3, got", hs.NumberOfNativeContexts)
}
if hs.NumberOfDetachedContexts != 0 {
t.Error("expect NumberOfDetachedContexts return 0, got", hs.NumberOfDetachedContexts)
}
}
func TestCallbackRegistry(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
cb := func(*v8go.FunctionCallbackInfo) *v8go.Value { return nil }
cb0 := iso.GetCallback(0)
if cb0 != nil {
t.Error("expected callback function to be <nil>")
}
ref1 := iso.RegisterCallback(cb)
if ref1 != 1 {
t.Errorf("expected callback ref == 1, got %d", ref1)
}
cb1 := iso.GetCallback(1)
if fmt.Sprintf("%p", cb1) != fmt.Sprintf("%p", cb) {
t.Errorf("unexpected callback function; want %p, got %p", cb, cb1)
}
}
func TestIsolateDispose(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
if iso.GetHeapStatistics().TotalHeapSize == 0 {
t.Error("Isolate incorrectly allocated")
}
iso.Dispose()
// noop when called multiple times
iso.Dispose()
// deprecated
iso.Close()
if iso.GetHeapStatistics().TotalHeapSize != 0 {
t.Error("Isolate not disposed correctly")
}
}
func TestIsolateGarbageCollection(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
val, _ := v8go.NewValue(iso, "some string")
fmt.Println(val.String())
tmpl, _ := v8go.NewObjectTemplate(iso)
tmpl.Set("foo", "bar")
v8go.NewContext(iso, tmpl)
iso.Dispose()
runtime.GC()
time.Sleep(time.Second)
}
func BenchmarkIsolateInitialization(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
vm, _ := v8go.NewIsolate()
vm.Close() // force disposal of the VM
}
}
func BenchmarkIsolateInitAndRun(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
vm, _ := v8go.NewIsolate()
ctx, _ := v8go.NewContext(vm)
ctx.RunScript(script, "main.js")
str, _ := json.Marshal(makeObject())
cmd := fmt.Sprintf("process(%s)", str)
ctx.RunScript(cmd, "cmd.js")
ctx.Close()
vm.Close() // force disposal of the VM
}
}
const script = `
const process = (record) => {
const res = [];
for (let [k, v] of Object.entries(record)) {
res.push({
name: k,
value: v,
});
}
return JSON.stringify(res);
};
`
func makeObject() interface{} {
return map[string]interface{}{
"a": rand.Intn(1000000),
"b": "AAAABBBBAAAABBBBAAAABBBBAAAABBBBAAAABBBB",
}
}