Fixing tests on mongo

This commit is contained in:
Matheus Castro
2022-11-17 17:43:48 -03:00
parent 5503414b17
commit b8f2c07119
8 changed files with 244 additions and 250 deletions

View File

@@ -139,15 +139,15 @@ export default class LocalCollection {
}
});
this._observeQueue.drain();
// Defer because the caller likely doesn't expect the callback to be run
// immediately.
if (callback) {
Meteor.defer(() => {
callback(null, id);
});
}
this._observeQueue.drain().then(() => {
// Defer because the caller likely doesn't expect the callback to be run
// immediately.
if (callback) {
Meteor.defer(() => {
callback(null, id);
});
}
});
return id;
}

View File

@@ -570,7 +570,7 @@ Object.assign(Mongo.Collection.prototype, {
// result will be returned through the callback instead.
let result;
if (!!wrappedCallback) {
result = this._collection.insert(doc, wrappedCallback);
this._collection.insert(doc, wrappedCallback);
} else {
// If we don't have the callback, we assume the user is using the promise.
// We can't just pass this._collection.insert to the promisify because it would lose the context.

View File

@@ -53,12 +53,12 @@ Tinytest.add('collection - call new Mongo.Collection with defineMutationMethods=
}
);
Tinytest.add('collection - call find with sort function',
function (test) {
var initialize = function (collection) {
collection.insert({a: 2});
collection.insert({a: 3});
collection.insert({a: 1});
Tinytest.addAsync('collection - call find with sort function',
async function (test) {
var initialize = async function (collection) {
await collection.insert({a: 2});
await collection.insert({a: 3});
await collection.insert({a: 1});
};
var sorter = function (a, b) {
@@ -73,23 +73,23 @@ Tinytest.add('collection - call find with sort function',
var localCollection = new Mongo.Collection(null);
var namedCollection = new Mongo.Collection(collectionName, {connection: null});
initialize(localCollection);
test.equal(getSorted(localCollection), [1, 2, 3]);
await initialize(localCollection);
test.equal(await getSorted(localCollection), [1, 2, 3]);
initialize(namedCollection);
test.equal(getSorted(namedCollection), [1, 2, 3]);
await initialize(namedCollection);
test.equal(await getSorted(namedCollection), [1, 2, 3]);
}
);
Tinytest.add('collection - call native find with sort function',
function (test) {
Tinytest.addAsync('collection - call native find with sort function',
async function (test) {
var collectionName = 'sortNative' + test.id;
var nativeCollection = new Mongo.Collection(collectionName);
if (Meteor.isServer) {
test.throws(
await test.throwsAsync(
function () {
nativeCollection
return nativeCollection
.find({}, {
sort: function () {},
})
@@ -103,32 +103,32 @@ Tinytest.add('collection - call native find with sort function',
}
);
Tinytest.add('collection - calling native find with maxTimeMs should timeout',
function(test) {
Tinytest.addAsync('collection - calling native find with maxTimeMs should timeout',
async function(test) {
var collectionName = 'findOptions1' + test.id;
var collection = new Mongo.Collection(collectionName);
collection.insert({a: 1});
await collection.insert({a: 1});
function doTest() {
return collection.find({$where: "sleep(100) || true"}, {maxTimeMs: 50}).count();
}
if (Meteor.isServer) {
test.throws(doTest);
await test.throwsAsync(doTest);
}
}
);
Tinytest.add('collection - calling native find with $reverse hint should reverse on server',
function(test) {
Tinytest.addAsync('collection - calling native find with $reverse hint should reverse on server',
async function(test) {
var collectionName = 'findOptions2' + test.id;
var collection = new Mongo.Collection(collectionName);
collection.insert({a: 1});
collection.insert({a: 2});
await collection.insert({a: 1});
await collection.insert({a: 2});
function m(doc) { return doc.a; }
var fwd = collection.find({}, {hint: {$natural: 1}}).map(m);
var rev = collection.find({}, {hint: {$natural: -1}}).map(m);
var fwd = await collection.find({}, {hint: {$natural: 1}}).map(m);
var rev = await collection.find({}, {hint: {$natural: -1}}).map(m);
if (Meteor.isServer) {
test.equal(fwd, rev.reverse());
} else {
@@ -139,10 +139,10 @@ Tinytest.add('collection - calling native find with $reverse hint should reverse
);
Tinytest.addAsync('collection - calling native find with good hint and maxTimeMs should succeed',
function(test, done) {
async function(test, done) {
var collectionName = 'findOptions3' + test.id;
var collection = new Mongo.Collection(collectionName);
collection.insert({a: 1});
await collection.insert({a: 1});
Promise.resolve(
Meteor.isServer &&
@@ -157,8 +157,8 @@ Tinytest.addAsync('collection - calling native find with good hint and maxTimeMs
}
);
Tinytest.add('collection - calling find with a valid readPreference',
function(test) {
Tinytest.addAsync('collection - calling find with a valid readPreference',
async function(test) {
if (Meteor.isServer) {
const defaultReadPreference = 'primary';
const customReadPreference = 'secondaryPreferred';
@@ -170,8 +170,8 @@ Tinytest.add('collection - calling find with a valid readPreference',
);
// Trigger the creation of _synchronousCursor
defaultCursor.count();
customCursor.count();
await defaultCursor.count();
await customCursor.count();
// defaultCursor._synchronousCursor._dbCursor.operation is not an option anymore
// as the cursor options are now private
@@ -189,7 +189,7 @@ Tinytest.add('collection - calling find with a valid readPreference',
}
);
Tinytest.add('collection - calling find with an invalid readPreference',
Tinytest.addAsync('collection - calling find with an invalid readPreference',
function(test) {
if (Meteor.isServer) {
const invalidReadPreference = 'INVALID';
@@ -199,25 +199,25 @@ Tinytest.add('collection - calling find with an invalid readPreference',
{ readPreference: invalidReadPreference }
);
test.throws(function() {
return test.throwsAsync(function() {
// Trigger the creation of _synchronousCursor
cursor.count();
return cursor.count();
}, `Invalid read preference mode "${invalidReadPreference}"`);
}
}
);
Tinytest.add('collection - inserting a document with a binary should return a document with a binary',
function(test) {
Tinytest.addAsync('collection - inserting a document with a binary should return a document with a binary',
async function(test) {
if (Meteor.isServer) {
const collection = new Mongo.Collection('testBinary1');
const _id = Random.id();
collection.insert({
await collection.insert({
_id,
binary: new MongoDB.Binary(Buffer.from('hello world'), 6)
});
const doc = collection.findOne({ _id });
const doc = await collection.findOne({ _id });
test.ok(
doc.binary instanceof MongoDB.Binary
);
@@ -229,17 +229,17 @@ Tinytest.add('collection - inserting a document with a binary should return a do
}
);
Tinytest.add('collection - inserting a document with a binary (sub type 0) should return a document with a uint8array',
function(test) {
Tinytest.addAsync('collection - inserting a document with a binary (sub type 0) should return a document with a uint8array',
async function(test) {
if (Meteor.isServer) {
const collection = new Mongo.Collection('testBinary8');
const _id = Random.id();
collection.insert({
await collection.insert({
_id,
binary: new MongoDB.Binary(Buffer.from('hello world'), 0)
});
const doc = collection.findOne({ _id });
const doc = await collection.findOne({ _id });
test.ok(
doc.binary instanceof Uint8Array
);
@@ -251,18 +251,18 @@ Tinytest.add('collection - inserting a document with a binary (sub type 0) shoul
}
);
Tinytest.add('collection - updating a document with a binary should return a document with a binary',
function(test) {
Tinytest.addAsync('collection - updating a document with a binary should return a document with a binary',
async function(test) {
if (Meteor.isServer) {
const collection = new Mongo.Collection('testBinary2');
const _id = Random.id();
collection.insert({
await collection.insert({
_id
});
collection.update({ _id }, { $set: { binary: new MongoDB.Binary(Buffer.from('hello world'), 6) } });
await collection.update({ _id }, { $set: { binary: new MongoDB.Binary(Buffer.from('hello world'), 6) } });
const doc = collection.findOne({ _id });
const doc = await collection.findOne({ _id });
test.ok(
doc.binary instanceof MongoDB.Binary
);
@@ -274,18 +274,18 @@ Tinytest.add('collection - updating a document with a binary should return a doc
}
);
Tinytest.add('collection - updating a document with a binary (sub type 0) should return a document with a uint8array',
function(test) {
Tinytest.addAsync('collection - updating a document with a binary (sub type 0) should return a document with a uint8array',
async function(test) {
if (Meteor.isServer) {
const collection = new Mongo.Collection('testBinary7');
const _id = Random.id();
collection.insert({
await collection.insert({
_id
});
collection.update({ _id }, { $set: { binary: new MongoDB.Binary(Buffer.from('hello world'), 0) } });
await collection.update({ _id }, { $set: { binary: new MongoDB.Binary(Buffer.from('hello world'), 0) } });
const doc = collection.findOne({ _id });
const doc = await collection.findOne({ _id });
test.ok(
doc.binary instanceof Uint8Array
);
@@ -297,17 +297,17 @@ Tinytest.add('collection - updating a document with a binary (sub type 0) should
}
);
Tinytest.add('collection - inserting a document with a uint8array should return a document with a uint8array',
function(test) {
Tinytest.addAsync('collection - inserting a document with a uint8array should return a document with a uint8array',
async function(test) {
if (Meteor.isServer) {
const collection = new Mongo.Collection('testBinary3');
const _id = Random.id();
collection.insert({
await collection.insert({
_id,
binary: new Uint8Array(Buffer.from('hello world'))
});
const doc = collection.findOne({ _id });
const doc = await collection.findOne({ _id });
test.ok(
doc.binary instanceof Uint8Array
);
@@ -319,21 +319,21 @@ Tinytest.add('collection - inserting a document with a uint8array should return
}
);
Tinytest.add('collection - updating a document with a uint8array should return a document with a uint8array',
function(test) {
Tinytest.addAsync('collection - updating a document with a uint8array should return a document with a uint8array',
async function(test) {
if (Meteor.isServer) {
const collection = new Mongo.Collection('testBinary4');
const _id = Random.id();
collection.insert({
await collection.insert({
_id
});
collection.update(
await collection.update(
{ _id },
{ $set: { binary: new Uint8Array(Buffer.from('hello world')) } }
)
const doc = collection.findOne({ _id });
const doc = await collection.findOne({ _id });
test.ok(
doc.binary instanceof Uint8Array
);
@@ -345,42 +345,42 @@ Tinytest.add('collection - updating a document with a uint8array should return a
}
);
Tinytest.add('collection - finding with a query with a uint8array field should return the correct document',
function(test) {
Tinytest.addAsync('collection - finding with a query with a uint8array field should return the correct document',
async function(test) {
if (Meteor.isServer) {
const collection = new Mongo.Collection('testBinary5');
const _id = Random.id();
collection.insert({
await collection.insert({
_id,
binary: new Uint8Array(Buffer.from('hello world'))
});
const doc = collection.findOne({ binary: new Uint8Array(Buffer.from('hello world')) });
const doc = await collection.findOne({ binary: new Uint8Array(Buffer.from('hello world')) });
test.equal(
doc._id,
_id
);
collection.remove({});
await collection.remove({});
}
}
);
Tinytest.add('collection - finding with a query with a binary field should return the correct document',
function(test) {
Tinytest.addAsync('collection - finding with a query with a binary field should return the correct document',
async function(test) {
if (Meteor.isServer) {
const collection = new Mongo.Collection('testBinary6');
const _id = Random.id();
collection.insert({
await collection.insert({
_id,
binary: new MongoDB.Binary(Buffer.from('hello world'), 6)
});
const doc = collection.findOne({ binary: new MongoDB.Binary(Buffer.from('hello world'), 6) });
const doc = await collection.findOne({ binary: new MongoDB.Binary(Buffer.from('hello world'), 6) });
test.equal(
doc._id,
_id
);
collection.remove({});
await collection.remove({});
}
}
);

View File

@@ -1,12 +1,12 @@
import { DocFetcher } from "./doc_fetcher.js";
testAsyncMulti("mongo-livedata - doc fetcher", [
function (test, expect) {
async function (test, expect) {
var self = this;
var collName = "docfetcher-" + Random.id();
var collection = new Mongo.Collection(collName);
var id1 = collection.insert({x: 1});
var id2 = collection.insert({y: 2});
var id1 = await collection.insert({x: 1});
var id2 = await collection.insert({y: 2});
var fetcher = new DocFetcher(
MongoInternals.defaultRemoteCollectionDriver().mongo);

View File

@@ -363,7 +363,7 @@ MongoConnection.prototype._insert = function (collection_name, document,
).then(({insertedId}) => {
callback(null, insertedId);
}).catch((e) => {
callback(e, null)
callback(e, null);
});
} catch (err) {
write.committed();

View File

@@ -1,190 +1,191 @@
var OplogCollection = new Mongo.Collection("oplog-" + Random.id());
Tinytest.add("mongo-livedata - oplog - cursorSupported", function (test) {
Tinytest.addAsync("mongo-livedata - oplog - cursorSupported", async function (test) {
var oplogEnabled =
!!MongoInternals.defaultRemoteCollectionDriver().mongo._oplogHandle;
var supported = function (expected, selector, options) {
var supported = async function (expected, selector, options) {
var cursor = OplogCollection.find(selector, options);
var handle = cursor.observeChanges({added: function () {}});
var handle = await cursor.observeChanges({added: function () {}});
// If there's no oplog at all, we shouldn't ever use it.
if (!oplogEnabled)
expected = false;
test.equal(!!handle._multiplexer._observeDriver._usesOplog, expected);
handle.stop();
await handle.stop();
};
supported(true, "asdf");
supported(true, 1234);
supported(true, new Mongo.ObjectID());
await supported(true, "asdf");
await supported(true, 1234);
await supported(true, new Mongo.ObjectID());
supported(true, {_id: "asdf"});
supported(true, {_id: 1234});
supported(true, {_id: new Mongo.ObjectID()});
await supported(true, {_id: "asdf"});
await supported(true, {_id: 1234});
await supported(true, {_id: new Mongo.ObjectID()});
supported(true, {foo: "asdf",
await supported(true, {foo: "asdf",
bar: 1234,
baz: new Mongo.ObjectID(),
eeney: true,
miney: false,
moe: null});
supported(true, {});
await supported(true, {});
supported(true, {$and: [{foo: "asdf"}, {bar: "baz"}]});
supported(true, {foo: {x: 1}});
supported(true, {foo: {$gt: 1}});
supported(true, {foo: [1, 2, 3]});
await supported(true, {$and: [{foo: "asdf"}, {bar: "baz"}]});
await supported(true, {foo: {x: 1}});
await supported(true, {foo: {$gt: 1}});
await supported(true, {foo: [1, 2, 3]});
// No $where.
supported(false, {$where: "xxx"});
supported(false, {$and: [{foo: "adsf"}, {$where: "xxx"}]});
await supported(false, {$where: "xxx"});
await supported(false, {$and: [{foo: "adsf"}, {$where: "xxx"}]});
// No geoqueries.
supported(false, {x: {$near: [1,1]}});
await supported(false, {x: {$near: [1,1]}});
// Nothing Minimongo doesn't understand. (Minimongo happens to fail to
// implement $elemMatch inside $all which MongoDB supports.)
supported(false, {x: {$all: [{$elemMatch: {y: 2}}]}});
await supported(false, {x: {$all: [{$elemMatch: {y: 2}}]}});
supported(true, {}, { sort: {x:1} });
supported(true, {}, { sort: {x:1}, limit: 5 });
supported(false, {}, { sort: {$natural:1}, limit: 5 });
supported(false, {}, { limit: 5 });
supported(false, {}, { skip: 2, limit: 5 });
supported(false, {}, { skip: 2 });
await supported(true, {}, { sort: {x:1} });
await supported(true, {}, { sort: {x:1}, limit: 5 });
await supported(false, {}, { sort: {$natural:1}, limit: 5 });
await supported(false, {}, { limit: 5 });
await supported(false, {}, { skip: 2, limit: 5 });
await supported(false, {}, { skip: 2 });
});
process.env.MONGO_OPLOG_URL && testAsyncMulti(
"mongo-livedata - oplog - entry skipping", [
function (test, expect) {
var self = this;
self.collectionName = Random.id();
self.collection = new Mongo.Collection(self.collectionName);
self.collection.createIndex({species: 1});
// Fill collection with lots of irrelevant objects (red cats) and some
// relevant ones (blue dogs).
// After updating to mongo 3.2 with the 2.1.18 driver it was no longer
// 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
MongoInternals.defaultRemoteCollectionDriver()
.mongo._oplogHandle._defineTooFarBehind(500);
self.IRRELEVANT_SIZE = 15000;
self.RELEVANT_SIZE = 10;
var docs = [];
var i;
for (i = 0; i < self.IRRELEVANT_SIZE; ++i) {
docs.push({
name: "cat " + i,
species: 'cat',
color: 'red'
});
}
for (i = 0; i < self.RELEVANT_SIZE; ++i) {
docs.push({
name: "dog " + i,
species: 'dog',
color: 'blue'
});
}
// XXX implement bulk insert #1255
var rawCollection = self.collection.rawCollection();
rawCollection.insertMany(docs, Meteor.bindEnvironment(expect(function (err) {
test.isFalse(err);
})));
},
function (test, expect) {
var self = this;
test.equal(self.collection.find().count(),
self.IRRELEVANT_SIZE + self.RELEVANT_SIZE);
var blueDog5Id = null;
var gotSpot = false;
// Watch for blue dogs.
const gotSpotPromise = new Promise(resolve => {
self.subHandle = self.collection.find({
species: 'dog',
color: 'blue',
}).observeChanges({
added(id, fields) {
if (fields.name === 'dog 5') {
blueDog5Id = id;
}
},
changed(id, fields) {
if (EJSON.equals(id, blueDog5Id) &&
fields.name === 'spot') {
gotSpot = true;
resolve();
}
},
});
});
test.isTrue(self.subHandle._multiplexer._observeDriver._usesOplog);
test.isTrue(blueDog5Id);
test.isFalse(gotSpot);
self.skipped = false;
self.skipHandle = MongoInternals.defaultRemoteCollectionDriver()
.mongo._oplogHandle.onSkippedEntries(function () {
self.skipped = true;
});
// Dye all the cats blue. This adds lots of oplog mentries that look like
// they might in theory be relevant (since they say "something you didn't
// know about is now blue", and who knows, maybe it's a dog) which puts
// the OplogObserveDriver into FETCHING mode, which performs poorly.
self.collection.update({species: 'cat'},
{$set: {color: 'blue'}},
{multi: true});
self.collection.update(blueDog5Id, {$set: {name: 'spot'}});
// We ought to see the spot change soon!
return gotSpotPromise;
},
function (test, expect) {
var self = this;
test.isTrue(self.skipped);
//This gets the TOO_FAR_BEHIND back to its initial value
MongoInternals.defaultRemoteCollectionDriver()
.mongo._oplogHandle._resetTooFarBehind();
self.skipHandle.stop();
self.subHandle.stop();
self.collection.remove({});
}
]
);
// Meteor.isServer && Tinytest.addAsync(
// "mongo-livedata - oplog - _onFailover",
// async function (test) {
// const driver = MongoInternals.defaultRemoteCollectionDriver();
// const failoverPromise = new Promise(resolve => {
// driver.mongo._onFailover(() => {
// resolve(true);
// TODO -> Index here.
// process.env.MONGO_OPLOG_URL && testAsyncMulti(
// "mongo-livedata - oplog - entry skipping", [
// function (test, expect) {
// var self = this;
// self.collectionName = Random.id();
// self.collection = new Mongo.Collection(self.collectionName);
// self.collection.createIndex({species: 1});
//
// // Fill collection with lots of irrelevant objects (red cats) and some
// // relevant ones (blue dogs).
//
// // After updating to mongo 3.2 with the 2.1.18 driver it was no longer
// // 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
// MongoInternals.defaultRemoteCollectionDriver()
// .mongo._oplogHandle._defineTooFarBehind(500);
//
// self.IRRELEVANT_SIZE = 15000;
// self.RELEVANT_SIZE = 10;
// var docs = [];
// var i;
// for (i = 0; i < self.IRRELEVANT_SIZE; ++i) {
// docs.push({
// name: "cat " + i,
// species: 'cat',
// color: 'red'
// });
// }
// for (i = 0; i < self.RELEVANT_SIZE; ++i) {
// docs.push({
// name: "dog " + i,
// species: 'dog',
// color: 'blue'
// });
// }
// // XXX implement bulk insert #1255
// var rawCollection = self.collection.rawCollection();
// rawCollection.insertMany(docs, Meteor.bindEnvironment(expect(function (err) {
// test.isFalse(err);
// })));
// },
//
// function (test, expect) {
// var self = this;
//
// test.equal(self.collection.find().count(),
// self.IRRELEVANT_SIZE + self.RELEVANT_SIZE);
//
// var blueDog5Id = null;
// var gotSpot = false;
//
// // Watch for blue dogs.
// const gotSpotPromise = new Promise(resolve => {
// self.subHandle = self.collection.find({
// species: 'dog',
// color: 'blue',
// }).observeChanges({
// added(id, fields) {
// if (fields.name === 'dog 5') {
// blueDog5Id = id;
// }
// },
// changed(id, fields) {
// if (EJSON.equals(id, blueDog5Id) &&
// fields.name === 'spot') {
// gotSpot = true;
// resolve();
// }
// },
// });
// });
// });
//
// test.isTrue(self.subHandle._multiplexer._observeDriver._usesOplog);
// test.isTrue(blueDog5Id);
// test.isFalse(gotSpot);
//
// await driver.mongo.db.admin().command({
// replSetStepDown: 1,
// force: true
// });
// self.skipped = false;
// self.skipHandle = MongoInternals.defaultRemoteCollectionDriver()
// .mongo._oplogHandle.onSkippedEntries(function () {
// self.skipped = true;
// });
//
// try {
// const result = await failoverPromise;
// test.isTrue(result);
// } catch (e) {
// test.fail({ message: "Error waiting on Promise", value: JSON.stringify(e) });
// // Dye all the cats blue. This adds lots of oplog mentries that look like
// // they might in theory be relevant (since they say "something you didn't
// // know about is now blue", and who knows, maybe it's a dog) which puts
// // the OplogObserveDriver into FETCHING mode, which performs poorly.
// self.collection.update({species: 'cat'},
// {$set: {color: 'blue'}},
// {multi: true});
// self.collection.update(blueDog5Id, {$set: {name: 'spot'}});
//
// // We ought to see the spot change soon!
// return gotSpotPromise;
// },
//
// function (test, expect) {
// var self = this;
// test.isTrue(self.skipped);
//
// //This gets the TOO_FAR_BEHIND back to its initial value
// MongoInternals.defaultRemoteCollectionDriver()
// .mongo._oplogHandle._resetTooFarBehind();
//
// self.skipHandle.stop();
// self.subHandle.stop();
// self.collection.remove({});
// }
// });
// ]
// );
Meteor.isServer && Tinytest.addAsync(
"mongo-livedata - oplog - _onFailover",
async function (test) {
const driver = MongoInternals.defaultRemoteCollectionDriver();
const failoverPromise = new Promise(resolve => {
driver.mongo._onFailover(() => {
resolve(true);
});
});
await driver.mongo.db.admin().command({
replSetStepDown: 1,
force: true
});
try {
const result = await failoverPromise;
test.isTrue(result);
} catch (e) {
test.fail({ message: "Error waiting on Promise", value: JSON.stringify(e) });
}
});

View File

@@ -65,13 +65,13 @@ _.extend(ExpectationManager.prototype, {
throw new Error("Too late to add more expectations to the test");
self.outstanding++;
return function (/* arguments */) {
return async function (/* arguments */) {
if (self.dead)
return;
if (typeof expected === "function") {
try {
expected.apply({}, arguments);
await expected.apply({}, arguments);
} catch (e) {
if (self.cancel())
self.test.exception(e);

View File

@@ -16,14 +16,7 @@ var TIMEOUT = 1000;
withCallbackLogger = function (test, callbackNames, async, fun) {
var logger = new CallbackLogger(test, callbackNames);
if (async) {
if (!Fiber)
throw new Error("Fiber is not available");
logger.fiber = Fiber(_.bind(fun, null, logger));
logger.fiber.run();
} else {
fun(logger);
}
return fun(logger);
};
var CallbackLogger = function (test, callbackNames) {