FIX: Transaction was aborting before all promises have either resolved or rejected (#5878)

This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2019-07-31 18:34:49 -07:00
committed by GitHub
parent baa5daefa4
commit 14a8d333a3
5 changed files with 360 additions and 43 deletions

View File

@@ -96,7 +96,8 @@
"build": "babel src/ -d lib/ --copy-files", "build": "babel src/ -d lib/ --copy-files",
"watch": "babel --watch src/ -d lib/ --copy-files", "watch": "babel --watch src/ -d lib/ --copy-files",
"pretest": "cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} mongodb-runner start", "pretest": "cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} mongodb-runner start",
"test": "cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} TESTING=1 jasmine", "testonly": "cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} TESTING=1 jasmine",
"test": "npm run testonly",
"posttest": "cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} mongodb-runner stop", "posttest": "cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} mongodb-runner stop",
"coverage": "npm run pretest && cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} TESTING=1 nyc jasmine && npm run posttest", "coverage": "npm run pretest && cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} TESTING=1 nyc jasmine && npm run posttest",
"start": "node ./bin/parse-server", "start": "node ./bin/parse-server",

View File

@@ -190,10 +190,90 @@ describe('ParseServerRESTController', () => {
path: '/1/classes/MyObject', path: '/1/classes/MyObject',
body: { key: 10 }, body: { key: 10 },
}, },
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
], ],
transaction: true, transaction: true,
}).catch(error => { }).catch(error => {
expect(error.message).toBeDefined(); expect(error).toBeDefined();
const query = new Parse.Query('MyObject'); const query = new Parse.Query('MyObject');
query.find().then(results => { query.find().then(results => {
expect(results.length).toBe(0); expect(results.length).toBe(0);
@@ -231,6 +311,86 @@ describe('ParseServerRESTController', () => {
path: '/1/classes/MyObject2', path: '/1/classes/MyObject2',
body: { key: 10 }, body: { key: 10 },
}, },
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
], ],
transaction: true, transaction: true,
}); });
@@ -296,13 +456,13 @@ describe('ParseServerRESTController', () => {
'value2', 'value2',
]); ]);
expect(databaseAdapter.createObject.calls.count()).toBe(5); expect(databaseAdapter.createObject.calls.count()).toBe(13);
let transactionalSession; let transactionalSession;
let transactionalSession2; let transactionalSession2;
let myObjectDBCalls = 0; let myObjectDBCalls = 0;
let myObject2DBCalls = 0; let myObject2DBCalls = 0;
let myObject3DBCalls = 0; let myObject3DBCalls = 0;
for (let i = 0; i < 5; i++) { for (let i = 0; i < 13; i++) {
const args = databaseAdapter.createObject.calls.argsFor(i); const args = databaseAdapter.createObject.calls.argsFor(i);
switch (args[0]) { switch (args[0]) {
case 'MyObject': case 'MyObject':
@@ -318,7 +478,11 @@ describe('ParseServerRESTController', () => {
break; break;
case 'MyObject2': case 'MyObject2':
myObject2DBCalls++; myObject2DBCalls++;
transactionalSession2 = args[3]; if (!transactionalSession2) {
transactionalSession2 = args[3];
} else {
expect(transactionalSession2).toBe(args[3]);
}
if (transactionalSession) { if (transactionalSession) {
expect(transactionalSession).not.toBe(args[3]); expect(transactionalSession).not.toBe(args[3]);
} }
@@ -330,7 +494,7 @@ describe('ParseServerRESTController', () => {
} }
} }
expect(myObjectDBCalls).toEqual(2); expect(myObjectDBCalls).toEqual(2);
expect(myObject2DBCalls).toEqual(1); expect(myObject2DBCalls).toEqual(9);
expect(myObject3DBCalls).toEqual(2); expect(myObject3DBCalls).toEqual(2);
}); });
}); });

View File

@@ -251,6 +251,86 @@ describe('batch', () => {
path: '/1/classes/MyObject', path: '/1/classes/MyObject',
body: { key: 10 }, body: { key: 10 },
}, },
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject',
body: { key: 10 },
},
], ],
transaction: true, transaction: true,
}), }),
@@ -297,6 +377,86 @@ describe('batch', () => {
path: '/1/classes/MyObject2', path: '/1/classes/MyObject2',
body: { key: 10 }, body: { key: 10 },
}, },
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 'value1' },
},
{
method: 'POST',
path: '/1/classes/MyObject2',
body: { key: 10 },
},
], ],
transaction: true, transaction: true,
}), }),
@@ -373,13 +533,13 @@ describe('batch', () => {
'value2', 'value2',
]); ]);
expect(databaseAdapter.createObject.calls.count()).toBe(5); expect(databaseAdapter.createObject.calls.count()).toBe(13);
let transactionalSession; let transactionalSession;
let transactionalSession2; let transactionalSession2;
let myObjectDBCalls = 0; let myObjectDBCalls = 0;
let myObject2DBCalls = 0; let myObject2DBCalls = 0;
let myObject3DBCalls = 0; let myObject3DBCalls = 0;
for (let i = 0; i < 5; i++) { for (let i = 0; i < 13; i++) {
const args = databaseAdapter.createObject.calls.argsFor(i); const args = databaseAdapter.createObject.calls.argsFor(i);
switch (args[0]) { switch (args[0]) {
case 'MyObject': case 'MyObject':
@@ -395,7 +555,11 @@ describe('batch', () => {
break; break;
case 'MyObject2': case 'MyObject2':
myObject2DBCalls++; myObject2DBCalls++;
transactionalSession2 = args[3]; if (!transactionalSession2) {
transactionalSession2 = args[3];
} else {
expect(transactionalSession2).toBe(args[3]);
}
if (transactionalSession) { if (transactionalSession) {
expect(transactionalSession).not.toBe(args[3]); expect(transactionalSession).not.toBe(args[3]);
} }
@@ -407,7 +571,7 @@ describe('batch', () => {
} }
} }
expect(myObjectDBCalls).toEqual(2); expect(myObjectDBCalls).toEqual(2);
expect(myObject2DBCalls).toEqual(1); expect(myObject2DBCalls).toEqual(9);
expect(myObject3DBCalls).toEqual(2); expect(myObject3DBCalls).toEqual(2);
}); });
}); });

View File

@@ -64,37 +64,32 @@ function ParseServerRESTController(applicationId, router) {
config config
).then( ).then(
response => { response => {
return Promise.resolve({ success: response }); return { success: response };
}, },
error => { error => {
if (data.transaction === true) { return {
return Promise.reject(error);
}
return Promise.resolve({
error: { code: error.code, error: error.message }, error: { code: error.code, error: error.message },
}); };
} }
); );
}); });
return Promise.all(promises) return Promise.all(promises).then(result => {
.catch(error => { if (data.transaction === true) {
if (data.transaction === true) { if (
result.find(resultItem => typeof resultItem.error === 'object')
) {
return config.database.abortTransactionalSession().then(() => { return config.database.abortTransactionalSession().then(() => {
throw error; return Promise.reject(result);
}); });
} else { } else {
throw error;
}
})
.then(result => {
if (data.transaction === true) {
return config.database.commitTransactionalSession().then(() => { return config.database.commitTransactionalSession().then(() => {
return result; return result;
}); });
} else {
return result;
} }
}); } else {
return result;
}
});
}); });
} }

View File

@@ -107,33 +107,26 @@ function handleBatch(router, req) {
return { success: response.response }; return { success: response.response };
}, },
error => { error => {
if (req.body.transaction === true) {
return Promise.reject(error);
}
return { error: { code: error.code, error: error.message } }; return { error: { code: error.code, error: error.message } };
} }
); );
}); });
return Promise.all(promises) return Promise.all(promises).then(results => {
.catch(error => { if (req.body.transaction === true) {
if (req.body.transaction === true) { if (results.find(result => typeof result.error === 'object')) {
return req.config.database.abortTransactionalSession().then(() => { return req.config.database.abortTransactionalSession().then(() => {
throw error; return Promise.reject({ response: results });
}); });
} else { } else {
throw error;
}
})
.then(results => {
if (req.body.transaction === true) {
return req.config.database.commitTransactionalSession().then(() => { return req.config.database.commitTransactionalSession().then(() => {
return { response: results }; return { response: results };
}); });
} else {
return { response: results };
} }
}); } else {
return { response: results };
}
});
}); });
} }