This commit is contained in:
Atul Varma 2008-06-18 17:28:38 -07:00
Родитель d2c2945873 6c5227493a
Коммит 8f20b9b0d9
6 изменённых файлов: 134 добавлений и 67 удалений

Просмотреть файл

@ -48,7 +48,8 @@ Cu.import("resource://weave/util.js");
* Asynchronous generator helpers
*/
let currentId = 0;
let gCurrentId = 0;
let gOutstandingGenerators = 0;
function AsyncException(initFrame, message) {
this.message = message;
@ -71,13 +72,14 @@ AsyncException.prototype = {
};
function Generator(thisArg, method, onComplete, args) {
gOutstandingGenerators++;
this._outstandingCbs = 0;
this._log = Log4Moz.Service.getLogger("Async.Generator");
this._log.level =
Log4Moz.Level[Utils.prefs.getCharPref("log.logger.async")];
this._thisArg = thisArg;
this._method = method;
this._id = currentId++;
this._id = gCurrentId++;
this.onComplete = onComplete;
this._args = args;
this._initFrame = Components.stack.caller;
@ -251,6 +253,7 @@ Generator.prototype = {
this._log.trace("Initial stack trace:\n" + this.trace);
}
}
gOutstandingGenerators--;
}
};
@ -280,6 +283,7 @@ function trace(frame, str) {
Async = {
get outstandingGenerators() { return gOutstandingGenerators; },
// Use:
// let gen = Async.run(this, this.fooGen, ...);

Просмотреть файл

@ -114,3 +114,87 @@ function makeAsyncTestRunner(generator) {
return run_test;
}
function FakePrefService(contents) {
Cu.import("resource://weave/util.js");
this.fakeContents = contents;
Utils.__prefs = this;
}
FakePrefService.prototype = {
_getPref: function fake__getPref(pref) {
Log4Moz.Service.rootLogger.trace("Getting pref: " + pref);
return this.fakeContents[pref];
},
getCharPref: function fake_getCharPref(pref) {
return this._getPref(pref);
},
getBoolPref: function fake_getBoolPref(pref) {
return this._getPref(pref);
},
getIntPref: function fake_getIntPref(pref) {
return this._getPref(pref);
},
addObserver: function fake_addObserver() {}
};
function makeFakeAsyncFunc(retval) {
Cu.import("resource://weave/async.js");
Function.prototype.async = Async.sugar;
function fakeAsyncFunc() {
let self = yield;
Utils.makeTimerForCall(self.cb);
yield;
self.done(retval);
}
return fakeAsyncFunc;
}
function FakeDAVService(contents) {
Cu.import("resource://weave/dav.js");
this.fakeContents = contents;
DAV.__proto__ = this;
this.checkLogin = makeFakeAsyncFunc(true);
}
FakeDAVService.prototype = {
PUT: function fake_PUT(path, data, onComplete) {
this.fakeContents[path] = data;
makeFakeAsyncFunc({status: 200}).async(this, onComplete);
},
GET: function fake_GET(path, onComplete) {
Log4Moz.Service.rootLogger.info("Retrieving " + path);
var result = {status: 404};
if (path in this.fakeContents)
result = {status: 200, responseText: this.fakeContents[path]};
return makeFakeAsyncFunc(result).async(this, onComplete);
},
MKCOL: function fake_MKCOL(path, onComplete) {
Log4Moz.Service.rootLogger.info("Creating dir " + path);
makeFakeAsyncFunc(true).async(this, onComplete);
}
};
function FakePasswordService(contents) {
Cu.import("resource://weave/util.js");
this.fakeContents = contents;
let self = this;
Utils.findPassword = function fake_findPassword(realm, username) {
Log4Moz.Service.rootLogger.trace("Password requested for " +
realm + ":" + username);
if (realm in self.fakeContents && username in self.fakeContents[realm])
return self.fakeContents[realm][username];
else
return null;
};
};

Просмотреть файл

@ -40,7 +40,11 @@ function run_test() {
do_check_eq(timesYielded, 2);
do_check_eq(Async.outstandingGenerators, 1);
do_check_true(fts.processCallback());
do_check_false(fts.processCallback());
do_check_eq(Async.outstandingGenerators, 0);
}

Просмотреть файл

@ -47,4 +47,5 @@ function run_test() {
runTestGenerator.async({});
for (var i = 0; fts.processCallback(); i++) {}
do_check_eq(i, 4);
do_check_eq(Async.outstandingGenerators, 0);
}

Просмотреть файл

@ -1,6 +1,22 @@
Cu.import("resource://weave/util.js");
Cu.import("resource://weave/async.js");
Cu.import("resource://weave/dav.js");
Cu.import("resource://weave/identity.js");
let __fakePasswords = {
'Mozilla Services Password': {foo: "bar"},
'Mozilla Services Encryption Passphrase': {foo: "passphrase"}
};
function run_test() {
var fpasses = new FakePasswordService(__fakePasswords);
var fprefs = new FakePrefService({"encryption" : "none"});
var fds = new FakeDAVService({});
var fts = new FakeTimerService();
var logStats = initTestLogging();
ID.set('Engine:PBE:default', new Identity('Mozilla Services Encryption Passphrase', 'foo'));
// The JS module we're testing, with all members exposed.
var passwords = loadInSandbox("resource://weave/engines/passwords.js");
@ -34,6 +50,11 @@ function run_test() {
do_check_false(psc._itemExists("invalid guid"));
do_check_true(psc._itemExists(fakeUserHash));
// Make sure the engine can sync.
var engine = new passwords.PasswordEngine();
engine.sync();
while (fts.processCallback()) {}
do_check_eq(logStats.errorsLogged, 0);
do_check_eq(Async.outstandingGenerators, 0);
}

Просмотреть файл

@ -1,59 +1,24 @@
Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/wrap.js");
Cu.import("resource://weave/async.js");
Cu.import("resource://weave/util.js");
Cu.import("resource://weave/dav.js");
Cu.import("resource://weave/crypto.js");
Cu.import("resource://weave/identity.js");
Function.prototype.async = Async.sugar;
function makeFakeAsyncFunc(retval) {
function fakeAsyncFunc() {
let self = yield;
Utils.makeTimerForCall(self.cb);
yield;
self.done(retval);
}
return fakeAsyncFunc;
}
function FakePrefs() {}
FakePrefs.prototype = {
__contents: {"log.logger.async" : "Debug",
"username" : "foo",
"serverURL" : "https://example.com/",
"encryption" : true,
"enabled" : true,
"schedule" : 0},
_getPref: function fake__getPref(pref) {
Log4Moz.Service.rootLogger.trace("Getting pref: " + pref);
return this.__contents[pref];
},
getCharPref: function fake_getCharPref(pref) {
return this._getPref(pref);
},
getBoolPref: function fake_getBoolPref(pref) {
return this._getPref(pref);
},
getIntPref: function fake_getIntPref(pref) {
return this._getPref(pref);
},
addObserver: function fake_addObserver() {}
let __fakePrefs = {
"log.logger.async" : "Debug",
"username" : "foo",
"serverURL" : "https://example.com/",
"encryption" : true,
"enabled" : true,
"schedule" : 0
};
Utils.__prefs = new FakePrefs();
let __fakeDAVContents = {
"meta/version" : "2",
"private/privkey" : "fake private key"
};
Utils.findPassword = function fake_findPassword(realm, username) {
let contents = {
'Mozilla Services Password': {foo: "bar"},
'Mozilla Services Encryption Passphrase': {foo: "passphrase"}
};
return contents[realm][username];
let __fakePasswords = {
'Mozilla Services Password': {foo: "bar"},
'Mozilla Services Encryption Passphrase': {foo: "passphrase"}
};
Crypto.__proto__ = {
@ -68,22 +33,6 @@ Crypto.__proto__ = {
}
};
DAV.__proto__ = {
checkLogin: makeFakeAsyncFunc(true),
__contents: {"meta/version" : "2",
"private/privkey" : "fake private key"},
GET: function fake_GET(path, onComplete) {
Log4Moz.Service.rootLogger.info("Retrieving " + path);
var result = {status: 404};
if (path in this.__contents)
result = {status: 200, responseText: this.__contents[path]};
return makeFakeAsyncFunc(result).async(this, onComplete);
}
};
let Service = loadInSandbox("resource://weave/service.js");
function TestService() {
@ -99,6 +48,9 @@ TestService.prototype = {
TestService.prototype.__proto__ = Service.WeaveSvc.prototype;
function test_login_works() {
var fds = new FakeDAVService(__fakeDAVContents);
var fprefs = new FakePrefService(__fakePrefs);
var fpasses = new FakePasswordService(__fakePasswords);
var fts = new FakeTimerService();
var logStats = initTestLogging();
var testService = new TestService();
@ -116,4 +68,5 @@ function test_login_works() {
do_check_true(finished);
do_check_true(successful);
do_check_eq(logStats.errorsLogged, 0);
do_check_eq(Async.outstandingGenerators, 0);
}