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 cea26ef
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 49 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
71 changes: 52 additions & 19 deletions cmd/xgo/runtime_gen/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type testInfo struct {
var effectMainModule string

func init() {
fmt.Printf("flag: %s\n", flags.STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT)
if flags.MAIN_MODULE != "" {
// fmt.Fprintf(os.Stderr, "DEBUG main module from flags: %s\n", flags.MAIN_MODULE)
effectMainModule = flags.MAIN_MODULE
Expand Down Expand Up @@ -155,16 +156,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 +205,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 +285,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 +327,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
1 change: 1 addition & 0 deletions runtime/test/trace/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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
71 changes: 52 additions & 19 deletions runtime/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type testInfo struct {
var effectMainModule string

func init() {
fmt.Printf("flag: %s\n", flags.STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT)
if flags.MAIN_MODULE != "" {
// fmt.Fprintf(os.Stderr, "DEBUG main module from flags: %s\n", flags.MAIN_MODULE)
effectMainModule = flags.MAIN_MODULE
Expand Down Expand Up @@ -155,16 +156,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 +205,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 +285,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 +327,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
34 changes: 28 additions & 6 deletions script/run-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/xhd2015/xgo/cmd/xgo/exec_tool"
"github.com/xhd2015/xgo/support/cmd"
)

Expand Down Expand Up @@ -73,6 +72,12 @@ type TestCase struct {
skipOnCover bool
}

// can be selected via --name
// use:
//
// go run ./scrip/run-test --list
//
// to list all names
var extraSubTests = []*TestCase{
{
name: "trace_without_dep",
Expand Down Expand Up @@ -150,9 +155,6 @@ var extraSubTests = []*TestCase{
name: "trace-snapshot",
dir: "runtime/test/trace/snapshot",
skipOnCover: true,
// TODO: let program configure it
env: []string{exec_tool.XGO_STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT + "=false"},
// flags: []string{"--strace-snapshot-main-module-default=false"},
},
{
name: "trace-custom-dir",
Expand Down Expand Up @@ -227,6 +229,8 @@ func main() {
var coverpkgs []string
var coverprofile string
var names []string

var list bool
for i := 0; i < n; i++ {
arg := args[i]
if arg == "--exclude" {
Expand Down Expand Up @@ -311,6 +315,10 @@ func main() {
installXgo = true
continue
}
if arg == "--list" {
list = true
continue
}
if arg == "--" {
if i+1 < n {
remainArgs = append(remainArgs, args[i+1:]...)
Expand All @@ -324,6 +332,14 @@ func main() {
fmt.Fprintf(os.Stderr, "unknown flag: %s\n", arg)
os.Exit(1)
}
if list {
for _, test := range extraSubTests {
if test.name != "" {
fmt.Println(test.name)
}
}
return
}
if coverprofile != "" {
absProfile, err := filepath.Abs(coverprofile)
if err != nil {
Expand All @@ -334,11 +350,17 @@ func main() {
}
goRelease := "go-release"
var goroots []string
_, err := os.Stat(goRelease)
if err != nil {
_, statErr := os.Stat(goRelease)
if statErr != nil {
if !os.IsNotExist(statErr) {
fmt.Fprintf(os.Stderr, "stat: %v\n", statErr)
os.Exit(1)
return
}
// use default GOROOT
goroot := runtime.GOROOT()
if goroot == "" {
var err error
goroot, err = cmd.Output("go", "env", "GOROOT")
if err != nil {
fmt.Fprintf(os.Stderr, "cannot get GOROOT: %v\n", err)
Expand Down

0 comments on commit cea26ef

Please sign in to comment.