0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-13 22:48:31 -05:00
verdaccio/test/functional/performance/race.ts

144 lines
3.9 KiB
TypeScript
Raw Normal View History

2017-12-02 05:19:08 -05:00
import async from 'async';
import { HTTP_STATUS } from '../../../src/lib/constants';
2018-06-22 11:58:40 -05:00
let okTotalSum = 0;
import racePkg from '../fixtures/package';
export default function (server) {
2018-06-22 11:58:40 -05:00
describe('should test race condition on publish packages', () => {
const MAX_COUNT = 20;
const PKG_NAME = 'race';
const PUBLISHED = 'published';
const PRESENT = 'already present';
const UNAVAILABLE = 'unavailable';
2017-12-02 05:19:08 -05:00
beforeAll(function () {
return server
.putPackage(PKG_NAME, racePkg(PKG_NAME))
2018-06-22 11:58:40 -05:00
.status(HTTP_STATUS.CREATED)
.body_ok(/created new package/);
});
2017-12-02 05:19:08 -05:00
test('creating new package', () => {});
test('should uploading 10 same versions and ignore 9', (callback) => {
2018-06-22 11:58:40 -05:00
let listOfRequest = [];
for (let i = 0; i < MAX_COUNT; i++) {
// @ts-ignore
2018-06-22 11:58:40 -05:00
listOfRequest.push(function (callback) {
let data = racePkg(PKG_NAME);
data.rand = Math.random();
let _res;
server
.putVersion(PKG_NAME, '0.0.1', data)
.response(function (res) {
_res = res;
})
.then(function (body) {
callback(null, [_res, body]);
});
});
}
2018-06-22 11:58:40 -05:00
async.parallel(listOfRequest, function (err, response) {
let okCount = 0;
let failCount = 0;
2018-06-22 11:58:40 -05:00
expect(err).toBeNull();
// @ts-ignore
2018-06-22 11:58:40 -05:00
response.forEach(function (payload) {
// @ts-ignore
2018-06-22 11:58:40 -05:00
const [resp, body] = payload;
2018-06-22 11:58:40 -05:00
if (resp.statusCode === HTTP_STATUS.CREATED && ~body.ok.indexOf(PUBLISHED)) {
okCount++;
}
2018-06-22 11:58:40 -05:00
if (resp.statusCode === HTTP_STATUS.CONFLICT && ~body.error.indexOf(PRESENT)) {
failCount++;
}
2018-06-22 11:58:40 -05:00
if (
resp.statusCode === HTTP_STATUS.SERVICE_UNAVAILABLE &&
~body.error.indexOf(UNAVAILABLE)
) {
2018-06-22 11:58:40 -05:00
failCount++;
}
});
2018-06-22 11:58:40 -05:00
expect(okCount + failCount).toEqual(MAX_COUNT);
expect(okCount).toEqual(1);
expect(failCount).toEqual(MAX_COUNT - 1);
okTotalSum += okCount;
callback();
});
});
test('shoul uploading 10 diff versions and accept 10', (callback) => {
2018-06-22 11:58:40 -05:00
const listofRequest = [];
for (let i = 0; i < MAX_COUNT; i++) {
// @ts-ignore
2018-06-22 11:58:40 -05:00
listofRequest.push(function (callback) {
let _res;
server
.putVersion(PKG_NAME, '0.1.' + String(i), racePkg(PKG_NAME))
2018-06-22 11:58:40 -05:00
.response(function (res) {
_res = res;
})
.then(function (body) {
callback(null, [_res, body]);
});
});
}
2018-06-22 11:58:40 -05:00
async.parallel(listofRequest, function (err, response) {
let okcount = 0;
let failcount = 0;
2018-06-22 11:58:40 -05:00
expect(err).toBeNull();
// @ts-ignore
2018-06-22 11:58:40 -05:00
response.forEach(function (payload) {
// @ts-ignore
2018-06-22 11:58:40 -05:00
const [response, body] = payload;
if (response.statusCode === HTTP_STATUS.CREATED && ~body.ok.indexOf(PUBLISHED)) {
okcount++;
}
2018-06-22 11:58:40 -05:00
if (response.statusCode === HTTP_STATUS.CONFLICT && ~body.error.indexOf(PRESENT)) {
failcount++;
}
if (
response.statusCode === HTTP_STATUS.SERVICE_UNAVAILABLE &&
~body.error.indexOf(UNAVAILABLE)
) {
failcount++;
}
});
2018-06-22 11:58:40 -05:00
expect(okcount + failcount).toEqual(MAX_COUNT);
expect(okcount).toEqual(MAX_COUNT);
expect(failcount).toEqual(0);
// should be more than 1
expect(okcount).not.toEqual(1);
okTotalSum += okcount;
callback();
});
});
afterAll(function () {
return server
.getPackage(PKG_NAME)
.status(HTTP_STATUS.OK)
.then(function (body) {
// eslint-disable-next-line jest/no-standalone-expect
expect(Object.keys(body.versions)).toHaveLength(okTotalSum);
});
});
});
2017-12-02 05:19:08 -05:00
}