Skip to content
Open
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
7 changes: 6 additions & 1 deletion devices/devicekit/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ type jsonRPCError struct {
Message string `json:"message"`
}

const defaultRPCTimeout = 10 * time.Second
// a var so tests can shorten it
var defaultRPCTimeout = 10 * time.Second

// dumpUITimeout gives the agent time to snapshot a heavy screen, while keeping
// the request below the http client's own timeout
const dumpUITimeout = 55 * time.Second

func (c *DeviceKitClient) CallRPC(method string, params any) (json.RawMessage, error) {
return c.CallRPCWithTimeout(method, params, defaultRPCTimeout)
Expand Down
4 changes: 2 additions & 2 deletions devices/devicekit/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func filterSourceElements(source sourceTreeElement) []types.ScreenElement {
func (c *DeviceKitClient) GetSourceRaw() (any, error) {
startTime := time.Now()

result, err := c.CallRPC("device.dump.ui", map[string]string{"format": "raw"})
result, err := c.CallRPCWithTimeout("device.dump.ui", map[string]string{"format": "raw"}, dumpUITimeout)
if err != nil {
return nil, fmt.Errorf("failed to get source: %w", err)
}
Expand All @@ -148,7 +148,7 @@ func (c *DeviceKitClient) GetSourceRaw() (any, error) {
func (c *DeviceKitClient) GetSourceElements() ([]types.ScreenElement, error) {
startTime := time.Now()

result, err := c.CallRPC("device.dump.ui", map[string]string{"format": "json"})
result, err := c.CallRPCWithTimeout("device.dump.ui", map[string]string{"format": "json"}, dumpUITimeout)
if err != nil {
return nil, err
}
Expand Down
36 changes: 36 additions & 0 deletions devices/devicekit/source_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package devicekit

import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/mobile-next/mobilecli/types"
)
Expand Down Expand Up @@ -543,3 +546,36 @@ func TestFilterSourceElementsDoesNotMarkNonSwitchAsChecked(t *testing.T) {
t.Errorf("expected checked to be unset, got %v", *output[0].Checked)
}
}

// Snapshotting a heavy screen can take the agent longer than the default RPC
// timeout; dump ui has to wait for it instead of failing with "context
// deadline exceeded", see https://github.com/mobile-next/mobilecli/issues/448
func TestDumpUIWaitsForAnAgentSlowerThanTheDefaultRPCTimeout(t *testing.T) {
previous := defaultRPCTimeout
defaultRPCTimeout = 50 * time.Millisecond
t.Cleanup(func() { defaultRPCTimeout = previous })

slowAgent := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(200 * time.Millisecond)
_, _ = w.Write([]byte(`{"jsonrpc":"2.0","id":1,"result":{"type":"XCUIElementTypeApplication","rect":{"x":0,"y":0,"width":390,"height":844},"children":[{"type":"XCUIElementTypeButton","label":"Done","rect":{"x":16,"y":62,"width":44,"height":44}}]}}`))
}))
defer slowAgent.Close()

client := NewDeviceKitClient(slowAgent.URL)

if _, err := client.CallRPC("device.info", nil); err == nil {
t.Fatal("expected the agent to be slower than the default RPC timeout")
}

elements, err := client.GetSourceElements()
if err != nil {
t.Fatalf("GetSourceElements: %v", err)
}
if len(elements) != 1 || elementLabel(elements[0]) != "Done" {
t.Errorf("expected the Done button, got %+v", elements)
}

if _, err := client.GetSourceRaw(); err != nil {
t.Fatalf("GetSourceRaw: %v", err)
}
}
1 change: 1 addition & 0 deletions server/deadline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestExtendedWriteDeadlineForSlowMethods(t *testing.T) {
"device.apps.install": 3 * time.Minute,
"device.apps.uninstall": 3 * time.Minute,
"device.screenrecord.stop": 35 * time.Second,
"device.dump.ui": 60 * time.Second,
}

for method, want := range slowMethods {
Expand Down
2 changes: 2 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ func extendedWriteDeadline(method string) (time.Duration, bool) {
return 3 * time.Minute, true
case "device.screenrecord.stop":
return 35 * time.Second, true
case "device.dump.ui":
return 60 * time.Second, true
}
return 0, false
}
Expand Down