diff --git a/lib/core/messenger/local.js b/lib/core/messenger/local.js index b6d64d6428..c8a1e51362 100644 --- a/lib/core/messenger/local.js +++ b/lib/core/messenger/local.js @@ -88,29 +88,31 @@ class Messenger extends EventEmitter { * @return {Messenger} this */ send(action, data, to) { - const { egg } = this; - let application; - let agent; - let opposite; - if (egg.type === 'application') { - application = egg; - agent = egg.agent; - opposite = agent; - } else { - agent = egg; - application = egg.application; - opposite = application; - } - // use nextTick to keep it async as IPC messenger process.nextTick(() => { - if (application.messenger && (to === 'application' || to === 'both')) { + const { egg } = this; + let application; + let agent; + let opposite; + + if (egg.type === 'application') { + application = egg; + agent = egg.agent; + opposite = agent; + } else { + agent = egg; + application = egg.application; + opposite = application; + } + if (!to) to = egg.type === 'application' ? 'agent' : 'application'; + + if (application && application.messenger && (to === 'application' || to === 'both')) { application.messenger._onMessage({ action, data }); } - if (agent.messenger && (to === 'agent' || to === 'both')) { + if (agent && agent.messenger && (to === 'agent' || to === 'both')) { agent.messenger._onMessage({ action, data }); } - if (opposite.messenger && to === 'opposite') { + if (opposite && opposite.messenger && to === 'opposite') { opposite.messenger._onMessage({ action, data }); } }); diff --git a/test/lib/core/messenger/local.test.js b/test/lib/core/messenger/local.test.js index 7cf99f7bcc..73c4a8dcc7 100644 --- a/test/lib/core/messenger/local.test.js +++ b/test/lib/core/messenger/local.test.js @@ -3,6 +3,7 @@ const utils = require('../../../utils'); const pedding = require('pedding'); const assert = require('assert'); +const mm = require('egg-mock'); describe('test/lib/core/messenger/local.test.js', () => { let app; @@ -14,6 +15,7 @@ describe('test/lib/core/messenger/local.test.js', () => { after(() => app.close()); afterEach(() => { + mm.restore(); app.messenger.close(); app.agent.messenger.close(); }); @@ -161,4 +163,37 @@ describe('test/lib/core/messenger/local.test.js', () => { app.agent.messenger.sendTo(process.pid, 'sendTo-event', { foo: 'bar' }); }); }); + + describe('send()', () => { + it('app.messenger.send should not throw when app.agent not exist', () => { + mm(app, 'agent', undefined); + app.messenger.send('send-event', { foo: 'bar' }); + }); + + it('app.messenger.send should work', done => { + app.agent.messenger.once('send-event', msg => { + assert.deepEqual(msg, { foo: 'bar' }); + done(); + }); + + app.messenger.once('send-event', () => { + throw new Error('should not emit on app'); + }); + + app.messenger.send('send-event', { foo: 'bar' }); + }); + + it('agent.messenger.send should work', done => { + app.agent.messenger.once('send-event', () => { + throw new Error('should not emit on agent'); + }); + + app.messenger.once('send-event', msg => { + assert.deepEqual(msg, { foo: 'bar' }); + done(); + }); + + app.agent.messenger.send('send-event', { foo: 'bar' }); + }); + }); });