Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: clean up graceful-shutdown process + introduce stop and clearDBs methods in mock server #283

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"express-session": "^1.17.3",
"express-winston": "^4.2.0",
"extract-domain": "^2.4.1",
"http-graceful-shutdown": "^3.1.13",
"isemail": "^3.2.0",
"jsonwebtoken": "^9.0.0",
"lodash": "^4.17.21",
Expand Down
7 changes: 2 additions & 5 deletions packages/api/src/clickhouse/__tests__/clickhouse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import ms from 'ms';

import {
buildMetricSeries,
clearClickhouseTables,
closeDB,
generateBuildTeamEventFn,
getServer,
mockLogsPropertyTypeMappingsModel,
Expand All @@ -22,12 +20,11 @@ describe('clickhouse', () => {
});

afterAll(async () => {
await server.closeHttpServer();
await closeDB();
await server.stop();
});

afterEach(async () => {
await clearClickhouseTables();
await server.clearDBs();
jest.clearAllMocks();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import _ from 'lodash';
import ms from 'ms';

import * as clickhouse from '@/clickhouse';
import {
buildMetricSeries,
clearClickhouseTables,
getServer,
} from '@/fixtures';
import { buildMetricSeries, getServer } from '@/fixtures';

describe('clickhouse - getMultiSeriesChart', () => {
const server = getServer();
Expand All @@ -20,11 +16,11 @@ describe('clickhouse - getMultiSeriesChart', () => {
});

afterAll(async () => {
await server.closeHttpServer();
await server.stop();
});

afterEach(async () => {
await clearClickhouseTables();
await server.clearDBs();
jest.clearAllMocks();
});

Expand Down
23 changes: 15 additions & 8 deletions packages/api/src/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export const initCiEnvs = async () => {
};

class MockServer extends Server {
protected shouldHandleGracefulShutdown = false;

getHttpServer() {
return this.httpServer;
}
Expand All @@ -75,17 +77,28 @@ class MockServer extends Server {
await initCiEnvs();
}

closeHttpServer() {
stop() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should close http server and all db connections

return new Promise<void>((resolve, reject) => {
this.httpServer.close(err => {
if (err) {
reject(err);
return;
}
resolve();
super
.shutdown()
.then(() => resolve())
.catch(err => reject(err));
});
});
}

clearDBs() {
return Promise.all([
clearDBCollections(),
clearClickhouseTables(),
clearRedis(),
]);
}
}

class MockAPIServer extends MockServer {
Expand Down Expand Up @@ -113,10 +126,6 @@ export const getAgent = (server: MockServer) =>
export const getLoggedInAgent = async (server: MockServer) => {
const agent = getAgent(server);

await agent
.post('/register/password')
.send({ ...MOCK_USER, confirmPassword: 'wrong-password' })
.expect(400);
await agent
.post('/register/password')
.send({ ...MOCK_USER, confirmPassword: MOCK_USER.password })
Expand All @@ -129,8 +138,6 @@ export const getLoggedInAgent = async (server: MockServer) => {
throw Error('team or user not found');
}

await user.save();

// login app
await agent.post('/login/password').send(MOCK_USER).expect(302);

Expand Down
20 changes: 4 additions & 16 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { serializeError } from 'serialize-error';

import * as config from './config';
import Server from './server';
import { isOperationalError } from './utils/errors';
import logger from './utils/logger';
import * as config from '@/config';
import Server from '@/server';
import { isOperationalError } from '@/utils/errors';
import logger from '@/utils/logger';

const server = new Server();

Expand All @@ -22,16 +22,4 @@ process.on('unhandledRejection', (err: any) => {
logger.error(serializeError(err));
});

// graceful shutdown
process.on('SIGTERM', () => {
logger.info('SIGTERM signal received.');

if (config.IS_DEV) {
logger.info('Http server is forced to stop immediately.');
process.exit(0);
}

server.stop();
});

server.start().catch(e => logger.error(serializeError(e)));
14 changes: 3 additions & 11 deletions packages/api/src/routers/aggregator/__tests__/root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import _ from 'lodash';

import * as clickhouse from '@/clickhouse';
import { createTeam } from '@/controllers/team';
import {
clearClickhouseTables,
clearDBCollections,
closeDB,
getAgent,
getServer,
} from '@/fixtures';
import { getAgent, getServer } from '@/fixtures';
import { sleep } from '@/utils/common';

describe('aggregator root router', () => {
Expand All @@ -19,13 +13,11 @@ describe('aggregator root router', () => {
});

afterEach(async () => {
await clearDBCollections();
await clearClickhouseTables();
await server.clearDBs();
});

afterAll(async () => {
await server.closeHttpServer();
await closeDB();
await server.stop();
});

it('GET /health', async () => {
Expand Down
14 changes: 3 additions & 11 deletions packages/api/src/routers/api/__tests__/alerts.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
clearDBCollections,
closeDB,
getLoggedInAgent,
getServer,
makeAlert,
makeChart,
} from '@/fixtures';
import { getLoggedInAgent, getServer, makeAlert, makeChart } from '@/fixtures';

const MOCK_DASHBOARD = {
name: 'Test Dashboard',
Expand All @@ -21,12 +14,11 @@ describe('alerts router', () => {
});

afterEach(async () => {
await clearDBCollections();
await server.clearDBs();
});

afterAll(async () => {
await server.closeHttpServer();
await closeDB();
await server.stop();
});

it('has alerts attached to dashboards', async () => {
Expand Down
11 changes: 2 additions & 9 deletions packages/api/src/routers/api/__tests__/chart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import ms from 'ms';

import * as clickhouse from '@/clickhouse';
import {
clearClickhouseTables,
clearDBCollections,
clearRedis,
closeDB,
generateBuildTeamEventFn,
getLoggedInAgent,
getServer,
Expand All @@ -20,14 +16,11 @@ describe('charts router', () => {
});

afterEach(async () => {
await clearDBCollections();
await clearClickhouseTables();
await clearRedis();
await server.clearDBs();
});

afterAll(async () => {
await server.closeHttpServer();
await closeDB();
await server.stop();
});

it('GET /chart/services', async () => {
Expand Down
14 changes: 3 additions & 11 deletions packages/api/src/routers/api/__tests__/dashboard.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
clearDBCollections,
closeDB,
getLoggedInAgent,
getServer,
makeAlert,
makeChart,
} from '@/fixtures';
import { getLoggedInAgent, getServer, makeAlert, makeChart } from '@/fixtures';

const MOCK_DASHBOARD = {
name: 'Test Dashboard',
Expand All @@ -21,12 +14,11 @@ describe('dashboard router', () => {
});

afterEach(async () => {
await clearDBCollections();
await server.clearDBs();
});

afterAll(async () => {
await server.closeHttpServer();
await closeDB();
await server.stop();
});

it('deletes attached alerts when deleting charts', async () => {
Expand Down
17 changes: 3 additions & 14 deletions packages/api/src/routers/api/__tests__/metrics.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import * as clickhouse from '@/clickhouse';
import {
buildMetricSeries,
clearClickhouseTables,
clearDBCollections,
clearRedis,
closeDB,
getLoggedInAgent,
getServer,
} from '@/fixtures';
import { buildMetricSeries, getLoggedInAgent, getServer } from '@/fixtures';

describe('metrics router', () => {
const server = getServer();
Expand All @@ -17,14 +9,11 @@ describe('metrics router', () => {
});

afterEach(async () => {
await clearDBCollections();
await clearClickhouseTables();
await clearRedis();
await server.clearDBs();
});

afterAll(async () => {
await server.closeHttpServer();
await closeDB();
await server.stop();
});

it('GET /metrics/tags', async () => {
Expand Down
12 changes: 3 additions & 9 deletions packages/api/src/routers/api/__tests__/team.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import _ from 'lodash';

import {
clearDBCollections,
closeDB,
getLoggedInAgent,
getServer,
} from '@/fixtures';
import { getLoggedInAgent, getServer } from '@/fixtures';

describe('team router', () => {
const server = getServer();
Expand All @@ -15,12 +10,11 @@ describe('team router', () => {
});

afterEach(async () => {
await clearDBCollections();
await server.clearDBs();
});

afterAll(async () => {
await server.closeHttpServer();
await closeDB();
await server.stop();
});

it('GET /team', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import _ from 'lodash';

import {
clearDBCollections,
closeDB,
getLoggedInAgent,
getServer,
makeChart,
Expand All @@ -29,12 +27,11 @@ describe('/api/v1/alerts', () => {
});

afterEach(async () => {
await clearDBCollections();
await server.clearDBs();
});

afterAll(async () => {
await server.closeHttpServer();
await closeDB();
await server.stop();
});

it('CRUD Dashboard Alerts', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import ms from 'ms';

import * as clickhouse from '@/clickhouse';
import {
clearDBCollections,
closeDB,
generateBuildTeamEventFn,
getLoggedInAgent,
getServer,
Expand All @@ -19,12 +17,11 @@ describe('/api/v1/charts/series', () => {
});

afterEach(async () => {
await clearDBCollections();
await server.clearDBs();
});

afterAll(async () => {
await server.closeHttpServer();
await closeDB();
await server.stop();
});

const now = new Date('2022-01-05').getTime();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import _ from 'lodash';

import {
clearDBCollections,
closeDB,
getLoggedInAgent,
getServer,
makeExternalAlert,
Expand Down Expand Up @@ -37,12 +35,11 @@ describe('dashboard router', () => {
});

afterEach(async () => {
await clearDBCollections();
await server.clearDBs();
});

afterAll(async () => {
await server.closeHttpServer();
await closeDB();
await server.stop();
});

it('CRUD /dashboards', async () => {
Expand Down