2026-05-30 14:21:43 +08:00
|
|
|
package js
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
2026-06-08 20:47:30 +08:00
|
|
|
|
|
|
|
|
"apigo.cc/go/cast"
|
2026-05-30 14:21:43 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestPoolVersioning(t *testing.T) {
|
2026-06-08 20:47:30 +08:00
|
|
|
p := NewPool()
|
2026-05-30 14:21:43 +08:00
|
|
|
// 1. Define initial function
|
2026-06-08 20:47:30 +08:00
|
|
|
p.Define(`function hello(name) { return "Hello " + name; }`, "hello.js", int64(100))
|
|
|
|
|
|
|
|
|
|
if !p.CheckVersion("hello.js", 100) {
|
|
|
|
|
t.Error("expected CheckVersion to be true for v100")
|
|
|
|
|
}
|
|
|
|
|
if p.CheckVersion("hello.js", 101) {
|
|
|
|
|
t.Error("expected CheckVersion to be false for v101")
|
|
|
|
|
}
|
2026-05-30 14:21:43 +08:00
|
|
|
|
2026-06-08 20:47:30 +08:00
|
|
|
res, err := p.Call(context.Background(), "hello", []any{"World"})
|
2026-05-30 14:21:43 +08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if res != "Hello World" {
|
|
|
|
|
t.Errorf("expected 'Hello World', got %v", res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Define new function (incremental update)
|
2026-06-08 20:47:30 +08:00
|
|
|
p.Define(`function add(a, b) { return a + b; }`)
|
2026-05-30 14:21:43 +08:00
|
|
|
|
2026-06-08 20:47:30 +08:00
|
|
|
res, err = p.Call(context.Background(), "add", []any{1, 2})
|
2026-05-30 14:21:43 +08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-06-08 20:47:30 +08:00
|
|
|
if cast.To[int64](res) != 3 {
|
2026-05-30 14:21:43 +08:00
|
|
|
t.Errorf("expected 3, got %v", res)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-08 20:47:30 +08:00
|
|
|
// 3. Check FuncList
|
|
|
|
|
funcs := p.FuncList()
|
|
|
|
|
foundHello := false
|
|
|
|
|
foundAdd := false
|
|
|
|
|
for _, f := range funcs {
|
|
|
|
|
if f == "hello" {
|
|
|
|
|
foundHello = true
|
|
|
|
|
}
|
|
|
|
|
if f == "add" {
|
|
|
|
|
foundAdd = true
|
|
|
|
|
}
|
2026-05-30 14:21:43 +08:00
|
|
|
}
|
2026-06-08 20:47:30 +08:00
|
|
|
if !foundHello || !foundAdd {
|
|
|
|
|
t.Errorf("FuncList missing functions: %v", funcs)
|
2026-05-30 14:21:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPoolConcurrent(t *testing.T) {
|
|
|
|
|
Define(`function heavy(n) {
|
|
|
|
|
let s = 0;
|
|
|
|
|
for(let i=0; i<n; i++) s += i;
|
|
|
|
|
return s;
|
|
|
|
|
}`)
|
|
|
|
|
|
|
|
|
|
t.Run("Parallel", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
go func() {
|
2026-05-30 15:33:57 +08:00
|
|
|
_, _ = Call(context.Background(), "heavy", []any{1000})
|
2026-05-30 14:21:43 +08:00
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|