adb_cli/tests/devicePoller.test.ts
2026-09-07 16:16:22 +08:00

52 lines
1.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, it } from 'vitest';
import type { Device } from '../src/adb/deviceParser.js';
import { DevicePoller } from '../src/adb/devicePoller.js';
function delayedDevices(delayMs: number, serial: string): Promise<Device[]> {
return new Promise((resolve) => {
setTimeout(() => resolve([{ serial, state: 'device' }]), delayMs);
});
}
describe('DevicePoller 防重入(delivery D4)', () => {
it('上一次轮询未返回时跳过本次,返回 null', async () => {
let callCount = 0;
const poller = new DevicePoller(() => {
callCount++;
return delayedDevices(30, `serial-${callCount}`);
});
const first = poller.poll();
const second = await poller.poll();
expect(second).toBeNull(); // 被跳过
expect(await first).toEqual([{ serial: 'serial-1', state: 'device' }]);
expect(callCount).toBe(1);
});
it('上一次完成后可再次正常轮询', async () => {
let callCount = 0;
const poller = new DevicePoller(() => {
callCount++;
return delayedDevices(5, `serial-${callCount}`);
});
await poller.poll();
const next = await poller.poll();
expect(next).toEqual([{ serial: 'serial-2', state: 'device' }]);
expect(callCount).toBe(2);
});
it('轮询抛错后不卡死,可继续轮询', async () => {
let shouldThrow = true;
const poller = new DevicePoller(async () => {
if (shouldThrow) throw new Error('adb failed');
return [];
});
await expect(poller.poll()).rejects.toThrow('adb failed');
shouldThrow = false;
expect(await poller.poll()).toEqual([]);
});
});