Skip to content

Commit

Permalink
Merge pull request #1957 from ably/fix-recovery-tests
Browse files Browse the repository at this point in the history
Fix failing connection recovery tests
  • Loading branch information
lawrence-forooghian authored Jan 27, 2025
2 parents 31d6f2e + 8bc268f commit 65a89fb
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions test/browser/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,26 +213,6 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) {
});
});

// strip instanceID and handleID from connectionKey */
function connectionHmac(key) {
/* connectionKey has the form <instanceID>!<hmac>-<handleID> */

/* remove the handleID from the end of key */
let k = key.split('-')[0];

/* skip the server instanceID if present, as reconnects may be routed to different frontends */
if (k.includes('!')) {
k = k.split('!')[1];
}
return k;
}

/* uses internal realtime knowledge of the format of the connection key to
* check if a connection key is the result of a successful recovery of another */
function sameConnection(keyA, keyB) {
return connectionHmac(keyA) === connectionHmac(keyB);
}

/**
* @specpartial RTN16 test
* @specpartial RTN16d
Expand All @@ -251,7 +231,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) {
helper.monitorConnection(done, realtime);

realtime.connection.once('connected', function () {
var connectionKey = realtime.connection.key;
var connectionId = realtime.connection.id;
document.dispatchEvent(refreshEvent);
try {
expect(realtime.connection.state).to.equal(
Expand All @@ -267,10 +247,10 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) {
var newRealtime = helper.AblyRealtime(realtimeOpts);
newRealtime.connection.once('connected', function () {
try {
expect(
sameConnection(connectionKey, newRealtime.connection.key),
expect(newRealtime.connection.id).to.equal(
connectionId,
'Check new realtime recovered the connection from the cookie',
).to.be.ok;
);
} catch (err) {
helper.closeAndFinish(done, [realtime, newRealtime], err);
return;
Expand All @@ -295,7 +275,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) {
helper.monitorConnection(done, realtime);

realtime.connection.once('connected', function () {
var connectionKey = realtime.connection.key;
var connectionId = realtime.connection.id;
document.dispatchEvent(refreshEvent);
try {
expect(realtime.connection.state).to.equal(
Expand All @@ -311,10 +291,10 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) {
var newRealtime = helper.AblyRealtime(realtimeOpts);
newRealtime.connection.once('connected', function () {
try {
expect(
!sameConnection(connectionKey, newRealtime.connection.key),
expect(newRealtime.connection.id).not.to.equal(
connectionId,
'Check new realtime created a new connection',
).to.be.ok;
);
} catch (err) {
helper.closeAndFinish(done, [realtime, newRealtime], err);
return;
Expand Down Expand Up @@ -359,7 +339,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) {
helper.monitorConnection(done, realtime);

realtime.connection.once('connected', function () {
var connectionKey = realtime.connection.key,
var connectionId = realtime.connection.id,
recoveryKey = realtime.connection.recoveryKey;

document.dispatchEvent(refreshEvent);
Expand All @@ -377,8 +357,7 @@ define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) {
var newRealtime = helper.AblyRealtime({ recover: recoveryKey });
newRealtime.connection.once('connected', function () {
try {
expect(sameConnection(connectionKey, newRealtime.connection.key), 'Check new realtime recovered the old')
.to.be.ok;
expect(newRealtime.connection.id).to.equal(connectionId, 'Check new realtime recovered the old');
} catch (err) {
helper.closeAndFinish(done, [realtime, newRealtime], err);
return;
Expand Down

0 comments on commit 65a89fb

Please sign in to comment.