Bug 572223 - too much cookies.sqlite io. Part 4: tests. r=sdwilsh

This commit is contained in:
Dan Witte 2010-07-29 12:17:43 -07:00
parent e25f157ff9
commit 8528b7d534
4 changed files with 131 additions and 28 deletions

View File

@ -17,36 +17,33 @@ XPCOMUtils.defineLazyServiceGetter(Services, "cookiemgr",
"@mozilla.org/cookiemanager;1",
"nsICookieManager2");
// Close and reload the cookie database.
function do_reload_profile(generator, profile, cleanse) {
function _observer(generator, service, topic) {
Services.obs.addObserver(this, topic, false);
function _observer(generator, service, topic) {
Services.obs.addObserver(this, topic, false);
this.service = service;
this.generator = generator;
this.topic = topic;
}
this.service = service;
this.generator = generator;
this.topic = topic;
}
_observer.prototype = {
observe: function (subject, topic, data) {
do_check_eq(this.topic, topic);
_observer.prototype = {
observe: function (subject, topic, data) {
do_check_eq(this.topic, topic);
Services.obs.removeObserver(this, this.topic);
Services.obs.removeObserver(this, this.topic);
// Fire the notification to reload the database, and continue executing
// the generator function.
this.service.observe(null, "profile-do-change", "");
// Continue executing the generator function.
if (this.generator)
this.generator.next();
this.generator = null;
this.service = null;
this.topic = null;
}
this.generator = null;
this.service = null;
this.topic = null;
}
}
let dbfile = profile.QueryInterface(Ci.nsILocalFile).clone();
dbfile.append("cookies.sqlite");
// Close the cookie database. If a generator is supplied, it will be invoked
// once the close is complete.
function do_close_profile(generator, cleanse) {
// Register an observer for db close.
let service = Services.cookies.QueryInterface(Ci.nsIObserver);
let obs = new _observer(generator, service, "cookie-db-closed");
@ -55,6 +52,17 @@ function do_reload_profile(generator, profile, cleanse) {
service.observe(null, "profile-before-change", cleanse ? cleanse : "");
}
// Load the cookie database. If a generator is supplied, it will be invoked
// once the load is complete.
function do_load_profile(generator) {
// Register an observer for read completion.
let service = Services.cookies.QueryInterface(Ci.nsIObserver);
let obs = new _observer(generator, service, "cookie-db-read");
// Load the profile.
service.observe(null, "profile-do-change", "");
}
// Set four cookies; with & without channel, http and non-http; and test
// the cookie count against 'expected' after each set.
function do_set_cookies(uri, channel, session, expected) {
@ -73,3 +81,17 @@ function do_set_cookies(uri, channel, session, expected) {
Services.cookies.setCookieStringFromHttp(uri, null, null, "hot=dog" + suffix, null, channel);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri.host), expected[3]);
}
function do_count_enumerator(enumerator) {
let i = 0;
while (enumerator.hasMoreElements()) {
enumerator.getNext();
++i;
}
return i;
}
function do_count_cookies() {
return do_count_enumerator(Services.cookiemgr.enumerator);
}

View File

@ -47,14 +47,24 @@ function do_run_test() {
do_set_cookies(uri2, channel2, true, [1, 2, 3, 4]);
// fake a profile change
do_reload_profile(test_generator, profile);
do_close_profile(test_generator);
yield;
do_load_profile();
do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 4);
do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
// Again, but don't wait for the async close to complete. This should always
// work, since we blocked on close above and haven't kicked off any writes
// since then.
do_close_profile();
do_load_profile();
do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 4);
do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
// cleanse them
do_reload_profile(test_generator, profile, "shutdown-cleanse");
do_close_profile(test_generator, "shutdown-cleanse");
yield;
do_load_profile();
do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 0);
do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
@ -64,8 +74,9 @@ function do_run_test() {
do_set_cookies(uri2, channel2, true, [1, 2, 3, 4]);
// fake a profile change
do_reload_profile(test_generator, profile);
do_close_profile(test_generator);
yield;
do_load_profile();
do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 0);
do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);

View File

@ -0,0 +1,67 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
// test cookie database asynchronous read operation.
let test_generator = do_run_test();
function run_test() {
do_test_pending();
test_generator.next();
}
function finish_test() {
do_execute_soon(function() {
test_generator.close();
do_test_finished();
});
}
function do_run_test() {
// Set up a profile.
let profile = do_get_profile();
for (let i = 0; i < 3000; ++i) {
let uri = NetUtil.newURI("http://" + i + ".com/");
Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null);
}
do_check_eq(do_count_cookies(), 3000);
// fake a profile change
do_close_profile(test_generator);
yield;
do_load_profile();
// test a few random cookies
do_check_eq(Services.cookiemgr.countCookiesFromHost("2000.com"), 1);
do_check_eq(Services.cookiemgr.countCookiesFromHost("abc.com"), 0);
do_check_eq(Services.cookiemgr.countCookiesFromHost("100.com"), 1);
do_check_eq(Services.cookiemgr.countCookiesFromHost("1400.com"), 1);
do_check_eq(Services.cookiemgr.countCookiesFromHost("xyz.com"), 0);
// force synchronous load of everything
do_check_eq(do_count_cookies(), 3000);
// check that everything's precisely correct
for (let i = 0; i < 3000; ++i) {
let host = i.toString() + ".com";
do_check_eq(Services.cookiemgr.countCookiesFromHost(host), 1);
}
// reload again, but wait for async read completion
do_close_profile(test_generator);
yield;
do_load_profile(test_generator);
yield;
// check that everything's precisely correct
for (let i = 0; i < 3000; ++i) {
let host = i.toString() + ".com";
do_check_eq(Services.cookiemgr.countCookiesFromHost(host), 1);
}
finish_test();
}

View File

@ -47,14 +47,16 @@ function do_run_test() {
do_set_cookies(uri2, channel1, true, [1, 2, 3, 4]);
// fake a profile change
do_reload_profile(test_generator, profile);
do_close_profile(test_generator);
yield;
do_load_profile();
do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 4);
do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
// cleanse them
do_reload_profile(test_generator, profile, "shutdown-cleanse");
do_close_profile(test_generator, "shutdown-cleanse");
yield;
do_load_profile();
do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 0);
do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
@ -64,8 +66,9 @@ function do_run_test() {
do_set_cookies(uri2, channel1, true, [1, 2, 3, 4]);
// fake a profile change
do_reload_profile(test_generator, profile);
do_close_profile(test_generator);
yield;
do_load_profile();
do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 0);
do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);