Skip to content

Commit

Permalink
api: Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
victorges committed Apr 26, 2024
1 parent 5b65876 commit fb9aa19
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import serverPromise from "../test-server";
import { TestClient, clearDatabase } from "../test-helpers";
import { sleep } from "../util";
import { jobsDb } from "../store";
import { Webhook } from "../schema/types";

let server;
let mockAdminUser;
Expand Down Expand Up @@ -288,8 +290,22 @@ describe("controllers/webhook", () => {
});
expect(setActiveRes).toBeDefined();
expect(setActiveRes.status).toBe(204);
// const setActiveResJson = await setActiveRes.json()
// expect(setActiveResJson).toBeDefined()

let hooksCalled = 0;
for (let i = 0; i < 5 && hooksCalled < 2; i++) {
await sleep(500);

hooksCalled = 0;
for (const id of [webhookResJson.id, webhookResJson2.id]) {
const res = await client.get(`/webhook/${id}`);
expect(res.status).toBe(200);
const { status } = (await res.json()) as Webhook;
if (status.lastTriggeredAt >= now) {
hooksCalled++;
}
}
}
expect(hooksCalled).toBe(2);
}, 20000);

it("records webhook logs", async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export default async function makeApp(params: CliArgs) {
process.off("unhandledRejection", unhandledRejection);
listener?.close();
await tracking.flushAll();
await store.close();
await db.close();
await jobsDb.close();
};

// Handle SIGTERM gracefully. It's polite, and Kubernetes likes it.
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/store/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,6 @@ async function ensureDatabase(postgresUrl: string) {
pool.end();
adminPool.end();
}

export const db = new DB();
export const jobsDb = new DB();
2 changes: 1 addition & 1 deletion packages/api/src/store/experiment-table.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sql from "sql-template-strings";

import { Experiment } from "../schema/types";
import { db } from ".";
import { db } from "./db";
import { ForbiddenError, NotFoundError } from "./errors";
import Table from "./table";
import { WithID } from "./types";
Expand Down
5 changes: 2 additions & 3 deletions packages/api/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Model from "./model";
import { DB, PostgresParams } from "./db";
import { db, jobsDb, DB, PostgresParams } from "./db";

export const db = new DB();
export const jobsDb = new DB();
export { db, jobsDb };

// Helper function to start database and boot up legacy store
export default async function makeStore(
Expand Down
4 changes: 0 additions & 4 deletions packages/api/src/store/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,4 @@ export default class Model implements IStore {

return responses;
}

async close() {
return this.db.close();
}
}
1 change: 0 additions & 1 deletion packages/api/src/types/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ export interface IStore {
ready: Promise<void>;

get<T extends StoredObject>(id: string, cleanWriteOnly?: boolean): Promise<T>;
close(): Promise<void>;
replace(data: StoredObject): Promise<void>;
list<T = StoredObject>(
args: IStoreListArgs
Expand Down
7 changes: 3 additions & 4 deletions packages/api/src/webhooks/cannon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,7 @@ describe("webhook cannon", () => {
expect(callCount).toBe(1);
expect(receivedEvent).toBe("stream.started");

// at this point the semaphore is re-acquired cause we just waited (acquired) above

sem = semaphore();
await server.queue.publishWebhook("events.stream.idle", {
type: "webhook_event",
id: "webhook_test_42",
Expand All @@ -302,6 +301,7 @@ describe("webhook cannon", () => {
expect(receivedEvent).toBe("stream.idle");

// does not receive some random event
sem = semaphore();
await server.queue.publishWebhook("events.stream.unknown" as any, {
type: "webhook_event",
id: "webhook_test_93",
Expand All @@ -311,8 +311,7 @@ describe("webhook cannon", () => {
userId: nonAdminUser.id,
});

const err = await sem.wait(1000).catch((err) => err);
expect(err?.message).toBe("timeout");
await sem.wait(1000);
expect(callCount).toBe(2);
});

Expand Down

0 comments on commit fb9aa19

Please sign in to comment.