diff --git a/cmd/xgo/trace/main.go b/cmd/xgo/trace/main.go index b106b798..1dfbaf48 100644 --- a/cmd/xgo/trace/main.go +++ b/cmd/xgo/trace/main.go @@ -12,7 +12,9 @@ import ( "os/exec" "runtime" "runtime/debug" + "strconv" "strings" + "syscall" "time" "github.com/xhd2015/xgo/support/cmd" @@ -77,24 +79,43 @@ func serveFile(file string) { } w.Write(output) }) - port := os.Getenv("PORT") - if port == "" { - port = "7070" + var autoIncrPort bool + var port int + portStr := os.Getenv("PORT") + if portStr == "" { + port = 7070 + portStr = strconv.Itoa(port) + autoIncrPort = true } - url := fmt.Sprintf("http://localhost:%s", port) - fmt.Printf("Server listen at %s\n", url) - - go func() { - time.Sleep(500 * time.Millisecond) - openCmd := "open" - if runtime.GOOS == "windows" { - openCmd = "explorer" - } - cmd.Run(openCmd, url) - }() - err := http.ListenAndServe(fmt.Sprintf(":%s", port), server) - if err != nil { + for { + portErr := make(chan struct{}) + go func() { + select { + case <-time.After(500 * time.Millisecond): + case <-portErr: + return + } + url := fmt.Sprintf("http://localhost:%s", portStr) + fmt.Printf("Server listen at %s\n", url) + + openCmd := "open" + if runtime.GOOS == "windows" { + openCmd = "explorer" + } + cmd.Run(openCmd, url) + }() + err := http.ListenAndServe(fmt.Sprintf(":%s", portStr), server) + if err == nil { + break + } + close(portErr) + var syscallErr syscall.Errno + if autoIncrPort && errors.As(err, &syscallErr) && syscallErr == syscall.EADDRINUSE { + port++ + portStr = strconv.Itoa(port) + continue + } panic(err) } } diff --git a/cmd/xgo/version.go b/cmd/xgo/version.go index b809324c..1e26512d 100644 --- a/cmd/xgo/version.go +++ b/cmd/xgo/version.go @@ -3,8 +3,8 @@ package main import "fmt" const VERSION = "1.0.25" -const REVISION = "ae8695c56e8c7f1976d409c7fba953041983c299+1" -const NUMBER = 187 +const REVISION = "965c202f092256db5171d680d92030aa720cca4d+1" +const NUMBER = 188 func getRevision() string { revSuffix := "" diff --git a/doc/demo/demo_test.go b/doc/demo/demo_test.go index 4a7184df..c1701c77 100644 --- a/doc/demo/demo_test.go +++ b/doc/demo/demo_test.go @@ -1,10 +1,8 @@ package demo import ( - "context" "testing" - "github.com/xhd2015/xgo/runtime/core" "github.com/xhd2015/xgo/runtime/mock" ) @@ -12,9 +10,8 @@ func MyFunc() string { return "my func" } func TestFuncMock(t *testing.T) { - mock.Mock(MyFunc, func(ctx context.Context, fn *core.FuncInfo, args core.Object, results core.Object) error { - results.GetFieldIndex(0).Set("mock func") - return nil + mock.Patch(MyFunc, func() string { + return "mock func" }) text := MyFunc() if text != "mock func" { diff --git a/runtime/core/version.go b/runtime/core/version.go index 61342fab..63e7b7c4 100644 --- a/runtime/core/version.go +++ b/runtime/core/version.go @@ -7,8 +7,8 @@ import ( ) const VERSION = "1.0.25" -const REVISION = "ae8695c56e8c7f1976d409c7fba953041983c299+1" -const NUMBER = 187 +const REVISION = "965c202f092256db5171d680d92030aa720cca4d+1" +const NUMBER = 188 // these fields will be filled by compiler const XGO_VERSION = ""