Skip to content

Commit

Permalink
resume: add recover test
Browse files Browse the repository at this point in the history
  • Loading branch information
andydunstall authored and SimonWoolf committed Jan 16, 2023
1 parent a68255b commit e1c04a9
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions test/realtime/resume.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,5 +624,87 @@ define(['shared_helper', 'async', 'chai'], function (helper, async, chai) {
closeAndFinish(done, [sender_realtime, receiver_realtime, resumed_receiver_realtime], err);
}
});

// Tests recovering multiple channels only receives the expected messages.
it('recover multiple channels', async function () {
const NUM_MSGS = 5;

const txRest = helper.AblyRest();
const rxRealtime = helper.AblyRealtime(
{
transports: [helper.bestTransport],
promises: true,
},
true
);

const channelNames = Array(5)
.fill()
.map(() => String(Math.random()));
const rxChannels = channelNames.map((name) => rxRealtime.channels.get(name));

await Promise.all(rxChannels.map((channel) => channel.attach()));

// Do a few publishes on each channel so the channelSerial is set.
await Promise.all(
channelNames.map(async (name) => {
const tx = txRest.channels.get(name);
const rx = rxRealtime.channels.get(name);

for (let i = 0; i < NUM_MSGS; i++) {
const pSubscribe = rx.subscriptions.once();
await tx.publish(null, null);
await pSubscribe;
}
})
);

const connectionId = rxRealtime.connection.id;
const connectionKey = rxRealtime.connection.key;

// Get the recovery key before becoming suspended as it will be reset.
const recoveryKey = rxRealtime.connection.recoveryKey;

await new Promise((resolve) => helper.becomeSuspended(rxRealtime, resolve));

// Send some messages after we've detached that should be recovered.
for (const name of channelNames) {
const tx = txRest.channels.get(name);
for (let i = 0; i < NUM_MSGS; i++) {
await tx.publish('sentWhileDisconnected', null);
}
}

const rxRealtimeRecover = helper.AblyRealtime({ recover: recoveryKey });
const rxRecoverChannels = channelNames.map((name) => rxRealtimeRecover.channels.get(name));

// Attach recovered channels and check we receive the expected messages.
await Promise.all(
rxRecoverChannels.map(async (channel) => {
await channel.attach();

await new Promise((resolve) => {
let recoveredCount = 0;
channel.subscribe((msg) => {
// Check we don't receive unexpected messages.
expect(msg.name).to.equal('sentWhileDisconnected');

recoveredCount++;
if (recoveredCount === NUM_MSGS) {
resolve();
}
});
});
})
);

// RTN16d: After recovery expect the connection ID to be the same but the
// key should have updated.
expect(rxRealtimeRecover.connection.id).to.equal(connectionId);
expect(rxRealtimeRecover.connection.key).to.not.equal(connectionKey);

await rxRealtime.close();
await rxRealtimeRecover.close();
});
});
});

0 comments on commit e1c04a9

Please sign in to comment.