Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ const jestConfig = {
"<rootDir>/packages/uix-core/src/__helpers__/jest.messagechannel.cjs",
],
}),
sdkProject("host", {}),
sdkProject("host", {
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
}),
sdkProject("guest", {
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
}),
sdkProject("host-react", {
testMatch: [
"<rootDir>/packages/uix-host-react/src/**/*.test.ts",
Expand Down
32 changes: 31 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions packages/uix-guest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"scripts": {
"build": "tsup",
"build:esm": "tsup --format esm,cjs,iife",
"test": "NODE_ENV=test jest",
"test:watch": "NODE_ENV=test jest --watch",
"watch": "tsup --watch --silent"
},
"browserslist": [
Expand All @@ -22,6 +24,12 @@
"ajv": "^8.12.0",
"js-yaml": "^4.1.0"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3"
},
"files": [
"README.md",
"dist",
Expand Down
145 changes: 145 additions & 0 deletions packages/uix-guest/src/guest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { Guest } from "./guest";
import type {
CrossRealmObject,
GuestApis,
HostConnection,
} from "@adobe/uix-core";

interface TestHostApis extends GuestApis {
someNamespace: { someMethod: () => Promise<unknown> };
}

type TestGuest = Guest<{
incoming: TestHostApis;
outgoing: GuestApis;
sharedContext: Record<string, unknown>;
}>;

// Mock connectParentWindow from uix-core so we can control the guest's
// "host connection" without a real postMessage handshake.
jest.mock("@adobe/uix-core", () => {
const actual = jest.requireActual("@adobe/uix-core");
return {
...actual,
connectParentWindow: jest.fn(),
};
});

import { connectParentWindow } from "@adobe/uix-core";
const mockConnectParentWindow = connectParentWindow as jest.MockedFunction<
typeof connectParentWindow
>;

function connectGuest(
invokeHostMethod: jest.Mock,
config?: { timeout?: number; callTimeout?: number }
) {
const fakeRemoteApi = {
invokeHostMethod,
getSharedContext: jest.fn().mockResolvedValue({}),
getConfiguration: jest.fn().mockResolvedValue({}),
};
const fakeHostConnection = {
getRemoteApi: () => fakeRemoteApi,
} as unknown as CrossRealmObject<HostConnection>;

mockConnectParentWindow.mockResolvedValue(fakeHostConnection);

const guest: TestGuest = new Guest({ id: "test-guest", ...config });
return guest.connect().then(() => guest);
}

describe("Guest host-method call timeout", () => {
beforeEach(() => {
jest.useFakeTimers();
mockConnectParentWindow.mockReset();
});

afterEach(() => {
jest.useRealTimers();
});

// A host method call used to be subject to a hardcoded 10000ms timeout in
// guest.ts's `host` proxy, decoupled from GuestConfig.timeout (which only
// ever governed the initial connectParentWindow handshake, not per-call
// RPC timeouts). This meant any call that legitimately took between 10s
// and 20s (e.g. because the host was still resolving a load batch
// containing an unrelated slow/broken guest) failed with a false "timed
// out" error, since Port's own connection timeout defaults to 20000ms
// (port.ts's defaultOptions.timeout) -- longer than the old 10000ms call
// ceiling.
//
// The fix: GuestConfig.callTimeout, independent of GuestConfig.timeout,
// defaulting to 20000ms to match Port's default.
it("uses the default 20000ms call timeout, independent of a smaller GuestConfig.timeout (connect timeout)", async () => {
// Never resolves/rejects: simulates a host method call that is still
// legitimately in flight (e.g. the host is mid-batch loading a sibling
// extension) rather than one that has actually failed.
const invokeHostMethod = jest.fn(() => new Promise(() => undefined));
// A small connect timeout should have no bearing on the call timeout.
const guest = await connectGuest(invokeHostMethod, { timeout: 5000 });

const callPromise = guest.host.someNamespace.someMethod();
const assertion = expect(callPromise).rejects.toThrow(
"timed out after 20000ms"
);

await jest.advanceTimersByTimeAsync(20001);
await assertion;
});

it("times out a host method call at 20000ms with no config set (default)", async () => {
const invokeHostMethod = jest.fn(() => new Promise(() => undefined));
const guest = await connectGuest(invokeHostMethod);

const callPromise = guest.host.someNamespace.someMethod();
const assertion = expect(callPromise).rejects.toThrow(
"timed out after 20000ms"
);

await jest.advanceTimersByTimeAsync(20001);
await assertion;
});

it("respects an explicit GuestConfig.callTimeout override", async () => {
const invokeHostMethod = jest.fn(() => new Promise(() => undefined));
const guest = await connectGuest(invokeHostMethod, { callTimeout: 5000 });

const callPromise = guest.host.someNamespace.someMethod();
const assertion = expect(callPromise).rejects.toThrow(
"timed out after 5000ms"
);

await jest.advanceTimersByTimeAsync(5001);
await assertion;
});

it("succeeds when the host responds comfortably inside the call timeout (control case)", async () => {
const invokeHostMethod = jest.fn(
() => new Promise((resolve) => setTimeout(() => resolve("ok"), 500))
);
const guest = await connectGuest(invokeHostMethod);

const callPromise = guest.host.someNamespace.someMethod();
const assertion = expect(callPromise).resolves.toBe("ok");

await jest.advanceTimersByTimeAsync(501);
await assertion;
});

// The scenario from the investigation: a call that resolves at 12000ms --
// past the old, buggy 10000ms ceiling, but comfortably inside the new
// 20000ms default -- now succeeds instead of spuriously timing out.
it("does not time out a call that resolves at 12000ms, past the old 10000ms ceiling", async () => {
const invokeHostMethod = jest.fn(
() => new Promise((resolve) => setTimeout(() => resolve("ok"), 12000))
);
const guest = await connectGuest(invokeHostMethod);

const callPromise = guest.host.someNamespace.someMethod();
const assertion = expect(callPromise).resolves.toBe("ok");

await jest.advanceTimersByTimeAsync(12001);
await assertion;
});
});
30 changes: 16 additions & 14 deletions packages/uix-guest/src/guest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ export interface GuestConfig {
* Time out and stop trying to reach the host after this many milliseconds
*/
timeout?: number;
/**
* Time to wait for a single call to a host method to resolve before
* rejecting it as timed out, in milliseconds. Defaults to 20000, matching
* {@link @adobe/uix-host#Port}'s default connection timeout -- keep this
* at or above that value (or the host's configured `guestOptions.timeout`,
* if overridden) so a host that is still legitimately establishing or
* settling a batch of connections doesn't cause an in-flight call from an
* already-connected guest to fail with a false timeout.
*/
callTimeout?: number;
}

/**
Expand Down Expand Up @@ -180,6 +190,9 @@ export class Guest<
if (typeof config.timeout === "number") {
this.timeout = config.timeout;
}
if (typeof config.callTimeout === "number") {
this.callTimeout = config.callTimeout;
}
if (config.debug) {
this.logger = debugGuest(this);
}
Expand All @@ -206,7 +219,7 @@ export class Guest<
this.hostConnection.getRemoteApi().invokeHostMethod,
address
),
10000
this.callTimeout
);
return result;
} catch (e) {
Expand Down Expand Up @@ -241,21 +254,10 @@ export class Guest<
invoker: RemoteMethodInvoker<unknown>,
address: HostMethodAddress<unknown[]>
): Promise<any> {
const final = setTimeout(() => {
return new Promise((resolve, reject) =>
reject(`${address} doesn't exist`)
);
}, 20000);
const res = await this.invokeChecker(invoker, address);
return new Promise((resolve) => {
clearTimeout(final);
return resolve(res);
}).catch((e) => {
clearTimeout(final);
return e;
});
return this.invokeChecker(invoker, address);
}
private timeout = 20000;
private callTimeout = 20000;
protected hostConnectionPromise: Promise<CrossRealmObject<HostConnection>>;
protected hostConnection!: CrossRealmObject<HostConnection>;
/** @internal */
Expand Down
Loading
Loading