github.com/suixinio/gopher-lua@v0.0.0-20230314172526-3c6bff009a9a/interrupt_test.go (about)

     1  package lua
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  // go test -timeout 30s -run ^Test_loop_close github.com/suixinio/rulex/test -v -count=1
    10  func Benchmark_Test_loop_close(b *testing.B) {
    11  	var s1 = `
    12  	function f()
    13  		while true do
    14  		print("Hello World")
    15  		end
    16  	end
    17  	f()
    18  `
    19  	var luaVM = NewState()
    20  	ctx, cancel := context.WithCancel(context.Background())
    21  	luaVM.SetContext(ctx)
    22  	go func() {
    23  		err := luaVM.DoString(s1)
    24  		if err != nil {
    25  			b.Fail()
    26  		}
    27  	}()
    28  	time.Sleep(1 * time.Second)
    29  	cancel()
    30  	b.Log("luaVM.cancel()")
    31  	luaVM.Close()
    32  	b.Log("luaVM.Close()")
    33  	time.Sleep(2 * time.Second)
    34  }
    35  func Test_loop_close(t *testing.T) {
    36  	var s1 = `
    37  		function f()
    38  			while true do
    39  			-- print("Hello World")
    40  			end
    41  		end
    42  		f()
    43  	`
    44  	var luaVM = NewState()
    45  	ctx, cancel := context.WithCancel(context.Background())
    46  	luaVM.SetContext(ctx)
    47  	go func() {
    48  		err := luaVM.DoString(s1)
    49  		if err != nil {
    50  			t.Fail()
    51  		}
    52  	}()
    53  	time.Sleep(1 * time.Second)
    54  	cancel()
    55  	t.Log("luaVM.cancel()")
    56  	luaVM.Close()
    57  	t.Log("luaVM.Close()")
    58  	time.Sleep(2 * time.Second)
    59  }