私はNode.jsとExpressでAPIを開発しています。私はユニットテストを書くためにMochaとSupertestを使っています。私は、私のエラー処理がうまくいっているかどうかを見るために、ほぼ無作為のパラメータですべてのルートをテストするテストの大きなファイルを持っています。タイムアウトが超過したのはなぜですか?
私の要求がタイムアウトし始めたまで、すべてが素晴らしかったです。
これは、多かれ少なかれ私のコードです:
var supertest = require("supertest");
var should = require("should");
var server = supertest.agent("http://localhost:3000");
function requestAuth(url, type, auth, params, callback) {
if (params == null) {
server[type](url)
.type('form')
.auth(auth.email, auth.password)
.expect("Content-type",/json/)
.expect(200)
.end(callback);
}
else {
server[type](url)
.send(params)
.auth(auth.email, auth.password)
.type('form')
.expect("Content-type",/json/)
.expect(200)
.end(callback);
}
}
describe('Testing route 1', function() {
describe('Testing param 1 error handling', function() {
it('should return error 1', function(done) {
requestAuth(route1, "post", {email: email, password: password}, {param1: "blahblahblah"},
function(err, res) {
res.body.should.have.property('error');
done();
});
});
it('should return error 2', function(done) {
requestAuth(route1, "post", {email: email, password: password}, {param1: "blahblahblah"},
function(err, res) {
res.body.should.have.property('error');
done();
});
});
// etc
});
describe('Testing param 2 error handling', function() {
it('should return error 3', function(done) {
requestAuth(route1, "post", {email: email, password: password}, {param1: "blahblahblah"},
function(err, res) {
res.body.should.have.property('error');
done();
});
});
it('should return error 4', function(done) {
requestAuth(route1, "post", {email: email, password: password}, {param1: "blahblahblah"},
function(err, res) {
res.body.should.have.property('error');
done();
});
});
// etc
});
//etc
});
describe('Testing route 2', function() {
//etc
});
私は多くのテストを持っていることを除いて。 は、ある時点で、私はルート8をテストしていたときに、すべてのテストは、次のメッセージで失敗し始めていることとしましょう:
12) Route 8 Testing Param 1 error handling should return error 1:
Error: timeout of 2000ms exceeded
at null.<anonymous> (/usr/lib/nodejs/mocha/lib/runnable.js:139:19)
at Timer.listOnTimeout (timers.js:92:15)
私は本当にそれを取得しないでください。それ以来、すべてがうまくいった、すべての要求の最後に行われた、良いことがあります。ルートは問題ありませんが、サーバ側では何も起こりません。これは本当に変です...
また、もしルート8のテストが奇妙になり、私がルート7のテストをコメントすると、ルート9のテストは間違った動作を開始します。
これはsupertestから来ていると思います。それは過負荷になっている可能性がありますか?どうすればそれを修正できますか?
ご回答いただきありがとうございます。
ええ、私はこれを知っています。私の要求は2秒以上かかることはなく、APIに到達することさえできません。 – Devz
大規模なスイートごとに、サーバー初期化をbeforeステートメントに移そうとしましたか?これは、非同期呼び出しが期待どおりに動作し、すべての要求が完了する前にサーバーが切断されているため、奇妙な原因となる可能性があります。 – gelliott181
これは、テストの数に依存しているので、解くのは少し難しいです。これは非同期の問題のように聞こえ、Expressは実行していない場合を除き、確実に応答しなくてはなりません。テストが失敗する前に – gelliott181