Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
This commit is contained in:
parent
93e3a48004
commit
b38dea0597
25901 changed files with 9982925 additions and 369 deletions
63
gaia/tests/jsmarionette/client/socket-retry-connect/index.js
Executable file
63
gaia/tests/jsmarionette/client/socket-retry-connect/index.js
Executable file
|
|
@ -0,0 +1,63 @@
|
|||
'use strict';
|
||||
var debug = require('debug')('socket-retry-connect'),
|
||||
net = require('net');
|
||||
|
||||
var MAX_TRIES = 10;
|
||||
var WAIT_BETWEEN_TRIES_MS = 250;
|
||||
|
||||
/**
|
||||
* Tries to connect to a given socket location.
|
||||
* Time between retires grows in relation to attempts (attempt * RETRY_TIMER).
|
||||
*
|
||||
* waitForSocket({ port: 2828, maxTries: 10 }, function(err, socket) {
|
||||
* });
|
||||
*
|
||||
* Note- there is a third argument used to recursion that should
|
||||
* never be used publicly.
|
||||
*
|
||||
* Options:
|
||||
* - (Number) port: to connect to.
|
||||
* - (String) host: to connect to.
|
||||
* - (Number) tries: number of times to attempt the connect.
|
||||
*
|
||||
* @param {Object} options for connection.
|
||||
* @param {Function} callback [err, socket].
|
||||
*/
|
||||
function waitForSocket(options, callback, _tries) {
|
||||
if (!options.port)
|
||||
throw new Error('.port is a required option');
|
||||
|
||||
var maxTries = options.tries || MAX_TRIES;
|
||||
var host = options.host || 'localhost';
|
||||
var port = options.port;
|
||||
|
||||
|
||||
debug('attempt to open socket', port, host);
|
||||
_tries = _tries || 0;
|
||||
if (_tries >= maxTries)
|
||||
return callback(new Error('cannot open socket'));
|
||||
|
||||
function handleError() {
|
||||
debug('socket is not ready trying');
|
||||
// retry connection
|
||||
setTimeout(
|
||||
waitForSocket,
|
||||
// wait at least WAIT_BETWEEN_TRIES_MS or a multiplier
|
||||
// of the attempts.
|
||||
(WAIT_BETWEEN_TRIES_MS * _tries) || WAIT_BETWEEN_TRIES_MS,
|
||||
options,
|
||||
callback,
|
||||
++_tries
|
||||
);
|
||||
}
|
||||
|
||||
var socket = net.connect(port, host, function(one, two) {
|
||||
debug('connected', port, host);
|
||||
socket.removeListener('error', handleError);
|
||||
callback(null, socket);
|
||||
});
|
||||
socket.once('error', handleError);
|
||||
}
|
||||
|
||||
module.exports.waitForSocket = waitForSocket;
|
||||
|
||||
11
gaia/tests/jsmarionette/client/socket-retry-connect/package.json
Executable file
11
gaia/tests/jsmarionette/client/socket-retry-connect/package.json
Executable file
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "socket-retry-connect",
|
||||
"version": "0.0.1",
|
||||
"author": "James Lal [:lightsofapollo]",
|
||||
"description": "Attempt to connect to socket multiple times",
|
||||
"main": "index.js",
|
||||
|
||||
"dependencies" : {
|
||||
"debug": "*"
|
||||
}
|
||||
}
|
||||
57
gaia/tests/jsmarionette/client/socket-retry-connect/test.js
Executable file
57
gaia/tests/jsmarionette/client/socket-retry-connect/test.js
Executable file
|
|
@ -0,0 +1,57 @@
|
|||
'use strict';
|
||||
suite('socket retry connect', function() {
|
||||
var PORT = 60012;
|
||||
var assert = require('assert'),
|
||||
subject = require('./index').waitForSocket,
|
||||
net = require('net');
|
||||
|
||||
var _server;
|
||||
function createServer(onConnect) {
|
||||
onConnect = onConnect || function() {};
|
||||
// saves reference to last server
|
||||
return (_server = net.createServer(onConnect).listen(PORT));
|
||||
}
|
||||
|
||||
teardown(function() {
|
||||
// make sure we actually close the thing
|
||||
try {
|
||||
_server.close();
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
function returnsSocket(callback) {
|
||||
callback = callback || function() {};
|
||||
return function(err, socket) {
|
||||
assert.ok(!err, err && err.message);
|
||||
assert.ok(socket instanceof net.Socket);
|
||||
callback();
|
||||
};
|
||||
}
|
||||
|
||||
test('socket is open', function(done) {
|
||||
var pending = 2;
|
||||
function next() {
|
||||
if (--pending === 0) {
|
||||
done();
|
||||
}
|
||||
}
|
||||
// verify that both the server gets hit and socket callback fires.
|
||||
createServer(next);
|
||||
subject({ port: PORT }, returnsSocket(next));
|
||||
});
|
||||
|
||||
test('waits before connection is open', function(done) {
|
||||
this.timeout('5s');
|
||||
setTimeout(createServer, 575);
|
||||
subject({ port: PORT }, returnsSocket(done));
|
||||
});
|
||||
|
||||
test('fails to connect after retires', function(done) {
|
||||
this.timeout('3s');
|
||||
subject({ port: PORT, tries: 2 }, function(err) {
|
||||
assert.ok(err);
|
||||
assert.ok(err.message.match(/socket/));
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue