Skip to content

Commit

Permalink
Make some tests return a Promise
Browse files Browse the repository at this point in the history
These are the tests that call RealtimeChannel#processMessage at their
top level. This is preparation for making that method asynchronous as
part of #1293 (making ICipher.decrypt asynchronous).
  • Loading branch information
lawrence-forooghian committed Jun 1, 2023
1 parent e9affa4 commit 3e04d10
Showing 1 changed file with 176 additions and 144 deletions.
320 changes: 176 additions & 144 deletions test/realtime/sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
* sync in progress, then do one sync, then a second with a slightly
* different presence set
*/
it('sync_existing_set', function (done) {
it('sync_existing_set', async function () {
var realtime = helper.AblyRealtime({ autoConnect: false }),
channelName = 'syncexistingset',
channel = realtime.channels.get(channelName);
Expand All @@ -55,96 +55,102 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
})
);

async.series(
[
function (cb) {
channel.processMessage({
action: 16,
channel: channelName,
presence: [
{
action: 'present',
clientId: 'one',
connectionId: 'one_connid',
id: 'one_connid:0:0',
timestamp: 1e12,
},
{
action: 'present',
clientId: 'two',
connectionId: 'two_connid',
id: 'two_connid:0:0',
timestamp: 1e12,
},
],
});
cb();
},
function (cb) {
channel.presence.get(function (err, results) {
try {
expect(results.length).to.equal(2, 'Check correct number of results');
expect(channel.presence.syncComplete, 'Check in sync').to.be.ok;
expect(extractClientIds(results)).to.deep.equal(['one', 'two'], 'check correct members');
} catch (err) {
await new Promise(function (resolve, reject) {
var done = function (err) {
err ? reject(err) : resolve();
};

async.series(
[
function (cb) {
channel.processMessage({
action: 16,
channel: channelName,
presence: [
{
action: 'present',
clientId: 'one',
connectionId: 'one_connid',
id: 'one_connid:0:0',
timestamp: 1e12,
},
{
action: 'present',
clientId: 'two',
connectionId: 'two_connid',
id: 'two_connid:0:0',
timestamp: 1e12,
},
],
});
cb();
},
function (cb) {
channel.presence.get(function (err, results) {
try {
expect(results.length).to.equal(2, 'Check correct number of results');
expect(channel.presence.syncComplete, 'Check in sync').to.be.ok;
expect(extractClientIds(results)).to.deep.equal(['one', 'two'], 'check correct members');
} catch (err) {
cb(err);
return;
}
cb(err);
return;
}
cb(err);
});
},
function (cb) {
/* Trigger another sync. Two has gone without so much as a `leave` message! */
channel.processMessage({
action: 16,
channel: channelName,
presence: [
{
action: 'present',
clientId: 'one',
connectionId: 'one_connid',
id: 'one_connid:0:0',
timestamp: 1e12,
},
{
action: 'present',
clientId: 'three',
connectionId: 'three_connid',
id: 'three_connid:0:0',
timestamp: 1e12,
},
],
});
cb();
},
function (cb) {
channel.presence.get(function (err, results) {
try {
expect(results.length).to.equal(2, 'Check correct number of results');
expect(channel.presence.syncComplete, 'Check in sync').to.be.ok;
expect(extractClientIds(results)).to.deep.equal(
['one', 'three'],
'check two has gone and three is there'
);
} catch (err) {
});
},
function (cb) {
/* Trigger another sync. Two has gone without so much as a `leave` message! */
channel.processMessage({
action: 16,
channel: channelName,
presence: [
{
action: 'present',
clientId: 'one',
connectionId: 'one_connid',
id: 'one_connid:0:0',
timestamp: 1e12,
},
{
action: 'present',
clientId: 'three',
connectionId: 'three_connid',
id: 'three_connid:0:0',
timestamp: 1e12,
},
],
});
cb();
},
function (cb) {
channel.presence.get(function (err, results) {
try {
expect(results.length).to.equal(2, 'Check correct number of results');
expect(channel.presence.syncComplete, 'Check in sync').to.be.ok;
expect(extractClientIds(results)).to.deep.equal(
['one', 'three'],
'check two has gone and three is there'
);
} catch (err) {
cb(err);
return;
}
cb(err);
return;
}
cb(err);
});
},
],
function (err) {
closeAndFinish(done, realtime, err);
}
);
});
},
],
function (err) {
closeAndFinish(done, realtime, err);
}
);
});
});

/*
* Sync with an existing presence set and a presence member added in the
* middle of the sync should should discard the former, but not the latter
* */
it('sync_member_arrives_in_middle', function (done) {
it('sync_member_arrives_in_middle', async function () {
var realtime = helper.AblyRealtime({ autoConnect: false }),
channelName = 'sync_member_arrives_in_middle',
channel = realtime.channels.get(channelName);
Expand Down Expand Up @@ -217,27 +223,36 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
],
});

channel.presence.get(function (err, results) {
if (err) {
closeAndFinish(done, realtime, err);
return;
}
try {
expect(results.length).to.equal(3, 'Check correct number of results');
expect(channel.presence.syncComplete, 'Check in sync').to.be.ok;
expect(extractClientIds(results)).to.deep.equal(['four', 'three', 'two'], 'check expected presence members');
} catch (err) {
closeAndFinish(done, realtime, err);
return;
}
closeAndFinish(done, realtime);
await new Promise(function (resolve, reject) {
var done = function (err) {
err ? reject(err) : resolve();
};

channel.presence.get(function (err, results) {
if (err) {
closeAndFinish(done, realtime, err);
return;
}
try {
expect(results.length).to.equal(3, 'Check correct number of results');
expect(channel.presence.syncComplete, 'Check in sync').to.be.ok;
expect(extractClientIds(results)).to.deep.equal(
['four', 'three', 'two'],
'check expected presence members'
);
} catch (err) {
closeAndFinish(done, realtime, err);
return;
}
closeAndFinish(done, realtime);
});
});
});

/*
* Presence message that was in the sync arrives again as a normal message, after it's come in the sync
*/
it('sync_member_arrives_normally_after_came_in_sync', function (done) {
it('sync_member_arrives_normally_after_came_in_sync', async function () {
var realtime = helper.AblyRealtime({ autoConnect: false }),
channelName = 'sync_member_arrives_normally_after_came_in_sync',
channel = realtime.channels.get(channelName);
Expand Down Expand Up @@ -294,27 +309,33 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
],
});

channel.presence.get(function (err, results) {
if (err) {
closeAndFinish(done, realtime, err);
return;
}
try {
expect(results.length).to.equal(2, 'Check correct number of results');
expect(channel.presence.syncComplete, 'Check in sync').to.be.ok;
expect(extractClientIds(results)).to.deep.equal(['one', 'two'], 'check expected presence members');
} catch (err) {
closeAndFinish(done, realtime, err);
return;
}
closeAndFinish(done, realtime);
await new Promise(function (resolve, reject) {
var done = function (err) {
err ? reject(err) : resolve();
};

channel.presence.get(function (err, results) {
if (err) {
closeAndFinish(done, realtime, err);
return;
}
try {
expect(results.length).to.equal(2, 'Check correct number of results');
expect(channel.presence.syncComplete, 'Check in sync').to.be.ok;
expect(extractClientIds(results)).to.deep.equal(['one', 'two'], 'check expected presence members');
} catch (err) {
closeAndFinish(done, realtime, err);
return;
}
closeAndFinish(done, realtime);
});
});
});

/*
* Presence message that will be in the sync arrives as a normal message, before it comes in the sync
*/
it('sync_member_arrives_normally_before_comes_in_sync', function (done) {
it('sync_member_arrives_normally_before_comes_in_sync', async function () {
var realtime = helper.AblyRealtime({ autoConnect: false }),
channelName = 'sync_member_arrives_normally_before_comes_in_sync',
channel = realtime.channels.get(channelName);
Expand Down Expand Up @@ -371,28 +392,34 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
],
});

channel.presence.get(function (err, results) {
if (err) {
closeAndFinish(done, realtime, err);
return;
}
try {
expect(results.length).to.equal(2, 'Check correct number of results');
expect(channel.presence.syncComplete, 'Check in sync').to.be.ok;
expect(extractClientIds(results)).to.deep.equal(['one', 'two'], 'check expected presence members');
} catch (err) {
closeAndFinish(done, realtime, err);
return;
}
closeAndFinish(done, realtime);
await new Promise(function (resolve, reject) {
var done = function (err) {
err ? reject(err) : resolve();
};

channel.presence.get(function (err, results) {
if (err) {
closeAndFinish(done, realtime, err);
return;
}
try {
expect(results.length).to.equal(2, 'Check correct number of results');
expect(channel.presence.syncComplete, 'Check in sync').to.be.ok;
expect(extractClientIds(results)).to.deep.equal(['one', 'two'], 'check expected presence members');
} catch (err) {
closeAndFinish(done, realtime, err);
return;
}
closeAndFinish(done, realtime);
});
});
});

/*
* Get several presence messages with various combinations of msgserial,
* index, and synthesized leaves, check that the end result is correct
*/
it('presence_ordering', function (done) {
it('presence_ordering', async function () {
var realtime = helper.AblyRealtime({ autoConnect: false }),
channelName = 'sync_ordering',
channel = realtime.channels.get(channelName);
Expand Down Expand Up @@ -518,22 +545,27 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
],
});

channel.presence.get(function (err, results) {
if (err) {
closeAndFinish(done, realtime, err);
return;
}
try {
expect(results.length).to.equal(2, 'Check correct number of results');
expect(channel.presence.syncComplete, 'Check in sync').to.be.ok;
expect(extractClientIds(results)).to.deep.equal(['one', 'two'], 'check expected presence members');
expect(extractMember(results, 'one').data).to.equal('onedata', 'check correct data on one');
expect(extractMember(results, 'two').data).to.equal('twodata', 'check correct data on two');
} catch (err) {
closeAndFinish(done, realtime, err);
return;
}
closeAndFinish(done, realtime);
await new Promise(function (resolve, reject) {
var done = function (err) {
err ? reject(err) : resolve();
};
channel.presence.get(function (err, results) {
if (err) {
closeAndFinish(done, realtime, err);
return;
}
try {
expect(results.length).to.equal(2, 'Check correct number of results');
expect(channel.presence.syncComplete, 'Check in sync').to.be.ok;
expect(extractClientIds(results)).to.deep.equal(['one', 'two'], 'check expected presence members');
expect(extractMember(results, 'one').data).to.equal('onedata', 'check correct data on one');
expect(extractMember(results, 'two').data).to.equal('twodata', 'check correct data on two');
} catch (err) {
closeAndFinish(done, realtime, err);
return;
}
closeAndFinish(done, realtime);
});
});
});

Expand Down

0 comments on commit 3e04d10

Please sign in to comment.