茉莉花打开和关闭Db连接

时间:2016-09-23 07:54:58

标签: node.js unit-testing jasmine gulp-jasmine

我有多个单元测试文件。我想避免连接它们。我需要关闭mongoose连接才能gulp-jasmineexit。我还想避免将连接处理放入it块,因为它不属于那里。

如果我将连接/断开连接功能移动到beforeAllafterAll,例如

代码

单元测试

describe "a unit test"
  beforeAll (done)->
    db = testSettings.connectDB (err)->
      throw err if err?
      done()

  ...

  afterAll (done)->
    testSettings.disconnectDB (err)->
      throw err if err?
      done()

然后Jasmine在beforeAll可以正确断开数据库之前执行下一个描述afterAll

(Jasmine Docs)[http://jasmine.github.io/2.1/introduction.html#section-Setup_and_Teardown]

  

但是,请小心使用beforeAll和afterAll!既然他们不是   在规格之间重置,很容易在两者之间意外泄漏   你的规格,以便他们错误地通过或失败。

连接功能

connections = 0

exports.connectDB = (callback) ->
  configDB = require(applicationDir + 'backend/config/database.js')(environment)

  if connections == 0
    mongoose.connect configDB.url,{auth:{authdb:configDB.authdb}}, (err)->
      if (err)
        console.log(err)
        return callback err

    db = mongoose.connection
    db.on 'error', console.error.bind(console, 'connection error:')

    db.once 'open', () ->
      connections++
      return callback null, db
      #console.log "Database established"
      #Delete all data and seed new data

  else
    db = mongoose.connection
    return callback null, db

exports.disconnectDB = (callback) ->
  mongoose.disconnect (err)->
    return callback err if err?
    connections--
    return callback()

编辑:

收听断开连接事件也不起作用: exports.disconnectDB =(回调) - >   console.log" DISCONNECTING FROM DB",mongoose.connection.readyState   mongoose.disconnect(错误) - >     返回错误如果错误?     connections--     console.log"应该断开连接",mongoose.connection.readyState#不是因为state是3 0>断开     return callback()

mongoose.connection'已断开',() - >     console.log" DIS",mongoose.connection.readyState     return callback()

错误

{ Error: Trying to open unclosed connection.

问题

如何正确开放&关闭我与gulp-jasmine的连接?

1 个答案:

答案 0 :(得分:0)

这似乎有用,但仍然想知道为什么我无法使用disconnected事件:

connections = 0
disConnectionRetries = 0
MAX_DISCONNECTION_RETRIES = 10

connectDB = (callback) ->
  console.log "CONNECTING TO DB", mongoose.connection.connectionState
  configDB = require(applicationDir + 'backend/config/database.js')(environment)

  if connections == 0
    mongoose.connect configDB.url,{auth:{authdb:configDB.authdb}}, (err)->
      if (err)
        console.log(err)
        return callback err

    db = mongoose.connection
    db.on 'error', console.error.bind(console, 'connection error:')

    db.once 'open', () ->
      connectionRetries = 0
      connections++
      return callback null, db
      #console.log "Database established"
      #Delete all data and seed new data

  else
    db = mongoose.connection
    return callback null, db

exports.disconnectDB = (callback) ->
  console.log "DISCONNECTING FROM DB", mongoose.connection.readyState
  mongoose.disconnect (err)->
    return callback err if err?
    connections--
    isDisconnected(callback)

isDisconnected = (callback)->
  console.log "SHOULD BE DISCONNETCED", mongoose.connection.readyState
  throw new Error "Cannot disconnect more than #{MAX_DISCONNECTION_RETRIES} retries" if disConnectionRetries > MAX_DISCONNECTION_RETRIES
  if mongoose.connection.readyState == 0
    disConnectionRetries = 0
    return callback()
  else
    disConnectionRetries++
    return setTimeout ()->
      isDisconnected(callback)
    , 50
相关问题