2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const moment = require('moment');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const http = require('http');
|
2020-05-01 21:29:42 +03:00
|
|
|
const express = require('../../../../core/shared/express');
|
2020-04-29 18:44:27 +03:00
|
|
|
const SchedulingDefault = require('../../../../core/server/adapters/scheduling/SchedulingDefault');
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
describe('Scheduling Default Adapter', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const scope = {};
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2016-06-15 10:40:53 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
scope.adapter = new SchedulingDefault();
|
2016-05-19 14:49:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2016-05-19 14:49:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('success', function () {
|
|
|
|
it('addJob (schedule)', function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(scope.adapter, 'run');
|
|
|
|
sinon.stub(scope.adapter, '_execute');
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const dates = [
|
2016-05-19 14:49:22 +03:00
|
|
|
moment().add(1, 'day').subtract(30, 'seconds').toDate(),
|
|
|
|
moment().add(7, 'minutes').toDate(),
|
|
|
|
|
|
|
|
// over 10minutes offset
|
|
|
|
moment().add(12, 'minutes').toDate(),
|
|
|
|
moment().add(20, 'minutes').toDate(),
|
|
|
|
moment().add(15, 'minutes').toDate(),
|
|
|
|
moment().add(15, 'minutes').add(10, 'seconds').toDate(),
|
|
|
|
moment().add(15, 'minutes').subtract(30, 'seconds').toDate(),
|
|
|
|
moment().add(50, 'seconds').toDate()
|
|
|
|
];
|
|
|
|
|
|
|
|
dates.forEach(function (time) {
|
|
|
|
scope.adapter._addJob({
|
|
|
|
time: time,
|
|
|
|
url: 'something'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// 2 jobs get immediately executed
|
|
|
|
should.not.exist(scope.adapter.allJobs[moment(dates[1]).valueOf()]);
|
|
|
|
should.not.exist(scope.adapter.allJobs[moment(dates[7]).valueOf()]);
|
|
|
|
scope.adapter._execute.calledTwice.should.eql(true);
|
|
|
|
|
|
|
|
Object.keys(scope.adapter.allJobs).length.should.eql(dates.length - 2);
|
|
|
|
Object.keys(scope.adapter.allJobs).should.eql([
|
|
|
|
moment(dates[2]).valueOf().toString(),
|
|
|
|
moment(dates[6]).valueOf().toString(),
|
|
|
|
moment(dates[4]).valueOf().toString(),
|
|
|
|
moment(dates[5]).valueOf().toString(),
|
|
|
|
moment(dates[3]).valueOf().toString(),
|
|
|
|
moment(dates[0]).valueOf().toString()
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2019-05-06 12:11:43 +03:00
|
|
|
it('reschedule: default', function (done) {
|
|
|
|
sinon.stub(scope.adapter, '_pingUrl');
|
|
|
|
|
|
|
|
const time = moment().add(20, 'milliseconds').valueOf();
|
|
|
|
|
|
|
|
scope.adapter.schedule({
|
|
|
|
time: time,
|
|
|
|
url: 'something',
|
|
|
|
extra: {
|
|
|
|
oldTime: null,
|
|
|
|
method: 'PUT'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-20 09:46:26 +03:00
|
|
|
/**Reschedule is now unschedule+schedule */
|
|
|
|
scope.adapter.unschedule({
|
2019-05-06 12:11:43 +03:00
|
|
|
time: time,
|
|
|
|
url: 'something',
|
|
|
|
extra: {
|
|
|
|
oldTime: time,
|
|
|
|
method: 'PUT'
|
|
|
|
}
|
|
|
|
});
|
2019-11-20 09:46:26 +03:00
|
|
|
scope.adapter.schedule({
|
|
|
|
time: time,
|
|
|
|
url: 'something',
|
|
|
|
extra: {
|
|
|
|
oldTime: null,
|
|
|
|
method: 'PUT'
|
|
|
|
}
|
|
|
|
});
|
2019-05-06 12:11:43 +03:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
scope.adapter._pingUrl.calledOnce.should.eql(true);
|
|
|
|
done();
|
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('reschedule: simulate restart', function (done) {
|
|
|
|
sinon.stub(scope.adapter, '_pingUrl');
|
|
|
|
|
|
|
|
const time = moment().add(20, 'milliseconds').valueOf();
|
|
|
|
|
2019-11-20 09:46:26 +03:00
|
|
|
scope.adapter.unschedule({
|
2019-08-19 14:41:09 +03:00
|
|
|
time: time,
|
2019-05-06 12:11:43 +03:00
|
|
|
url: 'something',
|
|
|
|
extra: {
|
2019-08-19 14:41:09 +03:00
|
|
|
oldTime: time,
|
2019-05-06 12:11:43 +03:00
|
|
|
method: 'PUT'
|
|
|
|
}
|
|
|
|
}, {bootstrap: true});
|
|
|
|
|
2019-11-20 09:46:26 +03:00
|
|
|
scope.adapter.schedule({
|
|
|
|
time: time,
|
|
|
|
url: 'something',
|
|
|
|
extra: {
|
|
|
|
oldTime: null,
|
|
|
|
method: 'PUT'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-06 12:11:43 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
scope.adapter._pingUrl.calledOnce.should.eql(true);
|
|
|
|
done();
|
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
it('run', function (done) {
|
2017-11-28 23:38:30 +03:00
|
|
|
// 1000 jobs, but only the number x are under 1 minute
|
2020-04-29 18:44:27 +03:00
|
|
|
const timestamps = _.map(_.range(1000), function (i) {
|
|
|
|
return moment().add(i, 'seconds').valueOf();
|
|
|
|
});
|
|
|
|
|
|
|
|
const allJobs = {};
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(scope.adapter, '_execute').callsFake(function (nextJobs) {
|
2017-11-28 23:38:30 +03:00
|
|
|
Object.keys(nextJobs).length.should.eql(121);
|
|
|
|
Object.keys(scope.adapter.allJobs).length.should.eql(1000 - 121);
|
2016-05-19 14:49:22 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
timestamps.forEach(function (timestamp) {
|
|
|
|
allJobs[timestamp] = [{url: 'xxx'}];
|
|
|
|
});
|
|
|
|
|
|
|
|
scope.adapter.allJobs = allJobs;
|
2017-11-28 23:38:30 +03:00
|
|
|
scope.adapter.runTimeoutInMs = 100;
|
|
|
|
scope.adapter.offsetInMinutes = 1;
|
2016-05-19 14:49:22 +03:00
|
|
|
scope.adapter.run();
|
|
|
|
});
|
|
|
|
|
2017-09-05 21:23:11 +03:00
|
|
|
it('ensure recursive run works', function (done) {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.spy(scope.adapter, '_execute');
|
2017-09-05 21:23:11 +03:00
|
|
|
|
|
|
|
scope.adapter.allJobs = {};
|
2017-11-28 23:38:30 +03:00
|
|
|
scope.adapter.runTimeoutInMs = 10;
|
|
|
|
scope.adapter.offsetInMinutes = 1;
|
2017-09-05 21:23:11 +03:00
|
|
|
scope.adapter.run();
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
scope.adapter._execute.callCount.should.be.greaterThan(1);
|
|
|
|
done();
|
2017-11-28 23:38:30 +03:00
|
|
|
}, 30);
|
2017-09-05 21:23:11 +03:00
|
|
|
});
|
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
it('execute', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let pinged = 0;
|
|
|
|
const jobs = 3;
|
|
|
|
|
|
|
|
const timestamps = _.map(_.range(jobs), function (i) {
|
|
|
|
return moment().add(i * 50, 'milliseconds').valueOf();
|
|
|
|
});
|
|
|
|
|
|
|
|
const nextJobs = {};
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(scope.adapter, 'run');
|
|
|
|
sinon.stub(scope.adapter, '_pingUrl').callsFake(function () {
|
2016-05-19 14:49:22 +03:00
|
|
|
pinged = pinged + 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
timestamps.forEach(function (timestamp) {
|
|
|
|
nextJobs[timestamp] = [{url: 'xxx'}];
|
|
|
|
});
|
|
|
|
|
|
|
|
scope.adapter._execute(nextJobs);
|
|
|
|
|
|
|
|
(function retry() {
|
|
|
|
if (pinged !== jobs) {
|
2017-11-28 23:38:30 +03:00
|
|
|
return setTimeout(retry, 50);
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
done();
|
|
|
|
})();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('delete job (unschedule)', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let pinged = 0;
|
|
|
|
const jobsToDelete = {};
|
|
|
|
const jobsToExecute = {};
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(scope.adapter, 'run');
|
|
|
|
sinon.stub(scope.adapter, '_pingUrl').callsFake(function () {
|
2016-06-15 10:40:53 +03:00
|
|
|
pinged = pinged + 1;
|
|
|
|
});
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2016-06-15 10:40:53 +03:00
|
|
|
// add jobs to delete
|
2017-11-28 23:38:30 +03:00
|
|
|
jobsToDelete[moment().add(10, 'milliseconds').valueOf()] = [{url: '/1', time: 1234}];
|
|
|
|
jobsToDelete[moment().add(20, 'milliseconds').valueOf()] = [{url: '/2', time: 1235}];
|
|
|
|
jobsToDelete[moment().add(30, 'milliseconds').valueOf()] = [{url: '/1', time: 1234}];
|
|
|
|
jobsToDelete[moment().add(40, 'milliseconds').valueOf()] = [{url: '/3', time: 1236}];
|
2016-06-15 10:40:53 +03:00
|
|
|
|
|
|
|
_.map(jobsToDelete, function (value) {
|
2016-05-19 14:49:22 +03:00
|
|
|
scope.adapter._deleteJob(value[0]);
|
|
|
|
});
|
|
|
|
|
2016-06-15 10:40:53 +03:00
|
|
|
// add jobs, which will be pinged
|
2017-11-28 23:38:30 +03:00
|
|
|
jobsToExecute[moment().add(50, 'milliseconds').valueOf()] = [{url: '/1', time: 1234}];
|
|
|
|
jobsToExecute[moment().add(60, 'milliseconds').valueOf()] = [{url: '/1', time: 1234}];
|
|
|
|
jobsToExecute[moment().add(70, 'milliseconds').valueOf()] = [{url: '/1', time: 1234}];
|
|
|
|
jobsToExecute[moment().add(80, 'milliseconds').valueOf()] = [{url: '/4', time: 1237}];
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
// simulate execute is called
|
2016-06-15 10:40:53 +03:00
|
|
|
scope.adapter._execute(jobsToExecute);
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
(function retry() {
|
2016-06-15 10:40:53 +03:00
|
|
|
if (pinged !== 2) {
|
2017-11-28 23:38:30 +03:00
|
|
|
return setTimeout(retry, 50);
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
2016-06-15 10:40:53 +03:00
|
|
|
Object.keys(scope.adapter.deletedJobs).length.should.eql(2);
|
|
|
|
pinged.should.eql(2);
|
2016-05-19 14:49:22 +03:00
|
|
|
done();
|
|
|
|
})();
|
|
|
|
});
|
|
|
|
|
2016-06-28 21:14:29 +03:00
|
|
|
it('delete job (unschedule): time is null', function () {
|
|
|
|
scope.adapter._deleteJob({time: null, url: '/test'});
|
|
|
|
Object.keys(scope.adapter.deletedJobs).length.should.eql(0);
|
|
|
|
});
|
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
it('pingUrl (PUT)', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const app = express();
|
|
|
|
const server = http.createServer(app);
|
|
|
|
let wasPinged = false;
|
|
|
|
let reqBody;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
|
|
|
app.put('/ping', function (req, res) {
|
|
|
|
wasPinged = true;
|
|
|
|
reqBody = req.body;
|
|
|
|
res.sendStatus(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(1111);
|
|
|
|
|
|
|
|
scope.adapter._pingUrl({
|
|
|
|
url: 'http://localhost:1111/ping',
|
|
|
|
time: moment().add(1, 'second').valueOf(),
|
|
|
|
extra: {
|
|
|
|
httpMethod: 'PUT'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
(function retry() {
|
|
|
|
if (wasPinged) {
|
|
|
|
should.not.exist(reqBody.force);
|
|
|
|
return server.close(done);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(retry, 100);
|
|
|
|
})();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('pingUrl (GET)', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const app = express();
|
|
|
|
const server = http.createServer(app);
|
|
|
|
let wasPinged = false;
|
|
|
|
let reqQuery;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
app.get('/ping', function (req, res) {
|
|
|
|
wasPinged = true;
|
|
|
|
reqQuery = req.query;
|
|
|
|
res.sendStatus(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(1111);
|
|
|
|
|
|
|
|
scope.adapter._pingUrl({
|
|
|
|
url: 'http://localhost:1111/ping',
|
|
|
|
time: moment().add(1, 'second').valueOf(),
|
|
|
|
extra: {
|
|
|
|
httpMethod: 'GET'
|
|
|
|
}
|
2019-03-04 10:59:38 +03:00
|
|
|
}).then(() => {
|
|
|
|
wasPinged.should.be.true();
|
|
|
|
should.not.exist(reqQuery.force);
|
|
|
|
server.close(done);
|
2016-05-19 14:49:22 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('pingUrl (PUT, and detect publish in the past)', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const app = express();
|
|
|
|
const server = http.createServer(app);
|
|
|
|
let wasPinged = false;
|
|
|
|
let reqBody;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
|
|
|
app.put('/ping', function (req, res) {
|
|
|
|
wasPinged = true;
|
|
|
|
reqBody = req.body;
|
|
|
|
res.sendStatus(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(1111);
|
|
|
|
|
|
|
|
scope.adapter._pingUrl({
|
|
|
|
url: 'http://localhost:1111/ping',
|
|
|
|
time: moment().subtract(10, 'minutes').valueOf(),
|
|
|
|
extra: {
|
|
|
|
httpMethod: 'PUT'
|
|
|
|
}
|
2019-03-04 10:59:38 +03:00
|
|
|
}).then(() => {
|
|
|
|
wasPinged.should.be.true();
|
|
|
|
should.exist(reqBody.force);
|
|
|
|
server.close(done);
|
2016-05-19 14:49:22 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('pingUrl (GET, and detect publish in the past)', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const app = express();
|
|
|
|
const server = http.createServer(app);
|
|
|
|
let wasPinged = false;
|
|
|
|
let reqQuery;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
app.get('/ping', function (req, res) {
|
|
|
|
wasPinged = true;
|
|
|
|
reqQuery = req.query;
|
|
|
|
res.sendStatus(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(1111);
|
|
|
|
|
|
|
|
scope.adapter._pingUrl({
|
|
|
|
url: 'http://localhost:1111/ping',
|
|
|
|
time: moment().subtract(10, 'minutes').valueOf(),
|
|
|
|
extra: {
|
|
|
|
httpMethod: 'GET'
|
|
|
|
}
|
2019-03-04 10:59:38 +03:00
|
|
|
}).then(() => {
|
|
|
|
wasPinged.should.be.true();
|
|
|
|
should.exist(reqQuery.force);
|
|
|
|
server.close(done);
|
2016-05-19 14:49:22 +03:00
|
|
|
});
|
|
|
|
});
|
2016-06-14 17:08:49 +03:00
|
|
|
|
|
|
|
it('pingUrl, but blog returns 503', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const app = express();
|
|
|
|
const server = http.createServer(app);
|
|
|
|
let returned500Count = 0;
|
|
|
|
let returned200 = false;
|
|
|
|
let reqBody;
|
2016-06-14 17:08:49 +03:00
|
|
|
|
2017-11-28 23:38:30 +03:00
|
|
|
scope.adapter.retryTimeoutInMs = 10;
|
2016-06-14 17:08:49 +03:00
|
|
|
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
app.put('/ping', function (req, res) {
|
|
|
|
reqBody = req.body;
|
|
|
|
|
|
|
|
if (returned500Count === 5) {
|
|
|
|
returned200 = true;
|
|
|
|
return res.sendStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
returned500Count = returned500Count + 1;
|
|
|
|
res.sendStatus(503);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(1111);
|
|
|
|
|
|
|
|
scope.adapter._pingUrl({
|
|
|
|
url: 'http://localhost:1111/ping',
|
|
|
|
time: moment().valueOf(),
|
|
|
|
extra: {
|
|
|
|
httpMethod: 'PUT'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
(function retry() {
|
|
|
|
if (returned200) {
|
|
|
|
should.exist(reqBody.force);
|
|
|
|
return server.close(done);
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:38:30 +03:00
|
|
|
setTimeout(retry, 50);
|
2016-06-14 17:08:49 +03:00
|
|
|
})();
|
|
|
|
});
|
2016-05-19 14:49:22 +03:00
|
|
|
});
|
|
|
|
});
|