0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00
verdaccio/test/functional/performance/race.ts

130 lines
3.7 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';
2017-12-02 05:19:08 -05:00
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 () {
2018-06-22 11:58:40 -05:00
return server.putPackage(PKG_NAME, racePkg(PKG_NAME))
.status(HTTP_STATUS.CREATED)
.body_ok(/created new package/);
});
2017-12-02 05:19:08 -05:00
test('creating new package', () => {});
2018-06-22 11:58:40 -05:00
test('should uploading 10 same versions and ignore 9', callback => {
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;
2018-06-22 11:58:40 -05:00
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)) {
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();
});
});
2018-06-22 11:58:40 -05:00
test('shoul uploading 10 diff versions and accept 10', callback => {
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))
.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++;
}
2018-06-22 11:58:40 -05:00
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();
});
});
2018-06-22 11:58:40 -05:00
afterAll(function() {
return server.getPackage(PKG_NAME).status(HTTP_STATUS.OK).then(function (body) {
expect(Object.keys(body.versions)).toHaveLength(okTotalSum);
});
});
});
2017-12-02 05:19:08 -05:00
}