From cb5f73452b76fa3159a9db7e02ca614fc40ef5cd Mon Sep 17 00:00:00 2001 From: lvladika Date: Sun, 8 Nov 2026 21:49:17 +0100 Subject: [PATCH] perf(wda): stop a simulator's app with simctl before WebDriverAgent's terminate WebDriverAgent's terminate is XCTest's, which took 1.1-2.1 s to stop the app on a simulator; `simctl terminate` takes 0.2 s. A flow pays it at every launchApp of a running app (clearState's launch stops it before the reinstall) and at stopApp and killApp. With a session on a simulator, the driver now stops the app with simctl, then sends WebDriverAgent's terminate, which returns at once for an app that is not running and keeps the session's bookkeeping: an app of the session stopped behind its back reads as crashed at the next request. launchApp's stop before a launch goes through the same path. A real device stops it through WebDriverAgent only. --- pkg/driver/wda/commands.go | 14 +++++++++--- pkg/driver/wda/commands_test.go | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/pkg/driver/wda/commands.go b/pkg/driver/wda/commands.go index 15344f4b..d961ff27 100644 --- a/pkg/driver/wda/commands.go +++ b/pkg/driver/wda/commands.go @@ -1536,7 +1536,7 @@ func (d *Driver) launchApp(step *flow.LaunchAppStep) *core.CommandResult { // a flow checking what survives a restart tested nothing. resuming := false if hasArgs || step.StopApp == nil || *step.StopApp { - _ = d.client.TerminateApp(bundleID) + _ = d.terminateApp(bundleID) } else { resuming = d.inBackground(bundleID) } @@ -1584,13 +1584,21 @@ func (d *Driver) killApp(step *flow.KillAppStep) *core.CommandResult { // (e.g. "- stopApp" as the first step in a flow). func (d *Driver) terminateApp(bundleID string) error { if d.client.HasSession() { + // On a simulator simctl stops the app in 0.2 s, where WebDriverAgent's + // XCTest terminate took 1.1-2.1 s; WebDriverAgent's terminate after it + // returns at once and keeps the session's bookkeeping (an app of the + // session stopped behind its back reads as crashed at the next request). + if d.info != nil && d.info.IsSimulator && d.udid != "" { + if output, err := execCommand("xcrun", "simctl", "terminate", d.udid, bundleID).CombinedOutput(); err != nil { + logger.Debug("simctl terminate %s: %v: %s", bundleID, err, string(output)) + } + } return d.client.TerminateApp(bundleID) } // No WDA session — fall back to device-level terminate if d.info != nil && d.info.IsSimulator { - cmd := exec.Command("xcrun", "simctl", "terminate", d.udid, bundleID) - if output, err := cmd.CombinedOutput(); err != nil { + if output, err := execCommand("xcrun", "simctl", "terminate", d.udid, bundleID).CombinedOutput(); err != nil { // Ignore errors — app might not be running logger.Debug("simctl terminate %s: %v: %s", bundleID, err, string(output)) } diff --git a/pkg/driver/wda/commands_test.go b/pkg/driver/wda/commands_test.go index 3a984a93..3dbacaee 100644 --- a/pkg/driver/wda/commands_test.go +++ b/pkg/driver/wda/commands_test.go @@ -5447,3 +5447,41 @@ func TestEraseTextEmptyFieldShowingPlaceholder(t *testing.T) { t.Errorf("typed into an empty field: %q", typed) } } + +// On a simulator an app is stopped with simctl first (0.2 s; WebDriverAgent's +// XCTest terminate took 1.1-2.1 s), then through WebDriverAgent, which keeps +// its session's bookkeeping; a real device stops it through WebDriverAgent only. +func TestTerminateAppOnASimulatorUsesSimctlFirst(t *testing.T) { + var calls []string + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.URL.Path, "/wda/apps/terminate") { + calls = append(calls, "wda terminate") + } + jsonResponse(w, map[string]interface{}{"value": true}) + })) + defer server.Close() + + var got [][]string + withFakeExec(t, &got, false) + d := createTestDriver(server) + d.info.IsSimulator, d.udid = true, "SIM-UDID" + if err := d.terminateApp("com.example.app"); err != nil { + t.Fatalf("terminateApp: %v", err) + } + want := []string{"xcrun", "simctl", "terminate", "SIM-UDID", "com.example.app"} + if len(got) != 1 || strings.Join(got[0], " ") != strings.Join(want, " ") { + t.Errorf("exec calls = %v, want one %v", got, want) + } + if len(calls) != 1 { + t.Errorf("WDA terminate calls = %d, want 1 (its bookkeeping)", len(calls)) + } + + got, calls = nil, nil + d.info.IsSimulator, d.udid = false, "DEVICE-UDID" + if err := d.terminateApp("com.example.app"); err != nil { + t.Fatalf("terminateApp on a device: %v", err) + } + if len(got) != 0 || len(calls) != 1 { + t.Errorf("a device: exec calls %v, WDA terminate calls %d; want none and 1", got, len(calls)) + } +}