Skip to content

Commit 376fddd

Browse files
committed
Merge branch 'fix/android-observation-dump-timeout' into test/longpress-envelope-ledger-3326
2 parents c962e7a + cc4dd4a commit 376fddd

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎packages/platform-android/src/observation.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,34 @@ test('the helper focus call before the first dump does not spend the dump budget
257257

258258
expect(slowFocus.timeouts[0]).toBe(unhurried.timeouts[0]);
259259
});
260+
261+
test('a read that starts with the budget nearly spent never passes a non-positive timeout', async () => {
262+
// Each clock read advances 1 ms, so two reads of one deadline disagree by one millisecond.
263+
const originMs = Date.now();
264+
let elapsedMs = 0;
265+
const now = vi.spyOn(Date, 'now').mockImplementation(() => {
266+
elapsedMs += 1;
267+
return originMs + elapsedMs;
268+
});
269+
const timeouts: number[] = [];
270+
const host: AndroidObservationHost = {
271+
...hostFor(new Map()),
272+
runAdb: vi.fn(async (_device, _args, options) => {
273+
const timeoutMs = options?.timeoutMs ?? Number.POSITIVE_INFINITY;
274+
timeouts.push(timeoutMs);
275+
elapsedMs += timeoutMs - 2;
276+
return { exitCode: 0, stdout: '', stderr: '' };
277+
}),
278+
};
279+
try {
280+
await createAndroidObservationAdapter(host).readAppState({
281+
...device,
282+
id: 'emulator-rollover',
283+
});
284+
} finally {
285+
now.mockRestore();
286+
}
287+
288+
expect(timeouts.length).toBeGreaterThan(1);
289+
for (const timeoutMs of timeouts) expect(timeoutMs).toBeGreaterThan(0);
290+
});

‎packages/platform-android/src/observation.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ function createDumpReader(host: AndroidObservationHost, device: DeviceInfo): Dum
9797
const cached = dumps.get(key);
9898
if (cached) return cached;
9999
deadline ??= Deadline.fromTimeoutMs(DUMP_OBSERVATION_BUDGET_MS);
100-
if (deadline.isExpired()) return Promise.resolve('');
100+
const remainingMs = deadline.remainingMs();
101+
if (remainingMs <= 0) return Promise.resolve('');
101102
const pending = host
102103
.runAdb(device, deviceShellArgv('adb', 'shell', words), {
103104
allowFailure: true,
104-
timeoutMs: deadline.remainingMs(),
105+
timeoutMs: remainingMs,
105106
})
106107
.then(
107108
(result) => {

0 commit comments

Comments
 (0)