MCPcopy
hub / github.com/mongodb/node-mongodb-native / cleanup

Function cleanup

test/tools/mongodb-mock/index.js:56–91  ·  view source on GitHub ↗
(spy, callback)

Source from the content-addressed store, hash-verified

54}
55
56function cleanup(spy, callback) {
57 if (typeof spy === 'function') {
58 callback = spy;
59 spy = undefined;
60 }
61
62 if (spy) {
63 const alreadyDrained = spy.connectionCount() === 0;
64 const drainedPromise = !alreadyDrained
65 ? new Promise(resolve => spy.once('drained', () => resolve()))
66 : Promise.resolve();
67
68 const cleanupPromise = Promise.all(mockServers.map(server => server.destroy()))
69 .then(drainedPromise)
70 .then(() => {
71 mockServers = [];
72 })
73 .catch(err => {
74 mockServers = [];
75 throw err;
76 });
77
78 if (typeof callback !== 'function') {
79 return cleanupPromise;
80 }
81
82 return cleanupPromise.then(() => callback(null, null)).catch(err => callback(err, null));
83 } else {
84 const cleanupPromise = Promise.all(mockServers.map(server => server.destroy()));
85 if (typeof callback !== 'function') {
86 return cleanupPromise;
87 }
88
89 return cleanupPromise.then(() => callback(null, null)).catch(err => callback(err, null));
90 }
91}
92
93module.exports = {
94 createServer,

Callers 2

error.test.tsFile · 0.90
collection.test.tsFile · 0.90

Calls 5

onceMethod · 0.80
resolveFunction · 0.50
callbackFunction · 0.50
mapMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected