Skip to content

Commit

Permalink
fix snapshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Nov 10, 2024
1 parent fc8ddef commit 4415cd5
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 66 deletions.
19 changes: 16 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,30 @@ go run ./script/run-test

This will run all tests with all go versions found at the directory `go-release`.

If there isn't any, the default go is used.

Run a specific test:
```sh
# list all tests runnable by names
go run ./script/run-test --list

# run test by name
go run ./script/run-test --name trace-snapshot
# -a: reset caches
go run ./script/run-test --name trace-snapshot -run TestNoSnapshot -v --debug -a
```

We can also explicitly specify all expected go versions we want to pass:
```sh
go run ./script/run-test/ --include go1.17.13 --include go1.18.10 --include go1.19.13 --include go1.20.14 --include go1.21.8 --include go1.22.1
go run ./script/run-test --include go1.17.13 --include go1.18.10 --include go1.19.13 --include go1.20.14 --include go1.21.8 --include go1.22.1
```

If there were testing cache, we can force the test to re-run by adding a `-count=1` flag:
```sh
go run ./script/run-test/ --include go1.17.13 --include go1.18.10 --include go1.19.13 --include go1.20.14 --include go1.21.8 --include go1.22.1 -count=1
go run ./script/run-test --include go1.17.13 --include go1.18.10 --include go1.19.13 --include go1.20.14 --include go1.21.8 --include go1.22.1 -count=1
```

If a go version is not found in `go-release`, we can download it with:
If a go version is not found in `go-release`, it can be downloaded via:
```sh
go run ./script/download-go go1.22.1
```
Expand Down
70 changes: 51 additions & 19 deletions cmd/xgo/runtime_gen/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,24 @@ type CollectOptions struct {
// ignore result, will be set to nil
IgnoreResults bool
}
type flagType int

const (
flagType_default flagType = 0
flagType_true flagType = 1
flagType_false flagType = 2
)

type collectOpts struct {
name string
onComplete func(root *Root)
filters []func(stack *Stack) bool
postFilters []func(stack *Stack)
snapshotFilters []func(stack *Stack) bool
root *Root
options *CollectOptions
exportOptions *ExportOptions
name string
onComplete func(root *Root)
filters []func(stack *Stack) bool
postFilters []func(stack *Stack)
snapshotFilters []func(stack *Stack) bool
disableSnapshotMainModule flagType
root *Root
options *CollectOptions
exportOptions *ExportOptions
}

func Options() *collectOpts {
Expand Down Expand Up @@ -196,6 +204,19 @@ func (c *collectOpts) WithSnapshot(f func(stack *Stack) bool) *collectOpts {
return c
}

func (c *collectOpts) DisableSnapshotMainModule(v ...bool) *collectOpts {
b := true
if len(v) > 0 {
b = v[0]
}
flagType := flagType_false
if b {
flagType = flagType_true
}
c.disableSnapshotMainModule = flagType
return c
}

func (c *collectOpts) WithOptions(opts *CollectOptions) *collectOpts {
c.options = opts
return c
Expand Down Expand Up @@ -263,37 +284,34 @@ func handleTracePre(ctx context.Context, f *core.FuncInfo, args core.Object, res
var globalRoot interface{}
var localRoot *Root
var initial bool

var snapshotTrace bool
var checkMainModuleSnapshot bool
if localOpts == nil {
var globalLoaded bool
globalRoot, globalLoaded = stackMap.Load(key)
if !globalLoaded {
initial = true
}
checkMainModuleSnapshot = flags.STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT != "false"
} else {
if !checkFilters(stack, localOpts.filters) {
// do not collect trace if filtered out
return nil, trap.ErrSkip
}
var anySnapshot bool
for _, f := range localOpts.snapshotFilters {
if f(stack) {
anySnapshot = true
snapshotTrace = true
break
}
}

// check if allow main module to be defaultly snapshoted
if !anySnapshot && flags.STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT != "false" && effectMainModule != "" && strings.HasPrefix(f.Pkg, effectMainModule) {
// main_module or main_module/*
if len(f.Pkg) == len(effectMainModule) || f.Pkg[len(effectMainModule)] == '/' {
// fmt.Fprintf(os.Stderr, "DEBUG main module snapshot: %s of %s\n", f.Pkg, effectMainModule)
anySnapshot = true
if !snapshotTrace && localOpts.disableSnapshotMainModule != flagType_true {
if (localOpts.disableSnapshotMainModule == flagType_default && flags.STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT != "false") || localOpts.disableSnapshotMainModule == flagType_false {
checkMainModuleSnapshot = true
}
}
if anySnapshot {
stack.Snapshot = true
stack.Args = premarshal(stack.Args)
}

localRoot = localOpts.root
if localRoot == nil {
Expand All @@ -308,6 +326,20 @@ func handleTracePre(ctx context.Context, f *core.FuncInfo, args core.Object, res
}
}
}
if checkMainModuleSnapshot {
if effectMainModule != "" && strings.HasPrefix(f.Pkg, effectMainModule) {
// main_module or main_module/*
if len(f.Pkg) == len(effectMainModule) || f.Pkg[len(effectMainModule)] == '/' {
// fmt.Fprintf(os.Stderr, "DEBUG main module snapshot: %s of %s\n", f.Pkg, effectMainModule)
snapshotTrace = true
}
}
}
if snapshotTrace {
stack.Snapshot = true
stack.Args = premarshal(stack.Args)
}

if initial {
// initial stack
root := &Root{
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import "fmt"
// VERSION is manually updated when needed a new tag
// see also runtime/core/version.go
const VERSION = "1.0.51"
const REVISION = "38209d904dbe681a458641ad5e7f1c408c8a3626+1"
const NUMBER = 320
const REVISION = "fc8ddef4241567f46ff39cafac1b88c44a9edca4+1"
const NUMBER = 321

// the matching runtime/core's version
// manually updated
Expand Down
13 changes: 13 additions & 0 deletions runtime/test/timeout/timeout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package timeout

import (
"testing"
"time"
)

// go run ./script/run-test --name timeout --debug -v
func TestTimeout(t *testing.T) {
time.Sleep(600 * time.Millisecond)

t.Errorf("this test will fail after 600ms, however it should be captured earlier by the test runner and gets ignored")
}
7 changes: 1 addition & 6 deletions runtime/test/trace/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ import (
// NOTE: do not run with -cover, because
// extra function will be included in trace

// TODO: this test currently fails, but
// it is not so urgent to fix it, so
// I'll keep it here.
// once I got enough time to do, I'll
// try my best.
// see related https://github.com/xhd2015/xgo/issues/281
func TestNoSnapshot(t *testing.T) {
test(t, noSnapshotExpect, nil)
}
Expand All @@ -35,6 +29,7 @@ func test(t *testing.T, expectTrace string, f func(stack *trace.Stack) bool) {
if f != nil {
opts.WithSnapshot(f)
}
opts.DisableSnapshotMainModule()
opts.OnComplete(func(root *trace.Root) {
record = root
}).Collect(func() {
Expand Down
70 changes: 51 additions & 19 deletions runtime/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,24 @@ type CollectOptions struct {
// ignore result, will be set to nil
IgnoreResults bool
}
type flagType int

const (
flagType_default flagType = 0
flagType_true flagType = 1
flagType_false flagType = 2
)

type collectOpts struct {
name string
onComplete func(root *Root)
filters []func(stack *Stack) bool
postFilters []func(stack *Stack)
snapshotFilters []func(stack *Stack) bool
root *Root
options *CollectOptions
exportOptions *ExportOptions
name string
onComplete func(root *Root)
filters []func(stack *Stack) bool
postFilters []func(stack *Stack)
snapshotFilters []func(stack *Stack) bool
disableSnapshotMainModule flagType
root *Root
options *CollectOptions
exportOptions *ExportOptions
}

func Options() *collectOpts {
Expand Down Expand Up @@ -196,6 +204,19 @@ func (c *collectOpts) WithSnapshot(f func(stack *Stack) bool) *collectOpts {
return c
}

func (c *collectOpts) DisableSnapshotMainModule(v ...bool) *collectOpts {
b := true
if len(v) > 0 {
b = v[0]
}
flagType := flagType_false
if b {
flagType = flagType_true
}
c.disableSnapshotMainModule = flagType
return c
}

func (c *collectOpts) WithOptions(opts *CollectOptions) *collectOpts {
c.options = opts
return c
Expand Down Expand Up @@ -263,37 +284,34 @@ func handleTracePre(ctx context.Context, f *core.FuncInfo, args core.Object, res
var globalRoot interface{}
var localRoot *Root
var initial bool

var snapshotTrace bool
var checkMainModuleSnapshot bool
if localOpts == nil {
var globalLoaded bool
globalRoot, globalLoaded = stackMap.Load(key)
if !globalLoaded {
initial = true
}
checkMainModuleSnapshot = flags.STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT != "false"
} else {
if !checkFilters(stack, localOpts.filters) {
// do not collect trace if filtered out
return nil, trap.ErrSkip
}
var anySnapshot bool
for _, f := range localOpts.snapshotFilters {
if f(stack) {
anySnapshot = true
snapshotTrace = true
break
}
}

// check if allow main module to be defaultly snapshoted
if !anySnapshot && flags.STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT != "false" && effectMainModule != "" && strings.HasPrefix(f.Pkg, effectMainModule) {
// main_module or main_module/*
if len(f.Pkg) == len(effectMainModule) || f.Pkg[len(effectMainModule)] == '/' {
// fmt.Fprintf(os.Stderr, "DEBUG main module snapshot: %s of %s\n", f.Pkg, effectMainModule)
anySnapshot = true
if !snapshotTrace && localOpts.disableSnapshotMainModule != flagType_true {
if (localOpts.disableSnapshotMainModule == flagType_default && flags.STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT != "false") || localOpts.disableSnapshotMainModule == flagType_false {
checkMainModuleSnapshot = true
}
}
if anySnapshot {
stack.Snapshot = true
stack.Args = premarshal(stack.Args)
}

localRoot = localOpts.root
if localRoot == nil {
Expand All @@ -308,6 +326,20 @@ func handleTracePre(ctx context.Context, f *core.FuncInfo, args core.Object, res
}
}
}
if checkMainModuleSnapshot {
if effectMainModule != "" && strings.HasPrefix(f.Pkg, effectMainModule) {
// main_module or main_module/*
if len(f.Pkg) == len(effectMainModule) || f.Pkg[len(effectMainModule)] == '/' {
// fmt.Fprintf(os.Stderr, "DEBUG main module snapshot: %s of %s\n", f.Pkg, effectMainModule)
snapshotTrace = true
}
}
}
if snapshotTrace {
stack.Snapshot = true
stack.Args = premarshal(stack.Args)
}

if initial {
// initial stack
root := &Root{
Expand Down
Loading

0 comments on commit 4415cd5

Please sign in to comment.