/
connection-config-provider.js
71 lines (61 loc) · 2.25 KB
/
connection-config-provider.js
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
/*global expect*/
'use strict';
const _ = require('lodash');
const makeKnex = require('../../knex');
module.exports = function(config) {
describe('Connection configuration provider', function() {
let configWorkingCopy;
let providerInvocationCount;
let connectionConfigWorkingCopy;
this.beforeEach(() => {
configWorkingCopy = _.cloneDeep(config);
configWorkingCopy.pool.min = 1;
configWorkingCopy.pool.max = 2;
providerInvocationCount = 0;
connectionConfigWorkingCopy = configWorkingCopy.connection;
});
it('is not used when configuration is static', async function() {
return runTwoConcurrentTransactions(0);
});
it('can return a promise for a config object, which is reused when not given given an expiry checker', async () => {
configWorkingCopy.connection = () => {
++providerInvocationCount;
return Promise.resolve(connectionConfigWorkingCopy);
};
return runTwoConcurrentTransactions(1);
});
it('can return a config object, which is reused when not given given an expiry checker', async () => {
configWorkingCopy.connection = () => {
++providerInvocationCount;
return connectionConfigWorkingCopy;
};
return runTwoConcurrentTransactions(1);
});
it('reuses the same resolved config when not yet expired', async () => {
configWorkingCopy.connection = () => {
++providerInvocationCount;
return Object.assign(connectionConfigWorkingCopy, {
expirationChecker: () => false,
});
};
return runTwoConcurrentTransactions(1);
});
it('replaces the resolved config when expired', async () => {
configWorkingCopy.connection = () => {
++providerInvocationCount;
return Object.assign(connectionConfigWorkingCopy, {
expirationChecker: () => true,
});
};
return runTwoConcurrentTransactions(2);
});
async function runTwoConcurrentTransactions(expectedInvocationCount) {
const knex = makeKnex(configWorkingCopy);
await knex.transaction(async (trx) => {
await knex.transaction(async (trx2) => {});
});
await knex.destroy();
expect(providerInvocationCount).equals(expectedInvocationCount);
}
});
};