52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
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([]);
|
||
});
|
||
});
|