Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 17, 2024
1 parent 2e7079a commit 7208139
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 52 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ jobs:
with:
os: 'ubuntu-latest'
version: '18, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
[![Test coverage][codecov-image]][codecov-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/@eggjs/schedule.svg?style=flat)](https://nodejs.org/en/download/)

[npm-image]: https://img.shields.io/npm/v/@eggjs/schedule.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@eggjs/schedule
[codecov-image]: https://codecov.io/github/eggjs/egg-schedule/coverage.svg?branch=master
[codecov-url]: https://codecov.io/github/eggjs/egg-schedule?branch=master
[snyk-image]: https://snyk.io/test/npm/egg-schedule/badge.svg?style=flat-square
[snyk-image]: https://snyk.io/test/npm/@eggjs/schedule/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/@eggjs/schedule
[download-image]: https://img.shields.io/npm/dm/@eggjs/schedule.svg?style=flat-square
[download-url]: https://npmjs.org/package/@eggjs/schedule
Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
},
"description": "schedule plugin for egg, support corn job.",
"eggPlugin": {
"name": "schedule"
"name": "schedule",
"exports": {
"import": "./dist/esm",
"require": "./dist/commonjs"
}
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -34,7 +38,7 @@
"@types/safe-timers": "^1.1.2",
"egg": "beta",
"egg-bin": "6",
"egg-mock": "^5.15.1",
"egg-mock": "beta",
"egg-tracer": "2",
"eslint": "8",
"eslint-config-egg": "14",
Expand All @@ -44,8 +48,10 @@
},
"scripts": {
"lint": "eslint --cache src test --ext .ts",
"test": "npm run lint -- --fix && egg-bin test",
"ci": "npm run lint && egg-bin cov && npm run prepublishOnly && attw --pack",
"pretest": "npm run lint -- --fix && npm run prepublishOnly",
"test": "egg-bin test",
"preci": "npm run lint && npm run prepublishOnly",
"ci": "egg-bin cov && attw --pack",
"prepublishOnly": "tshy && tshy-after"
},
"author": "dead_horse",
Expand Down
4 changes: 4 additions & 0 deletions src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { debuglog } from 'node:util';
import type { Agent, ILifecycleBoot } from 'egg';
import { WorkerStrategy } from './lib/strategy/worker.js';
import { AllStrategy } from './lib/strategy/all.js';
import { ScheduleJobInfo } from './lib/types.js';

const debug = debuglog('@eggjs/schedule/agent');

export default class Boot implements ILifecycleBoot {
#agent: Agent;
constructor(agent: Agent) {
Expand All @@ -26,6 +29,7 @@ export default class Boot implements ILifecycleBoot {
this.#agent.messenger.once('egg-ready', () => {
// start schedule after worker ready
this.#agent.schedule.start();
debug('got egg-ready event, schedule start');
});
}
}
5 changes: 5 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { debuglog } from 'node:util';
import path from 'node:path';
import type {
Application, ILifecycleBoot, EggLogger,
} from 'egg';
import { ScheduleItem, ScheduleJobInfo } from './lib/types.js';

const debug = debuglog('@eggjs/schedule/app');

export default class Boot implements ILifecycleBoot {
#app: Application;
#logger: EggLogger;
Expand All @@ -13,6 +16,7 @@ export default class Boot implements ILifecycleBoot {
}

async didLoad(): Promise<void> {
debug('didLoad');
const scheduleWorker = this.#app.scheduleWorker;
await scheduleWorker.init();

Expand All @@ -26,6 +30,7 @@ export default class Boot implements ILifecycleBoot {

// register schedule event
this.#app.messenger.on('egg-schedule', async info => {
debug('app got "egg-schedule" message: %o', info);
const { id, key } = info;
this.#logger.debug(`[Job#${id}] ${key} await app ready`);
await this.#app.ready();
Expand Down
2 changes: 1 addition & 1 deletion src/app/extend/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default {
get schedule() {
if (!this[SCHEDULE]) {
this[SCHEDULE] = new Schedule(this);
this.beforeClose(() => {
this.lifecycle.registerBeforeClose(() => {
return this[SCHEDULE].close();
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/load_schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function getScheduleLoader(app: EggApplicationCore) {

export async function loadSchedule(app: EggApplicationCore) {
const dirs = [
app.loader.getLoadUnits().map(unit => path.join(unit.path, 'app/schedule')),
...app.loader.getLoadUnits().map(unit => path.join(unit.path, 'app/schedule')),
...app.config.schedule.directory,
];

Expand Down
11 changes: 8 additions & 3 deletions src/lib/strategy/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ export class BaseStrategy {
this.logger = this.agent.getLogger('scheduleLogger');
}

/** keep compatibility */
get schedule(): ScheduleConfig {
return this.scheduleConfig;
}

start() {
throw new TypeError(`[egg-schedule] ${this.key} strategy should override \`start()\` method`);
// empty loop by default
}

close() {
Expand Down Expand Up @@ -50,7 +55,7 @@ export class BaseStrategy {
args,
} as ScheduleJobInfo;

this.logger.debug(`[Job#${info.id}] ${info.key} triggered, send random by agent`);
this.logger.info(`[Job#${info.id}] ${info.key} triggered, send random by agent`);
this.agent.messenger.sendRandom('egg-schedule', info);
this.onJobStart(info);
}
Expand All @@ -74,7 +79,7 @@ export class BaseStrategy {
id: this.getSeqId(),
args,
} as ScheduleJobInfo;
this.logger.debug(`[Job#${info.id}] ${info.key} triggered, send all by agent`);
this.logger.info(`[Job#${info.id}] ${info.key} triggered, send all by agent`);
// send to all workers
this.agent.messenger.send('egg-schedule', info);
this.onJobStart(info);
Expand Down
20 changes: 10 additions & 10 deletions test/fixtures/customType/agent.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

module.exports = function(agent) {
class ClusterStrategy extends agent.ScheduleStrategy {
start() {
this.interval = setInterval(() => {
this.sendOne();
}, this.schedule.interval);
module.exports = class Boot {
constructor(agent) {
class ClusterStrategy extends agent.ScheduleStrategy {
start() {
this.interval = setInterval(() => {
this.sendOne();
}, this.schedule.interval);
}
}
agent.schedule.use('cluster', ClusterStrategy);
}
agent.schedule.use('cluster', ClusterStrategy);
};
}
2 changes: 0 additions & 2 deletions test/fixtures/customTypeWithoutStart/agent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

module.exports = function(agent) {
class ClusterStrategy extends agent.ScheduleStrategy {
constructor(...args) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/plugin/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.logger = {
level: 'debug',
};
2 changes: 0 additions & 2 deletions test/fixtures/worker/app/schedule/interval.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

exports.schedule = {
type: 'worker',
interval: '4s',
Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/worker/app/schedule/sub/cron.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

exports.schedule = {
type: 'worker',
cron: '*/5 * * * * *',
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/worker/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.logger = {
level: 'DEBUG',
consoleLevel: 'DEBUG',
coreLogger: {
level: 'DEBUG',
consoleLevel: 'DEBUG',
},
};
12 changes: 10 additions & 2 deletions test/fixtures/worker/config/plugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
'use strict';

exports.logrotator = true;
exports.onerror = false;
exports.session = false;
exports.i18n = false;
exports.watcher = false;
exports.multipart = false;
exports.security = false;
exports.development = false;
exports.static = false;
exports.jsonp = false;
exports.view = false;
46 changes: 22 additions & 24 deletions test/schedule.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { strict as assert } from 'node:assert';
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { setTimeout as sleep } from 'node:timers/promises';
import { MockApplication } from 'egg-mock';
import _mm from 'egg-mock';
import { mm } from 'egg-mock';

const mm = _mm.default;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

describe('test/schedule.test.ts', () => {
let app: MockApplication;
Expand All @@ -14,19 +16,20 @@ describe('test/schedule.test.ts', () => {
describe('schedule type worker', () => {
it.only('should support interval and cron', async () => {
app = mm.cluster({ baseDir: 'worker', workers: 2, cache: false });
// app.debug();
app.debug();
await app.ready();
await sleep(5000);
const log = getLogContent('worker');
// console.log(log);
assert(contains(log, 'interval') === 1);
assert(contains(log, 'cron') === 1);
console.log(log);
assert.equal(contains(log, 'interval'), 1);
assert.equal(contains(log, 'cron'), 1);

const scheduleLog = getScheduleLogContent('worker');
assert(contains(scheduleLog, 'cron.js executing by app') === 1);
assert(contains(scheduleLog, 'cron.js execute succeed') === 1);
assert(contains(scheduleLog, 'interval.js executing by app') === 1);
assert(contains(scheduleLog, 'interval.js execute succeed') === 1);
console.log(scheduleLog);
assert.equal(contains(scheduleLog, 'cron.js executing by app'), 1);
assert.equal(contains(scheduleLog, 'cron.js execute succeed'), 1);
assert.equal(contains(scheduleLog, 'interval.js executing by app'), 1);
assert.equal(contains(scheduleLog, 'interval.js execute succeed'), 1);
});

it('should support ctxStorage', async () => {
Expand Down Expand Up @@ -177,16 +180,16 @@ describe('test/schedule.test.ts', () => {
await sleep(5000);
const log = getLogContent('customTypeWithoutStart');
// console.log(log);
assert(contains(log, 'cluster_log') === 1);
assert.equal(contains(log, 'cluster_log'), 1);
});

it('should handler error', async () => {
app = mm.cluster({ baseDir: 'customTypeError', workers: 2 });
// app.debug();
await app.ready();
await sleep(1000);
// app.expect('code', 1);
// app.expect('stderr', /should provide clusterId/);
app.expect('code', 1);
app.expect('stderr', /should provide clusterId/);
});
});

Expand All @@ -196,7 +199,7 @@ describe('test/schedule.test.ts', () => {
// app.debug();
await app.ready();
await sleep(3000);
// app.expect('stderr', /schedule\.interval or schedule\.cron or schedule\.immediate must be present/);
app.expect('stderr', /schedule\.interval or schedule\.cron or schedule\.immediate must be present/);
});
});

Expand All @@ -205,7 +208,7 @@ describe('test/schedule.test.ts', () => {
app = mm.cluster({ baseDir: 'typeUndefined', workers: 2 });
await app.ready();
await sleep(3000);
// app.expect('stderr', /schedule type \[undefined\] is not defined/);
app.expect('stderr', /schedule type \[undefined\] is not defined/);
});
});

Expand All @@ -215,7 +218,7 @@ describe('test/schedule.test.ts', () => {
// app.debug();
await app.ready();
await sleep(1000);
// app.expect('stderr', /parse cron instruction\(invalid instruction\) error/);
app.expect('stderr', /parse cron instruction\(invalid instruction\) error/);
});
});

Expand Down Expand Up @@ -257,7 +260,7 @@ describe('test/schedule.test.ts', () => {
interval: 4000,
},
};
// app.agent.schedule.registerSchedule(schedule);
(app as any).agent.schedule.registerSchedule(schedule);
app.scheduleWorker.registerSchedule(schedule as any);

await app.runSchedule(key);
Expand All @@ -282,10 +285,10 @@ describe('test/schedule.test.ts', () => {
interval: 4000,
},
};
// app.agent.schedule.registerSchedule(schedule);
(app as any).agent.schedule.registerSchedule(schedule);
app.scheduleWorker.registerSchedule(schedule as any);

// app.agent.schedule.unregisterSchedule(schedule.key);
(app as any).agent.schedule.unregisterSchedule(schedule.key);
app.scheduleWorker.unregisterSchedule(schedule.key);

let err: any;
Expand Down Expand Up @@ -575,11 +578,6 @@ function getLogContent(name: string) {
return fs.readFileSync(logPath, 'utf8');
}

// function getErrorLogContent(name) {
// const logPath = path.join(__dirname, 'fixtures', name, 'logs', name, 'common-error.log');
// return fs.readFileSync(logPath, 'utf8');
// }

function getAgentLogContent(name: string) {
const logPath = path.join(__dirname, 'fixtures', name, 'logs', name, 'egg-agent.log');
return fs.readFileSync(logPath, 'utf8');
Expand Down

0 comments on commit 7208139

Please sign in to comment.