change: use context to contral ScreenRecord timeout or cancel

This commit is contained in:
lilong.129
2025-03-06 22:16:49 +08:00
parent e8025f9e65
commit 0d416e74a1
7 changed files with 126 additions and 27 deletions
+31
View File
@@ -4,6 +4,7 @@ package gadb
import (
"bytes"
"context"
"os"
"reflect"
"strings"
@@ -295,6 +296,36 @@ func TestDevice_Pull(t *testing.T) {
}
}
func TestDevice_ScreenRecord(t *testing.T) {
setupDevices(t)
for _, dev := range devices {
// screen record with time limit 5 seconds
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
if _, err := dev.ScreenRecord(ctx); err != nil {
assert.Nil(t, err)
}
cancel()
}
for _, dev := range devices {
// screen record with cancel signal
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error)
go func() {
_, err := dev.ScreenRecord(ctx)
done <- err
}()
// record for 3 seconds
time.Sleep(time.Second * 3)
cancel()
err := <-done
assert.Nil(t, err)
}
}
func TestDevice_RunShellCommandBackgroundWithBytes(t *testing.T) {
type fields struct {
adbClient Client