Skip to content

Commit

Permalink
Bug 1028887 - Cause tests to fail when autoclosing Sqlite.jsm. r=Yoric
Browse files Browse the repository at this point in the history
  • Loading branch information
brisad committed Jun 26, 2014
1 parent 6e38f19 commit 4980b5b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
15 changes: 13 additions & 2 deletions toolkit/modules/Sqlite.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ let connectionCounters = new Map();
*/
let isClosed = false;

this.Debugging = {
// Tests should fail if a connection auto closes. The exception is
// when finalization itself is tested, in which case this flag
// should be set to false.
failTestsOnAutoClose: true
};

// Displays a script error message
function logScriptError(message) {
let consoleMessage = Cc["@mozilla.org/scripterror;1"].
Expand All @@ -51,8 +58,12 @@ function logScriptError(message) {
Ci.nsIScriptError.errorFlag, "component javascript");
Services.console.logMessage(consoleMessage);

// Always dump errors, in case the Console Service isn't listening anymore
dump("*** " + message + "\n");
// This `Promise.reject` will cause tests to fail. The debugging
// flag can be used to suppress this for tests that explicitly
// test auto closes.
if (Debugging.failTestsOnAutoClose) {
Promise.reject(new Error(message));
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions toolkit/modules/tests/xpcshell/test_sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ function sleep(ms) {
return deferred.promise;
}

// When testing finalization, use this to tell Sqlite.jsm to not throw
// an uncatchable `Promise.reject`
function failTestsOnAutoClose(enabled) {
Cu.getGlobalForObject(Sqlite).Debugging.failTestsOnAutoClose = enabled;
}

function getConnection(dbName, extraOptions={}) {
let path = dbName + ".sqlite";
let options = {path: path};
Expand Down Expand Up @@ -911,6 +917,7 @@ add_task(function* test_readOnly_clone() {
* Test finalization
*/
add_task(function* test_closed_by_witness() {
failTestsOnAutoClose(false);
let c = yield getDummyDatabase("closed_by_witness");

Services.obs.notifyObservers(null, "sqlite-finalization-witness",
Expand All @@ -920,9 +927,11 @@ add_task(function* test_closed_by_witness() {
c._witness.forget();
yield c._connectionData._deferredClose.promise;
do_check_false(c._connectionData._open);
failTestsOnAutoClose(true);
});

add_task(function* test_warning_message_on_finalization() {
failTestsOnAutoClose(false);
let c = yield getDummyDatabase("warning_message_on_finalization");
let connectionIdentifier = c._connectionData._connectionIdentifier;
let deferred = Promise.defer();
Expand All @@ -946,9 +955,11 @@ add_task(function* test_warning_message_on_finalization() {

yield deferred.promise;
Services.console.unregisterListener(listener);
failTestsOnAutoClose(true);
});

add_task(function* test_error_message_on_unknown_finalization() {
failTestsOnAutoClose(false);
let deferred = Promise.defer();

let listener = {
Expand All @@ -965,6 +976,7 @@ add_task(function* test_error_message_on_unknown_finalization() {

yield deferred.promise;
Services.console.unregisterListener(listener);
failTestsOnAutoClose(true);
});

add_task(function* test_forget_witness_on_close() {
Expand All @@ -985,6 +997,7 @@ add_task(function* test_forget_witness_on_close() {
});

add_task(function* test_close_database_on_gc() {
failTestsOnAutoClose(false);
let deferred = Promise.defer();

for (let i = 0; i < 100; ++i) {
Expand All @@ -1003,4 +1016,5 @@ add_task(function* test_close_database_on_gc() {

Components.utils.forceGC();
yield deferred.promise;
failTestsOnAutoClose(true);
});

0 comments on commit 4980b5b

Please sign in to comment.