2017-12-02 05:19:08 -05:00
|
|
|
import async from 'async';
|
2021-03-14 02:42:46 -05:00
|
|
|
import { HTTP_STATUS } from '../../../src/lib/constants';
|
2017-08-06 14:54:15 -05:00
|
|
|
|
2018-06-22 11:58:40 -05:00
|
|
|
let okTotalSum = 0;
|
2021-03-14 02:42:46 -05:00
|
|
|
import racePkg from '../fixtures/package';
|
2017-08-06 14:54:15 -05:00
|
|
|
|
2021-03-14 02:42:46 -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 () {
|
2021-03-14 02:42:46 -05:00
|
|
|
return server
|
|
|
|
.putPackage(PKG_NAME, racePkg(PKG_NAME))
|
2018-06-22 11:58:40 -05:00
|
|
|
.status(HTTP_STATUS.CREATED)
|
2017-08-06 14:54:15 -05:00
|
|
|
.body_ok(/created new package/);
|
|
|
|
});
|
|
|
|
|
2017-12-02 05:19:08 -05:00
|
|
|
test('creating new package', () => {});
|
2017-08-06 14:54:15 -05:00
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
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++) {
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-06-22 11:58:40 -05:00
|
|
|
listOfRequest.push(function (callback) {
|
|
|
|
let data = racePkg(PKG_NAME);
|
2017-08-06 14:54:15 -05:00
|
|
|
data.rand = Math.random();
|
|
|
|
|
|
|
|
let _res;
|
2021-03-14 02:42:46 -05:00
|
|
|
server
|
|
|
|
.putVersion(PKG_NAME, '0.0.1', data)
|
|
|
|
.response(function (res) {
|
|
|
|
_res = res;
|
|
|
|
})
|
|
|
|
.then(function (body) {
|
|
|
|
callback(null, [_res, body]);
|
|
|
|
});
|
2017-08-06 14:54:15 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-22 11:58:40 -05:00
|
|
|
async.parallel(listOfRequest, function (err, response) {
|
|
|
|
let okCount = 0;
|
|
|
|
let failCount = 0;
|
2017-08-06 14:54:15 -05:00
|
|
|
|
2018-06-22 11:58:40 -05:00
|
|
|
expect(err).toBeNull();
|
2017-08-06 14:54:15 -05:00
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-06-22 11:58:40 -05:00
|
|
|
response.forEach(function (payload) {
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-06-22 11:58:40 -05:00
|
|
|
const [resp, body] = payload;
|
2017-08-06 14:54:15 -05:00
|
|
|
|
2018-06-22 11:58:40 -05:00
|
|
|
if (resp.statusCode === HTTP_STATUS.CREATED && ~body.ok.indexOf(PUBLISHED)) {
|
|
|
|
okCount++;
|
2017-08-06 14:54:15 -05:00
|
|
|
}
|
2018-06-22 11:58:40 -05:00
|
|
|
|
|
|
|
if (resp.statusCode === HTTP_STATUS.CONFLICT && ~body.error.indexOf(PRESENT)) {
|
|
|
|
failCount++;
|
2017-08-06 14:54:15 -05:00
|
|
|
}
|
2018-06-22 11:58:40 -05:00
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
if (
|
|
|
|
resp.statusCode === HTTP_STATUS.SERVICE_UNAVAILABLE &&
|
|
|
|
~body.error.indexOf(UNAVAILABLE)
|
|
|
|
) {
|
2018-06-22 11:58:40 -05:00
|
|
|
failCount++;
|
2017-08-06 14:54:15 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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;
|
2017-08-06 14:54:15 -05:00
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
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++) {
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-06-22 11:58:40 -05:00
|
|
|
listofRequest.push(function (callback) {
|
|
|
|
let _res;
|
2021-03-14 02:42:46 -05:00
|
|
|
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]);
|
|
|
|
});
|
|
|
|
});
|
2017-08-06 14:54:15 -05:00
|
|
|
}
|
|
|
|
|
2018-06-22 11:58:40 -05:00
|
|
|
async.parallel(listofRequest, function (err, response) {
|
2017-08-06 14:54:15 -05:00
|
|
|
let okcount = 0;
|
|
|
|
let failcount = 0;
|
|
|
|
|
2018-06-22 11:58:40 -05:00
|
|
|
expect(err).toBeNull();
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-06-22 11:58:40 -05:00
|
|
|
response.forEach(function (payload) {
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-06-22 11:58:40 -05:00
|
|
|
const [response, body] = payload;
|
|
|
|
|
|
|
|
if (response.statusCode === HTTP_STATUS.CREATED && ~body.ok.indexOf(PUBLISHED)) {
|
2017-08-06 14:54:15 -05:00
|
|
|
okcount++;
|
|
|
|
}
|
2018-06-22 11:58:40 -05:00
|
|
|
if (response.statusCode === HTTP_STATUS.CONFLICT && ~body.error.indexOf(PRESENT)) {
|
2017-08-06 14:54:15 -05:00
|
|
|
failcount++;
|
|
|
|
}
|
2021-03-14 02:42:46 -05:00
|
|
|
if (
|
|
|
|
response.statusCode === HTTP_STATUS.SERVICE_UNAVAILABLE &&
|
|
|
|
~body.error.indexOf(UNAVAILABLE)
|
|
|
|
) {
|
2017-08-06 14:54:15 -05:00
|
|
|
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;
|
2017-08-06 14:54:15 -05:00
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
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-08-06 14:54:15 -05:00
|
|
|
});
|
|
|
|
});
|
2017-12-02 05:19:08 -05:00
|
|
|
}
|