同时,节点以状态1退出.这终止了Teamcity,导致其认为测试失败 [英] Concurrently node exits with status 1. This halts Teamcity leading it to believe that the tests failed
问题描述
我试图用concurrently
一次运行两个脚本.基本命令如下所示:
I am trying to run two scripts at once with concurrently
. The basic command looks something like this:
concurrently -k --success first "node ./tools/mock-webapi/mock-webapi.js" "npm run test-single-run"
依次致电(本地):
"test-single-run": "karma start --single-run --browsers ChromeHeadless"
或在远程(teamcity主机)上:
Or on remote (teamcity host):
"test-teamcity": "karma start --reporters teamcity --single-run --browsers ChromeHeadless",
测试运行得很好(本地和远程).但是,我一直得到退出代码1.即使使用concurrently -k --success first
,即使使用--success first
,我仍然会得到code 1
.
The tests run just fine (local & remote). However, I keep getting exit code 1. Even if I use concurrently -k --success first
I still get a code 1
even with --success first
.
[1] 09 05 2018 17:56:54.032:WARN [launcher]: ChromeHeadless was not killed in 2000 ms, sending SIGKILL.
[1] npm run test-single-run exited with code 0
--> Sending SIGTERM to other processes..
[0] node ./tools/mock-webapi/mock-webapi.js exited with code 1
我尝试了各种方法使json-server
正常接收此信号.似乎什么都没用.
I tried various ways for json-server
to gracefully receive this signal. Nothing seems to work.
mock-webapi.js
process.on('SIGTERM', function (code) {
console.log('Handle SIGTERM', process.pid, code);
exitCode = 0;
server.close(function () {
process.exit(0);
});
});
process.on('SIGKILL', function (code) {
console.log('SIGKILL received...', code);
exitCode = 0;
server.close(function () {
process.exit(0);
});
});
process.on('SIGINT', function (code) {
console.log('SIGINT received...', code);
exitCode = 0;
server.close(function () {
process.exit(0);
});
});
推荐答案
最后找到了解决此问题的方法.我写了一个小脚本,将模拟webapi和业力测试作为子进程运行.
Finally found a solution to this problem. I wrote a little script that runs the mock webapi and the karma tests as child processes.
test-single-run-single-process.js
const spawn = require('cross-spawn');
/**
* Running the tests requires a mock webapi which is gladly provided by json-server.
* Running the tests in teamcity requires that everything is executed from one terminal.
* Different mock data sets can be easily used if the mockapi is a child process.
* We do not need to keep the mockWebapi alive between trials.
*
* After all the tests have succeeded we then close the json-server by sending (SIGINT).
* Finally the process will exit with code 0, which means for TeamCity that all is OK.
* Now it can proceed with the next build step.
* So basically we can run the tests both standalone and in one single terminal.
* Notice that the mockWebapi is a forked process so we can send messages to it.
*
* <!> Failed approach - Closing the mockwebapi
* Using kill command leaves no option for gracefull shutdown.
* SIGINT or SIGTERM signals are not received by the mockWebapi.
* The server will stay active keeping the 3000 port busy.
*
* mockWebapi.kill('SIGINT');
*/
const fork = require('child_process').fork
const mockWebapi = fork('./tools/mock-webapi/mock-webapi.js')
const karma = spawn(
`karma start --single-run --browsers ChromeHeadless`,
[], { stdio: 'inherit' }
);
// 1) Listen to karma exit
karma.on('close', (code, signal) => {
console.log('Karma closed. Code:', code, 'Signal:', signal);
code === 0 && gracefullyCloseMockWebapi(true);
});
karma.on('error', (code, signal) => {
console.log('Karma error. Code:', code, 'Signal:', signal);
gracefullyCloseMockWebapi(false);
});
let gracefullyCloseMockWebapi = (testsCompletedOk) => {
console.log('Gracefuly close webapi. Tests completed ok:', testsCompletedOk);
mockWebapi.send({ testsCompletedOk });
};
// 2) Finish the job, pass the exit code from mockWeabpi to the command line
mockWebapi.on('close', (code, signal) => {
console.log('Mock webapi closed. Code:', code, 'Signal:', signal);
process.exit(code);
});
mock-webapi.js
// Some project specific work is done before starting the server
/**
* <!> Failed approach - concurrently
* Dispatching the following command will exit with code 1.
* The SIGTERM or SIGINT handlers are not called after the tests are done.
* concurrently --kill-others --success first
* "node ./tools/mock-webapi/mock-webapi.js" "npm run test-single-run"
*
* process.on('SIGINT', (code) => {
* server.close(function () {
* process.exit(0);
* });
* });
*
* <!> Working approach
* Spawning a process child and sending it a message to self-close.
* Double check it with "echo Exit Code is %errorlevel%" after the processes terminated.
*/
function setupJsonServer(mocks, routes) {
let server = jsonServer.create(),
port = 3000;
const router = jsonServer.router(mocks);
server.use(jsonServer.defaults());
server.use(jsonServer.rewriter(routes));
server.use(router);
server.listen(port, () => {
console.log(`JSON Server is running at ${port}`)
console.log('Process id is', process.pid);
});
process.on('message', function ({ testsCompletedOk }) {
console.log('Mockwebapi will terminate. Tests completed ok', testsCompletedOk)
process.exit(testsCompletedOk === true ? 0 : 1)
});
}
其他阅读内容:
- https://medium .freecodecamp.org/node-js-child-processes-您需要知道的一切-e69498fe970a
- https://github.com/moxystudio/node-cross-spawn
- https://medium.com/@NorbertdeLangen/communication-between -nodejs-processes-4e68be42b917
- node.js子进程-生成&叉子
- 将大型数组传递给节点子进程
- https://azimi.me/2014/12/31/kill-child_process-node-js.html
- 如何在退出时杀死所有子进程?
- 杀死节点进程时杀死所有child_process
- 在Node.js中的两个不同进程之间进行通信
- https://medium.freecodecamp.org/node-js-child-processes-everything-you-need-to-know-e69498fe970a
- https://github.com/moxystudio/node-cross-spawn
- https://medium.com/@NorbertdeLangen/communicating-between-nodejs-processes-4e68be42b917
- node.js child process - difference between spawn & fork
- Pass large array to node child process
- https://azimi.me/2014/12/31/kill-child_process-node-js.html
- How to kill all child processes on exit?
- kill all child_process when node process is killed
- Communicating between two different processes in Node.js
这篇关于同时,节点以状态1退出.这终止了Teamcity,导致其认为测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!