From 666f60e9b11b22ae713124ce69fdfb744884b46f 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 | 60 +++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/test/realtime/message.test.js b/test/realtime/message.test.js index 9f54cce7d..d74ba00be 100644 --- a/test/realtime/message.test.js +++ b/test/realtime/message.test.js @@ -1438,36 +1438,40 @@ 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 we let any pending IO complete as messages may be queued up but not yet + // added to filteredMessages + setImmediate(() => { + 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); + }); }); var restChannel = rest.channels.get('chan'); restChannel.publish(testData);