Skip to content

Commit

Permalink
test/realtime/message: reduce subscribes to filtered channel flakes
Browse files Browse the repository at this point in the history
The test waits to receive an end message on a channel then it runs the
assertions, this can cause flakes because we dont know if the event loop
had any other pending IO which could include delivering the 2 expected
filtered messages. Run the assertions in a setImmediate so that it executes
in the next event loop run allowing any queued messages to be delivered.

When running the test with logging the messages were being delivered but
the handler was being called after the assertions had ran.

RAR-656
  • Loading branch information
Zariel committed Jan 7, 2025
1 parent 1d6483a commit 2f1c99d
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions test/realtime/message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1438,36 +1438,43 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, Helper, async

// Subscription to check all messages were received as expected
rtUnfilteredChannel.subscribe('end', function (msg) {
try {
expect(msg.data).to.equal(testData[testData.length - 1].data, 'Unexpected msg data received');

// Check that we receive expected messages on filtered channel
expect(filteredMessages.length).to.equal(2, 'Expect only two filtered message to be received');
expect(filteredMessages[0].data).to.equal(testData[0].data, 'Unexpected data received');
expect(filteredMessages[1].data).to.equal(testData[2].data, 'Unexpected data received');
expect(filteredMessages[0].extras.headers.name).to.equal(
testData[0].extras.headers.name,
'Unexpected header value received',
);
expect(filteredMessages[1].extras.headers.name).to.equal(
testData[2].extras.headers.name,
'Unexpected header value received',
);
// Check that message with header that doesn't meet filtering condition is not received.
for (msg of filteredMessages) {
expect(msg.extras.headers.number).to.equal(26095, 'Unexpected header filtering value received');
}
// Ensure all pending I/O operations complete and messages are processed
// before running assertions to avoid race conditions
//
// Using setTimeout with 0 timeout as setImmediate is not available
// in browsers.
setTimeout(() => {
try {
expect(msg.data).to.equal(testData[testData.length - 1].data, 'Unexpected msg data received');

// Check that we receive expected messages on filtered channel
expect(filteredMessages.length).to.equal(2, 'Expect only two filtered message to be received');
expect(filteredMessages[0].data).to.equal(testData[0].data, 'Unexpected data received');
expect(filteredMessages[1].data).to.equal(testData[2].data, 'Unexpected data received');
expect(filteredMessages[0].extras.headers.name).to.equal(
testData[0].extras.headers.name,
'Unexpected header value received',
);
expect(filteredMessages[1].extras.headers.name).to.equal(
testData[2].extras.headers.name,
'Unexpected header value received',
);
// Check that message with header that doesn't meet filtering condition is not received.
for (msg of filteredMessages) {
expect(msg.extras.headers.number).to.equal(26095, 'Unexpected header filtering value received');
}

// Check that we receive expected messages on unfiltered channel, including the `end` event message
expect(unFilteredMessages.length).to.equal(6, 'Expect only 6 unfiltered message to be received');
for (var i = 0; i < unFilteredMessages.length; i++) {
expect(unFilteredMessages[i].data).to.equal(testData[i].data, 'Unexpected data received');
// Check that we receive expected messages on unfiltered channel, including the `end` event message
expect(unFilteredMessages.length).to.equal(6, 'Expect only 6 unfiltered message to be received');
for (var i = 0; i < unFilteredMessages.length; i++) {
expect(unFilteredMessages[i].data).to.equal(testData[i].data, 'Unexpected data received');
}
} catch (err) {
helper.closeAndFinish(done, realtime, err);
return;
}
} catch (err) {
helper.closeAndFinish(done, realtime, err);
return;
}
helper.closeAndFinish(done, realtime);
helper.closeAndFinish(done, realtime);
}, 0);
});
var restChannel = rest.channels.get('chan');
restChannel.publish(testData);
Expand Down

0 comments on commit 2f1c99d

Please sign in to comment.