130 lines
3.0 KiB
Go
130 lines
3.0 KiB
Go
package service_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"apigo.cc/gojs"
|
|
_ "apigo.cc/gojs/console"
|
|
_ "apigo.cc/gojs/http"
|
|
_ "apigo.cc/gojs/service"
|
|
_ "apigo.cc/gojs/util"
|
|
"github.com/ssgo/u"
|
|
)
|
|
|
|
var rt *gojs.Runtime
|
|
var addr = ""
|
|
|
|
const runTimes = 100
|
|
|
|
func TestStart(t *testing.T) {
|
|
gojs.ExportForDev()
|
|
rt = gojs.New()
|
|
err := rt.StartFromFile("start.js")
|
|
if err != nil {
|
|
t.Fatal("start failed", err)
|
|
}
|
|
r, err := rt.RunMain()
|
|
if err != nil {
|
|
t.Fatal("start failed", err)
|
|
}
|
|
addr = u.String(r)
|
|
}
|
|
|
|
func TestStatic(t *testing.T) {
|
|
r, err := rt.RunCode("testStatic()")
|
|
if err != nil {
|
|
t.Fatal("test static failed, got error", err)
|
|
}
|
|
if r != true {
|
|
t.Fatal("test static failed, name not match", r)
|
|
}
|
|
}
|
|
|
|
// func TestJsEcho(t *testing.T) {
|
|
// for i := 0; i < runTimes; i++ {
|
|
// name := u.UniqueId()
|
|
// r, err := rt.RunCode("test('" + name + "')")
|
|
// if err != nil {
|
|
// t.Fatal("test js get failed, got error", err)
|
|
// } else if r != name {
|
|
// t.Fatal("test js get failed, name not match", r, name)
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// func TestGoEcho(t *testing.T) {
|
|
// hc := httpclient.GetClientH2C(0)
|
|
// for i := 0; i < runTimes; i++ {
|
|
// name := u.UniqueId()
|
|
// r := hc.Get("http://" + addr + "/echo?name=" + name)
|
|
// if r.Error != nil {
|
|
// t.Fatal("test go get failed, got error", r.Error)
|
|
// } else if r.String() != name {
|
|
// t.Fatal("test go get failed, name not match", r, name)
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// func TestJsAsyncEcho(t *testing.T) {
|
|
// ch := make(chan bool, runTimes)
|
|
// t1 := time.Now().UnixMilli()
|
|
// for i := 0; i < runTimes; i++ {
|
|
// go func() {
|
|
// name := u.UniqueId()
|
|
// r, err := rt.RunCode("test('" + name + "')")
|
|
// ch <- true
|
|
// if err != nil {
|
|
// t.Fatal("test js async get failed, got error", err)
|
|
// } else if r != name {
|
|
// t.Fatal("test js async get failed, name not match", r, name)
|
|
// }
|
|
// }()
|
|
// }
|
|
// for i := 0; i < runTimes; i++ {
|
|
// <-ch
|
|
// }
|
|
// t2 := time.Now().UnixMilli() - t1
|
|
// fmt.Println(u.BGreen("js async test time:"), t2, "ms")
|
|
// }
|
|
|
|
// func TestGoAsyncEcho(t *testing.T) {
|
|
// hc := httpclient.GetClientH2C(0)
|
|
// ch := make(chan bool, runTimes*10)
|
|
// t1 := time.Now().UnixMilli()
|
|
// lastName := ""
|
|
// lastResult := ""
|
|
// for i := 0; i < runTimes*10; i++ {
|
|
// name := fmt.Sprint("N", i)
|
|
// lastName = name
|
|
// go func() {
|
|
// r := hc.Get("http://" + addr + "/echo?name=" + name)
|
|
// lastResult = r.String()
|
|
// ch <- true
|
|
// if r.Error != nil {
|
|
// t.Fatal("test go async get failed, got error", r.Error)
|
|
// } else if r.String() != name {
|
|
// t.Fatal("test go async get failed, name not match", r, name)
|
|
// }
|
|
// }()
|
|
// }
|
|
// for i := 0; i < runTimes*10; i++ {
|
|
// <-ch
|
|
// }
|
|
// t2 := time.Now().UnixMilli() - t1
|
|
// fmt.Println(u.BGreen("go async test time:"), t2, "ms")
|
|
// fmt.Println(u.BGreen("last name:"), lastName, lastResult)
|
|
// }
|
|
|
|
func TestStop(t *testing.T) {
|
|
go func() {
|
|
time.Sleep(100 * time.Millisecond)
|
|
_, _ = rt.RunCode("s.stop()")
|
|
}()
|
|
gojs.WaitAll()
|
|
time.Sleep(100 * time.Millisecond)
|
|
// if err != nil {
|
|
// t.Fatal("stop failed", err)
|
|
// }
|
|
}
|