Skip to content

Commit

Permalink
make trace auto fail next port
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Apr 19, 2024
1 parent 965c202 commit 9028d0e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
53 changes: 37 additions & 16 deletions cmd/xgo/trace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"os/exec"
"runtime"
"runtime/debug"
"strconv"
"strings"
"syscall"
"time"

"github.com/xhd2015/xgo/support/cmd"
Expand Down Expand Up @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand Down
7 changes: 2 additions & 5 deletions doc/demo/demo_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package demo

import (
"context"
"testing"

"github.com/xhd2015/xgo/runtime/core"
"github.com/xhd2015/xgo/runtime/mock"
)

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" {
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down

0 comments on commit 9028d0e

Please sign in to comment.