如何使用摩卡测试一个集群的Express应用程序? [英] How to test a clustered Express app with Mocha?
问题描述
这是我的集群快速应用程序的简化版本:
Here is a simplified version of my cluster Express app:
/index.js
module.exports = process.env.CODE_COV
? require('./lib-cov/app')
: require('./lib/app');
/lib/app.js
var cluster = require('cluster'),
express = require('express'),
app = module.exports = express.createServer();
if (cluster.isMaster) {
// Considering I have 4 cores.
for (var i = 0; i < 4; ++i) {
cluster.fork();
}
} else {
// do app configurations, then...
// Don't listen to this port if the app is required from a test script.
if (!module.parent.parent) {
app.listen(8080);
}
}
/test/test1.js
/test/test1.js
var app = require('../');
app.listen(7777);
// send requests to app, then assert the response.
问题:
-
var app = require('../');
将不会在此集群环境中工作。哪些工作应用程序应该返回?是否应该返回集群对象而不是Express应用程序? - 现在,显然设置测试脚本中的端口将无法正常工作。您将如何将测试脚本中的端口设置到一组应用程序?
- 如何将请求发送到此应用集群?
var app = require('../');
will not work in this cluster environment. Which of the worker apps should it return? Should it return the cluster object instead of an Express app?- Now, obviously setting the port in the test script will not work. How would you set a port within a test script to a cluster of apps?
- How would you send requests to this cluster of apps?
我可以想到的唯一解决方案是有条件地关闭群集功能,并且只有在应用程序从测试脚本中请求时才运行一个应用程序( if(module .parent.parent)...
)
The only solution I can think of is to conditionally turn off the clustering feature and run only one app if the app is requested from a test script (if (module.parent.parent) ...
).
使用摩卡测试集群Express应用程序的任何其他方法?
Any other way to test a clustered Express app with Mocha?
推荐答案
自从我发布了这个问题以来已经很久了。因为没有人回答,我会自己回答这个问题。
It's been quite a long time since I have posted this question. Since no one has answered, I will answer to this question myself.
我一直保持 /index.js :
module.exports = process.env.CODE_COV
? require('./lib-cov/app')
: require('./lib/app');
在启动集群的 /lib/app.js 中,我有以下代码。简而言之,我只在非测试环境中启动集群。在测试环境中,集群不启动,但只有一个应用程序/工作程序本身启动,如 cluster.isMaster&&& !module.parent.parent
条件。
In /lib/app.js which starts the cluster, I have the following code. In brief, I start the cluster only in non-test environment. In test environment the cluster is not started but only one app/worker itself is started as defined in the cluster.isMaster && !module.parent.parent
condition.
var cluster = require('cluster'),
express = require('express'),
app = module.exports = express.createServer();
if (cluster.isMaster && !module.parent.parent) {
// Considering I have 4 cores.
for (var i = 0; i < 4; ++i) {
cluster.fork();
}
} else {
// do app configurations, then...
// Don't listen to this port if the app is required from a test script.
if (!module.parent.parent) {
app.listen(8080);
}
}
在上述情况下!只有当应用程序未由测试脚本启动时,才会将module.parent.parent
评估为真实对象。
In the above case !module.parent.parent
will be evaluated as a truthful object only if the application was not started by a test script.
-
module
是目前的 /lib/app.js 脚本。 -
module.parent
是其父 /index.js 脚本。 -
module.parent.parent如果应用程序是通过
是node index.js
直接启动的,未定义
。 >
-
module.parent.parent
是测试脚本,如果应用程序是通过其中一个脚本启动的。
module
is the current /lib/app.js script.module.parent
is its parent /index.js script.module.parent.parent
isundefined
if the application was started directly vianode index.js
.module.parent.parent
is the test script if the application was started via one of the scripts.
因此,我可以安全地启动脚本,我可以在其中设置自定义端口。
Thus, I can safely start the script where I can set a custom port.
/ test /test1.js
var app = require('../');
app.listen(7777);
// send requests to app, then assert the response.
同时如果我需要实际运行应用程序,即不进行测试,那么我运行 node index.js
,它将启动应用程序集群。
At the same time if I need to run the application in real, i.e. not for testing, then I run node index.js
and it will start up the cluster of applications.
这篇关于如何使用摩卡测试一个集群的Express应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!