fix(webui): stop idle polling and release background timers
This commit is contained in:
1 parent
652b9c4eda
commit
0f358b2ac2
8 files changed
+257
-131
No files matched your search
@@ -1,12 +1,57 @@
|
||||
import { getLogLevel, setLogLevel, subscribeLogs, type LogEntry } from '@snowluma/common/logger';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { StateBus } from '../src/webui/state-bus';
|
||||
import { startConnectionDiffLoop } from '../src/webui/connection-diff-loop';
|
||||
import {
|
||||
createConnectionPoller,
|
||||
type ConnectionDiffLoopOptions,
|
||||
} from '../src/webui/connection-diff-loop';
|
||||
import { comparableConnectionSnapshot } from '../src/webui/connection-snapshot';
|
||||
|
||||
const previousLogLevel = getLogLevel();
|
||||
|
||||
describe('startConnectionDiffLoop', () => {
|
||||
function startConnectionDiffLoop(options: ConnectionDiffLoopOptions) {
|
||||
return { dispose: createConnectionPoller(options)() };
|
||||
}
|
||||
|
||||
describe('connection polling', () => {
|
||||
it('shares one timer and releases it with the last subscriber', () => {
|
||||
const bus = new StateBus();
|
||||
const getSnapshot = vi.fn(() => []);
|
||||
const subscribe = createConnectionPoller({ bus, getSnapshot });
|
||||
vi.advanceTimersByTime(1000);
|
||||
expect(getSnapshot).not.toHaveBeenCalled();
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
const releaseFirst = subscribe();
|
||||
const releaseSecond = subscribe();
|
||||
expect(getSnapshot).toHaveBeenCalledOnce();
|
||||
expect(vi.getTimerCount()).toBe(1);
|
||||
releaseFirst();
|
||||
releaseFirst();
|
||||
vi.advanceTimersByTime(500);
|
||||
expect(getSnapshot).toHaveBeenCalledTimes(2);
|
||||
releaseSecond();
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
vi.advanceTimersByTime(1000);
|
||||
expect(getSnapshot).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('takes a fresh baseline on resubscription and detects changes from the first interval', () => {
|
||||
const bus = new StateBus();
|
||||
const publish = vi.spyOn(bus, 'publish');
|
||||
let snapshot = 'initial';
|
||||
const subscribe = createConnectionPoller({ bus, getSnapshot: () => snapshot });
|
||||
const release = subscribe();
|
||||
snapshot = 'connected';
|
||||
vi.advanceTimersByTime(500);
|
||||
expect(publish).toHaveBeenCalledOnce();
|
||||
release();
|
||||
snapshot = 'offline';
|
||||
const releaseNext = subscribe();
|
||||
vi.advanceTimersByTime(500);
|
||||
expect(publish).toHaveBeenCalledOnce();
|
||||
releaseNext();
|
||||
});
|
||||
|
||||
it('treats an undefined snapshot as an established baseline', () => {
|
||||
vi.useFakeTimers();
|
||||
const bus = new StateBus();
|
||||
@@ -26,6 +71,7 @@ describe('startConnectionDiffLoop', () => {
|
||||
});
|
||||
afterEach(() => {
|
||||
setLogLevel(previousLogLevel);
|
||||
vi.clearAllTimers();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
@@ -169,7 +215,7 @@ describe('startConnectionDiffLoop', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('first tick baselines the snapshot (no publish); only the second tick with a different value publishes', () => {
|
||||
it('publishes only when the current snapshot differs from the subscription baseline', () => {
|
||||
const bus = new StateBus();
|
||||
const seen: string[] = [];
|
||||
bus.subscribe((r) => seen.push(r));
|
||||
@@ -181,7 +227,7 @@ describe('startConnectionDiffLoop', () => {
|
||||
getSnapshot: () => snap,
|
||||
intervalMs: 500,
|
||||
});
|
||||
vi.advanceTimersByTime(500); // first observation — baseline, no publish
|
||||
vi.advanceTimersByTime(500);
|
||||
expect(seen).toEqual([]);
|
||||
snap = [
|
||||
{ uin: '12345', nickname: 'me', adapters: [{ kind: 'http', status: 'connected' }] },
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import type { OneBotManager } from '@snowluma/onebot/manager';
|
||||
import { initWebUI } from '../src/webui/server';
|
||||
import { StateBus } from '../src/webui/state-bus';
|
||||
import { findAvailablePort } from '../src/webui/port';
|
||||
|
||||
vi.mock('@hono/node-server', () => ({
|
||||
serve: vi.fn((_options: unknown, onListen: (info: { port: number }) => void) => {
|
||||
queueMicrotask(() => onListen({ port: 5099 }));
|
||||
return { close: (done: () => void) => done() };
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('../src/webui/port', () => ({
|
||||
findAvailablePort: vi.fn(async () => 5099),
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval'] });
|
||||
vi.stubEnv('SNOWLUMA_DEV_MODE', '1');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllTimers();
|
||||
vi.useRealTimers();
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
describe('WebUI background tasks', () => {
|
||||
it('releases token maintenance when the server stops', async () => {
|
||||
const server = await initWebUI(5099, {} as OneBotManager);
|
||||
expect(vi.getTimerCount()).toBe(1);
|
||||
await server.stop();
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
});
|
||||
|
||||
it('does not poll connections without a client and releases maintenance on stop', async () => {
|
||||
const getConnectionStatuses = vi.fn(() => []);
|
||||
const manager = { getConnectionStatuses } as unknown as OneBotManager;
|
||||
const server = await initWebUI(5099, manager, undefined, undefined, {
|
||||
stateBus: new StateBus(),
|
||||
});
|
||||
|
||||
try {
|
||||
await vi.advanceTimersByTimeAsync(60_000);
|
||||
expect(getConnectionStatuses).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
await server.stop();
|
||||
}
|
||||
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
});
|
||||
|
||||
it('does not leave background tasks after startup fails', async () => {
|
||||
vi.mocked(findAvailablePort).mockRejectedValueOnce(new Error('no port'));
|
||||
await expect(
|
||||
initWebUI(5099, {} as OneBotManager, undefined, undefined, {
|
||||
stateBus: new StateBus(),
|
||||
}),
|
||||
).rejects.toThrow('no port');
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user