From 2f1c99de8536e796062ba09cbbbfb86c8ca0d720 Mon Sep 17 00:00:00 2001 From: Chris Bannister Date: Tue, 7 Jan 2025 14:10:38 +0000 Subject: [PATCH] test/realtime/message: reduce subscribes to filtered channel flakes 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 --- test/realtime/message.test.js | 63 +++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/test/realtime/message.test.js b/test/realtime/message.test.js index 9f54cce7d..9c103472b 100644 --- a/test/realtime/message.test.js +++ b/test/realtime/message.test.js @@ -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);