Comparing version 0.2.2 to 0.2.3
@@ -62,3 +62,2 @@ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | ||
Connection.prototype.query = function (sql, timeout, callback) { | ||
if ((typeof sql) === 'object' && sql.params) { | ||
@@ -71,2 +70,5 @@ sql = this.format(sql.sql, sql.params); | ||
return this._conn.query(sql, function (e, r) { | ||
if (e && e.fatal && _self._flag > -1) { | ||
_self.emit('error', e); | ||
} | ||
callback(e ? _self._error(e) : null, r); | ||
@@ -78,6 +80,6 @@ }); | ||
callback(_self._error('QueryTimeout', 'Mysql query timeout after ' + timeout + ' ms')); | ||
callback = function (e, r) { | ||
_self.emit('late', e, r, sql); | ||
}; | ||
_self.emit('timeout', sql); | ||
callback = function () {}; | ||
}, timeout); | ||
_self._conn.query(sql, function (e, r) { | ||
@@ -87,2 +89,6 @@ clearTimeout(timer); | ||
callback(e ? _self._error(e) : null, r); | ||
if (e && e.fatal && _self._flag > -1) { | ||
_self.emit('error', e); | ||
} | ||
}); | ||
@@ -89,0 +95,0 @@ }; |
@@ -60,32 +60,44 @@ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | ||
var c = Connection.create(config); | ||
c.on('error', function () {}); | ||
c.on('error', function (e) { | ||
heartbeatER(e); | ||
}); | ||
var s = ''; | ||
conns.unshift(c); | ||
(function heartbeat() { | ||
function heartbeat() { | ||
c.query(hbsql, HEARTBEAT_TIMEOUT, function (e, r) { | ||
if (e) { | ||
o.emit('state'); | ||
conns.shift(); | ||
setTimeout(function () { | ||
if (c) { | ||
c.close(); | ||
c = null; | ||
} | ||
startup(o); | ||
}, pause); | ||
pause = Math.min(pause + pause, 60000); | ||
o.emit('error', e); | ||
heartbeatER(e); | ||
} else { | ||
tbeat = setTimeout(heartbeat, 10 * HEARTBEAT_TIMEOUT); | ||
pause = 100; | ||
var t = JSON.stringify(r); | ||
if (t !== s) { | ||
s = t; | ||
o.emit('state', r); | ||
} | ||
heartbeatOK(r); | ||
} | ||
}); | ||
})(); | ||
}; | ||
heartbeat(); | ||
function heartbeatOK(r) { | ||
tbeat = setTimeout(heartbeat, 10 * HEARTBEAT_TIMEOUT); | ||
pause = 100; | ||
var t = JSON.stringify(r); | ||
if (t !== s) { | ||
s = t; | ||
o.emit('state', r); | ||
} | ||
} | ||
function heartbeatER(e) { | ||
o.emit('state'); | ||
conns.shift(); | ||
setTimeout(function () { | ||
if (c) { | ||
c.close(); | ||
c = null; | ||
} | ||
startup(o); | ||
}, pause); | ||
pause = Math.min(pause + pause, 60000); | ||
o.emit('error', e); | ||
} | ||
}; | ||
@@ -150,3 +162,3 @@ /* }}} */ | ||
conns.push(c); | ||
['error', 'close'].forEach(function (k) { | ||
['error', 'timeout'].forEach(function (k) { | ||
c.once(k, function (e) { | ||
@@ -209,3 +221,3 @@ _remove(c, o); | ||
/* }}} */ | ||
/* {{{ public prototype query() */ | ||
@@ -212,0 +224,0 @@ /** |
{ | ||
"name": "easymysql", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"author": "Aleafs Zhang (zhangxc83@gmail.com)", | ||
@@ -5,0 +5,0 @@ "contributors": [], |
@@ -8,3 +8,11 @@ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | ||
var Cluster = rewire(__dirname + '/../lib/cluster.js'); | ||
var interceptor = require('interceptor'); | ||
var getBlocker = function (port, cb) { | ||
var cfg = Common.extend(); | ||
var _me = interceptor.create(util.format('%s:%d', cfg.host, cfg.port || 3306)); | ||
_me.listen(port, cb); | ||
return _me; | ||
}; | ||
describe('mysql cluster', function () { | ||
@@ -22,3 +30,3 @@ | ||
_me.addserver(Common.config); | ||
var _messages = []; | ||
@@ -38,3 +46,3 @@ _me.on('busy', function (n, m) { | ||
if (0 === (--num)) { | ||
_messages.should.includeEql(['busy', 1, 1, util.format('%s@%s:%d', | ||
_messages.should.includeEql(['busy', 1, 1, util.format('%s@%s:%d', | ||
Common.config.user, Common.config.host, Common.config.port)]); | ||
@@ -140,3 +148,40 @@ done(); | ||
it('should_server_no_response_works_fine', function (done) { | ||
var blocker = getBlocker(33062); | ||
var _config = Common.extend({ | ||
'host' : 'localhost', 'port' : 33062 | ||
}); | ||
var _me = Cluster.create({'maxconnections' : 1}); | ||
_me.addserver(_config); | ||
_me.query('SHOW DATABASES', function (e, r) { | ||
should.not.exist(e); | ||
Array.isArray(r).should.be.true; | ||
blocker.block(); | ||
setTimeout(afterBlock, 20); | ||
function afterBlock() { | ||
_me.query('SHOW DATABASES', 50, function (e, r) { | ||
should.exist(e); | ||
e.name.should.eql('QueryTimeout'); | ||
blocker.open(); | ||
setTimeout(afterOpen, 20); | ||
}); | ||
} | ||
function afterOpen() { | ||
_me.query('SHOW DATABASES', 200, function (e, r) { | ||
should.not.exist(e); | ||
done(); | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
@@ -37,12 +37,6 @@ /* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */ | ||
}); | ||
/* {{{ should_query_timeout_works_fine() */ | ||
it('should_query_timeout_works_fine', function (done) { | ||
var _me = Connection.create(Common.config); | ||
_me.on('late', function (e, r) { | ||
should.ok(!e); | ||
r.should.includeEql({'SLEEP(0.02)' : 0}); | ||
_me.close(); | ||
done(); | ||
}); | ||
@@ -61,2 +55,4 @@ var now = Date.now(); | ||
(Date.now() - now).should.below(20); | ||
_me.close(); | ||
done(); | ||
}); | ||
@@ -69,8 +65,2 @@ }); | ||
var _me = Connection.create(config); | ||
_me.on('late', function (e, r) { | ||
should.ok(!e); | ||
r.should.includeEql({'SLEEP(0.02)' : 0}); | ||
_me.close(); | ||
done(); | ||
}); | ||
@@ -90,2 +80,4 @@ var now = Date.now(); | ||
(Date.now() - now).should.below(20); | ||
_me.close(); | ||
done(); | ||
}); | ||
@@ -119,2 +111,3 @@ }); | ||
res.should.includeEql({'Database' : 'mysql'}); | ||
var afterClosed = function () { | ||
@@ -125,2 +118,3 @@ _events.should.eql([[ | ||
]]); | ||
_me.query('SHOW DATABASES', 100, function (error, res) { | ||
@@ -133,2 +127,3 @@ should.ok(error); | ||
}); | ||
}; | ||
@@ -135,0 +130,0 @@ |
33786
1042