Skip to content

Commit

Permalink
Add test for issue 87
Browse files Browse the repository at this point in the history
  • Loading branch information
Anonymous authored and eh-steve committed Dec 7, 2023
1 parent 58fac1d commit 2133bbf
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
29 changes: 29 additions & 0 deletions jit/jit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,35 @@ func TestJitEmbeddedStruct(t *testing.T) {
}
}

func TestSchedule(t *testing.T) {
conf := baseConfig
data := testData{
files: []string{"./testdata/test_schedule/test.go"},
pkg: "testdata/test_schedule",
}
testNames := []string{"BuildGoFiles", "BuildGoPackage", "BuildGoText"}

for _, testName := range testNames {
t.Run(testName, func(t *testing.T) {
module, symbols := buildLoadable(t, conf, testName, data)

test := symbols["Test"].(func())
for i := 0; i < 100; i++ {
fmt.Println("Test ", i)
test()
}

err := module.Unload()
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
})
}
}

func TestJitCGoCall(t *testing.T) {
if os.Getenv("CGO_ENABLED") == "0" {
t.Skip("CGo disabled")
Expand Down
82 changes: 82 additions & 0 deletions jit/testdata/test_schedule/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package test_schedule

import (
"fmt"
"runtime"
"sync"
"time"
)

func Test() {
var ch = make(chan bool, 20000)
var begin = make(chan bool)
var closeCh = make(chan bool)

w := sync.WaitGroup{}

w.Add(1)
go func() {
defer logPanic()
runtime.LockOSThread()
<-begin
fmt.Println("begin")
tm := time.Now()
for i := 0; i < 10000000; i++ {
<-ch
}
fmt.Println(time.Now().Sub(tm))
close(closeCh)
w.Done()
}()

for i := 0; i < 50000; i++ {
w.Add(1)
go func() {
defer logPanic()
var count int
load := 100
for {
count++
re := fmt.Sprint("haha")
if re == "haha" && count >= load {
count = 0
runtime.Gosched()
}
select {
case <-closeCh:
w.Done()
return
default:
}
}
}()
}

for i := 0; i < 20; i++ {
w.Add(1)
go func() {
defer logPanic()
for {
select {
case <-closeCh:
w.Done()
return
case ch <- true:
}
}
}()
}

fmt.Println("all start")
begin <- true

w.Wait()
}

func logPanic() {
if r := recover(); r != nil {
trace := make([]byte, 1024)
count := runtime.Stack(trace, false)
fmt.Printf("Stack of %d bytes: %s\n", count, trace[:count])
}
}

0 comments on commit 2133bbf

Please sign in to comment.