Skip to content

Commit

Permalink
Handle ECONNREFUSED properly
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge committed Oct 14, 2010
1 parent ffdc745 commit 8f599e3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/mysql/client.js
Expand Up @@ -6,7 +6,8 @@ var sys = require('./sys'),
Parser = require('./parser'),
OutgoingPacket = require('./outgoing_packet'),
Query = require('./query'),
EventEmitter = require('events').EventEmitter;
EventEmitter = require('events').EventEmitter,
netBinding = process.binding('net');

function Client(properties) {
if (!(this instanceof Client)) {
Expand Down Expand Up @@ -50,6 +51,13 @@ Client.prototype.connect = function(cb) {
connection.connect(self.port, self.host);
connection
.on('error', function(err) {
if (err.errno == netBinding.ECONNREFUSED) {
if (cb) {
cb(err);
}
return;
}

self.emit('error', err);
})
.on('data', function(b) {
Expand Down
21 changes: 18 additions & 3 deletions test/simple/test-client.js
Expand Up @@ -3,7 +3,8 @@ var StreamStub = GENTLY.stub('net', 'Stream'),
ParserStub = GENTLY.stub('./parser'),
OutgoingPacketStub = GENTLY.stub('./outgoing_packet'),
QueryStub = GENTLY.stub('./query'),
Parser = require('mysql/parser');
Parser = require('mysql/parser'),
netBinding = process.binding('net');

for (var k in Parser) {
ParserStub[k] = Parser[k];
Expand Down Expand Up @@ -58,7 +59,10 @@ test(function connect() {
var CONNECTION,
PARSER,
onConnection = {},
CB = function() {},
CB = function() {
CB_DELEGATE.apply(this, arguments);
},
CB_DELEGATE,
CONNECT_FN;

gently.expect(client, '_enqueue', function(task, cb) {
Expand Down Expand Up @@ -103,7 +107,7 @@ test(function connect() {
assert.strictEqual(client._connection, CONNECTION);
assert.strictEqual(client._parser, PARSER);

(function testConnectionError() {
(function testRandomConnectionError() {
var ERR = new Error('ouch');
gently.expect(client, 'emit', function(event, err) {
assert.equal(event, 'error');
Expand All @@ -113,6 +117,17 @@ test(function connect() {
onConnection.error(ERR);
})();

(function testConnectionRefusedError() {
var ERR = new Error('ouch');
ERR.errno = netBinding.ECONNREFUSED;

CB_DELEGATE = gently.expect(function connectCallback(err) {
assert.strictEqual(err, ERR);
});

onConnection.error(ERR);
})();

(function testOnConnectionData() {
var BUFFER = {};
gently.expect(PARSER, 'write', function(buffer) {
Expand Down
11 changes: 11 additions & 0 deletions test/system/test-client-connection-error.js
@@ -0,0 +1,11 @@
require('../common');
var Client = require('mysql').Client,
client = Client(TEST_CONFIG),
gently = new Gently(),
ECONNREFUSED = process.binding('net').ECONNREFUSED;

client.host = 'BADHOST';

client.connect(gently.expect(function connectCb(err, result) {
assert.equal(err.errno, ECONNREFUSED);
}));

0 comments on commit 8f599e3

Please sign in to comment.