github.com/dfcfw/lua@v0.0.0-20230325031207-0cc7ffb7b8b9/luar/race_test.go (about)

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