-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
102 lines (91 loc) · 2.32 KB
/
pool_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
package glu
import (
"fmt"
"github.com/ZenLiuCN/fn"
. "github.com/chzyer/test"
. "github.com/yuin/gopher-lua"
"testing"
)
var (
chunk1 *FunctionProto
chunk2 *FunctionProto
)
func init() {
var err error
chunk1, err = CompileChunk(`local a=1+1`, `bench`)
if err != nil {
panic(err)
}
chunk2, err = CompileChunk(`local a=1+1; assert(a~=2)`, `bench`)
if err != nil {
panic(err)
}
}
func BenchmarkPoolWithoutClose(b *testing.B) {
for i := 0; i < b.N; i++ {
x := Get()
if i%2 == 0 {
x.Push(x.NewFunctionFromProto(chunk1))
} else {
x.Push(x.NewFunctionFromProto(chunk2))
}
err := x.PCall(0, 0, nil)
if err != nil {
Put(x)
continue
}
Put(x)
}
}
func BenchmarkPoolWithClose(b *testing.B) {
for i := 0; i < b.N; i++ {
x := Get()
if i%2 == 0 {
x.Push(x.NewFunctionFromProto(chunk1))
} else {
x.Push(x.NewFunctionFromProto(chunk2))
}
err := x.PCall(0, 0, nil)
if err != nil {
x.Close()
continue
}
Put(x)
}
}
func TestStateTrace(t *testing.T) {
l := Get()
err := l.DoString(`
a=1 print(tostring(a).."1+")
`)
if err != nil {
return
}
println("before put: ", l.Polluted())
Put(l)
println("after put: ", l.Polluted())
}
func TestGluI1(t *testing.T) {
vmPool := CreatePool()
vm := vmPool.Get()
NotNil(vm)
vmaddr := fmt.Sprintf("%p", vm)
Equal(LNil, vm.GetGlobal("__SOME_KEY__")) // Passed
fn.Panic(vm.DoString("assert(__SOME_KEY__ == nil)")) // Passed, __SOME_KEY__ is nil in and out of lua
vm.SetGlobal("__SOME_KEY__", LString("ZZZZZZ"))
Equal(LString("ZZZZZZ"), vm.GetGlobal("__SOME_KEY__")) // Passed
fn.Panic(vm.DoString("assert(__SOME_KEY__ == 'ZZZZZZ')")) // Passed, __SOME_KEY__ is same in and out of lua
vmPool.Put(vm) // Recycle the vm
vm = vmPool.Get()
vmaddr2 := fmt.Sprintf("%p", vm)
Equal(vmaddr, vmaddr2) // vm is actually recycled
// Something strange happened....
// Global is not reset after recycle
Equal(LNil, vm.GetGlobal("__SOME_KEY__")) // Assertion failed, Value still exists out of lua
// But I cannot find it in lua, __SOME_KEY__ is nil
fn.Panic(vm.DoString("assert(__SOME_KEY__ == nil)")) // Passed, Nothing in lua
// What if I set it again?
vm.SetGlobal("__SOME_KEY__", LString("YYYYYY"))
Equal(LString("YYYYYY"), vm.GetGlobal("__SOME_KEY__")) // Passed
fn.Panic(vm.DoString("assert(__SOME_KEY__ == 'YYYYYY')")) // Passed
}