mirror of
https://github.com/outline/outline.git
synced 2026-11-26 23:50:14 +03:00
chore: Replace fixed sleeps and raised timeouts with signals (#14624)
This commit is contained in:
1 parent
3ec7837140
commit
4fca2ebc32
8 files changed
+46
-21
No files matched your search
@@ -27,8 +27,14 @@ describe("CoalescedFetch", () => {
|
||||
it("should run one more request after an in-flight request when forced", async () => {
|
||||
const queue = new CoalescedFetch();
|
||||
const requests = [deferred(), deferred()];
|
||||
const secondCall = deferred();
|
||||
let calls = 0;
|
||||
const fetch = vi.fn(() => requests[calls++].promise);
|
||||
const fetch = vi.fn(() => {
|
||||
if (calls === 1) {
|
||||
secondCall.resolve();
|
||||
}
|
||||
return requests[calls++].promise;
|
||||
});
|
||||
|
||||
const first = queue.run(fetch);
|
||||
const forced = queue.run(fetch, true);
|
||||
@@ -37,7 +43,8 @@ describe("CoalescedFetch", () => {
|
||||
|
||||
requests[0].resolve();
|
||||
await first;
|
||||
await vi.waitFor(() => expect(fetch).toHaveBeenCalledTimes(2));
|
||||
await secondCall.promise;
|
||||
expect(fetch).toHaveBeenCalledTimes(2);
|
||||
|
||||
let settled = false;
|
||||
void forced.then(() => {
|
||||
|
||||
@@ -8,6 +8,11 @@ import RootStore from "~/stores/RootStore";
|
||||
import StorePersistence from "./StorePersistence";
|
||||
|
||||
describe("StorePersistence", () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("round-trips models through IndexedDB into a fresh store", async () => {
|
||||
const teamId = "team-1";
|
||||
const source = new RootStore();
|
||||
@@ -125,13 +130,14 @@ describe("StorePersistence", () => {
|
||||
persistence.persist("doc-1");
|
||||
await persistence.flush();
|
||||
|
||||
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
||||
const target = new RootStore();
|
||||
await target.policies.enablePersistence(teamId);
|
||||
|
||||
// Drop the model without notifying persistence, so that any flush scheduled
|
||||
// during hydration would delete the persisted record when it fires.
|
||||
target.policies.data.delete("doc-1");
|
||||
await new Promise((resolve) => setTimeout(resolve, 1100));
|
||||
await vi.runAllTimersAsync();
|
||||
|
||||
const other = new RootStore();
|
||||
const otherPersistence = new StorePersistence(other.policies, teamId);
|
||||
@@ -173,6 +179,7 @@ describe("StorePersistence", () => {
|
||||
});
|
||||
|
||||
it("does not recreate the database from a pending flush once disabled", async () => {
|
||||
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
||||
const teamId = "team-10";
|
||||
const source = new RootStore();
|
||||
const persistence = new StorePersistence(source.policies, teamId);
|
||||
@@ -181,12 +188,16 @@ describe("StorePersistence", () => {
|
||||
teamId
|
||||
);
|
||||
|
||||
const open = vi.spyOn(indexedDB, "open");
|
||||
|
||||
source.policies.add({ id: "doc-1", abilities: { read: true } });
|
||||
persistence.persist("doc-1");
|
||||
|
||||
// Disabling while a debounced write is still scheduled, as logout does.
|
||||
persistence.disable();
|
||||
await new Promise((resolve) => setTimeout(resolve, 1100));
|
||||
await vi.runAllTimersAsync();
|
||||
|
||||
expect(open).not.toHaveBeenCalled();
|
||||
|
||||
const databases = await indexedDB.databases();
|
||||
expect(databases.map((database) => database.name)).not.toContain(
|
||||
|
||||
@@ -79,7 +79,9 @@ describe("ModelContext", () => {
|
||||
expect(registerModelContextTool(makeTool("five"), first.signal)).toBe(
|
||||
true
|
||||
);
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
await expect(registerTool.mock.results[0].value).rejects.toThrow(
|
||||
"rejected"
|
||||
);
|
||||
|
||||
const second = new AbortController();
|
||||
expect(registerModelContextTool(makeTool("five"), second.signal)).toBe(
|
||||
@@ -103,7 +105,9 @@ describe("ModelContext", () => {
|
||||
true
|
||||
);
|
||||
controller.abort();
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
await expect(registerTool.mock.results[0].value).rejects.toThrow(
|
||||
"signal is aborted"
|
||||
);
|
||||
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
warn.mockRestore();
|
||||
|
||||
@@ -21,9 +21,6 @@ import { randomUUID } from "node:crypto";
|
||||
|
||||
const server = getTestServer();
|
||||
|
||||
// Increase timeout for all tests in this file
|
||||
vi.setConfig({ testTimeout: 10000 });
|
||||
|
||||
describe("#files.create", () => {
|
||||
it("should fail with status 400 bad request if key is invalid", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
@@ -171,13 +171,13 @@ describe("View", () => {
|
||||
});
|
||||
const firstViewedAt = view1.updatedAt;
|
||||
|
||||
// Wait a bit to ensure time difference
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
vi.useFakeTimers({ toFake: ["Date"] });
|
||||
vi.setSystemTime(firstViewedAt.getTime() + 1000);
|
||||
|
||||
const view2 = await View.incrementOrCreate(createContext({ user }), {
|
||||
documentId: document.id,
|
||||
userId: user.id,
|
||||
});
|
||||
}).finally(() => vi.useRealTimers());
|
||||
|
||||
expect(view2.updatedAt.getTime()).toBeGreaterThan(
|
||||
firstViewedAt.getTime()
|
||||
|
||||
@@ -16,8 +16,6 @@ const props = {
|
||||
},
|
||||
};
|
||||
|
||||
vi.setConfig({ testTimeout: 30000 });
|
||||
|
||||
const daysAgo = (n: number) => subDays(new Date(), n);
|
||||
const dayStr = (d: Date) => format(d, "yyyy-MM-dd");
|
||||
|
||||
|
||||
@@ -15,8 +15,6 @@ const props = {
|
||||
const daysAgo = (n: number) => subDays(new Date(), n);
|
||||
const dayStr = (d: Date) => format(d, "yyyy-MM-dd");
|
||||
|
||||
vi.setConfig({ testTimeout: 30000 });
|
||||
|
||||
describe("RollupWeeklyDocumentInsightsTask", () => {
|
||||
let task: RollupWeeklyDocumentInsightsTask;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import dns from "node:dns";
|
||||
import http from "node:http";
|
||||
import { HttpProxyAgent } from "http-proxy-agent";
|
||||
import type { MockInstance } from "vitest";
|
||||
import env from "@server/env";
|
||||
import { server as msw } from "@server/test/msw";
|
||||
@@ -159,16 +160,25 @@ describe("fetch", () => {
|
||||
});
|
||||
|
||||
it("should not connect to the proxy after a timeout", async () => {
|
||||
let resolveLookup: (addresses: dns.LookupAddress[]) => void = () => {};
|
||||
const lookupSpy: MockInstance = vi.spyOn(dns.promises, "lookup");
|
||||
lookupSpy.mockImplementation(async () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 200));
|
||||
return [{ address: "192.0.2.1", family: 4 }];
|
||||
});
|
||||
lookupSpy.mockReturnValue(
|
||||
new Promise((resolve) => {
|
||||
resolveLookup = resolve;
|
||||
})
|
||||
);
|
||||
const connectSpy = vi.spyOn(HttpProxyAgent.prototype, "connect");
|
||||
|
||||
await expect(
|
||||
fetch("http://slow.example/", { timeout: 50 })
|
||||
).rejects.toThrow("Request timeout after 50ms");
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
expect(lookupSpy).toHaveBeenCalledTimes(1);
|
||||
|
||||
resolveLookup([{ address: "192.0.2.1", family: 4 }]);
|
||||
|
||||
// The connect path after the lookup only runs microtasks.
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
expect(connectSpy).not.toHaveBeenCalled();
|
||||
expect(proxy.connections).toEqual(0);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user