Files
lancedb/nodejs/__test__/job.test.ts
T
c90382e9c4 fix(node): handle rejected job progress callbacks (#5272)
Fixes #5271

## Summary

A promise returned from a job progress callback could reject outside the
SDK's synchronous error handler. This calls the observer synchronously
as before and attaches a rejection handler without awaiting it.

## Validation

- The regression test holds the observer promise open until `Job.wait`
completes, then rejects it and verifies the warning.
- All 4 tests in `nodejs/__test__/job.test.ts` pass using the native job
boundary stub and the published matching beta native declarations.
- A separate actual Node run with `--unhandled-rejections=strict` exits
at the observer error on main. It logs the error and continues with the
patch.
- Biome lint and format checks passed on the two changed files. `git
diff --check` passed.

---------

Co-authored-by: Divyam Talwar <237515611+DivyamTalwar@users.noreply.github.com>
Co-authored-by: Divyam Talwar <DivyamTalwar@users.noreply.github.com>
2026-11-30 21:46:02 +08:00

79 lines
3.0 KiB
TypeScript

// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
import { Job } from "../lancedb/job";
import type { Job as NativeJob } from "../lancedb/native";
describe("Job attempt contract", () => {
test("preserves lineage and unknown detailed states", async () => {
const inner = {
jobId: "attempt-2",
operationId: "attempt-1",
attempt: 2,
retryOf: "attempt-1",
lifecycleState: "future_publication_state",
sourceTableVersion: 7,
executionEpoch: 3,
status: jest.fn().mockResolvedValue("future_publication_state"),
} as unknown as NativeJob;
const job = new Job(inner);
expect(job.jobId).toBe("attempt-2");
expect(job.operationId).toBe("attempt-1");
expect(job.attempt).toBe(2);
expect(job.retryOf).toBe("attempt-1");
expect(await job.statusTyped()).toBe("future_publication_state");
expect(job.sourceTableVersion).toBe(7);
expect(job.executionEpoch).toBe(3);
});
test("passes wait deadlines through without cancelling execution", async () => {
const wait = jest.fn().mockRejectedValue(new Error("Timeout error"));
const cancel = jest.fn();
const job = new Job({ wait, cancel } as unknown as NativeJob);
await expect(job.wait({ timeoutMs: 1 })).rejects.toThrow("Timeout");
expect(wait).toHaveBeenCalledWith(undefined, 1);
expect(cancel).not.toHaveBeenCalled();
});
test("logs rejected progress observers without waiting for them", async () => {
const error = new Error("observer failed");
let reject!: (reason: Error) => void;
const pending = new Promise<void>((_resolve, rejectPromise) => {
reject = rejectPromise;
});
// Keep Jest running on the old implementation so the missing warning
// is the regression assertion, not an unrelated unhandled rejection.
pending.catch(() => undefined);
const onProgress = jest.fn().mockReturnValue(pending);
const wait = jest.fn(async (observe: (progress: object) => void) => {
observe({ state: "in_progress" });
});
const job = new Job({ wait } as unknown as NativeJob);
const warn = jest
.spyOn(console, "warn")
.mockImplementation(() => undefined);
try {
await expect(job.wait({ onProgress })).resolves.toBeUndefined();
expect(onProgress).toHaveBeenCalledWith({ state: "in_progress" });
reject(error);
await new Promise<void>((resolve) => setImmediate(resolve));
expect(warn).toHaveBeenCalledWith("Job progress callback threw:", error);
} finally {
warn.mockRestore();
}
});
test("retry retains the typed result decoder", async () => {
const successor = {
waitResult: jest.fn().mockResolvedValue({ value: 7 }),
} as unknown as NativeJob;
const inner = {
retry: jest.fn().mockResolvedValue(successor),
} as unknown as NativeJob;
const job = new Job(inner, (value) => (value as { value: number }).value);
const retry = await job.retry();
expect(await retry.wait()).toBe(7);
});
});