fix(sdk,transport): bounded prekey requests and a real registration retry (4.13.2)
An unreachable prekey server (packets dropped, not refused) held createShade for ~2 min per call, and a failed registration was never retried despite the log saying so. Every request now has a deadline, registration retries with backoff until it lands, and registration: 'background' lets an app open its identity offline. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
111
packages/shade-sdk/tests/registration.test.ts
Normal file
111
packages/shade-sdk/tests/registration.test.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import { createShade } from '../src/index.js';
|
||||
|
||||
/**
|
||||
* Opening an identity used to wait on the prekey server with no deadline:
|
||||
* with the server unreachable (packets dropped, not refused) `createShade`
|
||||
* hung until the kernel gave up on the TCP handshake, about two minutes —
|
||||
* and the failed registration was never retried, despite the log saying
|
||||
* it would be.
|
||||
*/
|
||||
describe('registration with an unreachable prekey server', () => {
|
||||
/** Never answers; honours abort like the real fetch. Counts calls. */
|
||||
function blackHole() {
|
||||
const urls: string[] = [];
|
||||
const fetch = ((url: string, init?: RequestInit) => {
|
||||
urls.push(url);
|
||||
return new Promise<Response>((_resolve, reject) => {
|
||||
init?.signal?.addEventListener('abort', () => reject(init.signal!.reason));
|
||||
});
|
||||
}) as unknown as typeof globalThis.fetch;
|
||||
return { fetch, urls };
|
||||
}
|
||||
|
||||
test("'background' opens the identity without waiting on the server", async () => {
|
||||
const net = blackHole();
|
||||
const started = Date.now();
|
||||
const shade = await createShade({
|
||||
prekeyServer: 'https://prekey.invalid',
|
||||
address: 'alice',
|
||||
autoReplenish: false,
|
||||
registration: 'background',
|
||||
requestTimeoutMs: 60_000,
|
||||
fetch: net.fetch,
|
||||
});
|
||||
try {
|
||||
expect(Date.now() - started).toBeLessThan(5_000);
|
||||
expect(shade.myAddress).toBe('alice');
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
expect(net.urls).toEqual(['https://prekey.invalid/v1/keys/register']);
|
||||
} finally {
|
||||
await shade.shutdown();
|
||||
}
|
||||
});
|
||||
|
||||
test("'await' gives up at the request deadline instead of hanging", async () => {
|
||||
const net = blackHole();
|
||||
const started = Date.now();
|
||||
const shade = await createShade({
|
||||
prekeyServer: 'https://prekey.invalid',
|
||||
address: 'bob',
|
||||
autoReplenish: false,
|
||||
requestTimeoutMs: 100,
|
||||
fetch: net.fetch,
|
||||
});
|
||||
try {
|
||||
expect(Date.now() - started).toBeLessThan(5_000);
|
||||
expect(net.urls).toHaveLength(1);
|
||||
} finally {
|
||||
await shade.shutdown();
|
||||
}
|
||||
});
|
||||
|
||||
test('a failed registration is retried until the server answers', async () => {
|
||||
let calls = 0;
|
||||
const fetch = (async () => {
|
||||
calls += 1;
|
||||
if (calls === 1) throw new TypeError('Unable to connect');
|
||||
return new Response('{}', { status: 200 });
|
||||
}) as unknown as typeof globalThis.fetch;
|
||||
const shade = await createShade({
|
||||
prekeyServer: 'https://prekey.invalid',
|
||||
address: 'carol',
|
||||
autoReplenish: false,
|
||||
fetch,
|
||||
});
|
||||
try {
|
||||
expect(calls).toBe(1);
|
||||
// First retry is due 5 s after the failure.
|
||||
await new Promise((r) => setTimeout(r, 5_300));
|
||||
expect(calls).toBe(2);
|
||||
} finally {
|
||||
await shade.shutdown();
|
||||
}
|
||||
}, 10_000);
|
||||
|
||||
test('shutdown stops the retries', async () => {
|
||||
let calls = 0;
|
||||
const fetch = (async () => {
|
||||
calls += 1;
|
||||
throw new TypeError('Unable to connect');
|
||||
}) as unknown as typeof globalThis.fetch;
|
||||
const shade = await createShade({
|
||||
prekeyServer: 'https://prekey.invalid',
|
||||
address: 'dave',
|
||||
autoReplenish: false,
|
||||
fetch,
|
||||
});
|
||||
await shade.shutdown();
|
||||
await new Promise((r) => setTimeout(r, 5_300));
|
||||
expect(calls).toBe(1);
|
||||
}, 10_000);
|
||||
|
||||
test('nonsense settings are refused up front', async () => {
|
||||
await expect(
|
||||
createShade({ prekeyServer: 'https://x.invalid', requestTimeoutMs: 0 }),
|
||||
).rejects.toThrow('requestTimeoutMs');
|
||||
await expect(
|
||||
createShade({ prekeyServer: 'https://x.invalid', registration: 'later' as never }),
|
||||
).rejects.toThrow('registration');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user