Skip to content

Commit

Permalink
feat: Upgrade to socket.io 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 15, 2015
1 parent f6947ce commit 0f2468f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 38 deletions.
13 changes: 3 additions & 10 deletions client/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ var Karma = function(socket, iframe, opener, navigator, location) {
var queryParams = util.parseQueryParams(location.search);
var browserId = queryParams.id || util.generateId('manual-');
var returnUrl = queryParams['return_url' + ''] || null;
var currentTransport;

var resultsBufferLimit = 1;
var resultsBufferLimit = 50;
var resultsBuffer = [];

this.VERSION = constant.VERSION;
Expand Down Expand Up @@ -104,7 +103,6 @@ var Karma = function(socket, iframe, opener, navigator, location) {

this.stringify = stringify;


var clearContext = function() {
reloadingContext = true;
navigateContextTo('about:blank');
Expand Down Expand Up @@ -218,14 +216,9 @@ var Karma = function(socket, iframe, opener, navigator, location) {

// report browser name, id
socket.on('connect', function() {
currentTransport = socket.socket.transport.name;

// TODO(vojta): make resultsBufferLimit configurable
if (currentTransport === 'websocket' || currentTransport === 'flashsocket') {
socket.io.engine.on('upgrade', function() {
resultsBufferLimit = 1;
} else {
resultsBufferLimit = 50;
}
})

socket.emit('register', {
name: navigator.userAgent,
Expand Down
15 changes: 7 additions & 8 deletions client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ var util = require('./util');
var KARMA_URL_ROOT = require('./constants').KARMA_URL_ROOT;


// connect socket.io
// https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO
var socket = io.connect('http://' + location.host, {
'reconnection delay': 500,
'reconnection limit': 2000,
'resource': KARMA_URL_ROOT.substr(1) + 'socket.io',
'sync disconnect on unload': true,
'max reconnection attempts': Infinity
// Connect to the server using socket.io http://socket.io
var socket = io('http://' + location.host, {
reconnectionDelay: 500,
reconnectionDelayMax: Infinity,
timeout: 2000,
path: '/' + KARMA_URL_ROOT.substr(1) + 'socket.io',
'sync disconnect on unload': true
});

// instantiate the updater of the view
Expand Down
2 changes: 1 addition & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ var Config = function() {
this.urlRoot = '/';
this.reportSlowerThan = 0;
this.loggers = [constant.CONSOLE_APPENDER];
this.transports = ['websocket', 'flashsocket', 'xhr-polling', 'jsonp-polling'];
this.transports = ['polling', 'websocket'];
this.plugins = ['karma-*'];
this.client = {
args: [],
Expand Down
19 changes: 7 additions & 12 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var io = require('socket.io');
var Server = require('socket.io');
var di = require('di');

var cfg = require('./config');
Expand Down Expand Up @@ -200,8 +200,8 @@ var start = function(injector, config, launcher, globalEmitter, preprocess, file
// to suppress "browser disconnect" warnings
// TODO(vojta): change the client to not send the event (if disconnected by purpose)
var sockets = socketServer.sockets.sockets;
Object.getOwnPropertyNames(sockets).forEach(function(key) {
var socket = sockets[key];

sockets.forEach(function(socket) {
socket.removeAllListeners('disconnect');
if (!socket.disconnected) {
socket.disconnect();
Expand Down Expand Up @@ -230,11 +230,6 @@ var start = function(injector, config, launcher, globalEmitter, preprocess, file
clearTimeout(closeTimeout);
removeAllListeners();
});

// shutdown socket.io flash transport, if defined
if (socketServer.flashPolicyServer) {
socketServer.flashPolicyServer.close();
}
});
};

Expand All @@ -258,16 +253,16 @@ start.$inject = ['injector', 'config', 'launcher', 'emitter', 'preprocess', 'fil


var createSocketIoServer = function(webServer, executor, config) {
var server = io.listen(webServer, {
var server = new Server(webServer, {
// avoid destroying http upgrades from socket.io to get proxied websockets working
'destroy upgrade': false,
destroyUpgrade: false,
// socket.io has a timeout (15s by default) before destroying a store (a data structure where
// data associated with a socket are stored). Unfortunately this timeout is not cleared
// properly on socket.io shutdown and this timeout prevents karma from exiting cleanly.
// We change this timeout to 0 to make Karma exit just after all tests were executed.
'client store expiration': 0,
//'client store expiration': 0,
logger: logger.create('socket.io', constant.LOG_ERROR),
resource: config.urlRoot + 'socket.io',
path: config.urlRoot + 'socket.io/',
transports: config.transports
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
],
"dependencies": {
"di": "~0.0.1",
"socket.io": "0.9.16",
"socket.io": "~1.3.5",
"chokidar": ">=0.8.2",
"glob": "~3.2.7",
"minimatch": "~0.2",
Expand Down
10 changes: 7 additions & 3 deletions test/client/karma.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('Karma', function() {


it('should buffer results when polling', function() {
setTransportTo('xhr-polling');
setTransportTo('polling');

// emit 49 results
for (var i = 1; i < 50; i++) {
Expand All @@ -138,7 +138,7 @@ describe('Karma', function() {


it('should buffer results when polling', function() {
setTransportTo('xhr-polling');
setTransportTo('polling');

// emit 40 results
for (var i = 1; i <= 40; i++) {
Expand All @@ -161,6 +161,8 @@ describe('Karma', function() {
log.push('start');
});

setTransportTo('websocket');

// adapter didn't call info({total: x})
k.result();
expect(log).toEqual(['start', 'result']);
Expand All @@ -178,6 +180,8 @@ describe('Karma', function() {

socket.on('start', spyStart);

setTransportTo('websocket');

k.info({total: 321});
k.result();
expect(log).toEqual(['start', 'result']);
Expand Down Expand Up @@ -237,7 +241,7 @@ describe('Karma', function() {
var spyResult = jasmine.createSpy('onResult');
socket.on('result', spyResult);

setTransportTo('xhr-polling');
setTransportTo('polling');

// emit 40 results
for (var i = 0; i < 40; i++) {
Expand Down
17 changes: 14 additions & 3 deletions test/client/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,26 @@ var Emitter = function() {
var MockSocket = function() {
Emitter.call(this);

this.socket = {transport: {name: 'websocket'}};
var transportName = 'websocket';

this.io = {
engine: {
on: function(event, cb) {
if (event === 'upgrade' && transportName === 'websocket') {
//setTimeout(cb, 0);
cb();
}
}
}
};

this.disconnect = function() {
this.emit('disconnect');
};

// MOCK API
this._setTransportNameTo = function(transportName) {
this.socket.transport.name = transportName;
this._setTransportNameTo = function(name) {
transportName = name;
};
};

Expand Down

0 comments on commit 0f2468f

Please sign in to comment.