make connection utility to capture messages

This commit is contained in:
Leonardo Venturini
2024-07-25 18:03:42 -04:00
parent 1a0a782724
commit ae6ec98e2e
3 changed files with 58 additions and 22 deletions

View File

@@ -447,20 +447,9 @@ Tinytest.addAsync("livedata server - waiting for Promise", (test, onComplete) =>
* https://github.com/meteor/meteor/issues/13212
*/
Tinytest.addAsync('livedata server - publish cursor is properly awaited', async function (test) {
const messages = []
let sub = null;
const { clientConn } = await getTestConnections(test)
const send = clientConn._stream.send
clientConn._stream.send = function (...args) {
send.apply(this, args)
messages.push(args[0])
}
clientConn._stream.on('message', message => messages.push(message));
const { conn, messages, cleanup } = await captureConnectionMessages(test);
const coll = new Mongo.Collection('items', {
defineMutationMethods: false,
@@ -482,7 +471,7 @@ Tinytest.addAsync('livedata server - publish cursor is properly awaited', async
const reactiveVar = new ReactiveVar(1);
const computation = Tracker.autorun(() => {
sub = clientConn.subscribe(publicationName, reactiveVar.get());
sub = conn.subscribe(publicationName, reactiveVar.get());
});
await sleep(100)
@@ -496,11 +485,13 @@ Tinytest.addAsync('livedata server - publish cursor is properly awaited', async
/**
* There shouldn't ever be `removed` messages here, otherwise the UI will glitch
*/
const parsedMessages = messages.map(m => EJSON.parse(m).msg)
const parsedMessages = messages.map(m => m.msg)
test.equal(parsedMessages, expectedMessages)
computation.stop();
cleanup()
});
function getTestConnections(test) {

View File

@@ -52,3 +52,34 @@ makeTestConnection = function (test, succeeded, failed) {
}
);
};
createTestConnectionPromise = function (test) {
return new Promise((resolve, reject) => {
makeTestConnection(test, resolve, reject);
});
};
captureConnectionMessages = async function (test) {
const messages = []
const conn = await createTestConnectionPromise(test);
const send = conn._stream.send;
conn._stream.send = function (...args) {
send.apply(this, args);
messages.push(EJSON.parse(args[0]));
}
conn._stream.on('message', message => messages.push(EJSON.parse(message)));
function cleanup() {
conn._stream.send = send
}
return {
conn,
messages,
cleanup
}
};

View File

@@ -22,15 +22,29 @@ Package.onUse(function (api) {
// the like.
api.use('ddp');
api.export([
'pollUntil', 'try_all_permutations',
'SeededRandom', 'clickElement', 'blurElement',
'focusElement', 'simulateEvent', 'getStyleProperty', 'canonicalizeHtml',
'renderToDiv', 'clickIt',
'withCallbackLogger', 'testAsyncMulti',
'simplePoll', 'runAndThrowIfNeeded',
'makeTestConnection', 'DomUtils', 'mockBehaviours', 'waitUntil']);
'pollUntil',
'try_all_permutations',
'SeededRandom',
'clickElement',
'blurElement',
'focusElement',
'simulateEvent',
'getStyleProperty',
'canonicalizeHtml',
'renderToDiv',
'clickIt',
'withCallbackLogger',
'testAsyncMulti',
'simplePoll',
'runAndThrowIfNeeded',
'DomUtils',
'mockBehaviours',
'waitUntil',
'makeTestConnection',
'createTestConnectionPromise',
'captureConnectionMessages',
]);
api.addFiles('try_all_permutations.js');
api.addFiles('async_multi.js');