Skip to content

Commit 19db13f

Browse files
committed
fix: retry set mjpeg framerate on wda due to a bug in agent
1 parent 33cc290 commit 19db13f

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

devices/wda/appium-settings.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ package wda
33
import "fmt"
44

55
func (c *WdaClient) SetAppiumSettings(settings map[string]interface{}) error {
6-
// create wda session
6+
// get or create wda session
77
sessionId, err := c.GetOrCreateSession()
88
if err != nil {
9-
return fmt.Errorf("failed to create wda session: %w", err)
9+
return fmt.Errorf("failed to get or create wda session: %w", err)
1010
}
1111

1212
// post settings to appium endpoint
1313
endpoint := fmt.Sprintf("session/%s/appium/settings", sessionId)
1414
_, err = c.PostEndpoint(endpoint, map[string]interface{}{
1515
"settings": settings,
1616
})
17+
1718
if err != nil {
1819
return fmt.Errorf("failed to set appium settings: %w", err)
1920
}
@@ -22,11 +23,20 @@ func (c *WdaClient) SetAppiumSettings(settings map[string]interface{}) error {
2223
}
2324

2425
func (c *WdaClient) SetMjpegFramerate(framerate int) error {
25-
err := c.SetAppiumSettings(map[string]interface{}{
26-
"mjpegServerFramerate": framerate,
27-
})
28-
if err != nil {
29-
return fmt.Errorf("failed to set mjpeg framerate: %w", err)
26+
var err error
27+
maxRetries := 5
28+
29+
for i := 0; i < maxRetries; i++ {
30+
err = c.SetAppiumSettings(map[string]interface{}{
31+
"mjpegServerFramerate": framerate,
32+
})
33+
34+
if err == nil {
35+
return nil
36+
}
37+
38+
fmt.Printf("attempt %d/%d failed to set mjpeg framerate: %v\n", i+1, maxRetries, err)
3039
}
31-
return nil
40+
41+
return fmt.Errorf("failed to set mjpeg framerate after %d attempts: %w", maxRetries, err)
3242
}

devices/wda/requests.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ import (
1212
"github.com/mobile-next/mobilecli/utils"
1313
)
1414

15+
type alwaysMatch struct {
16+
PlatformName string `json:"platformName"`
17+
}
18+
19+
type sessionCapabilities struct {
20+
AlwaysMatch alwaysMatch `json:"alwaysMatch"`
21+
}
22+
23+
type sessionRequest struct {
24+
Capabilities sessionCapabilities `json:"capabilities"`
25+
}
26+
1527
func (c *WdaClient) GetEndpoint(endpoint string) (map[string]interface{}, error) {
1628
url := fmt.Sprintf("%s/%s", c.baseURL, endpoint)
1729

@@ -27,6 +39,7 @@ func (c *WdaClient) GetEndpoint(endpoint string) (map[string]interface{}, error)
2739
if err != nil {
2840
return nil, fmt.Errorf("failed to fetch endpoint %s: %w", endpoint, err)
2941
}
42+
3043
defer func() { _ = resp.Body.Close() }()
3144

3245
// check HTTP status code
@@ -63,6 +76,7 @@ func (c *WdaClient) PostEndpoint(endpoint string, data interface{}) (map[string]
6376
if err != nil {
6477
return nil, fmt.Errorf("failed to create request: %w", err)
6578
}
79+
6680
req.Header.Set("Content-Type", "application/json")
6781

6882
resp, err := c.httpClient.Do(req)
@@ -144,14 +158,15 @@ func (c *WdaClient) WaitForAgent() error {
144158
}
145159

146160
func (c *WdaClient) CreateSession() (string, error) {
147-
response, err := c.PostEndpoint("session", map[string]interface{}{
148-
"capabilities": map[string]interface{}{
149-
"alwaysMatch": map[string]interface{}{
150-
"platformName": "iOS",
161+
request := sessionRequest{
162+
Capabilities: sessionCapabilities{
163+
AlwaysMatch: alwaysMatch{
164+
PlatformName: "iOS",
151165
},
152166
},
153-
})
167+
}
154168

169+
response, err := c.PostEndpoint("session", request)
155170
if err != nil {
156171
return "", fmt.Errorf("failed to create session: %w", err)
157172
}
@@ -175,6 +190,7 @@ func (c *WdaClient) GetOrCreateSession() (string, error) {
175190
if c.isSessionStillValid(c.sessionId) {
176191
return c.sessionId, nil
177192
}
193+
178194
// session is invalid, clear it and create a new one
179195
utils.Verbose("cached session %s is invalid, creating new session", c.sessionId)
180196
c.sessionId = ""
@@ -195,5 +211,10 @@ func (c *WdaClient) DeleteSession(sessionId string) error {
195211
if err != nil {
196212
return fmt.Errorf("failed to delete session %s: %w", sessionId, err)
197213
}
214+
215+
if c.sessionId == sessionId {
216+
c.sessionId = ""
217+
}
218+
198219
return nil
199220
}

0 commit comments

Comments
 (0)