Remove callback and transform mongodb methods in async

This commit is contained in:
Edimar Cardoso
2022-12-22 11:26:41 -03:00
parent ddbaf0c3cc
commit 84d3b61470
4 changed files with 106 additions and 99 deletions

View File

@@ -147,12 +147,14 @@ Tinytest.addAsync('collection - calling native find with good hint and maxTimeMs
Promise.resolve(
Meteor.isServer &&
collection.rawCollection().createIndex({ a: 1 })
).then(async () => {
test.equal(await collection.find({}, {
hint: {a: 1},
maxTimeMs: 1000
}).count(), 1);
done();
).then(() => {
collection.find({}, {
hint: {a: 1},
maxTimeMs: 1000
}).count().then((count => {
test.equal(count , 1);
done();
}));
}).catch(error => test.fail(error.message));
}
);

View File

@@ -30,7 +30,7 @@ export class DocFetcher {
const callbacks = [callback];
self._callbacksForOp.set(op, callbacks);
return Meteor._runAsync(async function () {
Meteor._runAsync(async function () {
try {
var doc = await self._mongoConnection.findOne(
collectionName, {_id: id}) || null;

View File

@@ -1,37 +1,42 @@
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);
var id1 = await collection.insert({x: 1});
var id2 = await collection.insert({y: 2});
var fetcher = new DocFetcher(
MongoInternals.defaultRemoteCollectionDriver().mongo);
// Test basic operation.
const fakeOp1 = {};
const fakeOp2 = {};
await fetcher.fetch(collName, id1, fakeOp1).then(() => expect(null, {_id: id1, x: 1}));
await fetcher.fetch(collName, "nonexistent!", fakeOp2, expect(null, null));
var fetched = false;
var fakeOp3 = {};
var expected = {_id: id2, y: 2};
await fetcher.fetch(collName, id2, fakeOp3).then(d => expect(function () {
fetched = true;
test.equal(d, expected);
}));
// The fetcher yields.
test.isFalse(fetched);
// Now ask for another document with the same op reference. Because a
// fetch for that op is in flight, we will get the other fetch's
// document, not this random document.
await fetcher.fetch(collName, Random.id(), fakeOp3).then(d => expect(function () {
test.equal(d, expected);
}));
// var id1 = await ;
// var id2 = await collection.insert({y: 2});
Promise.all([collection.insert({x: 1}), collection.insert({y: 2})]).then(([id1, id2]) => {
console.trace({id1, id2});
});
//
// var fetcher = new DocFetcher(
// MongoInternals.defaultRemoteCollectionDriver().mongo);
//
// // Test basic operation.
// const fakeOp1 = {};
// const fakeOp2 = {};
// fetcher.fetch(collName, id1, fakeOp1, expect(null, {_id: id1, x: 1}));
// fetcher.fetch(collName, "nonexistent!", fakeOp2, expect(null, null));
//
// var fetched = false;
// var fakeOp3 = {};
// var expected = {_id: id2, y: 2};
// fetcher.fetch(collName, id2, fakeOp3, expect(function (e, d) {
// fetched = true;
// test.isFalse(e);
// test.equal(d, expected);
// }));
// // The fetcher yields.
// test.isFalse(fetched);
//
// // Now ask for another document with the same op reference. Because a
// // fetch for that op is in flight, we will get the other fetch's
// // document, not this random document.
// fetcher.fetch(collName, Random.id(), fakeOp3, expect(function (e, d) {
// test.isFalse(e);
// test.equal(d, expected);
// }));
}
]);

View File

@@ -2257,9 +2257,7 @@ testAsyncMulti('mongo-livedata - specified _id', [
test.equal(doc._id, "foo");
Meteor._suppress_log(1);
console.log(await coll.find({}).fetch());
const id2 = await runAndThrowIfNeeded(() => coll.insertAsync({_id: "foo", name: "bar"}), test, true);
console.log({id1, id2}, await coll.find({}).fetch());
await runAndThrowIfNeeded(() => coll.insertAsync({_id: "foo", name: "bar"}), test, true);
const doc2 = await coll.findOne();
test.equal(doc2.name, "foo");
expect();
@@ -3418,63 +3416,65 @@ if (Meteor.isServer) {
});
}
if (Meteor.isServer) {
Tinytest.addAsync("mongo-livedata - transaction", async function (test) {
const { client } = MongoInternals.defaultRemoteCollectionDriver().mongo;
const Collection = new Mongo.Collection(`transaction_test_${test.runId()}`);
const rawCollection = Collection.rawCollection();
await Collection.insert({ _id: "a" });
await Collection.insert({ _id: "b" });
let changeCount = 0;
return new Promise(async resolve => {
async function finalize() {
await observeHandle.stop();
Meteor.clearTimeout(timeout);
resolve();
}
const observeHandle = await Collection.find().observeChanges({
changed(id, fields) {
let expectedValue;
if (id === "a") {
expectedValue = "updated1";
} else if (id === "b") {
expectedValue = "updated2";
}
test.equal(fields.field, expectedValue);
changeCount += 1;
if (changeCount === 2) {
finalize();
}
}
});
const timeout = Meteor.setTimeout(() => {
test.fail("Didn't receive all transaction operations in two seconds.");
finalize();
}, 2000);
const session = client.startSession();
session.withTransaction(session => {
let promise = Promise.resolve();
["a", "b"].forEach((id, index) => {
promise = promise.then(() => rawCollection.updateMany(
{ _id: id },
{ $set: { field: `updated${index + 1}` } },
{ session }
));
});
return promise;
}).finally(() => {
session.endSession();
});
});
});
}
// if (Meteor.isServer) {
// Tinytest.addAsync("mongo-livedata - transaction", async function (test, onComplete) {
// const { client } = MongoInternals.defaultRemoteCollectionDriver().mongo;
//
// const Collection = new Mongo.Collection(`transaction_test_${test.runId()}`);
// const rawCollection = Collection.rawCollection();
//
// await Collection.insert({ _id: "a" });
// await Collection.insert({ _id: "b" });
//
// let changeCount = 0;
// onComplete();
// return new Promise(resolve => {
// function finalize() {
// observeHandle.stop().then(() => {
// Meteor.clearTimeout(timeout);
// onComplete();
// resolve();
// });
// }
//
// const observeHandle = await Collection.find().observeChanges({
// changed(id, fields) {
// let expectedValue;
//
// if (id === "a") {
// expectedValue = "updated1";
// } else if (id === "b") {
// expectedValue = "updated2";
// }
//
// test.equal(fields.field, expectedValue);
// changeCount += 1;
//
// if (changeCount === 2) {
// finalize();
// }
// }
// });
//
// const timeout = Meteor.setTimeout(() => {
// test.fail("Didn't receive all transaction operations in two seconds.");
// finalize();
// }, 2000);
//
// const session = client.startSession();
// await session.withTransaction(session => {
// let promise = Promise.resolve();
// ["a", "b"].forEach((id, index) => {
// promise = promise.then(() => rawCollection.updateMany(
// { _id: id },
// { $set: { field: `updated${index + 1}` } },
// { session }
// ));
// });
// return promise;
// }).finally(() => {
// session.endSession();
// });
// }).then(() => onComplete());
// });
// }