services/sync/tests/unit/test_syncengine.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_syncengine.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,204 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Cu.import("resource://services-sync/engines.js");
     1.8 +Cu.import("resource://services-sync/service.js");
     1.9 +Cu.import("resource://services-sync/util.js");
    1.10 +Cu.import("resource://testing-common/services/sync/utils.js");
    1.11 +
    1.12 +function makeSteamEngine() {
    1.13 +  return new SyncEngine('Steam', Service);
    1.14 +}
    1.15 +
    1.16 +let server;
    1.17 +
    1.18 +function test_url_attributes() {
    1.19 +  _("SyncEngine url attributes");
    1.20 +  let syncTesting = new SyncTestingInfrastructure(server);
    1.21 +  Service.clusterURL = "https://cluster/";
    1.22 +  let engine = makeSteamEngine();
    1.23 +  try {
    1.24 +    do_check_eq(engine.storageURL, "https://cluster/1.1/foo/storage/");
    1.25 +    do_check_eq(engine.engineURL, "https://cluster/1.1/foo/storage/steam");
    1.26 +    do_check_eq(engine.metaURL, "https://cluster/1.1/foo/storage/meta/global");
    1.27 +  } finally {
    1.28 +    Svc.Prefs.resetBranch("");
    1.29 +  }
    1.30 +}
    1.31 +
    1.32 +function test_syncID() {
    1.33 +  _("SyncEngine.syncID corresponds to preference");
    1.34 +  let syncTesting = new SyncTestingInfrastructure(server);
    1.35 +  let engine = makeSteamEngine();
    1.36 +  try {
    1.37 +    // Ensure pristine environment
    1.38 +    do_check_eq(Svc.Prefs.get("steam.syncID"), undefined);
    1.39 +
    1.40 +    // Performing the first get on the attribute will generate a new GUID.
    1.41 +    do_check_eq(engine.syncID, "fake-guid-0");
    1.42 +    do_check_eq(Svc.Prefs.get("steam.syncID"), "fake-guid-0");
    1.43 +
    1.44 +    Svc.Prefs.set("steam.syncID", Utils.makeGUID());
    1.45 +    do_check_eq(Svc.Prefs.get("steam.syncID"), "fake-guid-1");
    1.46 +    do_check_eq(engine.syncID, "fake-guid-1");
    1.47 +  } finally {
    1.48 +    Svc.Prefs.resetBranch("");
    1.49 +  }
    1.50 +}
    1.51 +
    1.52 +function test_lastSync() {
    1.53 +  _("SyncEngine.lastSync and SyncEngine.lastSyncLocal correspond to preferences");
    1.54 +  let syncTesting = new SyncTestingInfrastructure(server);
    1.55 +  let engine = makeSteamEngine();
    1.56 +  try {
    1.57 +    // Ensure pristine environment
    1.58 +    do_check_eq(Svc.Prefs.get("steam.lastSync"), undefined);
    1.59 +    do_check_eq(engine.lastSync, 0);
    1.60 +    do_check_eq(Svc.Prefs.get("steam.lastSyncLocal"), undefined);
    1.61 +    do_check_eq(engine.lastSyncLocal, 0);
    1.62 +
    1.63 +    // Floats are properly stored as floats and synced with the preference
    1.64 +    engine.lastSync = 123.45;
    1.65 +    do_check_eq(engine.lastSync, 123.45);
    1.66 +    do_check_eq(Svc.Prefs.get("steam.lastSync"), "123.45");
    1.67 +
    1.68 +    // Integer is properly stored
    1.69 +    engine.lastSyncLocal = 67890;
    1.70 +    do_check_eq(engine.lastSyncLocal, 67890);
    1.71 +    do_check_eq(Svc.Prefs.get("steam.lastSyncLocal"), "67890");
    1.72 +
    1.73 +    // resetLastSync() resets the value (and preference) to 0
    1.74 +    engine.resetLastSync();
    1.75 +    do_check_eq(engine.lastSync, 0);
    1.76 +    do_check_eq(Svc.Prefs.get("steam.lastSync"), "0");
    1.77 +  } finally {
    1.78 +    Svc.Prefs.resetBranch("");
    1.79 +  }
    1.80 +}
    1.81 +
    1.82 +function test_toFetch() {
    1.83 +  _("SyncEngine.toFetch corresponds to file on disk");
    1.84 +  let syncTesting = new SyncTestingInfrastructure(server);
    1.85 +  const filename = "weave/toFetch/steam.json";
    1.86 +  let engine = makeSteamEngine();
    1.87 +  try {
    1.88 +    // Ensure pristine environment
    1.89 +    do_check_eq(engine.toFetch.length, 0);
    1.90 +
    1.91 +    // Write file to disk
    1.92 +    let toFetch = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
    1.93 +    engine.toFetch = toFetch;
    1.94 +    do_check_eq(engine.toFetch, toFetch);
    1.95 +    // toFetch is written asynchronously
    1.96 +    engine._store._sleep(0);
    1.97 +    let fakefile = syncTesting.fakeFilesystem.fakeContents[filename];
    1.98 +    do_check_eq(fakefile, JSON.stringify(toFetch));
    1.99 +
   1.100 +    // Read file from disk
   1.101 +    toFetch = [Utils.makeGUID(), Utils.makeGUID()];
   1.102 +    syncTesting.fakeFilesystem.fakeContents[filename] = JSON.stringify(toFetch);
   1.103 +    engine.loadToFetch();
   1.104 +    do_check_eq(engine.toFetch.length, 2);
   1.105 +    do_check_eq(engine.toFetch[0], toFetch[0]);
   1.106 +    do_check_eq(engine.toFetch[1], toFetch[1]);
   1.107 +  } finally {
   1.108 +    Svc.Prefs.resetBranch("");
   1.109 +  }
   1.110 +}
   1.111 +
   1.112 +function test_previousFailed() {
   1.113 +  _("SyncEngine.previousFailed corresponds to file on disk");
   1.114 +  let syncTesting = new SyncTestingInfrastructure(server);
   1.115 +  const filename = "weave/failed/steam.json";
   1.116 +  let engine = makeSteamEngine();
   1.117 +  try {
   1.118 +    // Ensure pristine environment
   1.119 +    do_check_eq(engine.previousFailed.length, 0);
   1.120 +
   1.121 +    // Write file to disk
   1.122 +    let previousFailed = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
   1.123 +    engine.previousFailed = previousFailed;
   1.124 +    do_check_eq(engine.previousFailed, previousFailed);
   1.125 +    // previousFailed is written asynchronously
   1.126 +    engine._store._sleep(0);
   1.127 +    let fakefile = syncTesting.fakeFilesystem.fakeContents[filename];
   1.128 +    do_check_eq(fakefile, JSON.stringify(previousFailed));
   1.129 +
   1.130 +    // Read file from disk
   1.131 +    previousFailed = [Utils.makeGUID(), Utils.makeGUID()];
   1.132 +    syncTesting.fakeFilesystem.fakeContents[filename] = JSON.stringify(previousFailed);
   1.133 +    engine.loadPreviousFailed();
   1.134 +    do_check_eq(engine.previousFailed.length, 2);
   1.135 +    do_check_eq(engine.previousFailed[0], previousFailed[0]);
   1.136 +    do_check_eq(engine.previousFailed[1], previousFailed[1]);
   1.137 +  } finally {
   1.138 +    Svc.Prefs.resetBranch("");
   1.139 +  }
   1.140 +}
   1.141 +
   1.142 +function test_resetClient() {
   1.143 +  _("SyncEngine.resetClient resets lastSync and toFetch");
   1.144 +  let syncTesting = new SyncTestingInfrastructure(server);
   1.145 +  let engine = makeSteamEngine();
   1.146 +  try {
   1.147 +    // Ensure pristine environment
   1.148 +    do_check_eq(Svc.Prefs.get("steam.lastSync"), undefined);
   1.149 +    do_check_eq(Svc.Prefs.get("steam.lastSyncLocal"), undefined);
   1.150 +    do_check_eq(engine.toFetch.length, 0);
   1.151 +
   1.152 +    engine.lastSync = 123.45;
   1.153 +    engine.lastSyncLocal = 67890;
   1.154 +    engine.toFetch = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
   1.155 +    engine.previousFailed = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
   1.156 +
   1.157 +    engine.resetClient();
   1.158 +    do_check_eq(engine.lastSync, 0);
   1.159 +    do_check_eq(engine.lastSyncLocal, 0);
   1.160 +    do_check_eq(engine.toFetch.length, 0);
   1.161 +    do_check_eq(engine.previousFailed.length, 0);
   1.162 +  } finally {
   1.163 +    Svc.Prefs.resetBranch("");
   1.164 +  }
   1.165 +}
   1.166 +
   1.167 +function test_wipeServer() {
   1.168 +  _("SyncEngine.wipeServer deletes server data and resets the client.");
   1.169 +  let engine = makeSteamEngine();
   1.170 +
   1.171 +  const PAYLOAD = 42;
   1.172 +  let steamCollection = new ServerWBO("steam", PAYLOAD);
   1.173 +  let server = httpd_setup({
   1.174 +    "/1.1/foo/storage/steam": steamCollection.handler()
   1.175 +  });
   1.176 +  let syncTesting = new SyncTestingInfrastructure(server);
   1.177 +  do_test_pending();
   1.178 +
   1.179 +  try {
   1.180 +    // Some data to reset.
   1.181 +    engine.lastSync = 123.45;
   1.182 +    engine.toFetch = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
   1.183 +
   1.184 +    _("Wipe server data and reset client.");
   1.185 +    engine.wipeServer();
   1.186 +    do_check_eq(steamCollection.payload, undefined);
   1.187 +    do_check_eq(engine.lastSync, 0);
   1.188 +    do_check_eq(engine.toFetch.length, 0);
   1.189 +
   1.190 +  } finally {
   1.191 +    server.stop(do_test_finished);
   1.192 +    Svc.Prefs.resetBranch("");
   1.193 +  }
   1.194 +}
   1.195 +
   1.196 +function run_test() {
   1.197 +  server = httpd_setup({});
   1.198 +  test_url_attributes();
   1.199 +  test_syncID();
   1.200 +  test_lastSync();
   1.201 +  test_toFetch();
   1.202 +  test_previousFailed();
   1.203 +  test_resetClient();
   1.204 +  test_wipeServer();
   1.205 +
   1.206 +  server.stop(run_next_test);
   1.207 +}

mercurial