mirror of
https://github.com/Zakkaus/doona.git
synced 2026-12-06 08:02:04 +00:00
fix(api): keep a host clock per backend and stop it flapping
Each backend profile now owns its clock, so a late response from the previous profile cannot move the new one, and the settings form test connection no longer resets the active clock. A shift starts above 5 s of skew and holds until the skew falls below 2.5 s.
This commit is contained in:
1 parent
d20742c656
commit
108110c54e
6 files changed
+153
-87
No files matched your search
+17
-4
@@ -1,14 +1,13 @@
|
||||
import {afterEach, describe, expect, it, vi} from 'vitest';
|
||||
import {createApi} from './client';
|
||||
import {ApiError} from './error';
|
||||
import {resetServerClock, serverNow} from './serverClock';
|
||||
import {createServerClock, selectServerClock} from './serverClock';
|
||||
import type {ApiEvent} from './model';
|
||||
|
||||
const acceptedBody = {operation_id: 'op-1', kind: 'reload', status: 'queued', href: '/api/v1/operations/op-1'};
|
||||
const json = (body: unknown, status = 200, headers?: HeadersInit) =>
|
||||
new Response(JSON.stringify(body), {status, headers: {'Content-Type': 'application/json', ...headers}});
|
||||
afterEach(() => {
|
||||
resetServerClock();
|
||||
vi.unstubAllGlobals();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
@@ -20,8 +19,22 @@ describe('native transport', () => {
|
||||
'fetch',
|
||||
vi.fn(async () => json({observed_at: new Date(behind).toISOString()}))
|
||||
);
|
||||
await createApi('https://honk.test').runtime();
|
||||
expect(Math.abs(serverNow() - behind)).toBeLessThan(1000);
|
||||
const clock = createServerClock();
|
||||
await createApi('https://honk.test', undefined, clock).runtime();
|
||||
expect(Math.abs(clock.now() - behind)).toBeLessThan(1000);
|
||||
});
|
||||
it('keeps a previous backend response off the new backend clock', async () => {
|
||||
const behind = Date.now() - 600_000;
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn(async () => json({observed_at: new Date(behind).toISOString()}))
|
||||
);
|
||||
const previous = createServerClock();
|
||||
const current = createServerClock();
|
||||
selectServerClock(current);
|
||||
await createApi('https://old.test', undefined, previous).runtime();
|
||||
expect(Math.abs(current.now() - Date.now())).toBeLessThan(1000);
|
||||
selectServerClock(createServerClock());
|
||||
});
|
||||
it('retries a refused mutation with the same body and idempotency key after the floor', async () => {
|
||||
vi.useFakeTimers();
|
||||
|
||||
+44
-41
@@ -19,13 +19,12 @@ import {readSse} from './sse';
|
||||
import {wait} from './wait';
|
||||
import {eventKinds} from './selectors';
|
||||
import {normalizeCapabilities} from './capabilities';
|
||||
import {noteServerTime, resetServerClock} from './serverClock';
|
||||
import {createServerClock, type ServerClock} from './serverClock';
|
||||
|
||||
const retryAfter = (response: Response) => Math.max(1, Number(response.headers.get('Retry-After')) || 1);
|
||||
|
||||
function data<T>(result: {data?: T; response: Response}): T {
|
||||
if (result.data === undefined) throw clientError(result.response.status, 'empty_response', 'Response has no JSON body', 'ui.errNoJson');
|
||||
noteServerTime((result.data as {observed_at?: unknown} | null)?.observed_at);
|
||||
return result.data;
|
||||
}
|
||||
|
||||
@@ -39,9 +38,13 @@ const MAX_REFUSALS = 3;
|
||||
const readOnlyPaths = ['/dns/query', '/config/validate', '/routing/trace'];
|
||||
|
||||
/** Base is the server root, optionally including a reverse-proxy prefix. */
|
||||
export function createApi(base: string, token?: string): Api {
|
||||
export function createApi(base: string, token?: string, clock: ServerClock = createServerClock()): Api {
|
||||
const baseUrl = base.replace(/\/+$/, '');
|
||||
resetServerClock();
|
||||
const read = <T>(result: {data?: T; response: Response}): T => {
|
||||
const value = data(result);
|
||||
clock.note((value as {observed_at?: unknown} | null)?.observed_at);
|
||||
return value;
|
||||
};
|
||||
const headers: Record<string, string> = {Accept: 'application/json'};
|
||||
if (token) headers.Authorization = 'Bearer ' + token;
|
||||
const client = createClient<paths>({
|
||||
@@ -190,21 +193,21 @@ export function createApi(base: string, token?: string): Api {
|
||||
});
|
||||
}
|
||||
return {
|
||||
discovery: async signal => data(await client.GET('/api', {signal})),
|
||||
version: async signal => data(await client.GET('/api/v1/version', {signal})),
|
||||
capabilities: async signal => normalizeCapabilities(data(await client.GET('/api/v1/capabilities', {signal}))),
|
||||
runtime: async signal => data(await client.GET('/api/v1/runtime', {signal})),
|
||||
runtimeOutbounds: async signal => data(await client.GET('/api/v1/runtime/outbounds', {signal})),
|
||||
trafficHistory: async (query, signal) => data(await client.GET('/api/v1/runtime/traffic/history', {params: {query}, signal})),
|
||||
memoryHistory: async (query, signal) => data(await client.GET('/api/v1/runtime/memory/history', {params: {query}, signal})),
|
||||
datapath: async (detail, signal) => data(await client.GET('/api/v1/datapath', {params: {query: {detail}}, signal})),
|
||||
runtimeMemory: async signal => data(await client.GET('/api/v1/runtime/memory', {signal})),
|
||||
nodes: async (query, signal) => data(await client.GET('/api/v1/nodes', {params: {query}, signal})),
|
||||
groups: async signal => data(await client.GET('/api/v1/groups', {signal})),
|
||||
group: async (id, signal) => data(await client.GET('/api/v1/groups/{groupId}', {params: {path: {groupId: id}}, signal})),
|
||||
selectGroup: async (groupId, body, signal) => data(await client.PUT('/api/v1/groups/{groupId}/selection', {params: {path: {groupId}}, body, signal})),
|
||||
discovery: async signal => read(await client.GET('/api', {signal})),
|
||||
version: async signal => read(await client.GET('/api/v1/version', {signal})),
|
||||
capabilities: async signal => normalizeCapabilities(read(await client.GET('/api/v1/capabilities', {signal}))),
|
||||
runtime: async signal => read(await client.GET('/api/v1/runtime', {signal})),
|
||||
runtimeOutbounds: async signal => read(await client.GET('/api/v1/runtime/outbounds', {signal})),
|
||||
trafficHistory: async (query, signal) => read(await client.GET('/api/v1/runtime/traffic/history', {params: {query}, signal})),
|
||||
memoryHistory: async (query, signal) => read(await client.GET('/api/v1/runtime/memory/history', {params: {query}, signal})),
|
||||
datapath: async (detail, signal) => read(await client.GET('/api/v1/datapath', {params: {query: {detail}}, signal})),
|
||||
runtimeMemory: async signal => read(await client.GET('/api/v1/runtime/memory', {signal})),
|
||||
nodes: async (query, signal) => read(await client.GET('/api/v1/nodes', {params: {query}, signal})),
|
||||
groups: async signal => read(await client.GET('/api/v1/groups', {signal})),
|
||||
group: async (id, signal) => read(await client.GET('/api/v1/groups/{groupId}', {params: {path: {groupId: id}}, signal})),
|
||||
selectGroup: async (groupId, body, signal) => read(await client.PUT('/api/v1/groups/{groupId}/selection', {params: {path: {groupId}}, body, signal})),
|
||||
clearGroupOverride: async (groupId, network, signal) =>
|
||||
data(await client.DELETE('/api/v1/groups/{groupId}/selection', {params: {path: {groupId}, query: {network}}, signal})),
|
||||
read(await client.DELETE('/api/v1/groups/{groupId}/selection', {params: {path: {groupId}, query: {network}}, signal})),
|
||||
patchGroup: async (groupId, body, ifMatch, signal) => {
|
||||
const result = await client.PATCH('/api/v1/groups/{groupId}', {
|
||||
params: {path: {groupId}, header: {'If-Match': ifMatch}},
|
||||
@@ -212,34 +215,34 @@ export function createApi(base: string, token?: string): Api {
|
||||
body,
|
||||
signal
|
||||
});
|
||||
const value = data(result);
|
||||
const value = read(result);
|
||||
return 'operation_id' in value ? accepted({data: value, response: result.response}) : value;
|
||||
},
|
||||
startProbe: async (body, signal) => accepted(await client.POST('/api/v1/probes', {body, headers: once(), signal})),
|
||||
connections: async (query, signal) => data(await client.GET('/api/v1/connections', {params: {query}, signal})),
|
||||
flows: async (query, signal) => data(await client.GET('/api/v1/flows', {params: {query}, signal})),
|
||||
connections: async (query, signal) => read(await client.GET('/api/v1/connections', {params: {query}, signal})),
|
||||
flows: async (query, signal) => read(await client.GET('/api/v1/flows', {params: {query}, signal})),
|
||||
// Readable in openapi-fetch drops required null fields from composed schemas.
|
||||
flow: async (id, signal) => data(await client.GET('/api/v1/flows/{flow_id}', {params: {path: {flow_id: id}}, signal})) as FlowDetail,
|
||||
dnsCache: async (query, signal) => data(await client.GET('/api/v1/dns/cache', {params: {query}, signal})),
|
||||
dnsLog: async (query, signal) => data(await client.GET('/api/v1/dns/log', {params: {query}, signal})),
|
||||
dnsQuery: async (domain, types, signal) => data(await client.GET('/api/v1/dns/query', {params: {query: {domain, type: types, detail: 'full'}}, signal})),
|
||||
flow: async (id, signal) => read(await client.GET('/api/v1/flows/{flow_id}', {params: {path: {flow_id: id}}, signal})) as FlowDetail,
|
||||
dnsCache: async (query, signal) => read(await client.GET('/api/v1/dns/cache', {params: {query}, signal})),
|
||||
dnsLog: async (query, signal) => read(await client.GET('/api/v1/dns/log', {params: {query}, signal})),
|
||||
dnsQuery: async (domain, types, signal) => read(await client.GET('/api/v1/dns/query', {params: {query: {domain, type: types, detail: 'full'}}, signal})),
|
||||
// 204 carries no body; the response middleware has already turned any error status into an ApiError.
|
||||
closeConnection: async (connection_id, signal) => {
|
||||
await client.DELETE('/api/v1/connections/{connection_id}', {params: {path: {connection_id}}, headers: once(), signal});
|
||||
},
|
||||
closeConnections: async (query, signal) => data(await client.DELETE('/api/v1/connections', {params: {query}, headers: once(), signal})),
|
||||
runtimeSettings: async signal => data(await client.GET('/api/v1/runtime/settings', {signal})),
|
||||
providers: async (query, signal) => data(await client.GET('/api/v1/providers', {params: {query}, signal})),
|
||||
closeConnections: async (query, signal) => read(await client.DELETE('/api/v1/connections', {params: {query}, headers: once(), signal})),
|
||||
runtimeSettings: async signal => read(await client.GET('/api/v1/runtime/settings', {signal})),
|
||||
providers: async (query, signal) => read(await client.GET('/api/v1/providers', {params: {query}, signal})),
|
||||
refreshProvider: async (id, signal) => accepted(await client.POST('/api/v1/providers/{id}/refresh', {params: {path: {id}}, headers: once(), signal})),
|
||||
createProvider: async (body, signal) => data(await client.POST('/api/v1/providers', {body, signal})),
|
||||
deleteProvider: async (id, signal) => data(await client.DELETE('/api/v1/providers/{id}', {params: {path: {id}}, signal})),
|
||||
createNode: async (body, signal) => data(await client.POST('/api/v1/nodes', {body, signal})),
|
||||
deleteNode: async (id, signal) => data(await client.DELETE('/api/v1/nodes/{id}', {params: {path: {id}}, signal})),
|
||||
geodata: async signal => data(await client.GET('/api/v1/geodata', {signal})),
|
||||
rules: async signal => data(await client.GET('/api/v1/rules', {signal})),
|
||||
createProvider: async (body, signal) => read(await client.POST('/api/v1/providers', {body, signal})),
|
||||
deleteProvider: async (id, signal) => read(await client.DELETE('/api/v1/providers/{id}', {params: {path: {id}}, signal})),
|
||||
createNode: async (body, signal) => read(await client.POST('/api/v1/nodes', {body, signal})),
|
||||
deleteNode: async (id, signal) => read(await client.DELETE('/api/v1/nodes/{id}', {params: {path: {id}}, signal})),
|
||||
geodata: async signal => read(await client.GET('/api/v1/geodata', {signal})),
|
||||
rules: async signal => read(await client.GET('/api/v1/rules', {signal})),
|
||||
updateGeodata: async signal => accepted(await client.POST('/api/v1/geodata/update', {headers: once(), signal})),
|
||||
config: async signal => data(await client.GET('/api/v1/config', {signal})),
|
||||
validateConfig: async (body, signal) => data(await client.POST('/api/v1/config/validate', {body, signal})),
|
||||
config: async signal => read(await client.GET('/api/v1/config', {signal})),
|
||||
validateConfig: async (body, signal) => read(await client.POST('/api/v1/config/validate', {body, signal})),
|
||||
replaceConfigSource: async (source_id, content, ifMatch, signal) =>
|
||||
accepted(
|
||||
await client.PUT('/api/v1/config/sources/{source_id}', {
|
||||
@@ -249,17 +252,17 @@ export function createApi(base: string, token?: string): Api {
|
||||
signal
|
||||
})
|
||||
),
|
||||
patchRuntimeSettings: async (body, signal) => data(await client.PATCH('/api/v1/runtime/settings', {body, headers: once(), signal})),
|
||||
deleteDnsEntry: async (entry_id, signal) => data(await client.DELETE('/api/v1/dns/cache/{entry_id}', {params: {path: {entry_id}}, signal})),
|
||||
flushDnsCache: async signal => data(await client.POST('/api/v1/dns/cache/flush', {body: {}, signal})),
|
||||
patchRuntimeSettings: async (body, signal) => read(await client.PATCH('/api/v1/runtime/settings', {body, headers: once(), signal})),
|
||||
deleteDnsEntry: async (entry_id, signal) => read(await client.DELETE('/api/v1/dns/cache/{entry_id}', {params: {path: {entry_id}}, signal})),
|
||||
flushDnsCache: async signal => read(await client.POST('/api/v1/dns/cache/flush', {body: {}, signal})),
|
||||
// Readable also drops SimulationDnsData.attempt_id, whose contract value is null.
|
||||
routingTrace: async (body, signal) => data(await client.POST('/api/v1/routing/trace', {body, signal})) as RoutingTraceResponse,
|
||||
routingTrace: async (body, signal) => read(await client.POST('/api/v1/routing/trace', {body, signal})) as RoutingTraceResponse,
|
||||
startReload: async signal => accepted(await client.POST('/api/v1/operations/reload', {body: {}, headers: once(), signal})),
|
||||
startSuspend: async signal => accepted(await client.POST('/api/v1/operations/suspend', {body: {}, headers: once(), signal})),
|
||||
startResume: async signal => accepted(await client.POST('/api/v1/operations/resume', {body: {}, headers: once(), signal})),
|
||||
operation: async (id, signal) => {
|
||||
const result = await client.GET('/api/v1/operations/{id}', {params: {path: {id}}, signal});
|
||||
return {...data(result), retryAfter: retryAfter(result.response)} as OperationState;
|
||||
return {...read(result), retryAfter: retryAfter(result.response)} as OperationState;
|
||||
},
|
||||
pollOperation,
|
||||
subscribeEvents,
|
||||
|
||||
+9
-3
@@ -1,5 +1,6 @@
|
||||
import type {Api} from './api';
|
||||
import {createApi} from './client';
|
||||
import {createServerClock, selectServerClock} from './serverClock';
|
||||
import {pinnedProfile, storageRevision, touchStorage} from './profiles';
|
||||
import {sessionExpiry, sessionToken} from './session';
|
||||
|
||||
@@ -26,9 +27,14 @@ export function getApi(): Api {
|
||||
const token = (profile && base ? sessionToken(profile.id, base) : null) ?? profile?.token;
|
||||
const key = JSON.stringify([profile?.id, base, token]);
|
||||
if (!selected || configuration !== key) {
|
||||
if (base && base !== 'mock') selected = createApi(base, token ?? undefined);
|
||||
else if (mockFactory) selected = mockFactory();
|
||||
else if (selected) {
|
||||
if (base && base !== 'mock') {
|
||||
const clock = createServerClock();
|
||||
selected = createApi(base, token ?? undefined, clock);
|
||||
selectServerClock(clock);
|
||||
} else if (mockFactory) {
|
||||
selected = mockFactory();
|
||||
selectServerClock(createServerClock());
|
||||
} else if (selected) {
|
||||
// This tab saved the mock and reloads: keep serving the current backend until the mock has loaded.
|
||||
void import('./mock').then(module => {
|
||||
mockFactory = module.createMockApi;
|
||||
|
||||
+49
-21
@@ -1,43 +1,71 @@
|
||||
import {afterEach, expect, it} from 'vitest';
|
||||
import {noteServerTime, resetServerClock, serverNow} from './serverClock';
|
||||
import {createServerClock, serverNow, selectServerClock} from './serverClock';
|
||||
|
||||
const at = (ms: number) => new Date(ms).toISOString();
|
||||
const local = Date.parse('2026-09-25T10:00:00Z');
|
||||
afterEach(resetServerClock);
|
||||
afterEach(() => selectServerClock(createServerClock()));
|
||||
|
||||
it('follows the browser clock until a response says otherwise', () => {
|
||||
expect(serverNow(local)).toBe(local);
|
||||
noteServerTime(undefined, local);
|
||||
noteServerTime('not a time', local);
|
||||
expect(serverNow(local)).toBe(local);
|
||||
const clock = createServerClock();
|
||||
expect(clock.now(local)).toBe(local);
|
||||
clock.note(undefined, local);
|
||||
clock.note('not a time', local);
|
||||
expect(clock.now(local)).toBe(local);
|
||||
});
|
||||
|
||||
it('ignores a skew of a few seconds', () => {
|
||||
noteServerTime(at(local - 3000), local);
|
||||
expect(serverNow(local)).toBe(local);
|
||||
const clock = createServerClock();
|
||||
clock.note(at(local - 3000), local);
|
||||
expect(clock.now(local)).toBe(local);
|
||||
});
|
||||
|
||||
it('shifts to a host clock that is minutes behind or ahead', () => {
|
||||
noteServerTime(at(local - 600_000), local);
|
||||
expect(serverNow(local)).toBe(local - 600_000);
|
||||
resetServerClock();
|
||||
noteServerTime(at(local + 120_000), local);
|
||||
expect(serverNow(local + 1000)).toBe(local + 121_000);
|
||||
const behind = createServerClock();
|
||||
behind.note(at(local - 600_000), local);
|
||||
expect(behind.now(local)).toBe(local - 600_000);
|
||||
const ahead = createServerClock();
|
||||
ahead.note(at(local + 120_000), local);
|
||||
expect(ahead.now(local + 1000)).toBe(local + 121_000);
|
||||
});
|
||||
|
||||
it('keeps a shift until the skew falls below half the tolerance, so it does not flap', () => {
|
||||
const clock = createServerClock();
|
||||
clock.note(at(local - 5500), local);
|
||||
expect(clock.now(local)).toBe(local - 5500);
|
||||
clock.note(at(local + 60_000 - 4500), local + 60_000);
|
||||
expect(clock.now(local + 60_000)).toBe(local + 60_000 - 4500);
|
||||
clock.note(at(local + 120_000 - 2000), local + 120_000);
|
||||
expect(clock.now(local + 120_000)).toBe(local + 120_000);
|
||||
clock.note(at(local + 180_000 - 4500), local + 180_000);
|
||||
expect(clock.now(local + 180_000)).toBe(local + 180_000);
|
||||
});
|
||||
|
||||
it('takes the freshest reading, since a retained snapshot only looks older', () => {
|
||||
noteServerTime(at(local - 600_000), local);
|
||||
noteServerTime(at(local - 630_000), local + 1000);
|
||||
expect(serverNow(local + 1000)).toBe(local - 599_000);
|
||||
const clock = createServerClock();
|
||||
clock.note(at(local - 600_000), local);
|
||||
clock.note(at(local - 630_000), local + 1000);
|
||||
expect(clock.now(local + 1000)).toBe(local - 599_000);
|
||||
});
|
||||
|
||||
it('forgets old readings, so a host clock corrected by NTP is followed', () => {
|
||||
noteServerTime(at(local + 120_000), local);
|
||||
noteServerTime(at(local + 120_000), local + 120_000);
|
||||
expect(serverNow(local + 120_000)).toBe(local + 120_000);
|
||||
const clock = createServerClock();
|
||||
clock.note(at(local + 120_000), local);
|
||||
clock.note(at(local + 120_000), local + 120_000);
|
||||
expect(clock.now(local + 120_000)).toBe(local + 120_000);
|
||||
});
|
||||
|
||||
it('keeps the last estimate while no response arrives', () => {
|
||||
noteServerTime(at(local - 600_000), local);
|
||||
expect(serverNow(local + 300_000)).toBe(local - 300_000);
|
||||
const clock = createServerClock();
|
||||
clock.note(at(local - 600_000), local);
|
||||
expect(clock.now(local + 300_000)).toBe(local - 300_000);
|
||||
});
|
||||
|
||||
it('reads relative times on the active backend clock only', () => {
|
||||
const previous = createServerClock();
|
||||
const current = createServerClock();
|
||||
selectServerClock(current);
|
||||
previous.note(at(local - 600_000), local);
|
||||
expect(serverNow(local)).toBe(local);
|
||||
current.note(at(local + 120_000), local);
|
||||
expect(serverNow(local)).toBe(local + 120_000);
|
||||
});
|
||||
+29
-15
@@ -1,25 +1,39 @@
|
||||
// The host's clock, for comparing against the timestamps it sends. A router without an RTC can run minutes off until
|
||||
// NTP syncs, and relative times or chart windows taken from the browser clock would then be wrong. A response's
|
||||
// observed_at is never later than the host's time on arrival, so the largest recent offset is the closest. A reading
|
||||
// expires a minute before the newest one, so a corrected host clock is followed; while no response arrives, the last
|
||||
// estimate stands. Skews of a few seconds are left alone.
|
||||
// observed_at is never later than the host's time on arrival, so the largest recent offset is the closest. Readings
|
||||
// age out a minute before the newest one, so a corrected host clock is followed; while no response arrives, the last
|
||||
// estimate stands. A shift starts above a few seconds of skew and stops below half that, so it does not flap.
|
||||
const tolerance = 5000;
|
||||
const span = 60_000;
|
||||
let readings: Array<{at: number; offset: number}> = [];
|
||||
|
||||
export function noteServerTime(observedAt: unknown, receivedAt = Date.now()) {
|
||||
const time = typeof observedAt === 'string' ? Date.parse(observedAt) : NaN;
|
||||
if (!Number.isFinite(time)) return;
|
||||
readings = readings.filter(reading => receivedAt - reading.at < span);
|
||||
readings.push({at: receivedAt, offset: time - receivedAt});
|
||||
export type ServerClock = {
|
||||
note(observedAt: unknown, receivedAt?: number): void;
|
||||
now(now?: number): number;
|
||||
};
|
||||
|
||||
export function createServerClock(): ServerClock {
|
||||
let readings: Array<{at: number; offset: number}> = [];
|
||||
let offset = 0;
|
||||
return {
|
||||
note(observedAt, receivedAt = Date.now()) {
|
||||
const time = typeof observedAt === 'string' ? Date.parse(observedAt) : NaN;
|
||||
if (!Number.isFinite(time)) return;
|
||||
readings = readings.filter(reading => receivedAt - reading.at < span);
|
||||
readings.push({at: receivedAt, offset: time - receivedAt});
|
||||
const latest = Math.max(...readings.map(reading => reading.offset));
|
||||
offset = Math.abs(latest) > (offset ? tolerance / 2 : tolerance) ? latest : 0;
|
||||
},
|
||||
now: (now = Date.now()) => now + offset
|
||||
};
|
||||
}
|
||||
|
||||
export function serverNow(now = Date.now()) {
|
||||
const offset = Math.max(...readings.map(reading => reading.offset));
|
||||
return Number.isFinite(offset) && Math.abs(offset) > tolerance ? now + offset : now;
|
||||
// Each backend has its own clock; the one in use is the one relative times and chart windows read.
|
||||
let active = createServerClock();
|
||||
|
||||
export function selectServerClock(clock: ServerClock) {
|
||||
active = clock;
|
||||
}
|
||||
|
||||
// A different backend has its own clock.
|
||||
export function resetServerClock() {
|
||||
readings = [];
|
||||
export function serverNow(now?: number) {
|
||||
return active.now(now);
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
import {afterEach, expect, it} from 'vitest';
|
||||
import {noteServerTime, resetServerClock} from '../../api/serverClock';
|
||||
import {createServerClock, selectServerClock} from '../../api/serverClock';
|
||||
import {historyTrafficSamples, trafficWindow} from './traffic';
|
||||
|
||||
afterEach(resetServerClock);
|
||||
afterEach(() => selectServerClock(createServerClock()));
|
||||
|
||||
it('places the live window on the host clock, so a lagging host still charts', () => {
|
||||
const host = Date.now() - 600_000;
|
||||
noteServerTime(new Date(host).toISOString());
|
||||
const clock = createServerClock();
|
||||
clock.note(new Date(host).toISOString());
|
||||
selectServerClock(clock);
|
||||
const samples = [5, 4, 3].map(back => ({time: host - back * 1000, up: 1, down: 1, connections: 1}));
|
||||
const series = trafficWindow({fine: [], coarse: []}, samples, 120);
|
||||
expect(series.timestamps).toEqual(samples.map(s => s.time));
|
||||
|
||||
Reference in new issue
Block a user