Skip to content
Merged
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
62 changes: 62 additions & 0 deletions packages/provider-webdriver/src/aws-device-farm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,68 @@ test('the allocation deadline lets startup run past the standalone 120s default'
assert.deepEqual(client.stopped, []);
});

test.each(['RUNNING', 'ERRORED', 'PENDING'] as const)(
'decides the final %s observation at the deadline',
async (status) => {
let now = 1_000;
vi.spyOn(Date, 'now').mockImplementation(() => now);
let polls = 0;
const client = fakeClient({ status: 'PENDING' }, () => {
polls += 1;
if (polls === 2) {
now = 2_000;
client.session.status = status;
}
});
client.session.endpoints = { appium: 'https://appium.example/wd/hub' };
const prepare = createAwsDeviceFarmPrepareSession({
...baseOptions(client),
startupTimeoutMs: 1_000,
pollIntervalMs: 1,
});
const result = prepare({ lease: makeLease(), base: baseSession() });
if (status === 'RUNNING') {
assert.equal((await result).providerSessionId, ARN);
assert.deepEqual(client.stopped, []);
} else {
await assert.rejects(result, (error: unknown) => {
assert.ok(error instanceof AppError);
assert.equal(error.details?.status, status);
assert.equal(error.details?.timeoutMs, status === 'PENDING' ? 1_000 : undefined);
return true;
});
assert.deepEqual(client.stopped, [ARN]);
}
assert.equal(polls, 2);
},
);

test('cancellation takes priority over a final RUNNING observation', async () => {
let now = 1_000;
vi.spyOn(Date, 'now').mockImplementation(() => now);
const controller = new AbortController();
const client = fakeClient({ status: 'RUNNING' }, () => {
now = 2_000;
controller.abort(createRequestCanceledError());
});
const prepare = createAwsDeviceFarmPrepareSession({
...baseOptions(client),
startupTimeoutMs: 1_000,
});
await assert.rejects(
prepare({ lease: makeLease(), req: { signal: controller.signal }, base: baseSession() }),
isRequestCanceledError,
);
assert.deepEqual(client.stopped, [ARN]);
});

test('a running session without an endpoint is stopped', async () => {
const client = fakeClient({ status: 'RUNNING' });
const prepare = createAwsDeviceFarmPrepareSession(baseOptions(client));
await assert.rejects(prepare({ lease: makeLease(), base: baseSession() }), AppError);
assert.deepEqual(client.stopped, [ARN]);
});

test('a session that reaches RUNNING is handed on and not stopped', async () => {
const client = fakeClient({
status: 'RUNNING',
Expand Down
3 changes: 2 additions & 1 deletion packages/provider-webdriver/src/aws-device-farm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,11 @@ async function waitForRunningRemoteAccessSession(
const deadline = req?.deadline ?? startedAt + (options.startupTimeoutMs ?? 120_000);
const signal = req?.signal;
let last = await options.client.getRemoteAccessSession(arn);
while (Date.now() < deadline) {
while (true) {
signal?.throwIfAborted();
if (last.status === 'RUNNING') return last;
throwIfRemoteAccessSessionEnded(last);
if (Date.now() >= deadline) break;
// Wake early on cancellation; the typed reason is rethrown at the loop top.
await sleep(pollIntervalMs, undefined, { signal }).catch(() => signal?.throwIfAborted());
last = await options.client.getRemoteAccessSession(arn);
Expand Down
Loading