From 766f4cb8e2dc0cf55099bce23b3956cd95910694 Mon Sep 17 00:00:00 2001 From: williamlardier Date: Thu, 12 Oct 2023 14:12:52 +0200 Subject: [PATCH] S3UTILS-148: update tests and stalled with mongodb breaking changes --- StalledRetry/MongoClientInterfaceStalled.js | 17 ++++++----- ...ucketMetricsAndUpdateBucketCapacityInfo.js | 2 +- tests/functional/countItems.js | 2 +- tests/functional/stalled.js | 2 +- tests/functional/utils/S3UtilsMongoClient.js | 29 +++++++++++++------ utils/S3UtilsMongoClient.js | 5 +--- 6 files changed, 33 insertions(+), 24 deletions(-) diff --git a/StalledRetry/MongoClientInterfaceStalled.js b/StalledRetry/MongoClientInterfaceStalled.js index a10e7fda..e4c1ea48 100644 --- a/StalledRetry/MongoClientInterfaceStalled.js +++ b/StalledRetry/MongoClientInterfaceStalled.js @@ -31,20 +31,20 @@ class MongoClientInterfaceStalled extends MongoClientInterface { }, }, { $project: reducedFields }, - ]); + ]).stream(); } - queueStalledObjects(expiredBy, cb) { + async queueStalledObjects(expiredBy, cb) { const cmpDate = new Date(); cmpDate.setHours(cmpDate.getHours() - expiredBy); const reqHandler = this.requestHandlerFactory(this.logger); let stalledCount = 0; - this.db.listCollections().toArray((err, collections) => { - if (err) { - return cb(err); - } + try { + const collections = await this.db.listCollections().toArray(); + let i = 0; return async.eachSeries(collections, (value, next) => { + i++; if (this._isSpecialCollection(value.name)) { // skip return next(); @@ -56,7 +56,6 @@ class MongoClientInterfaceStalled extends MongoClientInterface { this._getStalledObjectsByBucket(bucketName), this.logger, ); - return reqHandler.handleRequests(wrapper, (err, results) => { if (err) { this.logger.error( @@ -76,7 +75,9 @@ class MongoClientInterfaceStalled extends MongoClientInterface { return next(); }); }, err => cb(err, stalledCount)); - }); + } catch (err) { + return cb(err); + } } } diff --git a/tests/functional/collectBucketMetricsAndUpdateBucketCapacityInfo.js b/tests/functional/collectBucketMetricsAndUpdateBucketCapacityInfo.js index 0e6ea15a..b1def587 100644 --- a/tests/functional/collectBucketMetricsAndUpdateBucketCapacityInfo.js +++ b/tests/functional/collectBucketMetricsAndUpdateBucketCapacityInfo.js @@ -40,7 +40,7 @@ describe('collectBucketMetricsAndUpdateBucketCapacityInfo', () => { afterAll(done => { async.series([ - next => client.db.dropDatabase(next), + next => client.db.dropDatabase().then(() => next()).catch(() => next()), next => client.close(next), ], done); }); diff --git a/tests/functional/countItems.js b/tests/functional/countItems.js index 5e85a607..cbb01726 100644 --- a/tests/functional/countItems.js +++ b/tests/functional/countItems.js @@ -213,7 +213,7 @@ describe('CountItems', () => { afterAll(done => { async.series([ - next => client.db.dropDatabase(next), + next => client.db.dropDatabase().then(() => next()).catch(() => next()), next => client.close(next), ], done); }); diff --git a/tests/functional/stalled.js b/tests/functional/stalled.js index e6ddef67..a9d1b845 100644 --- a/tests/functional/stalled.js +++ b/tests/functional/stalled.js @@ -232,7 +232,7 @@ describe('StalledRetry', () => { afterAll(done => { async.series([ - next => mgoClient.db.dropDatabase(next), + next => mgoClient.db.dropDatabase().then(() => next()).catch(() => next()), next => mgoClient.close(next), ], done); }); diff --git a/tests/functional/utils/S3UtilsMongoClient.js b/tests/functional/utils/S3UtilsMongoClient.js index d060e55d..2cd89f41 100644 --- a/tests/functional/utils/S3UtilsMongoClient.js +++ b/tests/functional/utils/S3UtilsMongoClient.js @@ -834,19 +834,30 @@ describe('S3UtilsMongoClient::getBucketInfos', () => { done(); })); - it('Should ignore buckets in collection list but not in metastore', done => { + it('Should ignore buckets in collection list but not in metastore', async () => { const metastoreCollection = client.getCollection('__metastore'); - return metastoreCollection.deleteOne({ _id: buckets[0]._name }, err => { - assert.equal(err, null); + + // Delete a bucket from the metastore + const deleteResult = await metastoreCollection.deleteOne({ _id: buckets[0]._name }); + assert.equal(deleteResult.deletedCount, 1); + + // Fetch bucket information + const getBucketInfosPromise = new Promise((resolve, reject) => { client.getBucketInfos(logger, (err, data) => { - assert.equal(err, null); - assert.strictEqual(data.bucketCount, 2); - assert.strictEqual(JSON.stringify(data.bucketInfos.find(bucket => bucket._name === buckets[0]._name)), undefined); - assert.strictEqual(JSON.stringify(data.bucketInfos.find(bucket => bucket._name === buckets[1]._name)), JSON.stringify(buckets[1])); - assert.strictEqual(JSON.stringify(data.bucketInfos.find(bucket => bucket._name === buckets[2]._name)), JSON.stringify(buckets[2])); - done(); + if (err) { + return reject(err); + } + return resolve(data); }); }); + + const data = await getBucketInfosPromise; + + // Perform the assertions + assert.strictEqual(data.bucketCount, 2); + assert.strictEqual(JSON.stringify(data.bucketInfos.find(bucket => bucket._name === buckets[0]._name)), undefined); + assert.strictEqual(JSON.stringify(data.bucketInfos.find(bucket => bucket._name === buckets[1]._name)), JSON.stringify(buckets[1])); + assert.strictEqual(JSON.stringify(data.bucketInfos.find(bucket => bucket._name === buckets[2]._name)), JSON.stringify(buckets[2])); }); }); }); diff --git a/utils/S3UtilsMongoClient.js b/utils/S3UtilsMongoClient.js index 49f2a452..4a75d692 100644 --- a/utils/S3UtilsMongoClient.js +++ b/utils/S3UtilsMongoClient.js @@ -499,10 +499,7 @@ class S3UtilsMongoClient extends MongoClientInterface { async readStorageConsumptionMetrics(entityName, log, cb) { try { const i = this.getCollection(INFOSTORE); - const doc = await async.retry( - 3, - async () => i.findOne({ _id: entityName }), - ); + const doc = await i.findOne({ _id: entityName }); if (!doc) { return cb(errors.NoSuchEntity); }