Removed unnecessary awaits.

This commit is contained in:
Radosław Miernik
2022-07-26 13:48:08 +02:00
parent 39f04caa2b
commit a6319476ba
8 changed files with 44 additions and 81 deletions

View File

@@ -96,15 +96,7 @@ Mongo.Collection = function Collection(name, options) {
else if (Meteor.isClient) this._connection = Meteor.connection;
else this._connection = Meteor.server;
if (options._driver) {
if (typeof options._driver.open !== 'function') {
throw new Error('If you are creating the driver manually using new ' +
'MongoInternals.RemoteCollectionDriver then you need to use ' +
'Promise.await() or await on it since it is async in recent ' +
'versions of Meteor. ' +
'Read more: https://docs.meteor.com/changelog.html.');
}
} else {
if (!options._driver) {
// XXX This check assumes that webapp is loaded so that Meteor.server !==
// null. We should fully support the case of "want to use a Mongo-backed
// collection from Node code without webapp", but we don't yet.
@@ -115,7 +107,7 @@ Mongo.Collection = function Collection(name, options) {
typeof MongoInternals !== 'undefined' &&
MongoInternals.defaultRemoteCollectionDriver
) {
options._driver = MongoInternals.getDefaultRemoteCollectionDriver();
options._driver = MongoInternals.defaultRemoteCollectionDriver();
} else {
const { LocalCollectionDriver } = require('./local_collection_driver.js');
options._driver = LocalCollectionDriver;
@@ -907,6 +899,6 @@ ASYNC_COLLECTION_METHODS.forEach(methodName => {
if (Meteor.isServer) {
const userOptions = Meteor.settings?.packages?.mongo || {};
if (!userOptions?.skipStartupConnection && !process.env.METEOR_TEST_FAKE_MONGOD_CONTROL_PORT) {
Promise.await(MongoInternals.defaultRemoteCollectionDriver());
MongoInternals.defaultRemoteCollectionDriver();
}
}

View File

@@ -3,7 +3,7 @@ var Future = Npm.require('fibers/future');
import { DocFetcher } from "./doc_fetcher.js";
testAsyncMulti("mongo-livedata - doc fetcher", [
async function (test, expect) {
function (test, expect) {
var self = this;
var collName = "docfetcher-" + Random.id();
var collection = new Mongo.Collection(collName);
@@ -11,7 +11,7 @@ testAsyncMulti("mongo-livedata - doc fetcher", [
var id2 = collection.insert({y: 2});
var fetcher = new DocFetcher(
(await MongoInternals.defaultRemoteCollectionDriver()).mongo);
MongoInternals.defaultRemoteCollectionDriver().mongo);
// Test basic operation.
const fakeOp1 = {};

View File

@@ -1,15 +0,0 @@
// Returns an async function that will be executed at most one time,
// no matter how often you call it. Useful for lazy initialization.
export const onceAsync = func => {
let ran = false;
let memo = undefined;
return async function executeOnce() {
if (ran) return memo;
const memoPromise = func.apply(this, arguments);
memo = await memoPromise;
func = null;
ran = true;
return memo;
};
};

View File

@@ -12,6 +12,7 @@ import { normalizeProjection } from "./mongo_utils";
const path = require("path");
const util = require("util");
/** @type {import('mongodb')} */
var MongoDB = NpmModuleMongodb;
var Future = Npm.require('fibers/future');
import { DocFetcher } from "./doc_fetcher.js";
@@ -140,7 +141,7 @@ var replaceTypes = function (document, atomTransformer) {
};
MongoConnection = async function (url, options) {
MongoConnection = function (url, options) {
var self = this;
options = options || {};
self._observeMultiplexers = {};
@@ -219,11 +220,9 @@ MongoConnection = async function (url, options) {
}));
if (options.oplogUrl && ! Package['disable-oplog']) {
self._oplogHandle = await new OplogHandle(options.oplogUrl, self.db.databaseName);
self._oplogHandle = new OplogHandle(options.oplogUrl, self.db.databaseName);
self._docFetcher = new DocFetcher(self);
}
return self;
};
MongoConnection.prototype.close = function() {

View File

@@ -873,7 +873,7 @@ if (Meteor.isServer) {
// This test mainly checks the correctness of oplog code dealing with limited
// queries. Compitablity with poll-diff is added as well.
Tinytest.addAsync("mongo-livedata - observe sorted, limited " + idGeneration, async function (test) {
Tinytest.add("mongo-livedata - observe sorted, limited " + idGeneration, function (test) {
var run = test.runId();
var coll = new Mongo.Collection("observeLimit-"+run, collectionOptions);
@@ -936,7 +936,7 @@ if (Meteor.isServer) {
// Insert a doc and start observing.
var docId1 = ins({foo: 22, bar: 5});
await waitUntilOplogCaughtUp();
waitUntilOplogCaughtUp();
// State: [ 5:1 | ]!
var o = observer();
@@ -1218,7 +1218,7 @@ if (Meteor.isServer) {
onComplete();
});
Tinytest.addAsync("mongo-livedata - observe sorted, limited, big initial set" + idGeneration, async function (test) {
Tinytest.add("mongo-livedata - observe sorted, limited, big initial set" + idGeneration, function (test) {
var run = test.runId();
var coll = new Mongo.Collection("observeLimit-"+run, collectionOptions);
@@ -1276,7 +1276,7 @@ if (Meteor.isServer) {
// Ensure that we are past all the 'i' entries before we run the query, so
// that we get the expected phase transitions.
await waitUntilOplogCaughtUp();
waitUntilOplogCaughtUp();
var o = observer();
var usesOplog = o.handle._multiplexer._observeDriver._usesOplog;
@@ -2802,10 +2802,10 @@ if (Meteor.isServer) {
}
// This is a VERY white-box test.
Meteor.isServer && Tinytest.addAsync("mongo-livedata - oplog - _disableOplog", async function (test) {
Meteor.isServer && Tinytest.add("mongo-livedata - oplog - _disableOplog", function (test) {
var collName = Random.id();
var coll = new Mongo.Collection(collName);
if ((await MongoInternals.defaultRemoteCollectionDriver()).mongo._oplogHandle) {
if (MongoInternals.defaultRemoteCollectionDriver().mongo._oplogHandle) {
var observeWithOplog = coll.find({x: 5})
.observeChanges({added: function () {}});
test.isTrue(observeWithOplog._multiplexer._observeDriver._usesOplog);
@@ -2817,7 +2817,7 @@ Meteor.isServer && Tinytest.addAsync("mongo-livedata - oplog - _disableOplog", a
observeWithoutOplog.stop();
});
Meteor.isServer && Tinytest.addAsync("mongo-livedata - oplog - include selector fields", async function (test) {
Meteor.isServer && Tinytest.add("mongo-livedata - oplog - include selector fields", function (test) {
var collName = "includeSelector" + Random.id();
var coll = new Mongo.Collection(collName);
@@ -2828,7 +2828,7 @@ Meteor.isServer && Tinytest.addAsync("mongo-livedata - oplog - include selector
// during the observeChanges, the bug in question is not consistently
// reproduced.) We don't have to do this for polling observe (eg
// --disable-oplog).
await waitUntilOplogCaughtUp();
waitUntilOplogCaughtUp();
var output = [];
var handle = coll.find({a: 1, b: 2}, {fields: {c: 1}}).observeChanges({
@@ -2859,7 +2859,7 @@ Meteor.isServer && Tinytest.addAsync("mongo-livedata - oplog - include selector
handle.stop();
});
Meteor.isServer && Tinytest.addAsync("mongo-livedata - oplog - transform", async function (test) {
Meteor.isServer && Tinytest.add("mongo-livedata - oplog - transform", function (test) {
var collName = "oplogTransform" + Random.id();
var coll = new Mongo.Collection(collName);
@@ -2870,7 +2870,7 @@ Meteor.isServer && Tinytest.addAsync("mongo-livedata - oplog - transform", async
// during the observeChanges, the bug in question is not consistently
// reproduced.) We don't have to do this for polling observe (eg
// --disable-oplog).
await waitUntilOplogCaughtUp();
waitUntilOplogCaughtUp();
var cursor = coll.find({}, {transform: function (doc) {
return doc.x;
@@ -2899,17 +2899,17 @@ Meteor.isServer && Tinytest.addAsync("mongo-livedata - oplog - transform", async
});
Meteor.isServer && Tinytest.addAsync("mongo-livedata - oplog - drop collection/db", async function (test) {
Meteor.isServer && Tinytest.add("mongo-livedata - oplog - drop collection/db", function (test) {
// This test uses a random database, so it can be dropped without affecting
// anything else.
var mongodbUri = Npm.require('mongodb-uri');
var parsedUri = mongodbUri.parse(process.env.MONGO_URL);
parsedUri.database = 'dropDB' + Random.id();
var driver = Promise.await(new MongoInternals.RemoteCollectionDriver(
var driver = new MongoInternals.RemoteCollectionDriver(
mongodbUri.format(parsedUri), {
oplogUrl: process.env.MONGO_OPLOG_URL
}
));
);
var collName = "dropCollection" + Random.id();
var coll = new Mongo.Collection(collName, { _driver: driver });
@@ -2943,7 +2943,7 @@ Meteor.isServer && Tinytest.addAsync("mongo-livedata - oplog - drop collection/d
// Wait until we've processed the insert oplog entry, so that we are in a
// steady state (and we don't see the dropped docs because we are FETCHING).
await waitUntilOplogCaughtUp();
waitUntilOplogCaughtUp();
// Drop the collection. Should remove all docs.
runInFence(function () {
@@ -3105,9 +3105,9 @@ testAsyncMulti("mongo-livedata - oplog - update EJSON", [
]);
async function waitUntilOplogCaughtUp() {
function waitUntilOplogCaughtUp() {
var oplogHandle =
(await MongoInternals.defaultRemoteCollectionDriver()).mongo._oplogHandle;
MongoInternals.defaultRemoteCollectionDriver().mongo._oplogHandle;
if (oplogHandle)
oplogHandle.waitUntilCaughtUp();
}
@@ -3233,7 +3233,7 @@ Meteor.isServer && Tinytest.add(
"mongo-livedata - connection failure throws",
function (test) {
test.throws(function () {
Promise.await(new MongoInternals.Connection('mongodb://this-does-not-exist.test/asdf'));
new MongoInternals.Connection('mongodb://this-does-not-exist.test/asdf');
});
}
);
@@ -3433,8 +3433,8 @@ if (Meteor.isServer) {
}
if (Meteor.isServer) {
Tinytest.addAsync("mongo-livedata - transaction", async function (test) {
const { client } = (await MongoInternals.defaultRemoteCollectionDriver()).mongo;
Tinytest.addAsync("mongo-livedata - transaction", function (test) {
const { client } = MongoInternals.defaultRemoteCollectionDriver().mongo;
const Collection = new Mongo.Collection(`transaction_test_${test.runId()}`);
const rawCollection = Collection.rawCollection();

View File

@@ -26,7 +26,7 @@ idForOp = function (op) {
throw Error("Unknown op: " + EJSON.stringify(op));
};
OplogHandle = async function (oplogUrl, dbName) {
OplogHandle = function (oplogUrl, dbName) {
var self = this;
self._oplogUrl = oplogUrl;
self._dbName = dbName;
@@ -82,8 +82,7 @@ OplogHandle = async function (oplogUrl, dbName) {
self._entryQueue = new Meteor._DoubleEndedQueue();
self._workerActive = false;
await self._startTailing();
return self;
self._startTailing();
};
Object.assign(OplogHandle.prototype, {
@@ -186,7 +185,7 @@ Object.assign(OplogHandle.prototype, {
self._catchingUpFutures.splice(insertAfter, 0, {ts: ts, future: f});
f.wait();
},
_startTailing: async function () {
_startTailing: function () {
var self = this;
// First, make sure that we're talking to the local database.
var mongodbUri = Npm.require('mongodb-uri');
@@ -206,12 +205,12 @@ Object.assign(OplogHandle.prototype, {
//
// The tail connection will only ever be running a single tail command, so
// it only needs to make one underlying TCP connection.
self._oplogTailConnection = await new MongoConnection(
self._oplogTailConnection = new MongoConnection(
self._oplogUrl, {maxPoolSize: 1});
// XXX better docs, but: it's to get monotonic results
// XXX is it safe to say "if there's an in flight query, just use its
// results"? I don't think so but should consider that
self._oplogLastEntryConnection = await new MongoConnection(
self._oplogLastEntryConnection = new MongoConnection(
self._oplogUrl, {maxPoolSize: 1});
// Now, make sure that there actually is a repl set here. If not, oplog

View File

@@ -1,8 +1,8 @@
var OplogCollection = new Mongo.Collection("oplog-" + Random.id());
Tinytest.addAsync("mongo-livedata - oplog - cursorSupported", async function (test) {
Tinytest.add("mongo-livedata - oplog - cursorSupported", function (test) {
var oplogEnabled =
!!(await MongoInternals.defaultRemoteCollectionDriver()).mongo._oplogHandle;
!!MongoInternals.defaultRemoteCollectionDriver().mongo._oplogHandle;
var supported = function (expected, selector, options) {
var cursor = OplogCollection.find(selector, options);
@@ -55,7 +55,7 @@ Tinytest.addAsync("mongo-livedata - oplog - cursorSupported", async function (te
process.env.MONGO_OPLOG_URL && testAsyncMulti(
"mongo-livedata - oplog - entry skipping", [
async function (test, expect) {
function (test, expect) {
var self = this;
self.collectionName = Random.id();
self.collection = new Mongo.Collection(self.collectionName);
@@ -68,7 +68,7 @@ process.env.MONGO_OPLOG_URL && testAsyncMulti(
// possible to make this test fail with TOO_FAR_BEHIND = 2000.
// The documents waiting to be processed would hardly go beyond 1000
// using mongo 3.2 with WiredTiger
(await MongoInternals.defaultRemoteCollectionDriver())
MongoInternals.defaultRemoteCollectionDriver()
.mongo._oplogHandle._defineTooFarBehind(500);
self.IRRELEVANT_SIZE = 15000;
@@ -96,7 +96,7 @@ process.env.MONGO_OPLOG_URL && testAsyncMulti(
})));
},
async function (test, expect) {
function (test, expect) {
var self = this;
test.equal(self.collection.find().count(),
@@ -131,7 +131,7 @@ process.env.MONGO_OPLOG_URL && testAsyncMulti(
test.isFalse(gotSpot);
self.skipped = false;
self.skipHandle = (await MongoInternals.defaultRemoteCollectionDriver())
self.skipHandle = MongoInternals.defaultRemoteCollectionDriver()
.mongo._oplogHandle.onSkippedEntries(function () {
self.skipped = true;
});
@@ -149,12 +149,12 @@ process.env.MONGO_OPLOG_URL && testAsyncMulti(
return gotSpotPromise;
},
async function (test, expect) {
function (test, expect) {
var self = this;
test.isTrue(self.skipped);
//This gets the TOO_FAR_BEHIND back to its initial value
(await MongoInternals.defaultRemoteCollectionDriver())
MongoInternals.defaultRemoteCollectionDriver()
.mongo._oplogHandle._resetTooFarBehind();
self.skipHandle.stop();

View File

@@ -1,11 +1,7 @@
import { onceAsync } from './mongoAsyncUtils';
let defaultRemoteCollectionDriver = null;
MongoInternals.RemoteCollectionDriver = async function (
MongoInternals.RemoteCollectionDriver = function (
mongo_url, options) {
var self = this;
self.mongo = await new MongoConnection(mongo_url, options);
return self;
self.mongo = new MongoConnection(mongo_url, options);
};
Object.assign(MongoInternals.RemoteCollectionDriver.prototype, {
@@ -25,7 +21,7 @@ Object.assign(MongoInternals.RemoteCollectionDriver.prototype, {
// Create the singleton RemoteCollectionDriver only on demand, so we
// only require Mongo configuration if it's actually used (eg, not if
// you're only trying to receive data from a remote DDP server.)
MongoInternals.defaultRemoteCollectionDriver = onceAsync(async function () {
MongoInternals.defaultRemoteCollectionDriver = _.once(function () {
var connectionOptions = {};
var mongoUrl = process.env.MONGO_URL;
@@ -37,13 +33,5 @@ MongoInternals.defaultRemoteCollectionDriver = onceAsync(async function () {
if (! mongoUrl)
throw new Error("MONGO_URL must be set in environment");
defaultRemoteCollectionDriver = await new MongoInternals.RemoteCollectionDriver(mongoUrl, connectionOptions);
return defaultRemoteCollectionDriver;
return new MongoInternals.RemoteCollectionDriver(mongoUrl, connectionOptions);
});
MongoInternals.getDefaultRemoteCollectionDriver = function() {
if (!defaultRemoteCollectionDriver) {
throw new Meteor.Error('getDefaultRemoteCollectionDriver should be called only after mongo package evaluation, make sure your package is declared after mongo in .meteor/packages file.');
}
return defaultRemoteCollectionDriver;
}