Skip to content

Commit

Permalink
Update Realtime tests to use SDK’s Promise-based API
Browse files Browse the repository at this point in the history
I’ve chosen to do this with the lightest touch possible — that is,
maintaining the tests’ callback-based approach and simply bridging the
SDK’s Promise-based API back to callbacks. I did this for the sake of
reviewability and not accidentally changing the behaviour of the tests
in some subtle way that I’d then have to put time into understanding.

It would be good to, at some point, update the structure of the tests to
use `async` / `await`, to improve readability and to make them reflect
how the users actually interact with the Promise-based API in the real
world. I’ve split out into the separate task #1348.

Note that I haven’t made any changes to the calls to EventEmitter’s
`once` or `whenState` methods — we’re going to keep the callback-based
versions of those methods, since there is no Promise equivalent of being
able to turn them off using `off`.

Resolves #1213.
  • Loading branch information
lawrence-forooghian committed Jun 27, 2023
1 parent 050b95b commit 43a2d1d
Show file tree
Hide file tree
Showing 21 changed files with 734 additions and 682 deletions.
32 changes: 16 additions & 16 deletions test/browser/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
});

it('device_going_offline_causes_disconnected_state', function (done) {
var realtime = helper.AblyRealtime(),
var realtime = helper.AblyRealtimePromise(),
connection = realtime.connection,
offlineEvent = new Event('offline', { bubbles: true });

Expand Down Expand Up @@ -94,7 +94,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {

it('device_going_online_causes_disconnected_connection_to_reconnect_immediately', function (done) {
/* Give up trying to connect fairly quickly */
var realtime = helper.AblyRealtime({ realtimeRequestTimeout: 1000 }),
var realtime = helper.AblyRealtimePromise({ realtimeRequestTimeout: 1000 }),
connection = realtime.connection,
onlineEvent = new Event('online', { bubbles: true });

Expand Down Expand Up @@ -139,7 +139,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {

it('device_going_online_causes_suspended_connection_to_reconnect_immediately', function (done) {
/* move to suspended state after 2s of being disconnected */
var realtime = helper.AblyRealtime({
var realtime = helper.AblyRealtimePromise({
disconnectedRetryTimeout: 500,
realtimeRequestTimeout: 500,
connectionStateTtl: 2000,
Expand Down Expand Up @@ -183,7 +183,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
});

it('device_going_online_causes_connecting_connection_to_retry_attempt', function (done) {
var realtime = helper.AblyRealtime({}),
var realtime = helper.AblyRealtimePromise({}),
connection = realtime.connection,
onlineEvent = new Event('online', { bubbles: true }),
oldTransport,
Expand Down Expand Up @@ -223,7 +223,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
cb(true);
},
},
realtime = helper.AblyRealtime(realtimeOpts),
realtime = helper.AblyRealtimePromise(realtimeOpts),
refreshEvent = new Event('beforeunload', { bubbles: true });

monitorConnection(done, realtime);
Expand All @@ -242,7 +242,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
return;
}

var newRealtime = helper.AblyRealtime(realtimeOpts);
var newRealtime = helper.AblyRealtimePromise(realtimeOpts);
newRealtime.connection.once('connected', function () {
try {
expect(
Expand All @@ -264,7 +264,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
cb(false);
},
};
var realtime = helper.AblyRealtime(realtimeOpts),
var realtime = helper.AblyRealtimePromise(realtimeOpts),
refreshEvent = new Event('beforeunload', { bubbles: true });

monitorConnection(done, realtime);
Expand All @@ -283,7 +283,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
return;
}

var newRealtime = helper.AblyRealtime(realtimeOpts);
var newRealtime = helper.AblyRealtimePromise(realtimeOpts);
newRealtime.connection.once('connected', function () {
try {
expect(
Expand All @@ -301,7 +301,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
});

it('page_refresh_with_close_on_unload', function (done) {
var realtime = helper.AblyRealtime({ closeOnUnload: true }),
var realtime = helper.AblyRealtimePromise({ closeOnUnload: true }),
refreshEvent = new Event('beforeunload', { bubbles: true });

monitorConnection(done, realtime);
Expand All @@ -321,7 +321,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
});

it('page_refresh_with_manual_recovery', function (done) {
var realtime = helper.AblyRealtime({ closeOnUnload: false }),
var realtime = helper.AblyRealtimePromise({ closeOnUnload: false }),
refreshEvent = new Event('beforeunload', { bubbles: true });

monitorConnection(done, realtime);
Expand All @@ -342,7 +342,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
return;
}

var newRealtime = helper.AblyRealtime({ recover: recoveryKey });
var newRealtime = helper.AblyRealtimePromise({ recover: recoveryKey });
newRealtime.connection.once('connected', function () {
try {
expect(
Expand All @@ -359,7 +359,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
});

it('persist_preferred_transport', function (done) {
var realtime = helper.AblyRealtime();
var realtime = helper.AblyRealtimePromise();

realtime.connection.connectionManager.on(function (transport) {
if (this.event === 'transport.active' && transport.shortName === 'web_socket') {
Expand All @@ -381,7 +381,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
var transportPreferenceName = 'ably-transport-preference';
window.localStorage.setItem(transportPreferenceName, JSON.stringify({ value: 'web_socket' }));

var realtime = helper.AblyRealtime();
var realtime = helper.AblyRealtimePromise();

realtime.connection.connectionManager.on(function (transport) {
if (this.event === 'transport.active') {
Expand All @@ -400,7 +400,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
it('use_persisted_transport1', function (done) {
window.localStorage.setItem(transportPreferenceName, JSON.stringify({ value: 'xhr_streaming' }));

var realtime = helper.AblyRealtime();
var realtime = helper.AblyRealtimePromise();

realtime.connection.connectionManager.on(function (transport) {
if (this.event === 'transport.active') {
Expand All @@ -417,7 +417,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
});

it('browser_transports', function (done) {
var realtime = helper.AblyRealtime();
var realtime = helper.AblyRealtimePromise();
try {
expect(realtime.connection.connectionManager.baseTransport).to.equal('xhr_polling');
expect(realtime.connection.connectionManager.upgradeTransports).to.deep.equal([
Expand All @@ -437,7 +437,7 @@ define(['shared_helper', 'chai'], function (helper, chai) {
* realtime)
*/
it('connection behaviour with a proxy through which streaming is broken', function (done) {
const realtime = helper.AblyRealtime({
const realtime = helper.AblyRealtimePromise({
transportParams: {
simulateNoStreamingProxy: true,
maxStreamDuration: 7500,
Expand Down
7 changes: 4 additions & 3 deletions test/browser/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {
var rest;
var expect = chai.expect;
var whenPromiseSettles = helper.whenPromiseSettles;

describe('rest/http/fetch', function () {
this.timeout(60 * 1000);
Expand All @@ -11,7 +12,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {
initialXhrSupported = Ably.Rest.Platform.Config.xhrSupported;
Ably.Rest.Platform.Config.xhrSupported = false;
helper.setupApp(function () {
rest = helper.AblyRest();
rest = helper.AblyRestPromise();
done();
});
});
Expand All @@ -33,15 +34,15 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {

it('Should succeed in using fetch to publish a message', function (done) {
const channel = rest.channels.get('http_test_channel');
channel.publish('test', 'Testing fetch support', (err) => {
whenPromiseSettles(channel.publish('test', 'Testing fetch support'), (err) => {
expect(err).to.not.exist;
done();
});
});

it('Should pass errors correctly', function (done) {
const channel = rest.channels.get('');
channel.publish('test', 'Invalid message', (err) => {
whenPromiseSettles(channel.publish('test', 'Invalid message'), (err) => {
expect(err).to.exist;
done();
});
Expand Down
7 changes: 4 additions & 3 deletions test/browser/simple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {
var expect = chai.expect;
var whenPromiseSettles = helper.whenPromiseSettles;

describe('browser/simple', function () {
this.timeout(60 * 1000);
Expand All @@ -22,7 +23,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {
function realtimeConnection(transports) {
var options = {};
if (transports) options.transports = transports;
return helper.AblyRealtime(options);
return helper.AblyRealtimePromise(options);
}

function failWithin(timeInSeconds, done, ably, description) {
Expand Down Expand Up @@ -71,7 +72,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {
ably.connection.on('connected', function () {
connectionTimeout.stop();
heartbeatTimeout = failWithin(25, done, ably, 'wait for heartbeat');
ably.connection.ping(function (err) {
whenPromiseSettles(ably.connection.ping(), function (err) {
heartbeatTimeout.stop();
done(err);
ably.close();
Expand Down Expand Up @@ -115,7 +116,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, helper, chai) {
receiveMessagesTimeout = failWithin(15, done, ably, 'wait for published messages to be received');

timer = setInterval(function () {
channel.publish('event0', 'Hello world at: ' + new Date(), function (err) {
whenPromiseSettles(channel.publish('event0', 'Hello world at: ' + new Date()), function (err) {
sentCbCount++;
checkFinish();
});
Expand Down
14 changes: 14 additions & 0 deletions test/common/modules/shared_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ define([
});
}

/**
* Uses a callback to communicate the result of a `Promise`. The first argument passed to the callback will be either an error (when the promise is rejected) or `null` (when the promise is fulfilled). In the case where the promise is fulfilled, the resulting value will be passed to the callback as a second argument.
*/
function whenPromiseSettles(promise, callback) {
promise
.then((result) => {
callback(null, result);
})
.catch((err) => {
callback(err);
});
}

function simulateDroppedConnection(realtime) {
// Go into the 'disconnected' state before actually disconnecting the transports
// to avoid the instantaneous reconnect attempt that would be triggered in
Expand Down Expand Up @@ -237,5 +250,6 @@ define([
unroutableAddress: unroutableAddress,
arrFind: arrFind,
arrFilter: arrFilter,
whenPromiseSettles: whenPromiseSettles,
});
});
Loading

0 comments on commit 43a2d1d

Please sign in to comment.