Bug 692714 - Part 2: port tests to SyncServer for non-Aurora landing. r=philikon

This commit is contained in:
Richard Newman 2011-10-19 18:06:55 -07:00
Родитель dea37c5e0c
Коммит c42a36c54a
1 изменённых файлов: 30 добавлений и 80 удалений

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

@ -49,8 +49,6 @@ const reassignBody = "\"server request: node reassignment\"";
// API-compatible with SyncServer handler. Bind `handler` to something to use // API-compatible with SyncServer handler. Bind `handler` to something to use
// as a ServerCollection handler. // as a ServerCollection handler.
// We keep this format because in a patch or two we're going to switch to using
// SyncServer.
function handleReassign(handler, req, resp) { function handleReassign(handler, req, resp) {
resp.setStatusLine(req.httpVersion, 401, "Node reassignment"); resp.setStatusLine(req.httpVersion, 401, "Node reassignment");
resp.setHeader("Content-Type", "application/json"); resp.setHeader("Content-Type", "application/json");
@ -70,29 +68,10 @@ function installNodeHandler(server, next) {
Utils.nextTick(next); Utils.nextTick(next);
} }
let nodePath = "/user/1.0/johndoe/node/weave"; let nodePath = "/user/1.0/johndoe/node/weave";
server.registerPathHandler(nodePath, handleNodeRequest); server.server.registerPathHandler(nodePath, handleNodeRequest);
_("Registered node handler at " + nodePath); _("Registered node handler at " + nodePath);
} }
/**
* Optionally return a 401 for the provided handler, if the value of `name` in
* the `reassignments` object is true.
*/
let reassignments = {
"crypto": false,
"info": false,
"meta": false,
"rotary": false
};
function maybeReassign(handler, name) {
return function (request, response) {
if (reassignments[name]) {
return handleReassign(null, request, response);
}
return handler(request, response);
};
}
function prepareServer() { function prepareServer() {
Service.username = "johndoe"; Service.username = "johndoe";
Service.passphrase = "abcdeabcdeabcdeabcdeabcdea"; Service.passphrase = "abcdeabcdeabcdeabcdeabcdea";
@ -101,33 +80,10 @@ function prepareServer() {
Service.clusterURL = "http://localhost:8080/"; Service.clusterURL = "http://localhost:8080/";
do_check_eq(Service.userAPI, "http://localhost:8080/user/1.0/"); do_check_eq(Service.userAPI, "http://localhost:8080/user/1.0/");
let server = new SyncServer();
let collectionsHelper = track_collections_helper(); server.registerUser("johndoe");
let upd = collectionsHelper.with_updated_collection; server.start();
let collections = collectionsHelper.collections; return server;
let engine = Engines.get("rotary");
let engines = {rotary: {version: engine.version,
syncID: engine.syncID}};
let global = new ServerWBO("global", {engines: engines});
let rotary = new ServerCollection({}, true);
let clients = new ServerCollection({}, true);
let keys = new ServerWBO("keys");
let rotaryHandler = maybeReassign(upd("rotary", rotary.handler()), "rotary");
let cryptoHandler = maybeReassign(upd("crypto", keys.handler()), "crypto");
let metaHandler = maybeReassign(upd("meta", global.handler()), "meta");
let infoHandler = maybeReassign(collectionsHelper.handler, "info");
let server = httpd_setup({
"/1.1/johndoe/storage/clients": upd("clients", clients.handler()),
"/1.1/johndoe/storage/crypto/keys": cryptoHandler,
"/1.1/johndoe/storage/meta/global": metaHandler,
"/1.1/johndoe/storage/rotary": rotaryHandler,
"/1.1/johndoe/info/collections": infoHandler
});
return [server, global, rotary, collectionsHelper];
} }
/** /**
@ -190,7 +146,8 @@ function syncAndExpectNodeReassignment(server, firstNotification, undo,
add_test(function test_momentary_401_engine() { add_test(function test_momentary_401_engine() {
_("Test a failure for engine URLs that's resolved by reassignment."); _("Test a failure for engine URLs that's resolved by reassignment.");
let [server, global, rotary, collectionsHelper] = prepareServer(); let server = prepareServer();
let john = server.user("johndoe");
_("Enabling the Rotary engine."); _("Enabling the Rotary engine.");
let engine = Engines.get("rotary"); let engine = Engines.get("rotary");
@ -198,28 +155,26 @@ add_test(function test_momentary_401_engine() {
// We need the server to be correctly set up prior to experimenting. Do this // We need the server to be correctly set up prior to experimenting. Do this
// through a sync. // through a sync.
let g = {syncID: Service.syncID, let global = {syncID: Service.syncID,
storageVersion: STORAGE_VERSION, storageVersion: STORAGE_VERSION,
rotary: {version: engine.version, rotary: {version: engine.version,
syncID: engine.syncID}} syncID: engine.syncID}}
john.createCollection("meta").insert("global", global);
global.payload = JSON.stringify(g);
global.modified = new_timestamp();
collectionsHelper.update_collection("meta", global.modified);
_("First sync to prepare server contents."); _("First sync to prepare server contents.");
Service.sync(); Service.sync();
_("Setting up Rotary collection to 401."); _("Setting up Rotary collection to 401.");
reassignments["rotary"] = true; let rotary = john.createCollection("rotary");
let oldHandler = rotary.collectionHandler;
rotary.collectionHandler = handleReassign.bind(this, undefined);
// We want to verify that the clusterURL pref has been cleared after a 401 // We want to verify that the clusterURL pref has been cleared after a 401
// inside a sync. Flag the Rotary engine to need syncing. // inside a sync. Flag the Rotary engine to need syncing.
rotary.modified = new_timestamp() + 10; john.collection("rotary").timestamp += 1000;
collectionsHelper.update_collection("rotary", rotary.modified);
function undo() { function undo() {
reassignments["rotary"] = false; rotary.collectionHandler = oldHandler;
} }
syncAndExpectNodeReassignment(server, syncAndExpectNodeReassignment(server,
@ -232,16 +187,17 @@ add_test(function test_momentary_401_engine() {
// This test ends up being a failing fetch *after we're already logged in*. // This test ends up being a failing fetch *after we're already logged in*.
add_test(function test_momentary_401_info_collections() { add_test(function test_momentary_401_info_collections() {
_("Test a failure for info/collections that's resolved by reassignment."); _("Test a failure for info/collections that's resolved by reassignment.");
let [server, global, rotary] = prepareServer(); let server = prepareServer();
_("First sync to prepare server contents."); _("First sync to prepare server contents.");
Service.sync(); Service.sync();
// Return a 401 for info/collections requests. // Return a 401 for info requests, particularly info/collections.
reassignments["info"] = true; let oldHandler = server.toplevelHandlers.info;
server.toplevelHandlers.info = handleReassign;
function undo() { function undo() {
reassignments["info"] = false; server.toplevelHandlers.info = oldHandler;
} }
syncAndExpectNodeReassignment(server, syncAndExpectNodeReassignment(server,
@ -254,17 +210,14 @@ add_test(function test_momentary_401_info_collections() {
add_test(function test_momentary_401_storage() { add_test(function test_momentary_401_storage() {
_("Test a failure for any storage URL, not just engine parts. " + _("Test a failure for any storage URL, not just engine parts. " +
"Resolved by reassignment."); "Resolved by reassignment.");
let [server, global, rotary] = prepareServer(); let server = prepareServer();
// Return a 401 for all storage requests. // Return a 401 for all storage requests.
reassignments["crypto"] = true; let oldHandler = server.toplevelHandlers.storage;
reassignments["meta"] = true; server.toplevelHandlers.storage = handleReassign;
reassignments["rotary"] = true;
function undo() { function undo() {
reassignments["crypto"] = false; server.toplevelHandlers.storage = oldHandler;
reassignments["meta"] = false;
reassignments["rotary"] = false;
} }
syncAndExpectNodeReassignment(server, syncAndExpectNodeReassignment(server,
@ -278,12 +231,11 @@ add_test(function test_loop_avoidance() {
_("Test that a repeated failure doesn't result in a sync loop " + _("Test that a repeated failure doesn't result in a sync loop " +
"if node reassignment cannot resolve the failure."); "if node reassignment cannot resolve the failure.");
let [server, global, rotary] = prepareServer(); let server = prepareServer();
// Return a 401 for all storage requests. // Return a 401 for all storage requests.
reassignments["crypto"] = true; let oldHandler = server.toplevelHandlers.storage;
reassignments["meta"] = true; server.toplevelHandlers.storage = handleReassign;
reassignments["rotary"] = true;
let firstNotification = "weave:service:login:error"; let firstNotification = "weave:service:login:error";
let secondNotification = "weave:service:login:error"; let secondNotification = "weave:service:login:error";
@ -348,9 +300,7 @@ add_test(function test_loop_avoidance() {
do_check_true(!!SyncScheduler.syncTimer); do_check_true(!!SyncScheduler.syncTimer);
// Undo our evil scheme. // Undo our evil scheme.
reassignments["crypto"] = false; server.toplevelHandlers.storage = oldHandler;
reassignments["meta"] = false;
reassignments["rotary"] = false;
// Bring the timer forward to kick off a successful sync, so we can watch // Bring the timer forward to kick off a successful sync, so we can watch
// the pref get cleared. // the pref get cleared.