Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various new-upgrade and test fixes #131

Merged
merged 14 commits into from
Sep 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/lib/transport/comettransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ var CometTransport = (function() {
var items = this.decodeResponse(responseData);
if(items && items.length)
for(var i = 0; i < items.length; i++)
this.onChannelMessage(items[i]);
this.onChannelMessage(ProtocolMessage.fromDecoded(items[i]));
} catch (e) {
Logger.logAction(Logger.LOG_ERROR, 'CometTransport.onData()', 'Unexpected exception handing channel event: ' + e.stack);
}
Expand Down
4 changes: 4 additions & 0 deletions common/lib/transport/connectionmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -993,5 +993,9 @@ var ConnectionManager = (function() {
this.ping(this.activeProtocol.getTransport(), onPingComplete);
};

ConnectionManager.prototype.abort = function(error) {
this.activeProtocol.getTransport().abort(error);
};

return ConnectionManager;
})();
56 changes: 38 additions & 18 deletions spec/browser/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
var exports = {},
closeAndFinish = helper.closeAndFinish,
monitorConnection = helper.monitorConnection,
Defaults = Ably.Realtime.Defaults;
simulateDroppedConnection = helper.simulateDroppedConnection,
Defaults = Ably.Realtime.Defaults,
oldDisconnectTimeout = Defaults.disconnectTimeout,
oldSuspendedTimeout = Defaults.suspendedTimeout,
oldConnectTimeout = Defaults.connectTimeout;

function stopIfUnsupported(test) {
function supportedBrowser(test) {
if(document.body.ononline === undefined) {
test.done();
return;
console.log("Online events not supported; skipping connection.test.js");
return false;
}

// IE doesn't support creating your own events with new
try {
var testEvent = new Event("foo");
} catch(e) {
console.log("On IE; skipping connection.test.js");
return false;
}

return true;
}

exports.setup_realtime = function(test) {
Expand All @@ -26,8 +40,6 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
};

exports.device_going_offline_causes_disconnected_state = function(test) {
stopIfUnsupported(test);

var realtime = helper.AblyRealtime(),
connection = realtime.connection,
offlineEvent = new Event('offline', {'bubbles': true});
Expand All @@ -39,10 +51,10 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
var connectedAt = new Date().getTime()
connection.once('disconnected', function() {
var disconnectedAt = new Date().getTime();
test.ok(disconnectedAt - connectedAt < 250, 'Offline event caused connection to move to the disconnected state immediately (under 250ms)');
test.ok(disconnectedAt - connectedAt < 1500, 'Offline event caused connection to move to the disconnected state');
connection.once('connecting', function() {
var reconnectingAt = new Date().getTime();
test.ok(reconnectingAt - disconnectedAt < 250, 'Client automatically reattempts connection even if the state is still offline');
test.ok(reconnectingAt - disconnectedAt < 1500, 'Client automatically reattempts connection without waiting for disconnect timeout, even if the state is still offline');
connection.once('connected', function() {
test.ok(true, 'Successfully reconnected');
closeAndFinish(test, realtime);
Expand All @@ -56,8 +68,6 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
};

exports.device_going_online_causes_disconnected_connection_to_reconnect_immediately = function(test) {
stopIfUnsupported(test);

var realtime = helper.AblyRealtime(),
connection = realtime.connection,
onlineEvent = new Event('online', {'bubbles': true});
Expand All @@ -76,9 +86,10 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
var disconnectedAt = new Date();
test.ok(connection.state == 'disconnected', 'Connection should still be disconnected before we trigger it to connect');
connection.once('connecting', function() {
test.ok(new Date() - disconnectedAt < 250, 'Online event should have caused the connection to enter the connecting state immediately');
test.ok(new Date() - disconnectedAt < 1500, 'Online event should have caused the connection to enter the connecting state without waiting for disconnect timeout');
connection.once('connected', function() {
test.ok(true, 'Successfully reconnected');
Defaults.connectTimeout = oldConnectTimeout;
closeAndFinish(test, realtime);
})
});
Expand All @@ -89,10 +100,8 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
};

exports.device_going_online_causes_suspended_connection_to_reconnect_immediately = function(test) {
stopIfUnsupported(test);

Defaults.disconnectTimeout = 100; // retry connection more frequently
Defaults.suspendedTimeout = 1000; // move to suspended state after 1s of being disconencted
Defaults.disconnectTimeout = 200; // retry connection more frequently
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to be picky but we've used /* ... */ comment style throughout.

Defaults.suspendedTimeout = 2000; // move to suspended state after 2s of being disconnected

var realtime = helper.AblyRealtime(),
connection = realtime.connection,
Expand All @@ -104,20 +113,31 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
test.expect(2);
connection.on('failed', function () {
test.ok(false, 'connection to server failed');
closeAndFinish(test, realtime)
closeAndFinish(test, realtime);
});

connection.once('suspended', function() {
var suspendedAt = new Date();
test.ok(connection.state == 'suspended', 'Connection should still be suspended before we trigger it to connect');
connection.once('connecting', function() {
test.ok(new Date() - suspendedAt < 250, 'Online event should have caused the connection to enter the connecting state immediately');
test.ok(new Date() - suspendedAt < 1500, 'Online event should have caused the connection to enter the connecting state without waiting for suspended timeout');
Defaults.disconnectTimeout = oldDisconnectTimeout;
Defaults.suspendedTimeout = oldSuspendedTimeout;
closeAndFinish(test, realtime);
});
// simulate online event
document.dispatchEvent(onlineEvent);
});
};

return module.exports = helper.withTimeout(exports);
exports.clean99 = function(test) {
// Ensure defaults are reset
Defaults.connectTimeout = oldConnectTimeout;
Defaults.disconnectTimeout = oldDisconnectTimeout;
Defaults.suspendedTimeout = oldSuspendedTimeout;
test.done();
}

return module.exports = supportedBrowser() ? helper.withTimeout(exports) : {};

});
21 changes: 16 additions & 5 deletions spec/common/modules/shared_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,18 @@ define(['spec/common/modules/testapp_module', 'spec/common/modules/client_module
callbackOnClose(realtime, function(){ test.done(); })
};

var simulateDroppedConnection = function(realtime) {
// Go into the 'disconnected' state before actually disconnecting the transports
// to avoid the instantaneous reconnect attempt that would be triggered in
// notifyState by the active transport getting disconnected from a connected state
realtime.connection.once('disconnected', function(){
realtime.connection.connectionManager.disconnectAllTransports();
});
realtime.connection.connectionManager.requestState({state: 'disconnected'});
}

function callbackOnClose(realtime, callback) {
if(realtime.connection.connectionManager.activeProtocol === null) {
if(!realtime.connection.connectionManager.activeProtocol) {
console.log("No transport established; closing connection and calling test.done()")
realtime.close();
callback();
Expand Down Expand Up @@ -110,9 +120,10 @@ define(['spec/common/modules/testapp_module', 'spec/common/modules/client_module

loadTestData: testAppManager.loadJsonData,

displayError: displayError,
monitorConnection: monitorConnection,
closeAndFinish: closeAndFinish,
withTimeout: withTimeout
displayError: displayError,
monitorConnection: monitorConnection,
closeAndFinish: closeAndFinish,
simulateDroppedConnection: simulateDroppedConnection,
withTimeout: withTimeout
};
});
2 changes: 1 addition & 1 deletion spec/realtime/event_emitter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
delete expectedConnectionEvents[index];
test.ok(true, this.event + ' connection event received');
if(this.event == 'closed') {
closeAndFinish(test, realtime);
test.done();
}
} else {
test.ok(false, 'Unexpected ' + this.event + ' event received');
Expand Down
3 changes: 2 additions & 1 deletion spec/realtime/failure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ define(['ably', 'shared_helper', 'async'], function(Ably, helper, async) {
var exports = {},
closeAndFinish = helper.closeAndFinish,
monitorConnection = helper.monitorConnection,
simulateDroppedConnection = helper.simulateDroppedConnection,
// Ably.Realtime.ConnectionManager not defined in node
availableTransports = typeof Ably.Realtime.ConnectionManager === 'undefined' ? Ably.Realtime.Defaults.transports : Object.keys(Ably.Realtime.ConnectionManager.transports);

Expand Down Expand Up @@ -72,7 +73,7 @@ define(['ably', 'shared_helper', 'async'], function(Ably, helper, async) {
test.ok(false, 'connection state for transports ' + transports + ' should be disconnected, not failed');
cb(null, realtime);
});
realtime.connection.connectionManager.activeProtocol.transport.disconnect();
simulateDroppedConnection(realtime);
});
};
};
Expand Down
Loading