mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-02 19:17:10 +00:00
refactor(testrun): remove agent SDK connection setup
This commit is contained in:
1 parent
d21c5b1a14
commit
eea760d6ed
2 files changed
+12
-112
No files matched your search
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -80,3 +81,12 @@ func buildDriver(ctx context.Context, options Options, stdout io.Writer) (driver
|
|||||||
}
|
}
|
||||||
return driverClient, cleanup, nil
|
return driverClient, cleanup, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func pickFreePort() (int, error) {
|
||||||
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer listener.Close()
|
||||||
|
return listener.Addr().(*net.TCPAddr).Port, nil
|
||||||
|
}
|
||||||
+2
-112
@@ -5,13 +5,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/priyanshujain/sanderling/internal/agent"
|
|
||||||
"github.com/priyanshujain/sanderling/internal/android"
|
"github.com/priyanshujain/sanderling/internal/android"
|
||||||
"github.com/priyanshujain/sanderling/internal/bundler"
|
"github.com/priyanshujain/sanderling/internal/bundler"
|
||||||
"github.com/priyanshujain/sanderling/internal/ios"
|
"github.com/priyanshujain/sanderling/internal/ios"
|
||||||
@@ -20,11 +17,7 @@ import (
|
|||||||
"github.com/priyanshujain/sanderling/internal/verifier"
|
"github.com/priyanshujain/sanderling/internal/verifier"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const sidecarStartupTimeout = 30 * time.Second
|
||||||
socketName = "sanderling-agent"
|
|
||||||
sidecarStartupTimeout = 30 * time.Second
|
|
||||||
sdkAcceptTimeout = 60 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
// Options are the parameters for a single test pipeline run.
|
// Options are the parameters for a single test pipeline run.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
@@ -38,7 +31,7 @@ type Options struct {
|
|||||||
Output string
|
Output string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute runs the full test pipeline: bundle, connect SDK, verify properties.
|
// Execute runs the full test pipeline: bundle, launch app, verify properties.
|
||||||
func Execute(ctx context.Context, options Options, stdout io.Writer) error {
|
func Execute(ctx context.Context, options Options, stdout io.Writer) error {
|
||||||
switch options.Platform {
|
switch options.Platform {
|
||||||
case "android":
|
case "android":
|
||||||
@@ -75,101 +68,9 @@ func Execute(ctx context.Context, options Options, stdout io.Writer) error {
|
|||||||
}
|
}
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
var connection *agent.Conn
|
|
||||||
|
|
||||||
switch options.Platform {
|
|
||||||
case "android":
|
|
||||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("agent listener: %w", err)
|
|
||||||
}
|
|
||||||
defer listener.Close()
|
|
||||||
agentPort := listener.Addr().(*net.TCPAddr).Port
|
|
||||||
|
|
||||||
if err := android.AdbReverse(socketName, agentPort); err != nil {
|
|
||||||
return fmt.Errorf("adb reverse: %w", err)
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
if err := android.AdbReverseRemove(socketName); err != nil {
|
|
||||||
fmt.Fprintf(stdout, "warning: adb reverse cleanup: %v\n", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
fmt.Fprintf(stdout, "forwarded localabstract:%s -> tcp:%d\n", socketName, agentPort)
|
|
||||||
|
|
||||||
agentServer := agent.NewServer(listener)
|
|
||||||
|
|
||||||
type acceptResult struct {
|
|
||||||
conn *agent.Conn
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
acceptChannel := make(chan acceptResult, 1)
|
|
||||||
go func() {
|
|
||||||
acceptCtx, cancel := context.WithTimeout(ctx, sdkAcceptTimeout)
|
|
||||||
defer cancel()
|
|
||||||
conn, acceptErr := agentServer.Accept(acceptCtx)
|
|
||||||
acceptChannel <- acceptResult{conn: conn, err: acceptErr}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err := activeDriver.Launch(ctx, options.BundleID, false, nil); err != nil {
|
if err := activeDriver.Launch(ctx, options.BundleID, false, nil); err != nil {
|
||||||
return fmt.Errorf("launch app: %w", err)
|
return fmt.Errorf("launch app: %w", err)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(stdout, "launched %s; waiting for SDK to connect (%.0fs timeout)\n", options.BundleID, sdkAcceptTimeout.Seconds())
|
|
||||||
|
|
||||||
result := <-acceptChannel
|
|
||||||
if result.err != nil {
|
|
||||||
return fmt.Errorf("accept SDK: %w", result.err)
|
|
||||||
}
|
|
||||||
connection = result.conn
|
|
||||||
defer connection.Close()
|
|
||||||
hello := connection.Hello()
|
|
||||||
fmt.Fprintf(stdout, "SDK connected: platform=%s app=%s sdk=%s\n", hello.Platform, hello.AppPackage, hello.Version)
|
|
||||||
|
|
||||||
case "ios":
|
|
||||||
// iOS simulator shares the Mac's loopback — no port forwarding needed.
|
|
||||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("agent listener: %w", err)
|
|
||||||
}
|
|
||||||
defer listener.Close()
|
|
||||||
agentPort := listener.Addr().(*net.TCPAddr).Port
|
|
||||||
|
|
||||||
agentServer := agent.NewServer(listener)
|
|
||||||
|
|
||||||
type acceptResult struct {
|
|
||||||
conn *agent.Conn
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
acceptChannel := make(chan acceptResult, 1)
|
|
||||||
go func() {
|
|
||||||
acceptCtx, cancel := context.WithTimeout(ctx, sdkAcceptTimeout)
|
|
||||||
defer cancel()
|
|
||||||
conn, acceptErr := agentServer.Accept(acceptCtx)
|
|
||||||
acceptChannel <- acceptResult{conn: conn, err: acceptErr}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Launch app via XCTest with SANDERLING_PORT so the SDK can connect.
|
|
||||||
if err := activeDriver.Launch(ctx, options.BundleID, false, map[string]string{
|
|
||||||
"SANDERLING_PORT": strconv.Itoa(agentPort),
|
|
||||||
}); err != nil {
|
|
||||||
return fmt.Errorf("launch app: %w", err)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(stdout, "iOS app launched with SANDERLING_PORT=%d; waiting for SDK (%.0fs timeout)\n", agentPort, sdkAcceptTimeout.Seconds())
|
|
||||||
|
|
||||||
result := <-acceptChannel
|
|
||||||
if result.err != nil {
|
|
||||||
return fmt.Errorf("accept SDK: %w", result.err)
|
|
||||||
}
|
|
||||||
connection = result.conn
|
|
||||||
defer connection.Close()
|
|
||||||
hello := connection.Hello()
|
|
||||||
fmt.Fprintf(stdout, "SDK connected: platform=%s app=%s sdk=%s\n", hello.Platform, hello.AppPackage, hello.Version)
|
|
||||||
|
|
||||||
case "web":
|
|
||||||
fmt.Fprintln(stdout, "web mode: skipping SDK")
|
|
||||||
if err := activeDriver.Launch(ctx, options.BundleID, false, nil); err != nil {
|
|
||||||
return fmt.Errorf("launch app: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
seed := options.Seed
|
seed := options.Seed
|
||||||
if seed == 0 {
|
if seed == 0 {
|
||||||
@@ -212,10 +113,8 @@ func Execute(ctx context.Context, options Options, stdout io.Writer) error {
|
|||||||
fmt.Fprintf(stdout, "running for %s (seed=%d)\n", options.Duration, seed)
|
fmt.Fprintf(stdout, "running for %s (seed=%d)\n", options.Duration, seed)
|
||||||
summary, err := runner.Run(ctx, runner.Options{
|
summary, err := runner.Run(ctx, runner.Options{
|
||||||
Duration: options.Duration,
|
Duration: options.Duration,
|
||||||
SnapshotTimeout: 5 * time.Second,
|
|
||||||
IdleTimeout: 1 * time.Second,
|
IdleTimeout: 1 * time.Second,
|
||||||
BundleID: options.BundleID,
|
BundleID: options.BundleID,
|
||||||
Connection: connection,
|
|
||||||
Driver: activeDriver,
|
Driver: activeDriver,
|
||||||
Verifier: verifierInstance,
|
Verifier: verifierInstance,
|
||||||
TraceWriter: traceWriter,
|
TraceWriter: traceWriter,
|
||||||
@@ -269,12 +168,3 @@ func resolveSpecAPIPath(specPath string) string {
|
|||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func pickFreePort() (int, error) {
|
|
||||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
defer listener.Close()
|
|
||||||
return listener.Addr().(*net.TCPAddr).Port, nil
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user