2024-02-17 12:55:08 +08:00
|
|
|
package gojs_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"apigo.cloud/git/apigo/gojs"
|
|
|
|
"apigo.cloud/git/apigo/plugin"
|
|
|
|
"fmt"
|
|
|
|
"github.com/ssgo/log"
|
|
|
|
"github.com/ssgo/u"
|
|
|
|
"runtime"
|
2024-03-15 13:40:20 +08:00
|
|
|
"strings"
|
2024-02-17 12:55:08 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Object struct {
|
2024-05-27 13:12:15 +08:00
|
|
|
id string
|
|
|
|
plus func(int, int) int
|
|
|
|
baseNumber int
|
|
|
|
}
|
|
|
|
|
|
|
|
type PlusConfig struct {
|
|
|
|
BaseNumber int
|
|
|
|
Action func(int, int) int
|
2024-02-17 12:55:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (obj *Object) GetId() string {
|
|
|
|
return obj.id
|
|
|
|
}
|
|
|
|
|
2024-05-27 13:12:15 +08:00
|
|
|
func (obj *Object) SetPlusFuncAndRun(cb func(int, int) int) int {
|
|
|
|
obj.plus = cb
|
|
|
|
return obj.plus(1, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (obj *Object) SetPlusFunc(cb func(int, int) int) {
|
|
|
|
obj.plus = cb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (obj *Object) SetPlusFunc2(conf PlusConfig) {
|
|
|
|
obj.baseNumber = conf.BaseNumber
|
|
|
|
obj.plus = conf.Action
|
|
|
|
}
|
|
|
|
|
|
|
|
func (obj *Object) Plus(a ...int) int {
|
|
|
|
return obj.plus(a[0], a[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (obj *Object) PlusX(a int, b int) int {
|
|
|
|
return obj.plus(a, b) + obj.baseNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
func (obj *Object) AsyncPlus(a int, b int) int {
|
|
|
|
ch := make(chan bool, 1)
|
|
|
|
r := 0
|
|
|
|
go func() {
|
|
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
r = obj.plus(a, b)
|
|
|
|
ch <- true
|
|
|
|
}()
|
|
|
|
<-ch
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2024-03-15 13:40:20 +08:00
|
|
|
type TestBirthday struct {
|
|
|
|
Year int
|
|
|
|
Month int
|
|
|
|
Day int
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestBaseUser struct {
|
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestUser struct {
|
|
|
|
TestBaseUser
|
|
|
|
Birthday *TestBirthday
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestBirthday) String() string {
|
|
|
|
return fmt.Sprintln(b.Year, b.Month, b.Day)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *TestUser) GetBirthdayString() string {
|
|
|
|
return u.Birthday.String()
|
|
|
|
}
|
|
|
|
|
2024-02-17 12:55:08 +08:00
|
|
|
func init() {
|
|
|
|
defaultObject := Object{id: "o-00"}
|
2024-03-15 13:40:20 +08:00
|
|
|
plg := plugin.Plugin{
|
2024-02-17 12:55:08 +08:00
|
|
|
Id: "obj",
|
|
|
|
Name: "test obj plugin",
|
|
|
|
Objects: map[string]interface{}{
|
2024-03-15 13:40:20 +08:00
|
|
|
"name": "1233",
|
|
|
|
"list1": []interface{}{1, "2", map[string]interface{}{"aaa": 111, "bbb": "222"}, true},
|
|
|
|
"log": log.DefaultLogger.Info,
|
2024-02-17 12:55:08 +08:00
|
|
|
"getId": defaultObject.GetId,
|
|
|
|
"new": func(id string) interface{} {
|
|
|
|
return &Object{id: id}
|
|
|
|
},
|
2024-03-15 13:40:20 +08:00
|
|
|
"test1": func(id int, id2 uint16, id3 float64) *TestUser {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"echo": func(text string, echoFunc func(text string, t2 int) string, t3 []uint32, t4 *bool) interface{} {
|
|
|
|
return echoFunc(text, 0)
|
2024-02-17 12:55:08 +08:00
|
|
|
},
|
|
|
|
"echoTimes": func(echoFunc func(text string)) {
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
//time.Sleep(100 * time.Millisecond)
|
|
|
|
echoFunc(fmt.Sprint(i))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2024-03-15 13:40:20 +08:00
|
|
|
}
|
|
|
|
plugin.Register(plg)
|
|
|
|
gojs.SetPluginsConfig(map[string]plugin.Config{
|
|
|
|
"obj": plugin.Config{},
|
2024-02-17 12:55:08 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func test(t *testing.T, name string, check bool, extArgs ...interface{}) {
|
|
|
|
if check {
|
|
|
|
fmt.Println(u.Green(name), u.BGreen("[OK]"))
|
|
|
|
} else {
|
|
|
|
fmt.Println(u.Red(name), u.BRed("[Failed]"), fmt.Sprintln(extArgs...))
|
|
|
|
t.Error(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 13:40:20 +08:00
|
|
|
func TestTS(t *testing.T) {
|
|
|
|
plg := plugin.Get("obj")
|
2024-05-27 13:12:15 +08:00
|
|
|
exportCode, _ := gojs.MakePluginCode(plg)
|
|
|
|
test(t, "ts code", strings.Contains(exportCode, "(echoFunc: (text: string) => void)"), exportCode)
|
2024-03-15 13:40:20 +08:00
|
|
|
}
|
|
|
|
|
2024-02-17 12:55:08 +08:00
|
|
|
func TestGlobal(t *testing.T) {
|
|
|
|
code := `
|
2024-05-27 13:12:15 +08:00
|
|
|
import lg from 'logger'
|
|
|
|
import console from 'console'
|
2024-02-17 12:55:08 +08:00
|
|
|
log('test', 'name', 'log')
|
2024-05-27 13:12:15 +08:00
|
|
|
lg.info('test', {name:'log'})
|
|
|
|
console.info('test', {name:'log'})
|
2024-02-17 12:55:08 +08:00
|
|
|
return plus(number,2)
|
|
|
|
`
|
|
|
|
globals := map[string]interface{}{
|
|
|
|
"number": 9,
|
|
|
|
"log": log.DefaultLogger.Info,
|
|
|
|
"plus": func(i, j int) int { return i + j },
|
|
|
|
}
|
|
|
|
|
2024-03-24 12:20:58 +08:00
|
|
|
r, _ := gojs.Run(code, &gojs.RuntimeOption{
|
2024-03-15 13:40:20 +08:00
|
|
|
Globals: globals,
|
|
|
|
})
|
2024-02-17 12:55:08 +08:00
|
|
|
test(t, "call", u.Int(r) == 11, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlugin(t *testing.T) {
|
2024-03-24 12:20:58 +08:00
|
|
|
rt := gojs.New(nil)
|
|
|
|
rt.Exec("import obj from 'obj'")
|
|
|
|
r, _ := rt.Run("return obj.getId()")
|
2024-02-17 12:55:08 +08:00
|
|
|
test(t, "obj.getId()", u.String(r) == "o-00", r)
|
|
|
|
|
2024-03-24 12:20:58 +08:00
|
|
|
r, _ = rt.Run(`
|
2024-02-17 12:55:08 +08:00
|
|
|
o = obj.new('o-01')
|
|
|
|
return o.getId()
|
2024-03-24 12:20:58 +08:00
|
|
|
`)
|
2024-02-17 12:55:08 +08:00
|
|
|
test(t, "new obj.getId()", u.String(r) == "o-01", r)
|
|
|
|
|
|
|
|
t1 := time.Now()
|
2024-03-24 12:20:58 +08:00
|
|
|
r, _ = rt.Run(`
|
2024-02-17 12:55:08 +08:00
|
|
|
out = ''
|
|
|
|
obj.echo('123', function(text){
|
|
|
|
out = text
|
2024-03-15 13:40:20 +08:00
|
|
|
}, null)
|
2024-02-17 12:55:08 +08:00
|
|
|
return out
|
2024-03-24 12:20:58 +08:00
|
|
|
`)
|
2024-02-17 12:55:08 +08:00
|
|
|
t2 := time.Now()
|
2024-03-15 13:40:20 +08:00
|
|
|
fmt.Println("time:", t2.UnixMicro()-t1.UnixMicro())
|
2024-02-17 12:55:08 +08:00
|
|
|
test(t, "callback", u.String(r) == "123", r)
|
|
|
|
|
|
|
|
t1 = time.Now()
|
2024-03-24 12:20:58 +08:00
|
|
|
r, _ = rt.Run(`
|
2024-02-17 12:55:08 +08:00
|
|
|
out = ''
|
|
|
|
obj.echoTimes(function(text){
|
|
|
|
out += text
|
|
|
|
})
|
|
|
|
return out
|
2024-03-24 12:20:58 +08:00
|
|
|
`)
|
2024-02-17 12:55:08 +08:00
|
|
|
t2 = time.Now()
|
2024-03-15 13:40:20 +08:00
|
|
|
fmt.Println("time:", t2.UnixMicro()-t1.UnixMicro())
|
2024-02-17 12:55:08 +08:00
|
|
|
test(t, "callbacks", u.String(r) == "01234", r)
|
|
|
|
}
|
|
|
|
|
2024-05-27 13:12:15 +08:00
|
|
|
func TestCallback(t *testing.T) {
|
|
|
|
r, _ := gojs.Run(`
|
|
|
|
import obj from 'obj'
|
|
|
|
o = obj.new('cb')
|
|
|
|
return o.setPlusFuncAndRun(function (a, b){
|
|
|
|
return a+b
|
|
|
|
})
|
|
|
|
`, nil)
|
|
|
|
test(t, "plus with callback1", u.String(r) == "3", r)
|
|
|
|
|
|
|
|
r, _ = gojs.Run(`
|
|
|
|
import obj from 'obj'
|
|
|
|
o = obj.new('cb')
|
|
|
|
o.setPlusFunc(function(a, b){
|
|
|
|
return a+b
|
|
|
|
})
|
|
|
|
return o.plus(1, 2)
|
|
|
|
`, nil)
|
|
|
|
test(t, "plus with callback2.1", u.String(r) == "3", r)
|
|
|
|
|
|
|
|
r, _ = gojs.Run(`
|
|
|
|
import obj from 'obj'
|
|
|
|
o = obj.new('cb')
|
|
|
|
function plus(a, b){
|
|
|
|
return a+b
|
|
|
|
}
|
|
|
|
o.setPlusFunc(plus)
|
|
|
|
return o.plus(1, 2)
|
|
|
|
`, nil)
|
|
|
|
test(t, "plus with callback2.2", u.String(r) == "3", r)
|
|
|
|
|
|
|
|
r, _ = gojs.Run(`
|
|
|
|
import obj from 'obj'
|
|
|
|
o = obj.new('cb')
|
|
|
|
o.setPlusFunc2({action: function(a, b){
|
|
|
|
return a+b
|
|
|
|
}})
|
|
|
|
return o.plus(1, 2)
|
|
|
|
`, nil)
|
|
|
|
test(t, "plus with callback3.1", u.String(r) == "3", r)
|
|
|
|
|
|
|
|
r, _ = gojs.Run(`
|
|
|
|
import obj from 'obj'
|
|
|
|
o = obj.new('cb')
|
|
|
|
function plus(a, b){
|
|
|
|
return a+b
|
|
|
|
}
|
|
|
|
o.setPlusFunc2({baseNumber:10, action: plus})
|
|
|
|
return o.plusX(1, 2)
|
|
|
|
`, nil)
|
|
|
|
test(t, "plus with callback3.2", u.String(r) == "13", r)
|
|
|
|
|
|
|
|
rt := gojs.New(nil)
|
|
|
|
rt.Exec(`
|
|
|
|
import obj from 'obj'
|
|
|
|
o = obj.new('cb')
|
|
|
|
`)
|
|
|
|
rt.Run(`
|
|
|
|
o.setPlusFunc2({action: function(a, b){
|
|
|
|
return a+b
|
|
|
|
}})
|
|
|
|
`)
|
|
|
|
r, _ = rt.Run(`
|
|
|
|
return o.plus(1, 2)
|
|
|
|
`)
|
|
|
|
test(t, "plus with callback4.1", u.String(r) == "3", r)
|
|
|
|
|
|
|
|
r, _ = rt.Run(`
|
|
|
|
return o.asyncPlus(1, 2)
|
|
|
|
`)
|
|
|
|
// unexpected, maybe get exception in other go coroutine
|
|
|
|
test(t, "plus with callback4.2", u.String(r) == "3" || u.String(r) == "0", r)
|
|
|
|
rt.Close()
|
|
|
|
}
|
|
|
|
|
2024-02-17 12:55:08 +08:00
|
|
|
func BenchmarkEcho(tb *testing.B) {
|
|
|
|
tb.StopTimer()
|
|
|
|
ms1 := runtime.MemStats{}
|
|
|
|
runtime.ReadMemStats(&ms1)
|
|
|
|
tb.StartTimer()
|
|
|
|
for i := 0; i < tb.N; i++ {
|
2024-03-15 13:40:20 +08:00
|
|
|
gojs.Run(`return 1`, nil)
|
2024-02-17 12:55:08 +08:00
|
|
|
}
|
|
|
|
tb.StopTimer()
|
|
|
|
|
|
|
|
ms2 := runtime.MemStats{}
|
|
|
|
runtime.ReadMemStats(&ms2)
|
|
|
|
|
|
|
|
runtime.GC()
|
|
|
|
ms3 := runtime.MemStats{}
|
|
|
|
runtime.ReadMemStats(&ms3)
|
|
|
|
fmt.Println(">>", ms1.HeapInuse, ms2.HeapInuse, ms3.HeapInuse)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCallback(tb *testing.B) {
|
|
|
|
tb.StopTimer()
|
|
|
|
ms1 := runtime.MemStats{}
|
|
|
|
runtime.ReadMemStats(&ms1)
|
|
|
|
tb.StartTimer()
|
|
|
|
for i := 0; i < tb.N; i++ {
|
|
|
|
gojs.Run(`
|
2024-03-24 12:20:58 +08:00
|
|
|
import obj from 'obj'
|
2024-02-17 12:55:08 +08:00
|
|
|
out = ''
|
|
|
|
obj.echoTimes(function(text){
|
|
|
|
out += text
|
|
|
|
})
|
|
|
|
return out
|
2024-03-15 13:40:20 +08:00
|
|
|
`, nil)
|
2024-02-17 12:55:08 +08:00
|
|
|
}
|
|
|
|
tb.StopTimer()
|
|
|
|
|
|
|
|
ms2 := runtime.MemStats{}
|
|
|
|
runtime.ReadMemStats(&ms2)
|
|
|
|
|
|
|
|
runtime.GC()
|
|
|
|
ms3 := runtime.MemStats{}
|
|
|
|
runtime.ReadMemStats(&ms3)
|
|
|
|
fmt.Println(">>", ms1.HeapInuse, ms2.HeapInuse, ms3.HeapInuse)
|
|
|
|
}
|