Skip to content

Commit

Permalink
fix: ipc not work with worker_threads mode (#5210)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjfkai authored Jun 15, 2023
1 parent 27a4942 commit 03c8cf7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/core/messenger/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const debug = require('util').debuglog('egg:util:messenger:ipc');
const is = require('is-type-of');
const workerThreads = require('worker_threads');
const sendmessage = require('sendmessage');
const EventEmitter = require('events');

Expand All @@ -22,6 +23,9 @@ class Messenger extends EventEmitter {
});
this._onMessage = this._onMessage.bind(this);
process.on('message', this._onMessage);
if (!workerThreads.isMainThread) {
workerThreads.parentPort.on('message', this._onMessage);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"koa-override": "^3.0.0",
"ms": "^2.1.3",
"on-finished": "^2.4.1",
"sendmessage": "^1.1.0",
"sendmessage": "^2.0.0",
"urllib": "^2.33.0",
"urllib-next": "^3.9.0",
"utility": "^1.17.0",
Expand Down
27 changes: 27 additions & 0 deletions test/lib/core/messenger/ipc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,31 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
}
});
});

describe('worker_threads mode', () => {
let app;
before(() => {
mm.env('default');
app = utils.cluster('apps/messenger-app-agent', { workers: 1, startMode: 'worker_threads' });
app.coverage(false);
return app.ready();
});
after(() => app.close());

it('app should accept agent message', done => {
setTimeout(() => {
assert(count(app.stdout, 'agent2app') === 1);
assert(count(app.stdout, 'app2app') === 1);
assert(count(app.stdout, 'agent2agent') === 1);
assert(count(app.stdout, 'app2agent') === 1);
done();
}, 500);

function count(data, key) {
return data.split('\n').filter(line => {
return line.indexOf(key) >= 0;
}).length;
}
});
});
});

0 comments on commit 03c8cf7

Please sign in to comment.