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);