topkat-utils
Version:
A comprehensive collection of TypeScript/JavaScript utility functions for common programming tasks. Includes validation, object manipulation, date handling, string formatting, and more. Zero dependencies, fully typed, and optimized for performance.
49 lines • 2.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const transaction_utils_1 = require("./transaction-utils");
const timer_utils_1 = require("./timer-utils");
describe('TRANSACTION TEST', () => {
const statusArr = [];
const asyncCallback = (i) => async () => {
await (0, timer_utils_1.timeout)(1000);
statusArr.push(i);
};
it('should execute the async callback and resolve the result', async () => {
(0, transaction_utils_1.transaction)('test', asyncCallback(1), 1500);
(0, transaction_utils_1.transaction)('test', asyncCallback(2), 1500);
const now = Date.now();
await (0, transaction_utils_1.transaction)('test', asyncCallback(3), 1500);
const timeSpent = Date.now() - now;
expect(statusArr).toEqual([1, 2, 3]);
expect(timeSpent < 5000).toEqual(true);
expect(timeSpent > 2999).toEqual(true);
});
it('Second run', async () => {
(0, transaction_utils_1.transaction)('test', asyncCallback(4), 1500);
(0, transaction_utils_1.transaction)('test', asyncCallback(5), 1500);
const now = Date.now();
await (0, transaction_utils_1.transaction)('test', asyncCallback(6), 1500);
const timeSpent = Date.now() - now;
expect(statusArr).toEqual([1, 2, 3, 4, 5, 6]);
expect(timeSpent < 5000).toEqual(true);
expect(timeSpent > 2999).toEqual(true);
});
const infiniteCallback = async () => await (0, timer_utils_1.timeout)(5000);
it('Transaction timeout if the transaction is infinite and other transaction run correctly', async () => {
const now = Date.now();
let hasErr = false;
try {
await (0, transaction_utils_1.transaction)('test', infiniteCallback, 1000);
}
catch (err) {
hasErr = true;
}
const timeSpent = Date.now() - now;
await (0, transaction_utils_1.transaction)('test', asyncCallback(7), 1500);
expect(hasErr).toEqual(true);
expect(statusArr).toEqual([1, 2, 3, 4, 5, 6, 7]);
expect(timeSpent < 2000).toEqual(true);
expect(timeSpent > 999).toEqual(true);
});
});
//# sourceMappingURL=transaction-utils.spec.js.map