Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make trace fail auto try next port #74

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading