Skip to content

Commit

Permalink
Try a different way of testing using async API
Browse files Browse the repository at this point in the history
I won't make any changes to `once` or `whenState` because we want to
keep them

TODO remove specific Promise tests

TODO what is Ably.Realtime vs Ably.Realtime.Promise and have I
converted properly? I think these might only be direclty used in the
init and api tests

TODO what about uncaught promises

TODO a better name for ptcb

TODO remove the SkippingError

Resolves #1213.
  • Loading branch information
lawrence-forooghian committed Jun 20, 2023
1 parent 656414a commit 5fbeace
Show file tree
Hide file tree
Showing 21 changed files with 705 additions and 657 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 ptcb = helper.promiseToCallback;

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) => {
ptcb(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) => {
ptcb(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 ptcb = helper.promiseToCallback;

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) {
ptcb(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) {
ptcb(channel.publish('event0', 'Hello world at: ' + new Date()), function (err) {
sentCbCount++;
checkFinish();
});
Expand Down
18 changes: 18 additions & 0 deletions test/common/modules/shared_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ define([
});
}

function promiseToCallback(promise, callback) {
promise
.then((result) => {
callback(null, result);
})
.catch((err) => {
callback(err);
});
}

function promiseToCallbackSkippingError(promise, callback) {
promise.then((result) => {
callback(result);
});
}

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 @@ -238,5 +254,7 @@ define([
unroutableAddress: unroutableAddress,
arrFind: arrFind,
arrFilter: arrFilter,
promiseToCallback: promiseToCallback,
promiseToCallbackSkippingError: promiseToCallbackSkippingError,
});
});
Loading

0 comments on commit 5fbeace

Please sign in to comment.