| 1 |
// +build race |
| 2 |
|
| 3 |
package luar |
| 4 |
|
| 5 |
import ( |
| 6 |
"runtime" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/yuin/gopher-lua" |
| 10 |
) |
| 11 |
|
| 12 |
func Test_functhread(t *testing.T) { |
| 13 |
L := lua.NewState() |
| 14 |
defer L.Close() |
| 15 |
|
| 16 |
var fn func(x int) string |
| 17 |
L.SetGlobal("fn", New(L, &fn)) |
| 18 |
|
| 19 |
if err := L.DoString(`_ = fn ^ function(x) return tostring(x) .. "!" end`); err != nil { |
| 20 |
t.Fatal(err) |
| 21 |
} |
| 22 |
|
| 23 |
done := make(chan struct{}) |
| 24 |
defer close(done) |
| 25 |
go func() { |
| 26 |
for { |
| 27 |
select { |
| 28 |
case <-done: |
| 29 |
return |
| 30 |
default: |
| 31 |
} |
| 32 |
|
| 33 |
L.Push(lua.LNumber(1000)) |
| 34 |
|
| 35 |
runtime.Gosched() |
| 36 |
} |
| 37 |
}() |
| 38 |
|
| 39 |
if ret := fn(123); ret != "123!" { |
| 40 |
t.Fatalf("expected %#v, got %#v", "123!", ret) |
| 41 |
} |
| 42 |
} |