mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-07 08:27:35 +08:00
fix: listen install result by callback
This commit is contained in:
@@ -320,50 +320,38 @@ func (dev *AndroidDevice) installViaInstaller(app io.ReadSeeker, args ...string)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
quit := make(chan struct{})
|
|
||||||
done := make(chan error)
|
done := make(chan error)
|
||||||
defer func() { close(quit) }()
|
|
||||||
// 需要监听是否完成安装
|
|
||||||
go func() {
|
|
||||||
logcat := NewAdbLogcat(dev.d.Serial())
|
|
||||||
err = logcat.CatchLogcat("PackageInstallerCallback")
|
|
||||||
if err != nil {
|
|
||||||
done <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
scanner := bufio.NewScanner(logcat.reader)
|
|
||||||
defer func() {
|
defer func() {
|
||||||
close(done)
|
close(done)
|
||||||
_ = logcat.Stop()
|
|
||||||
}()
|
}()
|
||||||
for scanner.Scan() {
|
logcat := NewAdbLogcatWithCallback(dev.d.Serial(), func(line string) {
|
||||||
select {
|
|
||||||
case <-quit:
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
line := scanner.Text()
|
|
||||||
re := regexp.MustCompile(`\{.*?}`)
|
re := regexp.MustCompile(`\{.*?}`)
|
||||||
match := re.FindString(line)
|
match := re.FindString(line)
|
||||||
if match == "" {
|
if match == "" {
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
var result InstallResult
|
var result InstallResult
|
||||||
err := json.Unmarshal([]byte(match), &result)
|
err := json.Unmarshal([]byte(match), &result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn().Msg("parse Install msg line error: " + match)
|
log.Warn().Msg("parse Install msg line error: " + match)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
if result.Result == 0 {
|
if result.Result == 0 {
|
||||||
// 安装成功
|
// 安装成功
|
||||||
done <- nil
|
done <- nil
|
||||||
return
|
|
||||||
} else {
|
} else {
|
||||||
done <- errors.New(match)
|
done <- errors.New(match)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
err = logcat.CatchLogcat("PackageInstallerCallback")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
defer func() {
|
||||||
done <- errors.New("install failed by installer")
|
_ = logcat.Stop()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// 需要监听是否完成安装
|
||||||
args = strings.Split(InstallViaInstallerCommand, " ")
|
args = strings.Split(InstallViaInstallerCommand, " ")
|
||||||
args = append(args, appRemotePath)
|
args = append(args, appRemotePath)
|
||||||
_, err = dev.d.RunShellCommand("am", args[1:]...)
|
_, err = dev.d.RunShellCommand("am", args[1:]...)
|
||||||
@@ -374,7 +362,7 @@ func (dev *AndroidDevice) installViaInstaller(app io.ReadSeeker, args ...string)
|
|||||||
timeout := 1 * time.Minute
|
timeout := 1 * time.Minute
|
||||||
select {
|
select {
|
||||||
case err := <-done:
|
case err := <-done:
|
||||||
return err // 返回安装结果或错误
|
return err
|
||||||
case <-time.After(timeout):
|
case <-time.After(timeout):
|
||||||
return fmt.Errorf("installation timed out after %v", timeout)
|
return fmt.Errorf("installation timed out after %v", timeout)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user