-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller.test.ts
More file actions
71 lines (56 loc) · 2.21 KB
/
Copy pathcontroller.test.ts
File metadata and controls
71 lines (56 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import * as mongodb from 'mongodb';
import { DatabaseController } from './controller';
import { DatabaseConnectionError } from '../workerErrors';
import '../../env-test';
/**
* Test for the Database Controller module
*/
describe('Database Controller Test', () => {
const db = new DatabaseController(process.env.MONGO_EVENTS_DATABASE_URI);
describe('event', () => {
beforeAll(async () => {
await db.connect();
});
afterAll(async () => {
await db.close();
});
it('should return connection instance', async () => {
const connection = db.getConnection();
const result = connection instanceof mongodb.Db;
expect(result).toBe(true);
});
});
describe('initial handshake retry', () => {
const mongoModule = jest.requireActual('mongodb');
let connectSpy: jest.SpyInstance;
beforeEach(() => {
process.env.MONGO_RECONNECT_TRIES = '5';
process.env.MONGO_RECONNECT_INTERVAL = '1';
jest.spyOn(console, 'warn').mockImplementation(() => undefined);
connectSpy = jest.spyOn(mongoModule, 'connect');
});
afterEach(() => {
delete process.env.MONGO_RECONNECT_TRIES;
delete process.env.MONGO_RECONNECT_INTERVAL;
jest.restoreAllMocks();
});
it('retries the initial connection until it succeeds', async () => {
const fakeDb = {} as mongodb.Db;
const fakeClient = { db: jest.fn().mockReturnValue(fakeDb) } as unknown as mongodb.MongoClient;
connectSpy
.mockRejectedValueOnce(new Error('unreachable'))
.mockRejectedValueOnce(new Error('unreachable'))
.mockResolvedValueOnce(fakeClient);
const controller = new DatabaseController('mongodb://localhost:27017/test');
const result = await controller.connect();
expect(connectSpy).toHaveBeenCalledTimes(3);
expect(result).toBe(fakeDb);
});
it('throws DatabaseConnectionError after exhausting all retries', async () => {
connectSpy.mockRejectedValue(new Error('unreachable'));
const controller = new DatabaseController('mongodb://localhost:27017/test');
await expect(controller.connect()).rejects.toBeInstanceOf(DatabaseConnectionError);
expect(connectSpy).toHaveBeenCalledTimes(5);
});
});
});