2016-12-24 47 views
-1

我使用Express創建Node.js服務器。服務器連接到PostgreSQL數據庫。我正在學習使用Mocha進行測試,但現在測試在「beforeEach」掛鉤中超時。這很奇怪,因爲「beforeEach」掛鉤在某些it('should ...')情況下成功完成,但在後面的情況下超時。在摩卡測試中無法連接PostgreSQL很多次

我的測試現在有兩個文件:test/events.jstest/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代碼(如下)。我注意到,在「beforeEach」掛鉤失敗時,它僅打印行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

聖誕

我知道了,你正在做的是這麼長時間執行測試用例的過程創建一個新的實例給超時錯誤。你可以做一些池大小分配給這樣一個過程的每個實例: -

pg.defaults.poolSize 

還有一件事你每次執行查詢時連接到數據庫。把完成();在client.query之後。

+0

謝謝!將'doneConnect'參數添加到'pg.connect'的回調函數中,然後將'doneConnect();'添加到'done();'這樣的地方,使之成功!聖誕節快樂! – TuomasK

+0

哦,很酷,歡迎先生:) – Codesingh