我正在使用Express创建Node.js服务器。服务器连接到PostgreSQL数据库。我正在学习使用Mocha进行测试,但现在测试在" beforeEach"之前超时。钩。这很奇怪,因为" beforeEach"钩子在某些it('should ...')
个案例中成功完成,但在以后的情况下会超时。
我的测试现在分为两个文件:test/events.js
和test/announcements.js
。仅当我运行所有测试(两个文件)时才会发生超时错误。如果我单独运行这些,一切正常。
以下是我单独运行测试时得到的结果:
$ mocha --timeout 30000 "test/announcements.js"
Announcements
GET /api/announcements/list 200 84.757 ms - 175
✓ should GET all the announcements (271ms)
PUT /api/announcements/create 200 116.172 ms - 16
GET /api/announcements/list 200 18.715 ms - 238
✓ should PUT a new announcement (211ms)
GET /api/announcements/list 200 22.114 ms - 175
POST /api/announcements/edit 200 30.060 ms - 16
GET /api/announcements/list 200 9.408 ms - 153
✓ should POST an edit to existing announcement (108ms)
GET /api/announcements/list 200 22.030 ms - 175
POST /api/announcements/edit 404 13.532 ms - 49
GET /api/announcements/list 200 18.323 ms - 175
✓ should NOT POST an edit to inexisting announcement (152ms)
GET /api/announcements/list 200 27.353 ms - 175
DELETE /api/announcements/delete 200 15.780 ms - 16
GET /api/announcements/list 200 10.735 ms - 92
✓ should DELETE existing announcement (103ms)
GET /api/announcements/list 200 20.080 ms - 175
DELETE /api/announcements/delete 404 7.138 ms - 49
GET /api/announcements/list 200 5.113 ms - 175
✓ should NOT DELETE inexisting announcement (83ms)
6 passing (1s)
$ mocha --timeout 30000 "test/announcements.js"
Events
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 177.934 ms - 109
✓ should GET all the events (371ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
PUT /api/events/create 200 109.243 ms - 16
GET /api/events/list 200 10.463 ms - 144
✓ should PUT a new event (201ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 7.600 ms - 109
POST /api/events/edit 200 33.369 ms - 16
GET /api/events/list 200 5.774 ms - 97
✓ should POST an edit to existing event (89ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 10.483 ms - 109
POST /api/events/edit 404 12.057 ms - 42
GET /api/events/list 200 10.789 ms - 109
✓ should NOT POST an edit to inexisting event (99ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 10.907 ms - 109
DELETE /api/events/delete 200 16.145 ms - 16
GET /api/events/list 200 11.006 ms - 63
✓ should DELETE existing event (105ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 27.856 ms - 109
DELETE /api/events/delete 404 17.645 ms - 42
GET /api/events/list 200 16.777 ms - 109
✓ should NOT DELETE inexisting event (161ms)
6 passing (2s)
但如果我运行所有测试:
$ mocha --timeout 30000 "test/announcements.js" "test/events.js"
Announcements
GET /api/announcements/list 200 84.049 ms - 175
✓ should GET all the announcements (270ms)
PUT /api/announcements/create 200 132.603 ms - 16
GET /api/announcements/list 200 15.295 ms - 238
✓ should PUT a new announcement (256ms)
GET /api/announcements/list 200 32.020 ms - 175
POST /api/announcements/edit 200 24.557 ms - 16
GET /api/announcements/list 200 7.385 ms - 153
✓ should POST an edit to existing announcement (142ms)
GET /api/announcements/list 200 21.938 ms - 175
POST /api/announcements/edit 404 14.570 ms - 49
GET /api/announcements/list 200 26.202 ms - 175
✓ should NOT POST an edit to inexisting announcement (110ms)
GET /api/announcements/list 200 34.236 ms - 175
DELETE /api/announcements/delete 200 15.140 ms - 16
GET /api/announcements/list 200 10.347 ms - 92
✓ should DELETE existing announcement (108ms)
GET /api/announcements/list 200 27.241 ms - 175
DELETE /api/announcements/delete 404 18.707 ms - 49
GET /api/announcements/list 200 5.255 ms - 175
✓ should NOT DELETE inexisting announcement (95ms)
Events
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 122.535 ms - 109
✓ should GET all the events (137ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
PUT /api/events/create 200 11.871 ms - 16
GET /api/events/list 200 17.677 ms - 144
✓ should PUT a new event (87ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 7.059 ms - 109
POST /api/events/edit 200 9.469 ms - 16
GET /api/events/list 200 11.117 ms - 97
✓ should POST an edit to existing event (70ms)
DEBUG: "beforeEach" hook started
DEBUG: "beforeEach" hook connected
DEBUG: "beforeEach" hook queried 1
DEBUG: "beforeEach" hook queried 2
GET /api/events/list 200 14.007 ms - 109
POST /api/events/edit 404 10.613 ms - 42
GET /api/events/list 200 7.941 ms - 109
✓ should NOT POST an edit to inexisting event (82ms)
DEBUG: "beforeEach" hook started
1) "before each" hook for "should DELETE existing event"
10 passing (32s)
1 failing
1) Events "before each" hook for "should DELETE existing event":
Error: Timeout of 30000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
DEBUG: "beforeEach" hook ...
行来自我的test/events.js
代码(下方)。我注意到在失败之前"在每个"之前hook它只打印行DEBUG: "beforeEach" hook started
,但不打印行... hook connected
。那么这些连接到Postgres数据库有什么问题吗?
我检查了我的PostgreSQL日志文件,发现这个超时错误会导致该行返回日志:
LOG: could not receive data from client: Connection reset by peer
那么,发生了什么!
以下是test/events.js
的代码:
let chai = require('chai');
let chaiHttp = require('chai-http');
var pg = require('pg');
let server = require('../app.js');
let should = chai.should();
chai.use(chaiHttp);
describe('Events', () => {
beforeEach((done) => { // Create some data to the database
try {
console.log('DEBUG: "beforeEach" hook started');
pg.connect(process.env.DATABASE_URL, function(err, client) {
console.log('DEBUG: "beforeEach" hook connected');
if (err) {
console.log(err);
done(err);
} else {
client.query('DELETE FROM events', function(err, result) {
console.log('DEBUG: "beforeEach" hook queried 1');
if (err) {
console.log(err);
done(err);
} else {
client.query(' \
INSERT INTO events (title) VALUES \
(\'Fantastic title!\'), \
(\'Another fantastic title!\')', function(err, result) {
console.log('DEBUG: "beforeEach" hook queried 2');
if (err) {
console.log(err);
done(err);
} else {
done();
}
});
}
});
}
});
} catch (err) {
console.log(err);
}
});
it('should GET all the events', (done) => {
chai.request(server)
.get('/api/events/list')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.an('object');
res.body.success.should.equal(true);
res.body.data.should.be.a('array');
res.body.data.length.should.be.eql(2);
res.body.data[0].should.be.an('object');
res.body.data[0].title.should.equal('Fantastic title!');
res.body.data[1].should.be.an('object');
res.body.data[1].title.should.equal('Another fantastic title!');
done();
});
});
// There are 5 more very similar it('should ...') cases.
// I think they aren't important but tell me if I need to include them too.
});
答案 0 :(得分:0)
圣诞快乐
我明白了,你正在做的是为一个进程创建一个新实例,因此执行长测试用例会产生超时错误。你可以做的是为这个过程的每个实例分配一些池大小: -
pg.defaults.poolSize
每次执行查询时,还要再连接到数据库。 put done();在client.query行之后。