Add a heartbeat to detect hung connections (#1101)

* disconnect is now reconnect on heartbeat fail
This commit is contained in:
FKSRipple
2019-12-14 10:23:09 -08:00
committed by Elliot Lee
parent 5f92b230aa
commit 439a611a9e
3 changed files with 65 additions and 3 deletions

View File

@@ -266,6 +266,26 @@ describe('Connection', function() {
});
});
it('reconnect event on heartbeat failure', function(done) {
if (isBrowser) {
const phantomTest = /PhantomJS/;
if (phantomTest.test(navigator.userAgent)) {
// inside PhantomJS this one just hangs, so skip as not very relevant
done();
return;
}
}
// Set the heartbeat to less than the 1 second ping response
this.api.connection._timeout = 500;
// Drop the test runner timeout, since this should be a quick test
this.timeout(5000);
// Hook up a listener for the reconnect event
this.api.connection.on('reconnect', () => done());
// Trigger a heartbeat
this.api.connection._heartbeat();
});
it('should emit disconnected event with code 1000 (CLOSE_NORMAL)',
function(done
) {

View File

@@ -336,6 +336,19 @@ export function createMockRippled(port) {
}
});
mock.on('request_ping', function (request, conn) {
// NOTE: We give the response a timeout of 2 second, so that tests can
// set their timeout threshold to greater than or less than this number
// to test timeouts.
setTimeout(() => {
conn.send(createResponse(request, {
"result": {},
"status": "success",
"type": "response"
}));
}, 1000 * 2);
});
mock.on('request_tx', function (request, conn) {
assert.strictEqual(request.command, 'tx');
if (request.transaction === hashes.VALID_TRANSACTION_HASH) {