Skip to content

Commit

Permalink
fix: clear connection timeout on connection error (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensby authored Nov 1, 2022
1 parent 8ffc49e commit cbe31af
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,7 @@ class Connection extends EventEmitter {
this.connectOnPort(port, this.config.options.multiSubnetFailover, signal);
});
}, (err) => {
this.clearConnectTimer();
if (err.name === 'AbortError') {
// Ignore the AbortError for now, this is still handled by the connectTimer firing
return;
Expand Down Expand Up @@ -1983,6 +1984,7 @@ class Connection extends EventEmitter {
this.transitionTo(this.STATE.SENT_PRELOGIN);
});
}, (err) => {
this.clearConnectTimer();
if (err.name === 'AbortError') {
return;
}
Expand Down
46 changes: 46 additions & 0 deletions test/integration/connection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,52 @@ describe('Initiate Connect Test', function() {
connection.connect();
});

it('should clear timeouts when failing to connect', function(done) {
const connection = new Connection({
server: 'something.invalid',
options: { connectTimeout: 30000 },
});

connection.on('connect', (err) => {
try {
assert.instanceOf(err, ConnectionError);
assert.strictEqual(/** @type {ConnectionError} */(err).code, 'ESOCKET');
assert.strictEqual(connection.connectTimer, undefined);
done();
} catch (e) {
done(e);
}
});

connection.on('error', done);
connection.connect();

assert.isOk(connection.connectTimer);
});

it('should clear timeouts when failing to connect to named instance', function(done) {
const connection = new Connection({
server: 'something.invalid',
options: {
instanceName: 'inst',
connectTimeout: 30000,
},
});

connection.on('connect', (err) => {
assert.instanceOf(err, ConnectionError);
assert.strictEqual(/** @type {ConnectionError} */(err).code, 'EINSTLOOKUP');
assert.strictEqual(connection.connectTimer, undefined);

done();
});

connection.on('error', done);
connection.connect();

assert.isOk(connection.connectTimer);
});

it('should fail if no cipher can be negotiated', function(done) {
const config = getConfig();
config.options.encrypt = true;
Expand Down

0 comments on commit cbe31af

Please sign in to comment.