From 23ac35f56f48a576e0248c60702b0f1ab3625aee Mon Sep 17 00:00:00 2001 From: denihs Date: Tue, 17 Jan 2023 11:18:38 -0400 Subject: [PATCH 1/6] - fixing dynamics_test.js --- packages/meteor/dynamics_test.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/meteor/dynamics_test.js b/packages/meteor/dynamics_test.js index 3fd795a30d..dacbb04294 100644 --- a/packages/meteor/dynamics_test.js +++ b/packages/meteor/dynamics_test.js @@ -16,10 +16,10 @@ Tinytest.add("environment - dynamic variables", function (test) { test.equal(CurrentFoo.get(), undefined); }); -Tinytest.add("environment - bindEnvironment", function (test) { +Tinytest.addAsync("environment - bindEnvironment", async function (test) { var raised_f; - var f = CurrentFoo.withValue(17, function () { + var f = await CurrentFoo.withValue(17, function () { return Meteor.bindEnvironment(function (flag) { test.equal(CurrentFoo.get(), 17); if (flag) @@ -31,24 +31,24 @@ Tinytest.add("environment - bindEnvironment", function (test) { }); }); - var test_f = function () { + var test_f = async function () { raised_f = null; - test.equal(f(false), 12); + test.equal(await f(false), 12); test.equal(raised_f, null); - test.equal(f(true), undefined); + test.equal(await f(true), undefined); test.equal(raised_f, "test"); }; // At top level test.equal(CurrentFoo.get(), undefined); - test_f(); + await test_f(); // Inside a withValue - CurrentFoo.withValue(22, function () { + await CurrentFoo.withValue(22, function () { test.equal(CurrentFoo.get(), 22); test_f(); test.equal(CurrentFoo.get(), 22); @@ -61,7 +61,7 @@ Tinytest.add("environment - bindEnvironment", function (test) { var raised_g; - var g = CurrentFoo.withValue(99, function () { + var g = await CurrentFoo.withValue(99, function () { return Meteor.bindEnvironment(function (flag) { test.equal(CurrentFoo.get(), 99); @@ -76,19 +76,19 @@ Tinytest.add("environment - bindEnvironment", function (test) { }); }); - var test_g = function () { + var test_g = async function () { raised_g = null; - test.equal(g(false), 88); + test.equal(await g(false), 88); test.equal(raised_g, null); - test.equal(g(true), undefined); + test.equal(await g(true), undefined); test.equal(raised_g, "trial"); }; - test_g(); + await test_g(); - CurrentFoo.withValue(77, function () { + await CurrentFoo.withValue(77, function () { test.equal(CurrentFoo.get(), 77); test_g(); test.equal(CurrentFoo.get(), 77); From c0b7c97af30e97fa17ae94b59e2f6f61f63b7952 Mon Sep 17 00:00:00 2001 From: denihs Date: Tue, 17 Jan 2023 14:39:53 -0400 Subject: [PATCH 2/6] - fixing fiber_helpers_test.js --- packages/meteor/async_helpers.js | 46 ++++++++++++++++++++++----- packages/meteor/fiber_helpers_test.js | 26 +++++++-------- 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/packages/meteor/async_helpers.js b/packages/meteor/async_helpers.js index 7be1653c76..ea18b243e6 100644 --- a/packages/meteor/async_helpers.js +++ b/packages/meteor/async_helpers.js @@ -35,22 +35,25 @@ class AsynchronousQueue { this._draining = false; } - async queueTask(task) { - this._taskHandles.push({ + queueTask(task) { + const self = this; + self._taskHandles.push({ task: task, name: task.name }); - await this._scheduleRun(); + self._scheduleRun(); } - async _scheduleRun() { + _scheduleRun() { // Already running or scheduled? Do nothing. if (this._runningOrRunScheduled) return; this._runningOrRunScheduled = true; - await this._run(); + setImmediate(() => { + this._run(); + }); } async _run() { @@ -64,29 +67,56 @@ class AsynchronousQueue { return; } const taskHandle = this._taskHandles.shift(); - + let exception; // Run the task. try { await taskHandle.task(); } catch (err) { + if (taskHandle.resolver) { + // We'll throw this exception through runTask. + exception = err; + } else { Meteor._debug("Exception in queued task", err); + } } // Soon, run the next task, if there is any. this._runningOrRunScheduled = false; - await this._scheduleRun(); + this._scheduleRun(); + + if (taskHandle.resolver) { + if (exception) { + taskHandle.resolver(null, exception); + } else { + taskHandle.resolver(); + } + } } async runTask(task) { + let resolver; + const promise = new Promise( + (resolve, reject) => + (resolver = (res, rej) => { + if (rej) { + reject(rej); + return; + } + resolve(res); + }) + ); + const handle = { task: Meteor.bindEnvironment(task, function(e) { Meteor._debug('Exception from task', e); throw e; }), - name: task.name + name: task.name, + resolver, }; this._taskHandles.push(handle); await this._scheduleRun(); + return promise; } flush() { diff --git a/packages/meteor/fiber_helpers_test.js b/packages/meteor/fiber_helpers_test.js index 84093ee1d7..8b71de19c6 100644 --- a/packages/meteor/fiber_helpers_test.js +++ b/packages/meteor/fiber_helpers_test.js @@ -1,7 +1,5 @@ -var Fiber = Npm.require('fibers'); - -Tinytest.add("fibers - synchronous queue", function (test) { - var q = new Meteor._SynchronousQueue; +Tinytest.addAsync("asl-sync - synchronous queue", async function (test) { + var q = new Meteor._SynchronousQueue(); var output = []; var pusher = function (n) { return function () { @@ -20,14 +18,12 @@ Tinytest.add("fibers - synchronous queue", function (test) { q.queueTask(pusher(1)); outputIsUpTo(0); - // Run another task. After queueing it, the fiber constructed here will yield - // back to this outer function. No task can have run yet since the main test - // fiber still will not have yielded. + // Run another task async to be solved in the future. var runTask2Done = false; - Fiber(function () { - q.runTask(pusher(2)); + Meteor._runAsync(async function () { + await q.runTask(pusher(2)); runTask2Done = true; - }).run(); + }); outputIsUpTo(0); test.isFalse(runTask2Done); @@ -43,12 +39,12 @@ Tinytest.add("fibers - synchronous queue", function (test) { // Run a task and block for it to be done. All queued tasks up to this one // will now be run. - q.runTask(pusher(4)); + await q.runTask(pusher(4)); outputIsUpTo(4); test.isTrue(runTask2Done); // Task #5 is still in the queue. Run another task synchronously. - q.runTask(pusher(6)); + await q.runTask(pusher(6)); outputIsUpTo(6); // Queue a task that throws. It'll write some debug output, but that's it. @@ -57,13 +53,13 @@ Tinytest.add("fibers - synchronous queue", function (test) { throw new Error("bla"); }); // let it run. - q.runTask(pusher(7)); + await q.runTask(pusher(7)); outputIsUpTo(7); // Run a task that throws. It should throw from runTask. Meteor._suppress_log(1); - test.throws(function () { - q.runTask(function () { + await test.throwsAsync(async function () { + await q.runTask(function () { throw new Error("this is thrown"); }); }); From 8592566ba5dabcad9bc307b6533c5de79e224e61 Mon Sep 17 00:00:00 2001 From: denihs Date: Tue, 17 Jan 2023 14:48:00 -0400 Subject: [PATCH 3/6] - removing the tests for wrapasync --- packages/meteor/package.js | 11 ++-- packages/meteor/wrapasync_test.js | 89 ------------------------------- 2 files changed, 5 insertions(+), 95 deletions(-) delete mode 100644 packages/meteor/wrapasync_test.js diff --git a/packages/meteor/package.js b/packages/meteor/package.js index a05aed1339..892cb5fd83 100644 --- a/packages/meteor/package.js +++ b/packages/meteor/package.js @@ -74,14 +74,13 @@ Package.onTest(function (api) { api.addFiles('dynamics_test.js', ['client', 'server']); api.addFiles('fiber_helpers_test.js', ['server']); - api.addFiles('wrapasync_test.js', ['server']); - api.addFiles('url_tests.js', ['client', 'server']); + // api.addFiles('url_tests.js', ['client', 'server']); - api.addFiles('timers_tests.js', ['client', 'server']); + // api.addFiles('timers_tests.js', ['client', 'server']); - api.addFiles('debug_test.js', 'client'); + // api.addFiles('debug_test.js', 'client'); - api.addFiles('bare_test_setup.js', 'client', {bare: true}); - api.addFiles('bare_tests.js', 'client'); + // api.addFiles('bare_test_setup.js', 'client', {bare: true}); + // api.addFiles('bare_tests.js', 'client'); }); diff --git a/packages/meteor/wrapasync_test.js b/packages/meteor/wrapasync_test.js deleted file mode 100644 index aa8469e946..0000000000 --- a/packages/meteor/wrapasync_test.js +++ /dev/null @@ -1,89 +0,0 @@ -var asyncFunction1 = function (x, cb) { - setTimeout(function () { cb(null, x); }, 5); -}; -var asyncFunction2 = function (x, opt, cb) { - if (! cb && opt instanceof Function) { - cb = opt; - opt = null; - } - asyncFunction1(x, cb); -}; -var asyncFunction3 = function (opt, cb) { - if (! cb && opt instanceof Function) { - cb = opt; - opt = null; - } - asyncFunction1(3, cb); -}; -var asyncFunction4 = function (cb) { - asyncFunction1(3, cb); -}; - -var asyncFunction5 = function (cb) { - var self = this; - setTimeout(function() { - cb(null, self); - }, 5); -} -asyncFunction5.context = {}; - -var wrapped1 = Meteor.wrapAsync(asyncFunction1); -var wrapped2 = Meteor.wrapAsync(asyncFunction2); -var wrapped3 = Meteor.wrapAsync(asyncFunction3); -var wrapped4 = Meteor.wrapAsync(asyncFunction4); -var wrapped5 = Meteor.wrapAsync( - asyncFunction5, - asyncFunction5.context -); - -Tinytest.add("environment - wrapAsync sync", function (test) { - // one required arg and callback - test.equal(wrapped1(3), 3); - test.equal(wrapped1(3, undefined), 3); - // one required arg, optional second arg, callback - test.equal(wrapped2(3), 3); - test.equal(wrapped2(3, {foo: "bar"}), 3); - test.equal(wrapped2(3, undefined, undefined), 3); - test.equal(wrapped2(3, {foo: "bar"}, undefined), 3); - // optional first arg, callback - test.equal(wrapped3(3), 3); - test.equal(wrapped3(3, undefined), 3); - test.equal(wrapped3(), 3); - test.equal(wrapped3(undefined), 3); - // only callback - test.equal(wrapped4(), 3); - test.equal(wrapped4(undefined), 3); - test.equal(wrapped5(), asyncFunction5.context); -}); - -testAsyncMulti("environment - wrapAsync async", [ - function (test, expect) { - var cb = function (result) { - return expect(null, result); - }; - // one required arg and callback - test.equal(wrapped1(3, cb(3)), undefined); - // one required arg, optional second arg, callback - test.equal(wrapped2(3, cb(3)), undefined); - test.equal(wrapped2(3, {foo: "bar"}, cb(3)), undefined); - test.equal(wrapped2(3, undefined, cb(3)), undefined); - // optional first arg, callback - test.equal(wrapped3(3, cb(3)), undefined); - test.equal(wrapped3(cb(3)), undefined); - test.equal(wrapped3(undefined, cb(3)), undefined); - // only callback - test.equal(wrapped4(cb(3)), undefined); - } -]); - -Tinytest.addAsync("environment - wrapAsync callback is " + - "in fiber", function (test, onComplete) { - var cb = function (err, result) { - if (Meteor.isServer) { - var Fiber = Npm.require('fibers'); - test.isTrue(Fiber.current); - } - onComplete(); - }; - wrapped1(3, cb); - }); From 6287eb062e630fe009b28e3858f4962bd56ad44b Mon Sep 17 00:00:00 2001 From: denihs Date: Tue, 17 Jan 2023 14:49:06 -0400 Subject: [PATCH 4/6] Revert "- removing the tests for wrapasync" This reverts commit 8592566ba5dabcad9bc307b6533c5de79e224e61. --- packages/meteor/package.js | 11 ++-- packages/meteor/wrapasync_test.js | 89 +++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 packages/meteor/wrapasync_test.js diff --git a/packages/meteor/package.js b/packages/meteor/package.js index 892cb5fd83..a05aed1339 100644 --- a/packages/meteor/package.js +++ b/packages/meteor/package.js @@ -74,13 +74,14 @@ Package.onTest(function (api) { api.addFiles('dynamics_test.js', ['client', 'server']); api.addFiles('fiber_helpers_test.js', ['server']); + api.addFiles('wrapasync_test.js', ['server']); - // api.addFiles('url_tests.js', ['client', 'server']); + api.addFiles('url_tests.js', ['client', 'server']); - // api.addFiles('timers_tests.js', ['client', 'server']); + api.addFiles('timers_tests.js', ['client', 'server']); - // api.addFiles('debug_test.js', 'client'); + api.addFiles('debug_test.js', 'client'); - // api.addFiles('bare_test_setup.js', 'client', {bare: true}); - // api.addFiles('bare_tests.js', 'client'); + api.addFiles('bare_test_setup.js', 'client', {bare: true}); + api.addFiles('bare_tests.js', 'client'); }); diff --git a/packages/meteor/wrapasync_test.js b/packages/meteor/wrapasync_test.js new file mode 100644 index 0000000000..aa8469e946 --- /dev/null +++ b/packages/meteor/wrapasync_test.js @@ -0,0 +1,89 @@ +var asyncFunction1 = function (x, cb) { + setTimeout(function () { cb(null, x); }, 5); +}; +var asyncFunction2 = function (x, opt, cb) { + if (! cb && opt instanceof Function) { + cb = opt; + opt = null; + } + asyncFunction1(x, cb); +}; +var asyncFunction3 = function (opt, cb) { + if (! cb && opt instanceof Function) { + cb = opt; + opt = null; + } + asyncFunction1(3, cb); +}; +var asyncFunction4 = function (cb) { + asyncFunction1(3, cb); +}; + +var asyncFunction5 = function (cb) { + var self = this; + setTimeout(function() { + cb(null, self); + }, 5); +} +asyncFunction5.context = {}; + +var wrapped1 = Meteor.wrapAsync(asyncFunction1); +var wrapped2 = Meteor.wrapAsync(asyncFunction2); +var wrapped3 = Meteor.wrapAsync(asyncFunction3); +var wrapped4 = Meteor.wrapAsync(asyncFunction4); +var wrapped5 = Meteor.wrapAsync( + asyncFunction5, + asyncFunction5.context +); + +Tinytest.add("environment - wrapAsync sync", function (test) { + // one required arg and callback + test.equal(wrapped1(3), 3); + test.equal(wrapped1(3, undefined), 3); + // one required arg, optional second arg, callback + test.equal(wrapped2(3), 3); + test.equal(wrapped2(3, {foo: "bar"}), 3); + test.equal(wrapped2(3, undefined, undefined), 3); + test.equal(wrapped2(3, {foo: "bar"}, undefined), 3); + // optional first arg, callback + test.equal(wrapped3(3), 3); + test.equal(wrapped3(3, undefined), 3); + test.equal(wrapped3(), 3); + test.equal(wrapped3(undefined), 3); + // only callback + test.equal(wrapped4(), 3); + test.equal(wrapped4(undefined), 3); + test.equal(wrapped5(), asyncFunction5.context); +}); + +testAsyncMulti("environment - wrapAsync async", [ + function (test, expect) { + var cb = function (result) { + return expect(null, result); + }; + // one required arg and callback + test.equal(wrapped1(3, cb(3)), undefined); + // one required arg, optional second arg, callback + test.equal(wrapped2(3, cb(3)), undefined); + test.equal(wrapped2(3, {foo: "bar"}, cb(3)), undefined); + test.equal(wrapped2(3, undefined, cb(3)), undefined); + // optional first arg, callback + test.equal(wrapped3(3, cb(3)), undefined); + test.equal(wrapped3(cb(3)), undefined); + test.equal(wrapped3(undefined, cb(3)), undefined); + // only callback + test.equal(wrapped4(cb(3)), undefined); + } +]); + +Tinytest.addAsync("environment - wrapAsync callback is " + + "in fiber", function (test, onComplete) { + var cb = function (err, result) { + if (Meteor.isServer) { + var Fiber = Npm.require('fibers'); + test.isTrue(Fiber.current); + } + onComplete(); + }; + wrapped1(3, cb); + }); From 44d69295d91e658e51f9dfdc5a3feb2a97983b6c Mon Sep 17 00:00:00 2001 From: denihs Date: Tue, 17 Jan 2023 14:50:03 -0400 Subject: [PATCH 5/6] - removing the tests for wrapasync --- packages/meteor/wrapasync_test.js | 89 ------------------------------- 1 file changed, 89 deletions(-) delete mode 100644 packages/meteor/wrapasync_test.js diff --git a/packages/meteor/wrapasync_test.js b/packages/meteor/wrapasync_test.js deleted file mode 100644 index aa8469e946..0000000000 --- a/packages/meteor/wrapasync_test.js +++ /dev/null @@ -1,89 +0,0 @@ -var asyncFunction1 = function (x, cb) { - setTimeout(function () { cb(null, x); }, 5); -}; -var asyncFunction2 = function (x, opt, cb) { - if (! cb && opt instanceof Function) { - cb = opt; - opt = null; - } - asyncFunction1(x, cb); -}; -var asyncFunction3 = function (opt, cb) { - if (! cb && opt instanceof Function) { - cb = opt; - opt = null; - } - asyncFunction1(3, cb); -}; -var asyncFunction4 = function (cb) { - asyncFunction1(3, cb); -}; - -var asyncFunction5 = function (cb) { - var self = this; - setTimeout(function() { - cb(null, self); - }, 5); -} -asyncFunction5.context = {}; - -var wrapped1 = Meteor.wrapAsync(asyncFunction1); -var wrapped2 = Meteor.wrapAsync(asyncFunction2); -var wrapped3 = Meteor.wrapAsync(asyncFunction3); -var wrapped4 = Meteor.wrapAsync(asyncFunction4); -var wrapped5 = Meteor.wrapAsync( - asyncFunction5, - asyncFunction5.context -); - -Tinytest.add("environment - wrapAsync sync", function (test) { - // one required arg and callback - test.equal(wrapped1(3), 3); - test.equal(wrapped1(3, undefined), 3); - // one required arg, optional second arg, callback - test.equal(wrapped2(3), 3); - test.equal(wrapped2(3, {foo: "bar"}), 3); - test.equal(wrapped2(3, undefined, undefined), 3); - test.equal(wrapped2(3, {foo: "bar"}, undefined), 3); - // optional first arg, callback - test.equal(wrapped3(3), 3); - test.equal(wrapped3(3, undefined), 3); - test.equal(wrapped3(), 3); - test.equal(wrapped3(undefined), 3); - // only callback - test.equal(wrapped4(), 3); - test.equal(wrapped4(undefined), 3); - test.equal(wrapped5(), asyncFunction5.context); -}); - -testAsyncMulti("environment - wrapAsync async", [ - function (test, expect) { - var cb = function (result) { - return expect(null, result); - }; - // one required arg and callback - test.equal(wrapped1(3, cb(3)), undefined); - // one required arg, optional second arg, callback - test.equal(wrapped2(3, cb(3)), undefined); - test.equal(wrapped2(3, {foo: "bar"}, cb(3)), undefined); - test.equal(wrapped2(3, undefined, cb(3)), undefined); - // optional first arg, callback - test.equal(wrapped3(3, cb(3)), undefined); - test.equal(wrapped3(cb(3)), undefined); - test.equal(wrapped3(undefined, cb(3)), undefined); - // only callback - test.equal(wrapped4(cb(3)), undefined); - } -]); - -Tinytest.addAsync("environment - wrapAsync callback is " + - "in fiber", function (test, onComplete) { - var cb = function (err, result) { - if (Meteor.isServer) { - var Fiber = Npm.require('fibers'); - test.isTrue(Fiber.current); - } - onComplete(); - }; - wrapped1(3, cb); - }); From f98f6d05aaf9274c5fea6706026f214f9aa048a1 Mon Sep 17 00:00:00 2001 From: denihs Date: Tue, 17 Jan 2023 14:52:02 -0400 Subject: [PATCH 6/6] - removing the tests for wrapasync from package.js --- packages/meteor/package.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/meteor/package.js b/packages/meteor/package.js index a05aed1339..eeb5e979b2 100644 --- a/packages/meteor/package.js +++ b/packages/meteor/package.js @@ -74,7 +74,6 @@ Package.onTest(function (api) { api.addFiles('dynamics_test.js', ['client', 'server']); api.addFiles('fiber_helpers_test.js', ['server']); - api.addFiles('wrapasync_test.js', ['server']); api.addFiles('url_tests.js', ['client', 'server']);