Skip to content

Commit

Permalink
test: replace await loop with Promise.all (#810)
Browse files Browse the repository at this point in the history
Co-authored-by: 饶培泽 <peizerao@gmail.com>
  • Loading branch information
weiyie and PeterRao authored Jun 8, 2020
1 parent ef1e44a commit ae22ad1
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 161 deletions.
24 changes: 10 additions & 14 deletions test/browser/browser-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ if (process && process.browser) {
exports.cleanBucket = async function (store, bucket, multiversion) {
store.useBucket(bucket);
let result;
const subres = {};
const options = { subres };
const options = { versionId: null };

if (!multiversion) {
try {
Expand All @@ -38,13 +37,11 @@ exports.cleanBucket = async function (store, bucket, multiversion) {
});
}
result[deleteKey] = result[deleteKey] || [];
for (let i = 0; i < result[deleteKey].length; i++) {
const obj = result[deleteKey][i];
if (multiversion) {
subres.versionId = obj.versionId;
}
await store.delete(obj.name, options);
}

await Promise.all(result[deleteKey].map(_ =>
store.delete(_.name, multiversion ?
Object.assign({}, options, { versionId: _.versionId }) :
options)));
}
await handleDelete('objects');
if (multiversion) {
Expand All @@ -55,11 +52,10 @@ exports.cleanBucket = async function (store, bucket, multiversion) {
'max-uploads': 1000
});
const uploads = result.uploads || [];
/* eslint no-await-in-loop: [0] */
for (let i = 0; i < uploads.length; i++) {
const up = uploads[i];
await store.abortMultipartUpload(up.name, up.uploadId);
}
await Promise.all(uploads.map(_ => store.abortMultipartUpload(_.name, _.uploadId)));

const channels = (await store.listChannels()).channels.map(_ => _.Name);
await Promise.all(channels.map(_ => store.deleteChannel(_)));
await store.deleteBucket(bucket);
};

Expand Down
84 changes: 42 additions & 42 deletions test/browser/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ const cleanBucket = async (store) => {
'max-uploads': 1000
});
const uploads = result.uploads || [];
for (let i = 0; i < uploads.length; i++) {
const up = uploads[i];
await store.abortMultipartUpload(up.name, up.uploadId);
}
await Promise.all(uploads.map(_ => store.abortMultipartUpload(_.name, _.uploadId)));
};

describe('browser', () => {
Expand Down Expand Up @@ -855,20 +852,16 @@ describe('browser', () => {
'max-uploads': 1000
});
const uploads = result.uploads || [];
for (let i = 0; i < uploads.length; i++) {
const up = uploads[i];
await store.abortMultipartUpload(up.name, up.uploadId);
}
await Promise.all(uploads.map(_ => store.abortMultipartUpload(_.name, _.uploadId)));
});

it('should list by key marker', async () => {
const name = `${prefix}multipart/list-key`;
// var name = '/'
const ids = [];
for (let i = 0; i < 5; i++) {
const init = await store.initMultipartUpload(name + i);
ids.push(init.uploadId);
}

const ids = (await Promise.all(Array(5)
.fill(1).map((v, i) => store.initMultipartUpload(name + i))))
.map(_ => _.uploadId);

// list all uploads
let result = await store.listUploads({
'max-uploads': 10
Expand All @@ -895,12 +888,13 @@ describe('browser', () => {

it('should list by id marker', async () => {
const name = `${prefix}multipart/list-id`;
const ids = [];
for (let i = 0; i < 5; i++) {
const init = await store.initMultipartUpload(name);
ids.push(init.uploadId);
}
ids.sort();
const ids = (await Promise.all(Array(5)
.fill(1)
// eslint-disable-next-line no-unused-vars
.map(_ => store.initMultipartUpload(name))))
.map(_ => _.uploadId)
.sort();

// list all uploads
let result = await store.listUploads({
'max-uploads': 10
Expand All @@ -926,20 +920,20 @@ describe('browser', () => {
//
it('should list by id & key marker', async () => {
const fooName = `${prefix}multipart/list-foo`;
const fooIds = [];
for (let i = 0; i < 5; i++) {
const init = await store.initMultipartUpload(fooName);
fooIds.push(init.uploadId);
}
fooIds.sort();
const fooIds = (await Promise.all(Array(5)
.fill(1)
// eslint-disable-next-line no-unused-vars
.map(_ => store.initMultipartUpload(fooName))))
.map(_ => _.uploadId)
.sort();

const barName = `${prefix}multipart/list-bar`;
const barIds = [];
for (let i = 0; i < 5; i++) {
const result = await store.initMultipartUpload(barName);
barIds.push(result.uploadId);
}
barIds.sort();
const barIds = (await Promise.all(Array(5)
.fill(5)
// eslint-disable-next-line no-unused-vars
.map(_ => store.initMultipartUpload(barName))))
.map(_ => _.uploadId)
.sort();

// after 1
const result = await store.listUploads({
Expand Down Expand Up @@ -1181,16 +1175,22 @@ describe('browser', () => {
const init = await store.initMultipartUpload(name);
const { uploadId } = init;
const partSize = 100 * 1024;
const dones = [];
for (let i = 1; i <= 10; i++) {
const start = (i - 1) * partSize;
const end = Math.min(i * partSize, file.size);
const part = await store.uploadPart(name, uploadId, i, file, start, end);
dones.push({
number: i,
etag: part.res.headers.etag
});
}

const parts = await Promise.all(Array(10)
.fill(1)
.map((v, i) =>
store.uploadPart(
name,
uploadId,
i + 1,
file,
i * partSize,
Math.min((i + 1) * partSize, 10 * 100 * 1024)
)));
const dones = parts.map((_, i) => ({
number: i + 1,
etag: _.etag
}));

const result = await store.completeMultipartUpload(name, uploadId, dones);
assert.equal(result.res.status, 200);
Expand Down
30 changes: 9 additions & 21 deletions test/node/bucket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ describe('test/bucket.test.js', () => {
'max-keys': 20
});

/* eslint no-restricted-syntax: [0] */
for (const bucketObj of bucketResult.buckets) {
if (bucketObj.name.startsWith('ali-oss')) {
/* eslint no-await-in-loop: [0] */
config.region = bucketObj.region;
store = oss(config);
await utils.cleanBucket(store, bucketObj.name);
}
}
await Promise.all((bucketResult.buckets || [])
.filter(_ => _.name.startsWith('ali-oss'))
.map(_bucket =>
utils.cleanBucket(
oss(Object.assign(config, { region: _bucket.region })),
_bucket.name
)));

config.region = defaultRegion;
store = oss(config);
Expand Down Expand Up @@ -199,11 +197,7 @@ describe('test/bucket.test.js', () => {
before(async () => {
// create 2 buckets
listBucketsPrefix = `ali-oss-list-buckets-${prefix.replace(/[/.]/g, '-')}`;
for (let i = 0; i < 2; i++) {
const name = listBucketsPrefix + i;
const result = await store.putBucket(name);
assert.equal(result.res.status, 200);
}
await Promise.all(Array(2).fill(1).map((v, i) => store.putBucket(listBucketsPrefix + i)));
});

it('should list buckets by prefix', async () => {
Expand All @@ -226,14 +220,8 @@ describe('test/bucket.test.js', () => {
}
});

/* eslint no-empty: [0] */
after(async () => {
for (let i = 0; i < 2; i++) {
const name = listBucketsPrefix + i;
try {
await store.deleteBucket(name);
} catch (err) {}
}
await Promise.all(Array(2).fill(1).map((v, i) => store.deleteBucket(listBucketsPrefix + i)));
});
});

Expand Down
89 changes: 44 additions & 45 deletions test/node/multipart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,14 @@ describe('test/multipart.test.js', () => {
'max-uploads': 1000
});
const uploads = result.uploads || [];
for (let i = 0; i < uploads.length; i++) {
const up = uploads[i];
/* eslint no-await-in-loop: [0] */
await store.abortMultipartUpload(up.name, up.uploadId);
}
await Promise.all(uploads.map(_ => store.abortMultipartUpload(_.name, _.uploadId)));
});

it('should list by key marker', async () => {
const name = `${prefix}multipart/list-key`;
const ids = [];
for (let i = 0; i < 5; i++) {
const result = await store.initMultipartUpload(name + i);
ids.push(result.uploadId);
}
const ids = (await Promise.all(Array(5)
.fill(1).map((v, i) => store.initMultipartUpload(name + i))))
.map(_ => _.uploadId);
// list all uploads
let result = await store.listUploads({
'max-uploads': 10
Expand All @@ -73,12 +67,10 @@ describe('test/multipart.test.js', () => {

it('should list by id marker', async () => {
const name = `${prefix}multipart/list-id`;
const ids = [];
for (let i = 0; i < 5; i++) {
const result = await store.initMultipartUpload(name);
ids.push(result.uploadId);
}
ids.sort();
const ids = (await Promise.all(Array(5)
.fill(1)
.map(_ => store.initMultipartUpload(name))))
.map(_ => _.uploadId).sort();

// list all uploads
let result = await store.listUploads({
Expand Down Expand Up @@ -106,20 +98,18 @@ describe('test/multipart.test.js', () => {

it('should list by id & key marker', async () => {
const fooName = `${prefix}multipart/list-foo`;
const fooIds = [];
for (let i = 0; i < 5; i++) {
const result = await store.initMultipartUpload(fooName);
fooIds.push(result.uploadId);
}
fooIds.sort();
const fooIds = (await Promise.all(Array(5)
.fill(1)
.map(_ => store.initMultipartUpload(fooName))))
.map(_ => _.uploadId)
.sort();

const barName = `${prefix}multipart/list-bar`;
const barIds = [];
for (let i = 0; i < 5; i++) {
const result = await store.initMultipartUpload(barName);
barIds.push(result.uploadId);
}
barIds.sort();
const barIds = (await Promise.all(Array(5)
.fill(5)
.map(_ => store.initMultipartUpload(barName))))
.map(_ => _.uploadId)
.sort();

// after 1
let result = await store.listUploads({
Expand Down Expand Up @@ -410,16 +400,21 @@ describe('test/multipart.test.js', () => {
const init = await store.initMultipartUpload(name);
const { uploadId } = init;
const partSize = 100 * 1024;
const dones = [];
for (let i = 1; i <= 10; i++) {
const start = (i - 1) * partSize;
const end = Math.min(i * partSize, 10 * 100 * 1024);
const part = await store.uploadPart(name, uploadId, i, fileName, start, end);
dones.push({
number: i,
etag: part.etag
});
}
const parts = await Promise.all(Array(10)
.fill(1)
.map((v, i) =>
store.uploadPart(
name,
uploadId,
i + 1,
fileName,
i * partSize,
Math.min((i + 1) * partSize, 10 * 100 * 1024)
)));
const dones = parts.map((_, i) => ({
number: i + 1,
etag: _.etag
}));

const result = await store.completeMultipartUpload(name, uploadId, dones);
assert.equal(result.res.status, 200);
Expand Down Expand Up @@ -562,21 +557,25 @@ describe('test/multipart.test.js', () => {

const partSize = 100 * 1024;// 100kb
const dones = [];
// if file part is 10
for (let i = 1; i <= 10; i++) {
const uploadFn = async (i) => {
const start = partSize * (i - 1);
const end = Math.min(start + partSize, fileSize);
const range = `${start}-${end - 1}`;
/* eslint no-await-in-loop: [0] */
const part = await client.uploadPartCopy(
copyName
, result.uploadId, i, range, sourceData, {},
const part = await store.uploadPartCopy(
copyName,
result.uploadId,
i,
range,
sourceData,
{}
);
dones.push({
number: i,
etag: part.res.headers.etag
});
}
};

await Promise.all(Array(10).fill(1).map((v, i) => uploadFn(i + 1)));

const complete = await client.completeMultipartUpload(copyName, result.uploadId, dones);

Expand Down
Loading

0 comments on commit ae22ad1

Please sign in to comment.