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
26 changes: 21 additions & 5 deletions src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,17 @@ export class TestingBotDriver implements MobilewrightDriver {
};
this.allocated.set(sessionId, session);
this.everAllocated.set(sessionId, session);
if (this.options.sessionPerTest) {
// Workers rotating sessions on this slot need its device filter, which
// mobilewright's connect() does not forward.
this.runLog.append({
type: 'slot',
deviceId: sessionId,
deviceType: criteria.deviceType,
osVersion: criteria.osVersion,
deviceNamePattern: criteria.deviceNamePattern,
});
}
this.keepalive.start(sessionId);
debug('allocated session %s (%s %s) — https://testingbot.com/members/tests/%s',
sessionId, session.deviceName, session.osVersion, sessionId);
Expand Down Expand Up @@ -369,7 +380,7 @@ export class TestingBotDriver implements MobilewrightDriver {
}
this.rotatedDeviceIds.add(poolDeviceId);
}
sessionId ??= await this.startFreshSession(config);
sessionId ??= await this.startFreshSession(config, poolDeviceId);
this.active = { sessionId, platform: config.platform, deviceType: config.deviceType, connectedAt: Date.now() };
setActiveDriver(this);
debug('connected to session %s (slot %s)', sessionId, poolDeviceId);
Expand Down Expand Up @@ -415,12 +426,17 @@ export class TestingBotDriver implements MobilewrightDriver {
}

/** Create a new hub session for this slot from the connect() config. */
private async startFreshSession(config: ConnectionConfig): Promise<string> {
private async startFreshSession(config: ConnectionConfig, poolDeviceId: string): Promise<string> {
// mobilewright's connect() passes only platform/deviceId/deviceType, so
// the slot's deviceName/osVersion filter comes from the coordinator's
// allocate() record. Values on the config win when a caller provides them.
const slot = this.runLog.slotFor(poolDeviceId);
const configName = config.deviceName instanceof RegExp ? config.deviceName.source : config.deviceName;
const criteria: AllocationCriteria = {
platform: config.platform,
deviceType: config.deviceType,
osVersion: config.osVersion,
deviceNamePattern: config.deviceName instanceof RegExp ? config.deviceName.source : config.deviceName,
deviceType: config.deviceType ?? (slot?.deviceType as AllocationCriteria['deviceType']),
osVersion: config.osVersion ?? slot?.osVersion,
deviceNamePattern: configName ?? slot?.deviceNamePattern,
};
let pinned: PinnedDevice | undefined;
if (criteria.deviceType === 'real') {
Expand Down
21 changes: 20 additions & 1 deletion src/run-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,22 @@ export interface RecordingRecord {
output: string;
}

type Record_ = SessionRecord | AppRecord | RecordingRecord;
/**
* The criteria a pool slot was allocated with. mobilewright's connect() only
* forwards platform/deviceId/deviceType, so a worker rotating sessions
* (sessionPerTest) looks the slot's deviceName/osVersion filter up here —
* without it every rotated session would land on an arbitrary device.
*/
export interface SlotRecord {
type: 'slot';
/** The pool deviceId — the WebDriver session id allocate() returned. */
deviceId: string;
deviceType?: string;
osVersion?: string;
deviceNamePattern?: string;
}

type Record_ = SessionRecord | AppRecord | RecordingRecord | SlotRecord;

export interface MergedSession {
sessionId: string;
Expand Down Expand Up @@ -130,6 +145,10 @@ export class RunLog {
return this.read().filter((r): r is RecordingRecord => r.type === 'recording');
}

slotFor(deviceId: string): SlotRecord | undefined {
return this.read().find((r): r is SlotRecord => r.type === 'slot' && r.deviceId === deviceId);
}

appUrlsFor(platform: string, deviceType: string | undefined): string[] | undefined {
const apps = this.read().filter((r): r is AppRecord => r.type === 'app');
return (
Expand Down
2 changes: 2 additions & 0 deletions test/fake-hub/fake-hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ export class FakeHub {
if (path === '/v1/devices/available' || path === '/v1/devices') {
return rest({
devices: [
// Listed first so an unfiltered real-device pick lands on it.
{ id: 2, name: 'Pixel', platform_name: 'Android', version: '7.1' },
{ id: 1, name: 'Pixel 8', platform_name: 'Android', version: '14' },
{ id: 3, name: 'iPhone 15', platform_name: 'iOS', version: '17.4' },
],
Expand Down
25 changes: 25 additions & 0 deletions test/integration/pool-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,31 @@ describe('sessionPerTest against FakeHub', () => {
expect(hub.testUpdates.get(fresh.id)).toMatchObject({ 'test[success]': '1' });
});

it('keeps the slot\'s deviceName/osVersion filter on rotated sessions', async () => {
// mobilewright's connect() forwards only platform/deviceId/deviceType, so
// the rotated session must recover the filter from the allocate() record —
// otherwise it lands on any real device (e.g. an old "Pixel" on 7.1).
const opts = { ...options(), apps: { android: 'tb://prebuilt-android' } as const };
const coordinator = new TestingBotDriver(opts);
await coordinator.prepare();
const allocated = await coordinator.allocate(
{ platform: 'android', deviceType: 'real', deviceNamePattern: '^(Pixel 8|Galaxy S23)$', osVersion: '>=12' },
new Set(),
);
expect(hub.liveSessions()[0]!.capabilities['appium:deviceName']).toBe('Pixel 8');

const worker = new TestingBotDriver(opts);
await worker.connect({ platform: 'android', deviceId: allocated.deviceId, deviceType: 'real' });
await worker.disconnect();
await worker.connect({ platform: 'android', deviceId: allocated.deviceId, deviceType: 'real' });
const rotated = hub.liveSessions()[0]!;
expect(rotated.id).not.toBe(allocated.deviceId);
expect(rotated.capabilities['appium:deviceName']).toBe('Pixel 8');
expect(rotated.capabilities['appium:platformVersion']).toBe('14');
await worker.disconnect();
await coordinator.dispose();
});

it('never reports one test\'s failure on the session another test ran in', async () => {
// The reported defect, end to end through the real run file: two per-test
// sessions, the first test passes and the second fails. FakeHub sessions
Expand Down
Loading