prray
Version:
'Promisified' Array, comes with async method supports(such as mapAsync). And it is compatible with normal array.
109 lines (108 loc) • 4.75 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const ava_1 = require("ava");
const sinon = require("sinon");
const test_utils_1 = require("./test-utils");
const methods_1 = require("../src/methods");
// TODO: Update threshold
ava_1.default('loop with concurrency 100', (t) => __awaiter(this, void 0, void 0, function* () {
const arr = test_utils_1.genRandArr(1000);
const record = test_utils_1.timer();
let running = 0;
yield methods_1.loop(arr, () => __awaiter(this, void 0, void 0, function* () {
running++;
yield test_utils_1.delay(100);
t.true(running <= 100);
running--;
}), { concurrency: 100 });
t.true(test_utils_1.isClose(record(), 10 * 100));
}));
ava_1.default('loop with concurrency infinity', (t) => __awaiter(this, void 0, void 0, function* () {
const arr = test_utils_1.genRandArr(1000);
const record = test_utils_1.timer();
yield methods_1.loop(arr, () => test_utils_1.delay(100), {});
t.true(test_utils_1.isClose(record(), 100));
}));
ava_1.default('loop with concurrency 1', (t) => __awaiter(this, void 0, void 0, function* () {
const arr = test_utils_1.genRandArr(100);
const record = test_utils_1.timer();
yield methods_1.loop(arr, () => test_utils_1.delay(10), { concurrency: 1 });
t.true(test_utils_1.isClose(record(), 10 * 100, { threshold: 200 }));
}));
ava_1.default('loop with break', (t) => __awaiter(this, void 0, void 0, function* () {
const arr = [false, false, false, false, false, false, false, false]; // length 8
const record = test_utils_1.timer();
yield methods_1.loop(arr, (v, ix, prr, breakLoop) => __awaiter(this, void 0, void 0, function* () {
if (ix >= 6) {
breakLoop();
return;
}
prr[ix] = true;
yield test_utils_1.delay(100);
}), { concurrency: 2 });
t.true(test_utils_1.isClose(record(), 300));
t.deepEqual(arr, [true, true, true, true, true, true, false, false]);
}));
ava_1.default('loop with unhandled error', (t) => __awaiter(this, void 0, void 0, function* () {
const arr = [false, false, false, false, false, false, false, false]; // length 8
let isThrown = false;
const record = test_utils_1.timer();
try {
yield methods_1.loop(arr, (v, ix, prr) => __awaiter(this, void 0, void 0, function* () {
if (ix >= 6) {
throw new Error('err');
}
prr[ix] = true;
yield test_utils_1.delay(100);
}), { concurrency: 2 });
}
catch (e) {
isThrown = true;
}
t.true(test_utils_1.isClose(record(), 300));
t.deepEqual(arr, [true, true, true, true, true, true, false, false]);
t.true(isThrown);
}));
ava_1.default('loop with empty array', (t) => __awaiter(this, void 0, void 0, function* () {
const record = test_utils_1.timer();
yield methods_1.loop([], () => test_utils_1.delay(100), { concurrency: 10 });
t.true(test_utils_1.isClose(record(), 0));
}));
ava_1.default('loop with empty array, concurrency Infinity', (t) => __awaiter(this, void 0, void 0, function* () {
const record = test_utils_1.timer();
yield methods_1.loop([], () => test_utils_1.delay(100), {});
t.true(test_utils_1.isClose(record(), 0));
}));
ava_1.default('loop detail', (t) => __awaiter(this, void 0, void 0, function* () {
const arr = ['a', 'b', 'c', 'd', 'e', 'f'];
const func = sinon.fake();
yield methods_1.loop(arr, func, { concurrency: 2 });
t.is(func.called, true);
t.is(func.callCount, 6);
t.is(func.args[0][0], 'a');
t.is(func.args[0][1], 0);
t.is(func.args[0][2], arr);
t.is(func.args[1][0], 'b');
t.is(func.args[1][1], 1);
t.is(func.args[1][2], arr);
t.is(func.args[2][0], 'c');
t.is(func.args[2][1], 2);
t.is(func.args[2][2], arr);
t.is(func.args[3][0], 'd');
t.is(func.args[3][1], 3);
t.is(func.args[3][2], arr);
t.is(func.args[4][0], 'e');
t.is(func.args[4][1], 4);
t.is(func.args[4][2], arr);
t.is(func.args[5][0], 'f');
t.is(func.args[5][1], 5);
t.is(func.args[5][2], arr);
}));