2015-04-25 06:11:09 +00:00
|
|
|
describe("Zotero.DB", function() {
|
|
|
|
describe("#executeTransaction()", function () {
|
2015-05-01 20:54:35 +00:00
|
|
|
it("should nest concurrent transactions", Zotero.Promise.coroutine(function* () {
|
|
|
|
var tmpTable = "tmpWaitForTransactions";
|
|
|
|
yield Zotero.DB.queryAsync("CREATE TABLE " + tmpTable + " (foo INT)");
|
|
|
|
|
|
|
|
var resolve1, resolve2, reject1, reject2;
|
|
|
|
var promise1 = new Promise(function (resolve, reject) {
|
|
|
|
resolve1 = resolve;
|
|
|
|
reject1 = reject;
|
|
|
|
});
|
|
|
|
var promise2 = new Promise(function (resolve, reject) {
|
|
|
|
resolve2 = resolve;
|
|
|
|
reject2 = reject;
|
|
|
|
});
|
|
|
|
|
|
|
|
Zotero.DB.executeTransaction(function* () {
|
|
|
|
yield Zotero.Promise.delay(100);
|
|
|
|
assert.equal(Zotero.DB._asyncTransactionNestingLevel, 0);
|
|
|
|
yield Zotero.DB.queryAsync("INSERT INTO " + tmpTable + " VALUES (2)");
|
|
|
|
// Make sure we're still in a transaction
|
|
|
|
Zotero.DB.transactionDate;
|
|
|
|
})
|
|
|
|
.then(resolve1)
|
|
|
|
.catch(reject1);
|
|
|
|
|
|
|
|
Zotero.DB.executeTransaction(function* () {
|
|
|
|
assert.equal(Zotero.DB._asyncTransactionNestingLevel, 1);
|
|
|
|
yield Zotero.DB.queryAsync("INSERT INTO " + tmpTable + " VALUES (1)");
|
|
|
|
// Make sure we're still in a transaction
|
|
|
|
Zotero.DB.transactionDate;
|
|
|
|
})
|
|
|
|
.then(resolve2)
|
|
|
|
.catch(reject2);
|
|
|
|
|
|
|
|
yield Zotero.Promise.all([promise1, promise2]);
|
|
|
|
}));
|
|
|
|
|
2015-04-25 06:11:09 +00:00
|
|
|
it("should roll back on error", function* () {
|
2015-04-26 21:45:45 +00:00
|
|
|
var tmpTable = "tmpRollbackOnError";
|
|
|
|
yield Zotero.DB.queryAsync("CREATE TABLE " + tmpTable + " (foo INT)");
|
|
|
|
yield Zotero.DB.queryAsync("INSERT INTO " + tmpTable + " VALUES (1)");
|
2015-04-25 06:11:09 +00:00
|
|
|
try {
|
|
|
|
yield Zotero.DB.executeTransaction(function* () {
|
2015-04-26 21:45:45 +00:00
|
|
|
yield Zotero.DB.queryAsync("INSERT INTO " + tmpTable + " VALUES (2)");
|
2015-04-25 06:11:09 +00:00
|
|
|
throw 'Aborting transaction -- ignore';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
if (typeof e != 'string' || !e.startsWith('Aborting transaction')) throw e;
|
|
|
|
}
|
2015-04-26 21:45:45 +00:00
|
|
|
var count = yield Zotero.DB.valueQueryAsync("SELECT COUNT(*) FROM " + tmpTable + "");
|
2015-04-25 06:11:09 +00:00
|
|
|
assert.equal(count, 1);
|
|
|
|
|
|
|
|
var conn = yield Zotero.DB._getConnectionAsync();
|
|
|
|
assert.isFalse(conn.transactionInProgress);
|
|
|
|
|
2015-04-26 21:45:45 +00:00
|
|
|
yield Zotero.DB.queryAsync("DROP TABLE " + tmpTable);
|
2015-04-25 06:11:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should run onRollback callbacks", function* () {
|
2015-04-26 21:45:45 +00:00
|
|
|
var tmpTable = "tmpOnRollback";
|
2015-04-25 06:11:09 +00:00
|
|
|
var callbackRan = false;
|
2015-04-26 21:45:45 +00:00
|
|
|
yield Zotero.DB.queryAsync("CREATE TABLE " + tmpTable + " (foo INT)");
|
2015-04-25 06:11:09 +00:00
|
|
|
try {
|
|
|
|
yield Zotero.DB.executeTransaction(
|
|
|
|
function* () {
|
2015-04-26 21:45:45 +00:00
|
|
|
yield Zotero.DB.queryAsync("INSERT INTO " + tmpTable + " VALUES (1)");
|
2015-04-25 06:11:09 +00:00
|
|
|
throw 'Aborting transaction -- ignore';
|
|
|
|
},
|
|
|
|
{
|
|
|
|
onRollback: function () {
|
|
|
|
callbackRan = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
if (typeof e != 'string' || !e.startsWith('Aborting transaction')) throw e;
|
|
|
|
}
|
|
|
|
assert.ok(callbackRan);
|
|
|
|
|
2015-04-26 21:45:45 +00:00
|
|
|
yield Zotero.DB.queryAsync("DROP TABLE " + tmpTable);
|
2015-04-25 06:11:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should run onRollback callbacks for nested transactions", function* () {
|
2015-04-26 21:45:45 +00:00
|
|
|
var tmpTable = "tmpOnNestedRollback";
|
2015-04-25 06:11:09 +00:00
|
|
|
var callback1Ran = false;
|
|
|
|
var callback2Ran = false;
|
2015-04-26 21:45:45 +00:00
|
|
|
yield Zotero.DB.queryAsync("CREATE TABLE " + tmpTable + " (foo INT)");
|
2015-04-25 06:11:09 +00:00
|
|
|
try {
|
|
|
|
yield Zotero.DB.executeTransaction(function* () {
|
2015-04-26 21:45:45 +00:00
|
|
|
yield Zotero.DB.queryAsync("INSERT INTO " + tmpTable + " VALUES (1)");
|
2015-04-25 06:11:09 +00:00
|
|
|
yield Zotero.DB.executeTransaction(
|
|
|
|
function* () {
|
2015-04-26 21:45:45 +00:00
|
|
|
yield Zotero.DB.queryAsync("INSERT INTO " + tmpTable + " VALUES (2)");
|
2015-04-25 06:11:09 +00:00
|
|
|
|
|
|
|
throw 'Aborting transaction -- ignore';
|
|
|
|
},
|
|
|
|
{
|
|
|
|
onRollback: function () {
|
|
|
|
callback1Ran = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
{
|
|
|
|
onRollback: function () {
|
|
|
|
callback2Ran = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
if (typeof e != 'string' || !e.startsWith('Aborting transaction')) throw e;
|
|
|
|
}
|
|
|
|
assert.ok(callback1Ran);
|
|
|
|
assert.ok(callback2Ran);
|
|
|
|
|
2015-04-26 21:45:45 +00:00
|
|
|
yield Zotero.DB.queryAsync("DROP TABLE " + tmpTable);
|
2015-04-25 06:11:09 +00:00
|
|
|
});
|
2015-04-26 21:49:25 +00:00
|
|
|
|
|
|
|
it("should not commit nested transactions", function* () {
|
|
|
|
var tmpTable = "tmpNoCommitNested";
|
|
|
|
yield Zotero.DB.queryAsync("CREATE TABLE " + tmpTable + " (foo INT)");
|
|
|
|
try {
|
|
|
|
yield Zotero.DB.executeTransaction(function* () {
|
|
|
|
yield Zotero.DB.queryAsync("INSERT INTO " + tmpTable + " VALUES (1)");
|
|
|
|
yield Zotero.DB.executeTransaction(function* () {
|
|
|
|
yield Zotero.DB.queryAsync("INSERT INTO " + tmpTable + " VALUES (2)");
|
|
|
|
throw 'Aborting transaction -- ignore';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
if (typeof e != 'string' || !e.startsWith('Aborting transaction')) throw e;
|
|
|
|
}
|
|
|
|
var count = yield Zotero.DB.valueQueryAsync("SELECT COUNT(*) FROM " + tmpTable);
|
|
|
|
assert.equal(count, 0);
|
|
|
|
|
|
|
|
yield Zotero.DB.queryAsync("DROP TABLE " + tmpTable);
|
|
|
|
});
|
2015-04-25 06:11:09 +00:00
|
|
|
})
|
|
|
|
});
|