Skip to content

Commit

Permalink
During a peering from the hub side the hub will wait 30s or a spdy pi…
Browse files Browse the repository at this point in the history
…ng before the connection is considered timed out. When this happens it closes the underlying ./lib/web_socket.js which the ./lib/peer_client.js expects a error/close event to propagate back to the peer_client from the web_socket. (#359)

In the case when the connection times out waiting for the http response from the websocket server in the cloud the error or close event never are emitted as the socket isn't estabiished yet and we do nothing here this.socket is not set from here.
  • Loading branch information
AdamMagaluk authored Nov 1, 2017
1 parent 5a1bc56 commit 708a385
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/web_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ WebSocket.prototype.close = function() {
this.isClosed = true;
if(this.socket) {
this.socket.end();
// this.emit('close', null, null, true);
}
} else {
this.emit('close', null, null, true);
}
};

WebSocket.prototype.start = function() {
Expand Down
45 changes: 43 additions & 2 deletions test/test_peer_client.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var http = require('http');
var PeerClient = require('../lib/peer_client');
var assert = require('assert');
var zetta = require('../');
var Registry = require('./fixture/mem_registry');
var PeerRegistry = require('./fixture/mem_peer_registry');


var MockServer = { _name: '1234', httpServer: { spdyServer: {}}, log: {
var MockServer = { _name: '1234', httpServer: { spdyServer: {
on: function() {},
emit: function() {},
removeListener: function() {}
}}, log: {
emit: function() {}
}};

var MockSocket = function() {
EventEmitter.call(this);
this.setAddress = function() {};
Expand Down Expand Up @@ -64,4 +72,37 @@ describe('Peer Client', function() {
client.ws.emit('close');
}, 2);
})

it('should reconnect on timeout waiting for http res from ws server', function(done) {
var httpServer = http.createServer(function(req, res) {
console.log('on request:', req.url)
}).on('upgrade', function(req, socket, upgradeHead) {
console.log('on upgrade')
}).listen(0, function(err) {
if (err) {
done(err);
return;
}

var address = 'http://localhost:' + httpServer.address().port;
var client = new PeerClient(address, MockServer);
client.pingTimeout = 50;

// monkey patch createSocket so we can watch the reconnect
var origCreateSocket = client._createSocket;
client._createSocket = function() {
// Successfuly reconnected
if (client.retryCount === 1) {
httpServer.close();
client.close();
done();
return;
}
origCreateSocket.apply(client, arguments);
};

client.start();
});
})

});

0 comments on commit 708a385

Please sign in to comment.