poseidon-memcached
Version:
A future wrapper around the Node Memcached driver using Poseidon.
87 lines (64 loc) • 2.78 kB
text/coffeescript
Q = require 'q'
{Driver} = require '../index'
Memcached = require 'memcached'
describe 'The Driver class', ->
beforeEach ->
Driver.reset()
return
it 'has a static internal configuration object', ->
expect(Driver._configuration).to.deep.equal {}
it 'has an internal cache of connections', ->
expect(Driver._connections).to.deep.equal {}
describe 'the reset function', ->
beforeEach ->
Driver.configure('test', { servers: 'localhost:11212' })
Driver.openConnection('test')
it 'resets the driver connections and configuration caches', ->
Driver.reset()
expect(Driver._connections).to.deep.equal {}
expect(Driver._configuration).to.deep.equal {}
it 'closes all open connections', ->
sinon.spy(Driver, 'closeConnection')
Driver.configure('foo', { servers: 'localhost:11212' })
Driver.openConnection('foo')
Driver.reset()
expect(Driver.closeConnection).to.have.been.calledTwice
describe 'the configure function', ->
it 'saves the given configuration', ->
config =
servers: ['mordor:10000']
Driver.configure('mordor', config)
expect(Driver._configuration['mordor']).to.deep.equal config.servers
it 'throws an error if no configuration is provided', ->
invalidCall = ->
Driver.configure('test')
expect(invalidCall).to.throw Error, /Configuration object required/
describe 'the openConnection function', ->
beforeEach ->
Driver.configure('test', { servers: 'localhost:11212' })
afterEach ->
Driver.reset()
it 'calls the Memcached function and caches it', ->
conn = Driver.openConnection('test')
expect(conn).to.be.an.instanceof Memcached
expect(Driver._connections['test']).to.deep.equal conn
it 'returns the cached connection if it already exists', ->
Driver.openConnection('test')
conn = Driver.openConnection('test')
it 'throws an error if the connection was not configured', ->
invalidCall = ->
Driver.openConnection('foo')
expect(invalidCall).to.throw Error, /Connection not configured/
describe 'the closeConnection function', ->
it 'throws an error if the connection does not exist', ->
invalidCall = ->
Driver.closeConnection('foo')
expect(invalidCall).to.throw Error, /Connection does not exist/
it 'deletes the connection from the cache and calls the db close function', ->
db = null
Driver.configure('test', { servers: 'localhost:11212' })
Driver.openConnection('test')
sinon.spy(Memcached.prototype, 'end')
Driver.closeConnection('test')
expect(Driver._connections['test']).to.be.undefined
expect(Memcached.prototype.end).to.have.been.calledOnce