Skip to content

Commit

Permalink
add test case to reproduce #57
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Aug 10, 2012
1 parent c379ba2 commit 4d8e142
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
4 changes: 3 additions & 1 deletion lib/mongoskin/db.js
Expand Up @@ -90,12 +90,14 @@ SkinDb.prototype.open = function (callback) {
this.db = db;
this.state = STATE_OPEN;
} else {
db && db.close();
// close the openning connection.
this._dbconn.close();
this.db = null;
this.state = STATE_CLOSE;
}
this.emitter.emit('open', err, this.db);
}.bind(this);
console.log('state:%d', this.state);
callback && this.emitter.once('open', callback);
this.state = STATE_OPENNING;
this._dbconn.open(function (err, db) {
Expand Down
36 changes: 26 additions & 10 deletions test/db.js
Expand Up @@ -13,7 +13,9 @@
*/

var mongoskin = require('../');
var pedding = require('./utils/pedding');
var should = require('should');
var constant = require('../lib/mongoskin/constant');

describe('db.js', function () {

Expand Down Expand Up @@ -87,12 +89,32 @@ describe('db.js', function () {

it('should open a database connection with user auth fail', function (done) {
var db2 = mongoskin.db('test:test@localhost/mongoskin_test');
done = pedding(2, done);
db2.state.should.equal(constant.STATE_CLOSE);
db2.open(function (err, db) {
should.exist(err);
err.should.have.property('message', 'auth fails');
err.should.have.property('name', 'MongoError');
should.not.exist(db);
db2.state.should.equal(constant.STATE_CLOSE);
// open again
db2.open(function (err, db) {
should.exist(err);
err.should.have.property('message', 'auth fails');
err.should.have.property('name', 'MongoError');
should.not.exist(db);
db2.state.should.equal(constant.STATE_CLOSE);
db2.open(function (err, db) {
should.exist(err);
err.should.have.property('message', 'auth fails');
err.should.have.property('name', 'MongoError');
should.not.exist(db);
db2.state.should.equal(constant.STATE_CLOSE);
done();
});
});
});
db2.state.should.equal(constant.STATE_OPENNING);
db2.open(function (err, db) {
should.exist(err);
err.should.have.property('message', 'auth fails');
Expand All @@ -104,15 +126,12 @@ describe('db.js', function () {

it('should open 100 times ok', function (done) {
var db3 = mongoskin.db('localhost/mongoskin_test');
var counter = 0;
done = pedding(100, done);
for (var i = 0; i < 100; i++) {
db3.open(function (err, db) {
should.not.exist(err);
should.exist(db);
counter++;
if (counter === 100) {
done();
}
done();
});
}
});
Expand All @@ -139,15 +158,12 @@ describe('db.js', function () {

it('should close 100 times ok', function (done) {
var db3 = mongoskin.db('localhost/mongoskin_test');
var counter = 0;
done = pedding(100, done);
db.open();
for (var i = 0; i < 100; i++) {
db3.close(function (err) {
should.not.exist(err);
counter++;
if (counter === 100) {
done();
}
done();
});
}
});
Expand Down
25 changes: 25 additions & 0 deletions test/utils/pedding.js
@@ -0,0 +1,25 @@
/*!
* mongoskin - test/utils/pedding.js
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/

"use strict";

/**
* Module dependencies.
*/

module.exports = function (n, fn) {
var called = false;
return function (err) {
if (called) {
return;
}
if (err) {
called = true;
return fn(err);
}
--n || fn();
};
};

0 comments on commit 4d8e142

Please sign in to comment.