From b203f483363a59009b1780f9337bcadc56fc947e Mon Sep 17 00:00:00 2001 From: Taylor McKinnon Date: Wed, 9 Oct 2024 10:45:29 -0700 Subject: [PATCH] stash --- tests/functional/cron/testReindex.js | 137 +++++++++++++++++++++------ 1 file changed, 110 insertions(+), 27 deletions(-) diff --git a/tests/functional/cron/testReindex.js b/tests/functional/cron/testReindex.js index efbec689..e6ee66da 100644 --- a/tests/functional/cron/testReindex.js +++ b/tests/functional/cron/testReindex.js @@ -183,41 +183,124 @@ describe('UtapiReindex', () => { }); }); - describe('::_scheduleJob', function test() { + function waitUntilLockHasValue({ value, job }, cb) { + let shouldLeave; + let shouldCallJob = job !== undefined; + + async.doUntil( + next => + redis.get(REINDEX_LOCK_KEY, (err, res) => { + if (err) { + return next(err); + } + if (shouldCallJob) { + job(); + shouldCallJob = false; + } + shouldLeave = res === value; + return setTimeout(next, 200); + }), + next => next(null, shouldLeave), + cb, + ); + } + + function checkMetrics({ resource, expected }, cb) { + utils.listMetrics(resource, (err, res) => { + if (err) { + return cb(err); + } + if (res.code) { + return cb(new Error(res.message)); + } + const { storageUtilized, numberOfObjects } = expected; + assert.deepStrictEqual(res[0].storageUtilized, storageUtilized); + assert.deepStrictEqual(res[0].numberOfObjects, numberOfObjects); + return cb(); + }); + } + + describe('multiple runs', function test() { this.timeout(30000); - function waitUntilLockHasValue({ value, job }, cb) { - let shouldLeave; - let shouldCallJob = job !== undefined; + const bucket = `${mock.values.BUCKET_NAME}-multi-run`; + + beforeEach(() => { + bucketD + .setBucketContent({ + bucketName: bucket, + contentLength: 1024, + }) + .setBucketContent({ + bucketName: MPUBucket, + contentLength: 1024, + }) + .setBucketCount(count) + .createBuckets(); + }); - async.doUntil(next => redis.get(REINDEX_LOCK_KEY, (err, res) => { - if (err) { - return next(err); - } - if (shouldCallJob) { - job(); - shouldCallJob = false; - } - shouldLeave = res === value; - return setTimeout(next, 200); - }), - next => next(null, shouldLeave), cb); - } - - function checkMetrics({ resource, expected }, cb) { - utils.listMetrics(resource, (err, res) => { + afterEach(() => { + bucketD.clearBuckets(); + }); + + it('should not remove metrics for the previous interval if they have not changed', done => { + function job() { + reindex._scheduleJob(); + } + + // Wait until the scripts have finished reindexing. + async.series( + [ + next => waitUntilLockHasValue({ value: 'true', job }, next), + next => waitUntilLockHasValue({ value: null }, next), + ], + done, + ); + + utils.listMetrics(bucket, (err, res) => { if (err) { - return cb(err); + return done(err); } + if (res.code) { - return cb(new Error(res.message)); + return done(new Error(res.message)); } - const { storageUtilized, numberOfObjects } = expected; - assert.deepStrictEqual(res[0].storageUtilized, storageUtilized); - assert.deepStrictEqual(res[0].numberOfObjects, numberOfObjects); - return cb(); + const { storageUtilized, numberOfObjects } = res[0]; + assert.deepStrictEqual(storageUtilized, [0, 1024]); + assert.deepStrictEqual(numberOfObjects, [0, 1]); + + // Run the job again. + job(); + + // Wait until the scripts have finished reindexing. + async.series( + [ + next => + waitUntilLockHasValue({ value: 'true', job }, next), + next => + waitUntilLockHasValue({ value: null }, next), + ], + done, + ); + + utils.listMetrics(bucket, (err, res) => { + if (err) { + return done(err); + } + if (res.code) { + return done(new Error(res.message)); + } + const { storageUtilized, numberOfObjects } = res[0]; + assert.deepStrictEqual(storageUtilized, [0, 1024]); + assert.deepStrictEqual(numberOfObjects, [0, 1]); + return done(); + }); }); - } + }); + }); + + describe('::_scheduleJob', function test() { + this.timeout(30000); const bucketCounts = [1, 1001];