-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine_test.go
172 lines (162 loc) · 3.54 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
package engine
import (
"context"
"fmt"
"github.com/ZenLiuCN/fn"
"os"
"testing"
"time"
)
const jsFullTimeoutAsync =
// language=javascript
`
console.log("Begin "+"For timeout")
const newCounter=()=>{
let cnt=0
return {
inc:function (){
cnt++
console.log(cnt)
},
cnt:function (){
console.log('get',cnt)
return cnt
},
toString:()=>cnt
}
}
const counter=newCounter()
const out=()=>counter
new Promise((r, j) => {
console.log("job 0")
counter.inc()
setTimeout(()=>r(1),500)
}).then(v => {
console.log("job 1")
counter.inc()
return new Promise((r, j) => {
setTimeout(()=>r(v+1),500)
})}).then(v => {
console.log("job 2")
counter.inc()
return new Promise((r, j) => {
setTimeout(()=>r(v+1),500)
})
}).then(v => {
console.log("job 3")
counter.inc()
return new Promise((r, j) => {
setTimeout(()=>r(v+1),500)
})
}).then(v => {
console.log("job 4")
counter.inc()
return new Promise((r, j) => {
r(counter.cnt())
})
})
out()
`
func TestTimeout(t *testing.T) {
e := Get()
defer e.Free()
v, err := e.RunJs(jsFullTimeoutAsync)
if err != nil {
t.Log(err)
}
ctx, cc := context.WithTimeout(context.Background(), time.Second)
defer cc()
halts := e.AwaitWithContext(ctx)
if !halts.IsZero() {
e.Interrupt("shutdown for timeout")
}
if halts.IsZero() {
panic(fmt.Errorf("should not done:%s,%#v ", halts.String(), e.EventLoop))
}
t.Log(v)
}
func TestAwait(t *testing.T) {
e := Get()
defer e.Free()
v, err := e.RunJs(jsFullTimeoutAsync)
if err != nil {
t.Log(err)
}
halts := e.Await()
if !halts.IsZero() {
panic(fmt.Errorf("should done:%s,%#v ", halts.String(), e.EventLoop))
}
t.Log(v)
}
func TestContextJsSuccess(t *testing.T) {
e := Get()
defer e.Free()
ctx, cc := context.WithTimeout(context.Background(), time.Second*3)
defer cc()
v := fn.Panic1(e.RunJsContext(jsFullTimeoutAsync, time.Millisecond, ctx)).String()
if v != "5" {
panic("not real done")
}
}
func TestContextJsFailure(t *testing.T) {
e := Get()
defer e.Free()
ctx, cc := context.WithTimeout(context.Background(), time.Second*2)
defer cc()
v, err := e.RunJsContext(jsFullTimeoutAsync, time.Millisecond, ctx)
if err == nil {
panic("should not success " + v.String())
} else {
t.Log(v, err)
}
}
func TestRegisterResource(t *testing.T) {
e := Get()
defer e.Free()
fn.Panic1(e.RunJs(
//language=javascript
`
import os from 'go/os'
console.log(os)
const f=registerResource(os.open('engine.go'))
console.log(f.name())
`))
}
func TestNestRequire(t *testing.T) {
e := Get()
defer e.Free()
defer func() {
_ = os.RemoveAll("scripts")
}()
_ = os.MkdirAll("scripts/info", os.ModePerm)
_ = os.WriteFile("scripts/index.js", []byte(`
import {fn} from "./info/func"
console.log(fn())
`), os.ModePerm)
_ = os.WriteFile("scripts/info/func.js", []byte(`
export const fn=()=>{
console.log('from module')
return 'return from module'
}
`), os.ModePerm)
fn.Panic1(e.RunCode(CompileFile("./scripts/index.js", true)))
}
func TestNestRequireWithoutExt(t *testing.T) {
e := Get()
defer e.Free()
defer func() {
_ = os.RemoveAll("scripts")
}()
_ = os.MkdirAll("scripts", os.ModePerm)
_ = os.WriteFile("scripts/index.js", []byte(`
import {fn} from "./func"
console.log(fn())
`), os.ModePerm)
_ = os.WriteFile("scripts/func.ts", []byte(`
export const fn=()=>{
console.log('from module')
return 'return from module'
}
`), os.ModePerm)
fn.Panic1(e.RunCode(CompileFile("./scripts/index.js", true)))
}