Merge pull request #260 from strongloop/feature/fix-ping
Make sure error events are emitted by data source
This commit is contained in:
commit
dd412f9a6f
|
@ -291,6 +291,7 @@ DataSource.prototype.setup = function (name, settings) {
|
||||||
connector = result.connector;
|
connector = result.connector;
|
||||||
if (!connector) {
|
if (!connector) {
|
||||||
console.error(result.error);
|
console.error(result.error);
|
||||||
|
this.emit('error', new Error(result.error));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -310,6 +311,7 @@ DataSource.prototype.setup = function (name, settings) {
|
||||||
} else {
|
} else {
|
||||||
// The connection fails, let's report it and hope it will be recovered in the next call
|
// The connection fails, let's report it and hope it will be recovered in the next call
|
||||||
console.error('Connection fails: ', err, '\nIt will be retried for the next request.');
|
console.error('Connection fails: ', err, '\nIt will be retried for the next request.');
|
||||||
|
this.emit('error', err);
|
||||||
this.connecting = false;
|
this.connecting = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1861,15 +1863,21 @@ DataSource.prototype.ready = function (obj, args) {
|
||||||
var method = args.callee;
|
var method = args.callee;
|
||||||
// Set up a callback after the connection is established to continue the method call
|
// Set up a callback after the connection is established to continue the method call
|
||||||
|
|
||||||
var onConnected = null, onError = null;
|
var onConnected = null, onError = null, timeoutHandle = null;
|
||||||
onConnected = function () {
|
onConnected = function () {
|
||||||
// Remove the error handler
|
// Remove the error handler
|
||||||
self.removeListener('error', onError);
|
self.removeListener('error', onError);
|
||||||
|
if (timeoutHandle) {
|
||||||
|
clearTimeout(timeoutHandle);
|
||||||
|
}
|
||||||
method.apply(obj, [].slice.call(args));
|
method.apply(obj, [].slice.call(args));
|
||||||
};
|
};
|
||||||
onError = function (err) {
|
onError = function (err) {
|
||||||
// Remove the connected listener
|
// Remove the connected listener
|
||||||
self.removeListener('connected', onConnected);
|
self.removeListener('connected', onConnected);
|
||||||
|
if (timeoutHandle) {
|
||||||
|
clearTimeout(timeoutHandle);
|
||||||
|
}
|
||||||
var params = [].slice.call(args);
|
var params = [].slice.call(args);
|
||||||
var cb = params.pop();
|
var cb = params.pop();
|
||||||
if (typeof cb === 'function') {
|
if (typeof cb === 'function') {
|
||||||
|
@ -1878,6 +1886,19 @@ DataSource.prototype.ready = function (obj, args) {
|
||||||
};
|
};
|
||||||
this.once('connected', onConnected);
|
this.once('connected', onConnected);
|
||||||
this.once('error', onError);
|
this.once('error', onError);
|
||||||
|
|
||||||
|
// Set up a timeout to cancel the invocation
|
||||||
|
var timeout = this.settings.connectionTimeout || 5000;
|
||||||
|
timeoutHandle = setTimeout(function () {
|
||||||
|
self.removeListener('error', onError);
|
||||||
|
self.removeListener('connected', onConnected);
|
||||||
|
var params = [].slice.call(args);
|
||||||
|
var cb = params.pop();
|
||||||
|
if (typeof cb === 'function') {
|
||||||
|
cb(new Error('Timeout in connecting after ' + timeout + ' ms'));
|
||||||
|
}
|
||||||
|
}, timeout);
|
||||||
|
|
||||||
if (!this.connecting) {
|
if (!this.connecting) {
|
||||||
this.connect();
|
this.connect();
|
||||||
}
|
}
|
||||||
|
|
|
@ -245,6 +245,38 @@ describe('ModelBuilder define model', function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('DataSource ping', function() {
|
||||||
|
var ds = new DataSource('memory');
|
||||||
|
ds.settings.connectionTimeout = 50; // ms
|
||||||
|
ds.connector.connect = function(cb) {
|
||||||
|
// Mock up the long delay
|
||||||
|
setTimeout(cb, 100);
|
||||||
|
};
|
||||||
|
ds.connector.ping = function(cb) {
|
||||||
|
cb(new Error('bad connection 2'));
|
||||||
|
}
|
||||||
|
|
||||||
|
it('should report connection errors during ping', function(done) {
|
||||||
|
ds.ping(function(err) {
|
||||||
|
(!!err).should.be.true;
|
||||||
|
err.message.should.be.eql('bad connection 2');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should cancel invocation after timeout', function(done) {
|
||||||
|
ds.connected = false; // Force connect
|
||||||
|
var Post = ds.define('Post', {
|
||||||
|
title: { type: String, length: 255 }
|
||||||
|
});
|
||||||
|
Post.create(function(err) {
|
||||||
|
(!!err).should.be.true;
|
||||||
|
err.message.should.be.eql('Timeout in connecting after 50 ms');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('DataSource define model', function () {
|
describe('DataSource define model', function () {
|
||||||
it('should be able to define plain models', function () {
|
it('should be able to define plain models', function () {
|
||||||
var ds = new DataSource('memory');
|
var ds = new DataSource('memory');
|
||||||
|
|
Loading…
Reference in New Issue