From 9dfd19eada8bae7be212155a2989d0ccc410e8eb Mon Sep 17 00:00:00 2001 From: killa Date: Wed, 14 Nov 2018 23:30:48 +0800 Subject: [PATCH] fix: triggerServerDidReady should be triggered only once (#3190) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ##### Checklist - [x] `npm test` passes - [x] tests and/or benchmarks are included - [x] commit message follows commit guidelines ##### Affected core subsystem(s) Lifecycle ##### Description of change When worker restart, master process broadcast `egg-ready` event to all workers, so worker should listen `egg-ready` only once. --- lib/egg.js | 2 +- test/lib/egg.test.js | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/egg.js b/lib/egg.js index f10b70af06..7ba0a6e121 100644 --- a/lib/egg.js +++ b/lib/egg.js @@ -58,7 +58,7 @@ class EggApplication extends EggCore { // trigger serverDidReady hook when all app workers // and agent worker is ready - this.messenger.on('egg-ready', () => { + this.messenger.once('egg-ready', () => { this.lifecycle.triggerServerDidReady(); }); diff --git a/test/lib/egg.test.js b/test/lib/egg.test.js index 297d0a1141..dd7006c830 100644 --- a/test/lib/egg.test.js +++ b/test/lib/egg.test.js @@ -345,6 +345,26 @@ describe('test/lib/egg.test.js', () => { .expect(200); }); }); + + describe('egg-ready', () => { + let app; + before(() => { + app = utils.app('apps/demo'); + }); + after(() => app.close()); + + it('should only trigger once', async () => { + let triggerCount = 0; + mm(app.lifecycle, 'triggerServerDidReady', () => { + triggerCount++; + }); + await app.ready(); + app.messenger.emit('egg-ready'); + app.messenger.emit('egg-ready'); + app.messenger.emit('egg-ready'); + assert(triggerCount === 1); + }); + }); }); function readJson(p) {