From 45b2edff207cd952dbc73c3c402d72391b06131c Mon Sep 17 00:00:00 2001 From: popomore Date: Wed, 2 Nov 2016 18:34:25 +0800 Subject: [PATCH] feat: add more env SENDMESSAGE_ONE_PROCESS --- .travis.yml | 4 ++-- README.md | 2 ++ index.js | 2 +- package.json | 17 ++++++++++------- test/sendmessage.test.js | 29 +++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4897594..4d7657c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: node_js node_js: - - '0.11' - - '0.10' + - '4' + - '6' script: "npm run test-travis" after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" diff --git a/README.md b/README.md index febbe53..b1823a2 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,8 @@ If a process is not child process, this will just call `process.emit('message', sendmessage(process, {hello: 'this is a message to master'}); ``` +You can switch to `process.emit('message', message)` using `process.env.SENDMESSAGE_ONE_PROCESS` + ## Test ```bash diff --git a/index.js b/index.js index dffaf6f..db8a9db 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ module.exports = function send(child, message) { return setImmediate(child.emit.bind(child, 'message', message)); } - if (IS_NODE_DEV_RUNNER) { + if (IS_NODE_DEV_RUNNER || process.env.SENDMESSAGE_ONE_PROCESS) { // run with node-dev, only one process // https://github.com/node-modules/sendmessage/issues/1 return setImmediate(child.emit.bind(child, 'message', message)); diff --git a/package.json b/package.json index 9f13916..0604a4c 100644 --- a/package.json +++ b/package.json @@ -15,16 +15,15 @@ "cnpm": "npm install --registry=https://registry.npm.taobao.org", "contributors": "contributors -f plain -o AUTHORS" }, - "dependencies": { - - }, + "dependencies": {}, "devDependencies": { "autod": "*", "contributors": "*", - "should": "*", - "jshint": "*", "istanbul": "*", - "mocha": "*" + "jshint": "*", + "mm": "^1.5.1", + "mocha": "*", + "should": "*" }, "homepage": "https://github.com/node-modules/sendmessage", "repository": { @@ -37,7 +36,11 @@ "email": "fengmk2@gmail.com" }, "keywords": [ - "sendmessage", "send", "cluster", "message", "channel closed" + "sendmessage", + "send", + "cluster", + "message", + "channel closed" ], "engines": { "node": ">= 0.10.0" diff --git a/test/sendmessage.test.js b/test/sendmessage.test.js index ac9d773..1fcb24a 100644 --- a/test/sendmessage.test.js +++ b/test/sendmessage.test.js @@ -18,9 +18,12 @@ var path = require('path'); var should = require('should'); var childprocess = require('child_process'); var cluster = require('cluster'); +var mm = require('mm'); var sendmessage = require('../'); describe('sendmessage.test.js', function () { + afterEach(mm.restore); + describe('single process', function () { it('should emit message when process is not child process', function (done) { process.once('message', function (message) { @@ -146,4 +149,30 @@ describe('sendmessage.test.js', function () { }); }); }); + + it('should emit when SENDMESSAGE_ONE_PROCESS = true', function(done) { + var childfile = path.join(__dirname, 'child.js'); + var child = childprocess.fork(childfile); + mm(process.env, 'SENDMESSAGE_ONE_PROCESS', 'true'); + var msg; + child.once('message', function (message) { + message.should.eql({ + from: 'child', + hi: 'this is a message send to master' + }); + + sendmessage(child, { + from: 'master', + reply: 'this is a reply message send to child' + }); + + child.once('message', function(msg) { + msg.should.eql({ + from: 'master', + reply: 'this is a reply message send to child', + }); + done(); + }); + }); + }) });