BREAKING CHANGE: removed 'timeout' method of Request

added default timeout to Request - will emit 'timeout' event
and RippleError('tejTimeout') to callback
This commit is contained in:
Ivan Tivonenko
2015-10-02 04:22:51 +03:00
parent 8edc3b1f36
commit 225ca3f852
3 changed files with 65 additions and 45 deletions

View File

@@ -34,14 +34,15 @@ function Request(remote, command) {
command: command, command: command,
id: undefined id: undefined
}; };
this._timeout = this.remote.submission_timeout;
} }
util.inherits(Request, EventEmitter); util.inherits(Request, EventEmitter);
// Send the request to a remote. // Send the request to a remote.
Request.prototype.request = function(servers, callback_) { Request.prototype.request = function(servers, callback_) {
const self = this;
const callback = typeof servers === 'function' ? servers : callback_; const callback = typeof servers === 'function' ? servers : callback_;
const self = this;
if (this.requested) { if (this.requested) {
throw new Error('Already requested'); throw new Error('Already requested');
@@ -70,17 +71,25 @@ Request.prototype.request = function(servers, callback_) {
} }
} }
function onReconnect() { const timeout = setTimeout(() => {
doRequest(); if (typeof callback === 'function') {
callback(new RippleError('tejTimeout'));
} }
this.emit('timeout');
// just in case
this.emit = _.noop;
this.cancel();
}, this._timeout);
function onResponse() { function onResponse() {
self.remote.removeListener('connected', onReconnect); clearTimeout(timeout);
} }
if (this.remote.isConnected()) { if (this.remote.isConnected()) {
this.remote.on('connected', onReconnect); this.remote.on('connected', doRequest);
} }
this.once('response', onResponse); this.once('response', onResponse);
doRequest(); doRequest();
@@ -264,38 +273,11 @@ Request.prototype.callback = function(callback, successEvent, errorEvent) {
return this; return this;
}; };
Request.prototype.timeout = function(duration, callback) { Request.prototype.setTimeout = function(delay) {
const self = this; if (!_.isFinite(delay)) {
throw new Error('delay must be number');
function requested() {
self.timeout(duration, callback);
} }
this._timeout = delay;
if (!this.requested) {
// Defer until requested
return this.once('request', requested);
}
const emit = this.emit;
let timed_out = false;
const timeout = setTimeout(function() {
timed_out = true;
if (typeof callback === 'function') {
callback();
}
emit.call(self, 'timeout');
self.cancel();
}, duration);
this.emit = function() {
if (!timed_out) {
clearTimeout(timeout);
emit.apply(self, arguments);
}
};
return this; return this;
}; };

View File

@@ -698,7 +698,8 @@ TransactionManager.prototype._request = function(tx) {
tx.emit('postsubmit'); tx.emit('postsubmit');
submitRequest.timeout(self._submissionTimeout, requestTimeout); submitRequest.setTimeout(self._submissionTimeout);
submitRequest.once('timeout', requestTimeout);
}; };
/** /**

View File

@@ -65,6 +65,38 @@ describe('Request', function() {
}); });
it('Send request - reconnect', function(done) {
const server = makeServer('wss://localhost:5006');
let emitted = 0;
const remote = new Remote();
remote._connected = true;
remote._servers = [server];
server._request = function(req) {
assert(req instanceof Request);
assert.strictEqual(typeof req.message, 'object');
assert.strictEqual(req.message.command, 'server_info');
if (++emitted === 1) {
setTimeout(function() {
remote.emit('connected');
}, 2);
} if (emitted === 2) {
setTimeout(function() {
req.emit('success', SERVER_INFO);
req.emit('response', SERVER_INFO);
}, 2);
}
};
const request = new Request(remote, 'server_info');
request.callback(function() {
assert.strictEqual(emitted, 2);
done();
});
});
it('Send request -- filterRequest', function(done) { it('Send request -- filterRequest', function(done) {
const servers = [ const servers = [
makeServer('wss://localhost:5006'), makeServer('wss://localhost:5006'),
@@ -538,6 +570,7 @@ describe('Request', function() {
setTimeout(function() { setTimeout(function() {
successEmitted = true; successEmitted = true;
req.emit('success', SERVER_INFO); req.emit('success', SERVER_INFO);
req.emit('response', SERVER_INFO);
}, 200); }, 200);
}; };
@@ -546,8 +579,9 @@ describe('Request', function() {
remote._servers = [server]; remote._servers = [server];
const request = new Request(remote, 'server_info'); const request = new Request(remote, 'server_info');
request.setTimeout(10);
request.timeout(10, function() { request.on('timeout', function() {
setTimeout(function() { setTimeout(function() {
assert(successEmitted); assert(successEmitted);
done(); done();
@@ -568,7 +602,8 @@ describe('Request', function() {
assert.strictEqual(req.message.command, 'server_info'); assert.strictEqual(req.message.command, 'server_info');
setTimeout(function() { setTimeout(function() {
req.emit('success', SERVER_INFO); req.emit('success', SERVER_INFO);
}, 200); req.emit('response', SERVER_INFO);
}, 20);
}; };
const remote = new Remote(); const remote = new Remote();
@@ -583,13 +618,15 @@ describe('Request', function() {
timedOut = true; timedOut = true;
}); });
request.timeout(1000); request.setTimeout(100);
request.callback(function(err, res) { request.callback(function(err, res) {
assert(!timedOut); setTimeout(function() {
assert(!timedOut, 'must not timeout');
assert.ifError(err); assert.ifError(err);
assert.deepEqual(res, SERVER_INFO); assert.deepEqual(res, SERVER_INFO);
done(); done();
}, 100);
}); });
}); });