@simlock/e2e 0.0.0-stage → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +4 -2
- package/dist/errors.d.ts +3 -0
- package/dist/errors.js +22 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/install.d.ts +18 -0
- package/dist/install.js +87 -0
- package/dist/options.d.ts +29 -0
- package/dist/options.js +117 -0
- package/dist/pause.d.ts +1 -0
- package/dist/pause.js +17 -0
- package/dist/provider.d.ts +6 -0
- package/dist/provider.js +159 -0
- package/dist/queue.d.ts +10 -0
- package/dist/queue.js +56 -0
- package/dist/renew.d.ts +7 -0
- package/dist/renew.js +21 -0
- package/package.json +45 -3
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Szymon Chmal
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @simlock/e2e
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A device provider that leases iOS simulators and Android emulators from a simlock farm for the `e2e` test runner (`@e2e-dev/mobile`).
|
|
4
|
+
|
|
5
|
+
Setup, options and CI guidance: https://callstackincubator.github.io/simlock/docs/using-devices/ci/
|
package/dist/errors.d.ts
ADDED
package/dist/errors.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// why: these are the reasons agent-device's own client puts on an error when simlock gave no answer: the connection dropped, the body was not JSON (a proxy's 524 page), or the request timed out. Simlock's own reasons are upper case and say they were answered.
|
|
2
|
+
const NO_ANSWER_REASONS = new Set([
|
|
3
|
+
"daemon_response_invalid",
|
|
4
|
+
"daemon_response_read_failure",
|
|
5
|
+
"daemon_transport_failure",
|
|
6
|
+
"daemon_transport_timeout",
|
|
7
|
+
]);
|
|
8
|
+
function detailsOf(error) {
|
|
9
|
+
const details = error?.details;
|
|
10
|
+
return typeof details === "object" && details !== null ? details : {};
|
|
11
|
+
}
|
|
12
|
+
export function mayAnswerLater(error) {
|
|
13
|
+
const { reason, retriable } = detailsOf(error);
|
|
14
|
+
return retriable === true || (typeof reason === "string" && NO_ANSWER_REASONS.has(reason));
|
|
15
|
+
}
|
|
16
|
+
// why: an error with no string `details.reason` means simlock gave no answer the client kept: a proxy or connection error, a sign-in error (UNAUTHENTICATED, FORBIDDEN carry no reason: website/src/content/docs/docs/using-devices/agent-device.mdx:225), or a refused upload whose flat `reason` beside `code` (agent-device.mdx:214) never reaches `details`; callers treat all three as a cut (issue #1567, Tests B6).
|
|
17
|
+
export function saidNothing(error) {
|
|
18
|
+
return typeof detailsOf(error).reason !== "string";
|
|
19
|
+
}
|
|
20
|
+
export function messageOf(error) {
|
|
21
|
+
return error instanceof Error ? error.message : String(error);
|
|
22
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { createAgentDeviceClient } from "agent-device";
|
|
2
|
+
export declare function limiter(max: number): <T>(task: (signal: AbortSignal) => Promise<T>, signal: AbortSignal) => Promise<T>;
|
|
3
|
+
export declare class DeviceSetupFailed extends Error {
|
|
4
|
+
readonly deviceId: string;
|
|
5
|
+
constructor(message: string, deviceId: string, cause: unknown);
|
|
6
|
+
}
|
|
7
|
+
export interface Installation {
|
|
8
|
+
readonly apps: ReturnType<typeof createAgentDeviceClient>["apps"];
|
|
9
|
+
readonly platform: "ios" | "android";
|
|
10
|
+
readonly deviceId: string;
|
|
11
|
+
readonly limit: ReturnType<typeof limiter>;
|
|
12
|
+
readonly signal: AbortSignal;
|
|
13
|
+
readonly log: (line: string) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare function installAll(on: Installation, request: {
|
|
16
|
+
readonly appPath?: string | undefined;
|
|
17
|
+
readonly app?: string | undefined;
|
|
18
|
+
}, extraApps: readonly string[]): Promise<void>;
|
package/dist/install.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { mayAnswerLater, messageOf, saidNothing } from "./errors.js";
|
|
2
|
+
import { pause } from "./pause.js";
|
|
3
|
+
export function limiter(max) {
|
|
4
|
+
let active = 0;
|
|
5
|
+
const waiting = [];
|
|
6
|
+
return async (task, signal) => {
|
|
7
|
+
signal.throwIfAborted();
|
|
8
|
+
if (active < max)
|
|
9
|
+
active += 1;
|
|
10
|
+
else {
|
|
11
|
+
await new Promise((resolve, reject) => {
|
|
12
|
+
const turn = () => {
|
|
13
|
+
signal.removeEventListener("abort", leave);
|
|
14
|
+
resolve();
|
|
15
|
+
};
|
|
16
|
+
const leave = () => {
|
|
17
|
+
waiting.splice(waiting.indexOf(turn), 1);
|
|
18
|
+
reject(signal.reason);
|
|
19
|
+
};
|
|
20
|
+
waiting.push(turn);
|
|
21
|
+
signal.addEventListener("abort", leave, { once: true });
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
return await task(signal);
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
const next = waiting.shift();
|
|
29
|
+
if (next === undefined)
|
|
30
|
+
active -= 1;
|
|
31
|
+
else
|
|
32
|
+
next();
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
const TRIES = 3;
|
|
37
|
+
const BACKOFF_MS = 15_000;
|
|
38
|
+
// why: a missing simlock reason counts as a cut, sent three times and then thrown with no swap: a proxy's 524 page, and also a sign-in error (website/src/content/docs/docs/using-devices/agent-device.mdx:225) or an upload simlock refused (agent-device.mdx:214), which reach the client without a reason in `details`; only an error carrying a reason is judged a refused install.
|
|
39
|
+
function cut(error) {
|
|
40
|
+
return mayAnswerLater(error) || saidNothing(error);
|
|
41
|
+
}
|
|
42
|
+
async function sendAgain(send, signal) {
|
|
43
|
+
for (let attempt = 1;; attempt += 1) {
|
|
44
|
+
try {
|
|
45
|
+
return await send();
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (signal.aborted || attempt >= TRIES || !cut(error))
|
|
49
|
+
throw error;
|
|
50
|
+
await pause(BACKOFF_MS * attempt, signal);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// why: a device whose install failed for a reason the farm did not mark as passing is worth swapping; a cut install is not, so the caller can tell the two apart.
|
|
55
|
+
export class DeviceSetupFailed extends Error {
|
|
56
|
+
deviceId;
|
|
57
|
+
constructor(message, deviceId, cause) {
|
|
58
|
+
super(message, { cause });
|
|
59
|
+
this.deviceId = deviceId;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function installApp(on, appPath, app) {
|
|
63
|
+
on.signal.throwIfAborted();
|
|
64
|
+
const started = Date.now();
|
|
65
|
+
const device = on.platform === "android" ? { serial: on.deviceId } : { udid: on.deviceId };
|
|
66
|
+
try {
|
|
67
|
+
await sendAgain(() => on.limit((signal) => on.apps.install({
|
|
68
|
+
platform: on.platform,
|
|
69
|
+
...device,
|
|
70
|
+
appPath,
|
|
71
|
+
...(app === undefined ? {} : { app }),
|
|
72
|
+
signal,
|
|
73
|
+
}), on.signal), on.signal);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
if (on.signal.aborted || cut(error))
|
|
77
|
+
throw error;
|
|
78
|
+
throw new DeviceSetupFailed(messageOf(error), on.deviceId, error);
|
|
79
|
+
}
|
|
80
|
+
on.log(`simlock: installed ${app ?? appPath} in ${String(Date.now() - started)} ms`);
|
|
81
|
+
}
|
|
82
|
+
export async function installAll(on, request, extraApps) {
|
|
83
|
+
if (request.appPath !== undefined)
|
|
84
|
+
await installApp(on, request.appPath, request.app);
|
|
85
|
+
for (const extra of extraApps)
|
|
86
|
+
await installApp(on, extra, undefined);
|
|
87
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface SimlockDevicesOptions {
|
|
2
|
+
url: string;
|
|
3
|
+
token: string;
|
|
4
|
+
platform: "ios" | "android";
|
|
5
|
+
device: string;
|
|
6
|
+
osVersion?: string | undefined;
|
|
7
|
+
imageTag?: string | undefined;
|
|
8
|
+
deviceMode?: "slim" | "full" | undefined;
|
|
9
|
+
deviceBaseline?: string | undefined;
|
|
10
|
+
extraApps?: readonly string[] | undefined;
|
|
11
|
+
ttlMs?: number | undefined;
|
|
12
|
+
queueMs?: number | undefined;
|
|
13
|
+
installConcurrency?: number | undefined;
|
|
14
|
+
}
|
|
15
|
+
export interface ProviderOptions {
|
|
16
|
+
url: string;
|
|
17
|
+
token: string;
|
|
18
|
+
platform: "ios" | "android";
|
|
19
|
+
device: string;
|
|
20
|
+
osVersion?: string;
|
|
21
|
+
imageTag?: string;
|
|
22
|
+
deviceMode?: "slim" | "full";
|
|
23
|
+
deviceBaseline?: string;
|
|
24
|
+
extraApps: readonly string[];
|
|
25
|
+
ttlMs: number;
|
|
26
|
+
queueMs: number;
|
|
27
|
+
installConcurrency: number;
|
|
28
|
+
}
|
|
29
|
+
export declare function checkOptions(options: SimlockDevicesOptions): ProviderOptions;
|
package/dist/options.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
const KNOWN = new Set([
|
|
2
|
+
"url",
|
|
3
|
+
"token",
|
|
4
|
+
"platform",
|
|
5
|
+
"device",
|
|
6
|
+
"osVersion",
|
|
7
|
+
"imageTag",
|
|
8
|
+
"deviceMode",
|
|
9
|
+
"deviceBaseline",
|
|
10
|
+
"extraApps",
|
|
11
|
+
"ttlMs",
|
|
12
|
+
"queueMs",
|
|
13
|
+
"installConcurrency",
|
|
14
|
+
]);
|
|
15
|
+
const DEFAULT_TTL_MS = 180_000;
|
|
16
|
+
const DEFAULT_QUEUE_MS = 1_200_000;
|
|
17
|
+
const DEFAULT_INSTALL_CONCURRENCY = 3;
|
|
18
|
+
const MIN_TTL_MS = 30_000;
|
|
19
|
+
function show(value) {
|
|
20
|
+
return typeof value === "string" ? JSON.stringify(value) : String(value);
|
|
21
|
+
}
|
|
22
|
+
function fail(message) {
|
|
23
|
+
throw new Error(`simlockDevices: ${message}`);
|
|
24
|
+
}
|
|
25
|
+
function stripTrailingSlashes(value) {
|
|
26
|
+
let end = value.length;
|
|
27
|
+
while (end > 0 && value[end - 1] === "/")
|
|
28
|
+
end -= 1;
|
|
29
|
+
return value.slice(0, end);
|
|
30
|
+
}
|
|
31
|
+
function checkUrl(value) {
|
|
32
|
+
let parsed;
|
|
33
|
+
try {
|
|
34
|
+
parsed = new URL(String(value));
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
parsed = undefined;
|
|
38
|
+
}
|
|
39
|
+
if (typeof value !== "string" ||
|
|
40
|
+
(parsed?.protocol !== "http:" && parsed?.protocol !== "https:")) {
|
|
41
|
+
fail(`url must be an http(s) URL of the simlock gateway or worker; got ${show(value)}`);
|
|
42
|
+
}
|
|
43
|
+
const url = stripTrailingSlashes(value);
|
|
44
|
+
if (url.endsWith("/v1"))
|
|
45
|
+
fail(`url must not end in /v1; got ${show(value)}`);
|
|
46
|
+
return url;
|
|
47
|
+
}
|
|
48
|
+
function checkOptionalString(name, value) {
|
|
49
|
+
if (value === undefined)
|
|
50
|
+
return undefined;
|
|
51
|
+
if (typeof value !== "string" || value === "") {
|
|
52
|
+
fail(`${name} must be a non-empty string; got ${show(value)}`);
|
|
53
|
+
}
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
function checkNumber(name, value, fallback, accepts, rule) {
|
|
57
|
+
if (value === undefined)
|
|
58
|
+
return fallback;
|
|
59
|
+
if (typeof value !== "number" || !Number.isFinite(value) || !accepts(value)) {
|
|
60
|
+
fail(`${name} must be ${rule}; got ${show(value)}`);
|
|
61
|
+
}
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
function checkRequired(value, message) {
|
|
65
|
+
if (typeof value !== "string" || value === "")
|
|
66
|
+
fail(message);
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
function checkPlatform(value) {
|
|
70
|
+
if (value !== "ios" && value !== "android") {
|
|
71
|
+
fail(`platform must be "ios" or "android"; got ${show(value)}`);
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
function checkDeviceMode(value) {
|
|
76
|
+
if (value !== undefined && value !== "slim" && value !== "full") {
|
|
77
|
+
fail(`deviceMode must be "slim" or "full"; got ${show(value)}`);
|
|
78
|
+
}
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
function checkExtraApps(value) {
|
|
82
|
+
const apps = value ?? [];
|
|
83
|
+
if (!Array.isArray(apps) || !apps.every((app) => typeof app === "string")) {
|
|
84
|
+
fail("extraApps must be a list of paths");
|
|
85
|
+
}
|
|
86
|
+
return apps;
|
|
87
|
+
}
|
|
88
|
+
export function checkOptions(options) {
|
|
89
|
+
const given = options;
|
|
90
|
+
for (const name of Object.keys(given)) {
|
|
91
|
+
if (!KNOWN.has(name))
|
|
92
|
+
fail(`unknown option ${show(name)}`);
|
|
93
|
+
}
|
|
94
|
+
const url = checkUrl(given["url"]);
|
|
95
|
+
const token = checkRequired(given["token"], "token is required (a simlock agent token)");
|
|
96
|
+
const platform = checkPlatform(given["platform"]);
|
|
97
|
+
const device = checkRequired(given["device"], "device is required (a model the farm lists)");
|
|
98
|
+
const deviceMode = checkDeviceMode(given["deviceMode"]);
|
|
99
|
+
const extraApps = checkExtraApps(given["extraApps"]);
|
|
100
|
+
const osVersion = checkOptionalString("osVersion", given["osVersion"]);
|
|
101
|
+
const imageTag = checkOptionalString("imageTag", given["imageTag"]);
|
|
102
|
+
const deviceBaseline = checkOptionalString("deviceBaseline", given["deviceBaseline"]);
|
|
103
|
+
return {
|
|
104
|
+
url,
|
|
105
|
+
token,
|
|
106
|
+
platform,
|
|
107
|
+
device,
|
|
108
|
+
...(osVersion === undefined ? {} : { osVersion }),
|
|
109
|
+
...(imageTag === undefined ? {} : { imageTag }),
|
|
110
|
+
...(deviceMode === undefined ? {} : { deviceMode }),
|
|
111
|
+
...(deviceBaseline === undefined ? {} : { deviceBaseline }),
|
|
112
|
+
extraApps,
|
|
113
|
+
ttlMs: checkNumber("ttlMs", given["ttlMs"], DEFAULT_TTL_MS, (n) => n >= MIN_TTL_MS, `a number of at least ${MIN_TTL_MS}`),
|
|
114
|
+
queueMs: checkNumber("queueMs", given["queueMs"], DEFAULT_QUEUE_MS, (n) => n > 0, "a positive number"),
|
|
115
|
+
installConcurrency: checkNumber("installConcurrency", given["installConcurrency"], DEFAULT_INSTALL_CONCURRENCY, (n) => Number.isInteger(n) && n >= 1, "a whole number of at least 1"),
|
|
116
|
+
};
|
|
117
|
+
}
|
package/dist/pause.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function pause(ms: number, signal: AbortSignal): Promise<void>;
|
package/dist/pause.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function pause(ms, signal) {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
if (signal.aborted) {
|
|
4
|
+
reject(signal.reason);
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
const onAbort = () => {
|
|
8
|
+
clearTimeout(timer);
|
|
9
|
+
reject(signal.reason);
|
|
10
|
+
};
|
|
11
|
+
const timer = setTimeout(() => {
|
|
12
|
+
signal.removeEventListener("abort", onAbort);
|
|
13
|
+
resolve();
|
|
14
|
+
}, ms);
|
|
15
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
16
|
+
});
|
|
17
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { DeviceProvider } from "@e2e-dev/mobile";
|
|
2
|
+
import type { createAgentDeviceClient } from "agent-device";
|
|
3
|
+
import type { ProviderOptions } from "./options.js";
|
|
4
|
+
type AgentDeviceClientFactory = typeof createAgentDeviceClient;
|
|
5
|
+
export declare function createProvider(options: ProviderOptions, loadClient: () => Promise<AgentDeviceClientFactory>): DeviceProvider;
|
|
6
|
+
export {};
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { DeviceSetupFailed, installAll, limiter } from "./install.js";
|
|
3
|
+
import { messageOf } from "./errors.js";
|
|
4
|
+
import { allocateQueued } from "./queue.js";
|
|
5
|
+
import { startRenewal } from "./renew.js";
|
|
6
|
+
// why: the agent-device client wants a tenant; simlock overwrites it with one made from the token and the lease.
|
|
7
|
+
const TENANT = "simlock-e2e";
|
|
8
|
+
function wantedDevice(options) {
|
|
9
|
+
return {
|
|
10
|
+
platform: options.platform,
|
|
11
|
+
target: "mobile",
|
|
12
|
+
device: options.device,
|
|
13
|
+
ttlMs: options.ttlMs,
|
|
14
|
+
retainOnClose: true,
|
|
15
|
+
...(options.osVersion === undefined ? {} : { osVersion: options.osVersion }),
|
|
16
|
+
...(options.imageTag === undefined ? {} : { imageTag: options.imageTag }),
|
|
17
|
+
...(options.deviceMode === undefined ? {} : { deviceMode: options.deviceMode }),
|
|
18
|
+
...(options.deviceBaseline === undefined ? {} : { deviceBaseline: options.deviceBaseline }),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function deviceOf(lease) {
|
|
22
|
+
const deviceId = lease.deviceKey?.split(":").pop();
|
|
23
|
+
if (deviceId === undefined || deviceId === "") {
|
|
24
|
+
throw new Error(`simlock: lease ${lease.leaseId} names no device`);
|
|
25
|
+
}
|
|
26
|
+
return deviceId;
|
|
27
|
+
}
|
|
28
|
+
function scopeOf(lease, runId, sentClientId) {
|
|
29
|
+
return {
|
|
30
|
+
tenant: lease.tenantId,
|
|
31
|
+
runId,
|
|
32
|
+
leaseId: lease.leaseId,
|
|
33
|
+
leaseBackend: lease.backend,
|
|
34
|
+
leaseProvider: "proxy",
|
|
35
|
+
clientId: lease.clientId ?? sentClientId,
|
|
36
|
+
...(lease.deviceKey === undefined ? {} : { deviceKey: lease.deviceKey }),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export function createProvider(options, loadClient) {
|
|
40
|
+
const daemonBaseUrl = `${options.url}/v1/agent-device`;
|
|
41
|
+
const leaseBackend = `${options.platform}-instance`;
|
|
42
|
+
const withinLimit = limiter(options.installConcurrency);
|
|
43
|
+
const held = new Map();
|
|
44
|
+
const client = async (session, scope) => {
|
|
45
|
+
let factory;
|
|
46
|
+
try {
|
|
47
|
+
factory = await loadClient();
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
throw new Error("simlock: the agent-device package is not installed; add it to your project");
|
|
51
|
+
}
|
|
52
|
+
return factory({ daemonBaseUrl, daemonAuthToken: options.token, session, ...scope });
|
|
53
|
+
};
|
|
54
|
+
async function leaseOnce(request, attempt, deadline) {
|
|
55
|
+
const session = `e2e-${request.runId.slice(0, 12)}-${String(request.slot)}${attempt === 0 ? "" : `-r${String(attempt)}`}`;
|
|
56
|
+
const started = Date.now();
|
|
57
|
+
const clientId = randomBytes(8).toString("hex");
|
|
58
|
+
const farm = await client(session);
|
|
59
|
+
const scope = (leaseId) => ({
|
|
60
|
+
tenant: TENANT,
|
|
61
|
+
runId: request.runId,
|
|
62
|
+
leaseId,
|
|
63
|
+
clientId,
|
|
64
|
+
leaseBackend,
|
|
65
|
+
leaseProvider: "proxy",
|
|
66
|
+
});
|
|
67
|
+
const lease = await allocateQueued({
|
|
68
|
+
platform: options.platform,
|
|
69
|
+
queueMs: options.queueMs,
|
|
70
|
+
deadline,
|
|
71
|
+
signal: request.signal,
|
|
72
|
+
log: request.log,
|
|
73
|
+
release: (leaseId) => farm.leases.release(scope(leaseId)),
|
|
74
|
+
allocate: (leaseId, signal) => farm.leases.allocate({
|
|
75
|
+
...scope(leaseId),
|
|
76
|
+
...wantedDevice(options),
|
|
77
|
+
signal,
|
|
78
|
+
}),
|
|
79
|
+
});
|
|
80
|
+
let stopRenewing = () => undefined;
|
|
81
|
+
try {
|
|
82
|
+
const deviceId = deviceOf(lease);
|
|
83
|
+
request.log(`simlock: lease ${lease.leaseId} granted in ${String(Date.now() - started)} ms (${options.device} ${deviceId})`);
|
|
84
|
+
const leased = scopeOf(lease, request.runId, clientId);
|
|
85
|
+
const granted = {
|
|
86
|
+
id: lease.leaseId,
|
|
87
|
+
daemon: { baseUrl: daemonBaseUrl, authToken: options.token },
|
|
88
|
+
client: leased,
|
|
89
|
+
deviceId,
|
|
90
|
+
};
|
|
91
|
+
stopRenewing = startRenewal({
|
|
92
|
+
leaseId: lease.leaseId,
|
|
93
|
+
ttlMs: options.ttlMs,
|
|
94
|
+
log: request.log,
|
|
95
|
+
renew: () => farm.leases.heartbeat({ ...leased, ttlMs: options.ttlMs }),
|
|
96
|
+
});
|
|
97
|
+
const on = {
|
|
98
|
+
apps: (await client(session, leased)).apps,
|
|
99
|
+
platform: options.platform,
|
|
100
|
+
deviceId,
|
|
101
|
+
limit: withinLimit,
|
|
102
|
+
signal: request.signal,
|
|
103
|
+
log: request.log,
|
|
104
|
+
};
|
|
105
|
+
await installAll(on, request, options.extraApps);
|
|
106
|
+
held.set(lease.leaseId, { stop: stopRenewing, scope: leased });
|
|
107
|
+
return request.appPath !== undefined && request.app !== undefined
|
|
108
|
+
? { ...granted, installedApp: request.app }
|
|
109
|
+
: granted;
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
stopRenewing();
|
|
113
|
+
await farm.leases
|
|
114
|
+
.release({ ...scope(lease.leaseId), clientId: lease.clientId ?? clientId })
|
|
115
|
+
.catch(() => undefined);
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async function acquire(request) {
|
|
120
|
+
if (request.platform !== options.platform) {
|
|
121
|
+
throw new Error(`simlockDevices leases ${options.platform} devices; the target asked for ${request.platform}`);
|
|
122
|
+
}
|
|
123
|
+
// why: queueMs bounds the whole acquire, so a swap to another device keeps the first ask's deadline.
|
|
124
|
+
const deadline = Date.now() + options.queueMs;
|
|
125
|
+
try {
|
|
126
|
+
return await leaseOnce(request, 0, deadline);
|
|
127
|
+
}
|
|
128
|
+
catch (first) {
|
|
129
|
+
if (!(first instanceof DeviceSetupFailed))
|
|
130
|
+
throw first;
|
|
131
|
+
request.log(`simlock: install on ${first.deviceId} failed (${first.message}); leasing another device`);
|
|
132
|
+
try {
|
|
133
|
+
return await leaseOnce(request, 1, deadline);
|
|
134
|
+
}
|
|
135
|
+
catch (second) {
|
|
136
|
+
if (!(second instanceof DeviceSetupFailed))
|
|
137
|
+
throw second;
|
|
138
|
+
throw new Error(`simlock: could not set up a device after 2 tries: ${second.message}`, {
|
|
139
|
+
cause: second,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async function release(lease, context) {
|
|
145
|
+
const entry = held.get(lease.id);
|
|
146
|
+
if (entry === undefined)
|
|
147
|
+
return;
|
|
148
|
+
entry.stop();
|
|
149
|
+
held.delete(lease.id);
|
|
150
|
+
try {
|
|
151
|
+
const farm = await client(`release-${lease.id.slice(0, 8)}`);
|
|
152
|
+
await farm.leases.release({ ...entry.scope, signal: context.signal });
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
context.log(`simlock: releasing lease ${lease.id} failed: ${messageOf(error)}; it runs out by itself`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return { name: "simlock", acquire, release };
|
|
159
|
+
}
|
package/dist/queue.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface Ask<Lease> {
|
|
2
|
+
readonly platform: string;
|
|
3
|
+
readonly queueMs: number;
|
|
4
|
+
readonly deadline: number;
|
|
5
|
+
readonly signal: AbortSignal;
|
|
6
|
+
readonly log: (line: string) => void;
|
|
7
|
+
allocate(leaseId: string, signal: AbortSignal): Promise<Lease>;
|
|
8
|
+
release(leaseId: string): Promise<unknown>;
|
|
9
|
+
}
|
|
10
|
+
export declare function allocateQueued<Lease>(ask: Ask<Lease>): Promise<Lease>;
|
package/dist/queue.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { mayAnswerLater } from "./errors.js";
|
|
3
|
+
import { pause } from "./pause.js";
|
|
4
|
+
const PAUSE_MS = 10_000;
|
|
5
|
+
const PAUSE_JITTER_MS = 5_000;
|
|
6
|
+
export async function allocateQueued(ask) {
|
|
7
|
+
const { deadline } = ask;
|
|
8
|
+
const minutes = Math.max(1, Math.round(ask.queueMs / 60_000));
|
|
9
|
+
const noDevice = (cause) => new Error(`simlock: no free ${ask.platform} device on the farm within ${String(minutes)} min`, {
|
|
10
|
+
cause,
|
|
11
|
+
});
|
|
12
|
+
let announced = false;
|
|
13
|
+
for (;;) {
|
|
14
|
+
ask.signal.throwIfAborted();
|
|
15
|
+
const attempt = await tryOnce(ask, deadline, noDevice);
|
|
16
|
+
if ("lease" in attempt)
|
|
17
|
+
return attempt.lease;
|
|
18
|
+
if (!announced) {
|
|
19
|
+
announced = true;
|
|
20
|
+
ask.log(`simlock: no free ${ask.platform} device on the farm; waiting for one`);
|
|
21
|
+
}
|
|
22
|
+
await pause(PAUSE_MS + Math.floor(Math.random() * PAUSE_JITTER_MS), ask.signal);
|
|
23
|
+
if (Date.now() >= deadline)
|
|
24
|
+
throw noDevice();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function tryOnce(ask, deadline, noDevice) {
|
|
28
|
+
const leaseId = randomBytes(16).toString("hex");
|
|
29
|
+
const timeUp = new AbortController();
|
|
30
|
+
const timer = setTimeout(() => {
|
|
31
|
+
timeUp.abort();
|
|
32
|
+
}, Math.max(0, deadline - Date.now()));
|
|
33
|
+
const giveBack = () => ask.release(leaseId).catch(() => undefined);
|
|
34
|
+
try {
|
|
35
|
+
return { lease: await ask.allocate(leaseId, AbortSignal.any([ask.signal, timeUp.signal])) };
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (ask.signal.aborted) {
|
|
39
|
+
await giveBack();
|
|
40
|
+
throw ask.signal.reason;
|
|
41
|
+
}
|
|
42
|
+
if (timeUp.signal.aborted) {
|
|
43
|
+
await giveBack();
|
|
44
|
+
throw noDevice(error);
|
|
45
|
+
}
|
|
46
|
+
if (!mayAnswerLater(error))
|
|
47
|
+
throw error;
|
|
48
|
+
await giveBack();
|
|
49
|
+
if (Date.now() >= deadline)
|
|
50
|
+
throw noDevice(error);
|
|
51
|
+
return { retry: true };
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
clearTimeout(timer);
|
|
55
|
+
}
|
|
56
|
+
}
|
package/dist/renew.d.ts
ADDED
package/dist/renew.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { messageOf } from "./errors.js";
|
|
2
|
+
function endedByFarm(error) {
|
|
3
|
+
const details = error?.details;
|
|
4
|
+
return details?.reason === "LEASE_ENDED_BY_HOST";
|
|
5
|
+
}
|
|
6
|
+
export function startRenewal(on) {
|
|
7
|
+
const timer = setInterval(() => {
|
|
8
|
+
on.renew().catch((error) => {
|
|
9
|
+
if (endedByFarm(error)) {
|
|
10
|
+
clearInterval(timer);
|
|
11
|
+
on.log(`simlock: lease ${on.leaseId} was ended by the farm; failures on its device are infrastructure, not tests`);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
on.log(`simlock: renewing lease ${on.leaseId} failed: ${messageOf(error)}`);
|
|
15
|
+
});
|
|
16
|
+
}, Math.floor(on.ttlMs / 3));
|
|
17
|
+
timer.unref();
|
|
18
|
+
return () => {
|
|
19
|
+
clearInterval(timer);
|
|
20
|
+
};
|
|
21
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simlock/e2e",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "A device provider that leases iOS simulators and Android emulators from a simlock farm for the e2e test runner.",
|
|
5
|
+
"homepage": "https://github.com/callstackincubator/simlock",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/callstackincubator/simlock/issues"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Szymon Chmal <szymon@chmal.it> (V3RON)",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/callstackincubator/simlock",
|
|
14
|
+
"directory": "packages/e2e"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"!dist/**/*.test.js",
|
|
19
|
+
"!dist/**/*.test.d.ts",
|
|
20
|
+
"!dist/config.sample.*",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@e2e-dev/mobile": "0.12.7",
|
|
35
|
+
"agent-device": "0.21.37",
|
|
36
|
+
"e2e": "0.23.1"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@e2e-dev/mobile": ">=0.12.5 <1",
|
|
40
|
+
"agent-device": ">=0.21.37"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=22.13"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsc -p tsconfig.build.json"
|
|
47
|
+
}
|
|
6
48
|
}
|