Skip to content

Commit

Permalink
Fix timeout reconnect
Browse files Browse the repository at this point in the history
Also make sure client.connect is always set properly
  • Loading branch information
felixge committed Oct 14, 2010
1 parent 4ce2fa8 commit 2b3258c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/mysql/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Client.prototype.connect = function(cb) {
})
.on('end', function() {
if (self.ending) {
self.connected = false;
self.ending = false;
return;
}
Expand All @@ -65,6 +66,7 @@ Client.prototype.connect = function(cb) {
return;
}

self.connected = false;
self._prequeue(connect);
});

Expand Down Expand Up @@ -258,6 +260,7 @@ Client.prototype._handlePacket = function(packet) {
}

if (type == Parser.OK_PACKET) {
this.connected = true;
if (delegate) {
delegate(null, Client._packetToUserObject(packet));
}
Expand Down
7 changes: 7 additions & 0 deletions test/simple/test-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,16 @@ test(function connect() {
});

onConnection.end();
assert.equal(client.connected, false);
})();

(function testExpectedEnd() {
client.connected = false;
client.ending = true;

onConnection.end();
assert.equal(client.ending, false);
assert.equal(client.connected, false);
})();
});

Expand Down Expand Up @@ -492,14 +496,17 @@ test(function _handlePacket() {

client._queue = [TASK];
client._handlePacket(PACKET);
assert.equal(client.connected, true);
})();

(function testNoDelegateOk() {
var PACKET = {type: Parser.OK_PACKET};
client._queue = [{}];
client.connected = false;

gently.expect(client, '_dequeue');
client._handlePacket(PACKET);
assert.equal(client.connected, true);
})();

(function testNormalError() {
Expand Down
10 changes: 9 additions & 1 deletion test/system/test-client-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ require('../common');
var Client = require('mysql').Client,
client = new Client(TEST_CONFIG),
gently = new Gently(),
timeoutHappened = false,
timeout = setTimeout(function() {
throw new Error('MySql timeout did not happen');
if (!timeoutHappened) {
throw new Error('MySql timeout did not happen');
}

gently.verify();
}, 5000);

client.connect();
Expand All @@ -14,6 +19,9 @@ client.query('SET wait_timeout = 1');
client.query('SET net_read_timeout = 1');

client._connection.on('end', function() {
timeoutHappened = true;
assert.equal(client.connected, false);

client.query('SELECT 1', gently.expect(function afterTimeoutSelect(err) {
clearTimeout(timeout);
assert.ifError(err);
Expand Down

0 comments on commit 2b3258c

Please sign in to comment.