Skip to content

Commit

Permalink
kill after browser is ready to be killed
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Sep 17, 2020
1 parent 3da9455 commit de69d06
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
17 changes: 11 additions & 6 deletions browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,20 @@ func (s *S) TestMonitor() {
s.EqualValues(-32602, utils.MustReadJSON(res.Body).Get("code").Int())
}

func (s *S) TestMonitorEnv() {
defaults.Monitor = ":0"
func (s *S) TestMonitorErr() {
defaults.Monitor = "abc"
defer defaults.ResetWithEnv()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
l := launcher.New()
u := l.MustLaunch()
defer func() {
utils.Sleep(1) // if kill too fast, the parent process of the browser may not be ready
l.Kill()
}()

b := rod.New().Context(ctx).MustConnect()
defer b.MustClose()
s.Panics(func() {
rod.New().ControlURL(u).MustConnect()
})
}

func (s *S) TestRemoteLaunch() {
Expand Down
17 changes: 9 additions & 8 deletions lib/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (l *Launcher) Launch() (string, error) {

u, err := l.getURL()
if err != nil {
go l.kill()
l.Kill()
return "", err
}

Expand Down Expand Up @@ -371,6 +371,14 @@ func (l *Launcher) PID() int {
return l.pid
}

// Kill the browser process
func (l *Launcher) Kill() {
p, err := os.FindProcess(l.PID())
if err == nil {
_ = p.Kill()
}
}

// Cleanup wait until the Browser exits and release related resources
func (l *Launcher) Cleanup() {
<-l.exit
Expand All @@ -379,13 +387,6 @@ func (l *Launcher) Cleanup() {
_ = os.RemoveAll(dir)
}

func (l *Launcher) kill() {
p, err := os.FindProcess(l.pid)
if err == nil {
_ = p.Kill()
}
}

func (l *Launcher) normalizeFlag(name string) string {
return strings.TrimLeft(name, "-")
}
16 changes: 8 additions & 8 deletions lib/launcher/launcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func TestLaunch(t *testing.T) {
defer func() { defaults.ResetWithEnv() }()

l := launcher.New()
defer func() { kill(l.PID()) }()
defer func() {
utils.Sleep(1)
l.Kill()
}()

url := l.MustLaunch()

Expand All @@ -57,7 +60,10 @@ func TestLaunch(t *testing.T) {

func TestLaunchUserMode(t *testing.T) {
l := launcher.NewUserMode()
defer func() { kill(l.PID()) }()
defer func() {
utils.Sleep(1)
l.Kill()
}()

_, has := l.Get("not-exists")
assert.False(t, has)
Expand Down Expand Up @@ -123,9 +129,3 @@ func skipDownload(t *testing.T) {
t.SkipNow()
}
}

func kill(pid int) {
ps, err := os.FindProcess(pid)
utils.E(err)
utils.E(ps.Kill())
}
2 changes: 1 addition & 1 deletion lib/launcher/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (p *Proxy) launch(w http.ResponseWriter, r *http.Request) {

u := l.MustLaunch()
defer func() {
l.kill()
l.Kill()

if _, has := l.Get(flagKeepUserDataDir); !has {
l.Cleanup()
Expand Down

0 comments on commit de69d06

Please sign in to comment.