perf: reduce memory usage upon running server tests (#3949)

* perf: reduce memory usage upon running server tests

* perf: plug leaks in server/routes

* perf: plug leaks in server/scripts

* perf: plug leaks in server/policies

* perf: plug leaks in server/models

* perf: plug leaks in server/middlewares

* perf: plug leaks in server/commands

* fix: missing await on db.flush

* perf: plug leaks in server/queues

* chore: remove unused legacy funcs

* fix: await on db.flush

* perf: await on GC to run in between tests

* fix: remove db refs

* fix: revert embeds

* perf: plug leaks in shared/i18n
This commit is contained in:
Apoorv Mishra
2022-08-11 21:39:17 +05:30
committed by GitHub
parent 8e1f42a9cb
commit 0c51bfb899
68 changed files with 463 additions and 178 deletions

View File

@@ -1,12 +1,18 @@
import { Backlink } from "@server/models";
import { buildDocument } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import BacklinksProcessor from "./BacklinksProcessor";
const ip = "127.0.0.1";
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("documents.publish", () => {
test("should create new backlink records", async () => {
@@ -161,7 +167,7 @@ describe("documents.update", () => {
ip,
});
document.text = `First link is gone
[this is a another link](${yetAnotherDocument.url})`;
await document.save();

View File

@@ -5,14 +5,20 @@ import {
buildCollection,
buildUser,
} from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import NotificationsProcessor from "./NotificationsProcessor";
jest.mock("@server/emails/templates/DocumentNotificationEmail");
const ip = "127.0.0.1";
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("documents.publish", () => {
test("should not send a notification to author", async () => {

View File

@@ -1,12 +1,18 @@
import { Revision } from "@server/models";
import { buildDocument } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import RevisionsProcessor from "./RevisionsProcessor";
const ip = "127.0.0.1";
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("documents.update.debounced", () => {
test("should create a revision", async () => {

View File

@@ -1,18 +1,21 @@
import { buildUser, buildWebhookSubscription } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import { UserEvent } from "@server/types";
import DeliverWebhookTask from "../tasks/DeliverWebhookTask";
import WebhookProcessor from "./WebhookProcessor";
jest.mock("@server/queues/tasks/DeliverWebhookTask");
const ip = "127.0.0.1";
beforeEach(() => flushdb());
beforeEach(() => {
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
const ip = "127.0.0.1";
describe("WebhookProcessor", () => {
test("it schedules a delivery for the event", async () => {
const subscription = await buildWebhookSubscription({

View File

@@ -1,10 +1,14 @@
import { subDays } from "date-fns";
import { Document } from "@server/models";
import { buildDocument } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import CleanupDeletedDocumentsTask from "./CleanupDeletedDocumentsTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("CleanupDeletedDocumentsTask", () => {
it("should not destroy documents not deleted", async () => {

View File

@@ -6,10 +6,14 @@ import {
buildWebhookSubscription,
buildViewer,
} from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import CleanupDemotedUserTask from "./CleanupDemotedUserTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("CleanupDemotedUserTask", () => {
it("should delete api keys for suspended user", async () => {

View File

@@ -5,10 +5,14 @@ import {
FileOperationType,
} from "@server/models/FileOperation";
import { buildFileOperation } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import CleanupExpiredFileOperationsTask from "./CleanupExpiredFileOperationsTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("CleanupExpiredFileOperationsTask", () => {
it("should expire exports older than 15 days ago", async () => {

View File

@@ -1,10 +1,14 @@
import { subDays } from "date-fns";
import { WebhookDelivery } from "@server/models";
import { buildWebhookDelivery } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import CleanupWebhookDeliveriesTask from "./CleanupWebhookDeliveriesTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
const deliveryExists = async (delivery: WebhookDelivery) => {
const results = await WebhookDelivery.findOne({ where: { id: delivery.id } });

View File

@@ -6,12 +6,16 @@ import {
buildWebhookDelivery,
buildWebhookSubscription,
} from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import { UserEvent } from "@server/types";
import DeliverWebhookTask from "./DeliverWebhookTask";
beforeEach(() => flushdb());
beforeEach(() => {
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
fetchMock.resetMocks();
fetchMock.doMock();

View File

@@ -2,10 +2,14 @@ import fs from "fs";
import path from "path";
import { FileOperation } from "@server/models";
import { buildFileOperation } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import ImportMarkdownZipTask from "./ImportMarkdownZipTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("ImportMarkdownZipTask", () => {
it("should import the documents, attachments", async () => {

View File

@@ -2,10 +2,14 @@ import fs from "fs";
import path from "path";
import { FileOperation } from "@server/models";
import { buildFileOperation } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import ImportNotionTask from "./ImportNotionTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("ImportNotionTask", () => {
it("should import successfully from a Markdown export", async () => {

View File

@@ -1,10 +1,14 @@
import { subDays } from "date-fns";
import InviteReminderEmail from "@server/emails/templates/InviteReminderEmail";
import { buildInvite } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import InviteReminderTask from "./InviteReminderTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("InviteReminderTask", () => {
it("should not destroy documents not deleted", async () => {