Skip to content

Commit 4ddfd2b

Browse files
committed
Trying to improve mongodb cleanup so circleci closes
1 parent 660a13f commit 4ddfd2b

File tree

7 files changed

+71
-41
lines changed

7 files changed

+71
-41
lines changed

.mocharc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
'reporter': 'mocha-multi',
5+
'reporter-option': ['spec=-,xunit=xunit/test-results.xml'],
6+
'require': './test/fixtures.js',
7+
'timeout': '10000',
8+
};

docker-compose.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ services:
66
image: mongo
77
expose:
88
- 27017
9-
logging:
10-
driver: "none"
119

1210
rsscloud:
1311
build: .
@@ -21,8 +19,6 @@ services:
2119
- 5337
2220
depends_on:
2321
- mongodb
24-
logging:
25-
driver: "none"
2622

2723
rsscloud-tests:
2824
build: .

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"import-data": "node ./bin/import-data.js",
1010
"jshint": "jshint ./**/*.js",
1111
"eslint": "eslint --fix controllers/ services/ test/",
12-
"test": "mocha -R mocha-multi --reporter-options spec=-,xunit=xunit/test-results.xml --timeout 10000",
12+
"test": "mocha",
1313
"test-api": "docker-compose up --build --abort-on-container-exit"
1414
},
1515
"engines": {

services/mongodb.js

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,51 @@
44
const { MongoClient } = require('mongodb'),
55
state = {};
66

7-
module.exports = {
8-
connect: async function (name, uri) {
9-
if (state[name]) {
10-
return;
11-
}
12-
13-
const client = await MongoClient.connect(uri, { useUnifiedTopology: true });
14-
15-
state[name] = client;
16-
17-
// console.log(`${name} Database Connected`);
18-
19-
return state[name].db();
20-
},
21-
get: function (name) {
22-
return state[name].db();
23-
},
24-
close: async function (name) {
25-
if (state[name]) {
26-
return state[name].close()
27-
.then(() => {
28-
delete state[name];
29-
});
30-
}
7+
async function connect(name, uri) {
8+
if (state[name]) {
9+
return;
3110
}
11+
12+
const client = await MongoClient.connect(uri, { useUnifiedTopology: true });
13+
14+
state[name] = client;
15+
16+
// console.log(`${name} Database Connected`);
17+
18+
return state[name].db();
19+
}
20+
21+
function get(name) {
22+
return state[name].db();
23+
}
24+
25+
async function close(name) {
26+
if (state[name]) {
27+
return state[name].close()
28+
.finally(() => {
29+
delete state[name];
30+
});
31+
}
32+
}
33+
34+
async function closeAll() {
35+
await Promise.all(Object.keys(state).map(name => close));
36+
}
37+
38+
function cleanup() {
39+
closeAll()
40+
.finally(() => {
41+
process.exit();
42+
});
43+
}
44+
45+
process.on('SIGINT', cleanup);
46+
process.on('SIGTERM', cleanup);
47+
48+
module.exports = {
49+
connect,
50+
get,
51+
close,
52+
closeAll
3253
};
3354
}());

test/fixtures.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const mongodb = require("./mongodb");
2+
3+
exports.mochaGlobalSetup = async function () {
4+
await mongodb.before();
5+
};
6+
7+
exports.mochaGlobalTeardown = async function () {
8+
await mongodb.after();
9+
};

test/mongodb.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ async function upsertSubscriptions(subscriptions) {
2626
module.exports = {
2727
addSubscription: async function (resourceUrl, notifyProcedure, apiurl, protocol) {
2828
const subscriptions = await fetchSubscriptions(resourceUrl);
29+
2930
initSubscription(subscriptions, notifyProcedure, apiurl, protocol);
3031
await upsertSubscriptions(subscriptions);
3132

32-
const index = subscriptions.pleaseNotify.findIndex(subscription => {
33+
let index = subscriptions.pleaseNotify.findIndex(subscription => {
3334
return subscription.url === apiurl;
3435
});
3536

@@ -40,25 +41,22 @@ module.exports = {
4041
throw Error(`Cannot find ${apiurl} subscription`);
4142
},
4243
updateSubscription: async function (resourceUrl, subscription) {
43-
const subscriptions = await fetchSubscriptions(resourceUrl);
44-
const index = subscriptions.pleaseNotify.findIndex(match => {
45-
return subscription.url === match.url;
46-
});
44+
const subscriptions = await fetchSubscriptions(resourceUrl),
45+
index = subscriptions.pleaseNotify.findIndex(match => {
46+
return subscription.url === match.url;
47+
});
4748

4849
if (-1 !== index) {
4950
subscriptions.pleaseNotify[index] = subscription;
5051
await upsertSubscriptions(subscriptions);
5152
return subscriptions.pleaseNotify[index];
5253
}
5354

54-
throw Error(`Cannot find ${apiurl} subscription`);
55+
throw Error(`Cannot find ${subscription.url} subscription`);
5556
},
5657
before: async function () {
57-
const db = await mongodb.connect('rsscloud', config.mongodbUri);
58-
58+
await mongodb.connect('rsscloud', config.mongodbUri);
5959
console.log(` → MongoDB 'rsscloud' Database Connected`);
60-
61-
return db;
6260
},
6361
after: async function () {
6462
return mongodb.close('rsscloud');

test/ping.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,10 @@ for (const pingProtocol of ['XML-RPC', 'REST']) {
5757

5858
describe(`Ping ${pingProtocol} to ${protocol} returning ${returnFormat}`, function () {
5959
before(async function () {
60-
await mongodb.before();
6160
await mock.before();
6261
});
6362

6463
after(async function () {
65-
await mongodb.after();
6664
await mock.after();
6765
});
6866

0 commit comments

Comments
 (0)