services/sync/tests/unit/test_syncengine.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 Cu.import("resource://services-sync/engines.js");
michael@0 5 Cu.import("resource://services-sync/service.js");
michael@0 6 Cu.import("resource://services-sync/util.js");
michael@0 7 Cu.import("resource://testing-common/services/sync/utils.js");
michael@0 8
michael@0 9 function makeSteamEngine() {
michael@0 10 return new SyncEngine('Steam', Service);
michael@0 11 }
michael@0 12
michael@0 13 let server;
michael@0 14
michael@0 15 function test_url_attributes() {
michael@0 16 _("SyncEngine url attributes");
michael@0 17 let syncTesting = new SyncTestingInfrastructure(server);
michael@0 18 Service.clusterURL = "https://cluster/";
michael@0 19 let engine = makeSteamEngine();
michael@0 20 try {
michael@0 21 do_check_eq(engine.storageURL, "https://cluster/1.1/foo/storage/");
michael@0 22 do_check_eq(engine.engineURL, "https://cluster/1.1/foo/storage/steam");
michael@0 23 do_check_eq(engine.metaURL, "https://cluster/1.1/foo/storage/meta/global");
michael@0 24 } finally {
michael@0 25 Svc.Prefs.resetBranch("");
michael@0 26 }
michael@0 27 }
michael@0 28
michael@0 29 function test_syncID() {
michael@0 30 _("SyncEngine.syncID corresponds to preference");
michael@0 31 let syncTesting = new SyncTestingInfrastructure(server);
michael@0 32 let engine = makeSteamEngine();
michael@0 33 try {
michael@0 34 // Ensure pristine environment
michael@0 35 do_check_eq(Svc.Prefs.get("steam.syncID"), undefined);
michael@0 36
michael@0 37 // Performing the first get on the attribute will generate a new GUID.
michael@0 38 do_check_eq(engine.syncID, "fake-guid-0");
michael@0 39 do_check_eq(Svc.Prefs.get("steam.syncID"), "fake-guid-0");
michael@0 40
michael@0 41 Svc.Prefs.set("steam.syncID", Utils.makeGUID());
michael@0 42 do_check_eq(Svc.Prefs.get("steam.syncID"), "fake-guid-1");
michael@0 43 do_check_eq(engine.syncID, "fake-guid-1");
michael@0 44 } finally {
michael@0 45 Svc.Prefs.resetBranch("");
michael@0 46 }
michael@0 47 }
michael@0 48
michael@0 49 function test_lastSync() {
michael@0 50 _("SyncEngine.lastSync and SyncEngine.lastSyncLocal correspond to preferences");
michael@0 51 let syncTesting = new SyncTestingInfrastructure(server);
michael@0 52 let engine = makeSteamEngine();
michael@0 53 try {
michael@0 54 // Ensure pristine environment
michael@0 55 do_check_eq(Svc.Prefs.get("steam.lastSync"), undefined);
michael@0 56 do_check_eq(engine.lastSync, 0);
michael@0 57 do_check_eq(Svc.Prefs.get("steam.lastSyncLocal"), undefined);
michael@0 58 do_check_eq(engine.lastSyncLocal, 0);
michael@0 59
michael@0 60 // Floats are properly stored as floats and synced with the preference
michael@0 61 engine.lastSync = 123.45;
michael@0 62 do_check_eq(engine.lastSync, 123.45);
michael@0 63 do_check_eq(Svc.Prefs.get("steam.lastSync"), "123.45");
michael@0 64
michael@0 65 // Integer is properly stored
michael@0 66 engine.lastSyncLocal = 67890;
michael@0 67 do_check_eq(engine.lastSyncLocal, 67890);
michael@0 68 do_check_eq(Svc.Prefs.get("steam.lastSyncLocal"), "67890");
michael@0 69
michael@0 70 // resetLastSync() resets the value (and preference) to 0
michael@0 71 engine.resetLastSync();
michael@0 72 do_check_eq(engine.lastSync, 0);
michael@0 73 do_check_eq(Svc.Prefs.get("steam.lastSync"), "0");
michael@0 74 } finally {
michael@0 75 Svc.Prefs.resetBranch("");
michael@0 76 }
michael@0 77 }
michael@0 78
michael@0 79 function test_toFetch() {
michael@0 80 _("SyncEngine.toFetch corresponds to file on disk");
michael@0 81 let syncTesting = new SyncTestingInfrastructure(server);
michael@0 82 const filename = "weave/toFetch/steam.json";
michael@0 83 let engine = makeSteamEngine();
michael@0 84 try {
michael@0 85 // Ensure pristine environment
michael@0 86 do_check_eq(engine.toFetch.length, 0);
michael@0 87
michael@0 88 // Write file to disk
michael@0 89 let toFetch = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
michael@0 90 engine.toFetch = toFetch;
michael@0 91 do_check_eq(engine.toFetch, toFetch);
michael@0 92 // toFetch is written asynchronously
michael@0 93 engine._store._sleep(0);
michael@0 94 let fakefile = syncTesting.fakeFilesystem.fakeContents[filename];
michael@0 95 do_check_eq(fakefile, JSON.stringify(toFetch));
michael@0 96
michael@0 97 // Read file from disk
michael@0 98 toFetch = [Utils.makeGUID(), Utils.makeGUID()];
michael@0 99 syncTesting.fakeFilesystem.fakeContents[filename] = JSON.stringify(toFetch);
michael@0 100 engine.loadToFetch();
michael@0 101 do_check_eq(engine.toFetch.length, 2);
michael@0 102 do_check_eq(engine.toFetch[0], toFetch[0]);
michael@0 103 do_check_eq(engine.toFetch[1], toFetch[1]);
michael@0 104 } finally {
michael@0 105 Svc.Prefs.resetBranch("");
michael@0 106 }
michael@0 107 }
michael@0 108
michael@0 109 function test_previousFailed() {
michael@0 110 _("SyncEngine.previousFailed corresponds to file on disk");
michael@0 111 let syncTesting = new SyncTestingInfrastructure(server);
michael@0 112 const filename = "weave/failed/steam.json";
michael@0 113 let engine = makeSteamEngine();
michael@0 114 try {
michael@0 115 // Ensure pristine environment
michael@0 116 do_check_eq(engine.previousFailed.length, 0);
michael@0 117
michael@0 118 // Write file to disk
michael@0 119 let previousFailed = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
michael@0 120 engine.previousFailed = previousFailed;
michael@0 121 do_check_eq(engine.previousFailed, previousFailed);
michael@0 122 // previousFailed is written asynchronously
michael@0 123 engine._store._sleep(0);
michael@0 124 let fakefile = syncTesting.fakeFilesystem.fakeContents[filename];
michael@0 125 do_check_eq(fakefile, JSON.stringify(previousFailed));
michael@0 126
michael@0 127 // Read file from disk
michael@0 128 previousFailed = [Utils.makeGUID(), Utils.makeGUID()];
michael@0 129 syncTesting.fakeFilesystem.fakeContents[filename] = JSON.stringify(previousFailed);
michael@0 130 engine.loadPreviousFailed();
michael@0 131 do_check_eq(engine.previousFailed.length, 2);
michael@0 132 do_check_eq(engine.previousFailed[0], previousFailed[0]);
michael@0 133 do_check_eq(engine.previousFailed[1], previousFailed[1]);
michael@0 134 } finally {
michael@0 135 Svc.Prefs.resetBranch("");
michael@0 136 }
michael@0 137 }
michael@0 138
michael@0 139 function test_resetClient() {
michael@0 140 _("SyncEngine.resetClient resets lastSync and toFetch");
michael@0 141 let syncTesting = new SyncTestingInfrastructure(server);
michael@0 142 let engine = makeSteamEngine();
michael@0 143 try {
michael@0 144 // Ensure pristine environment
michael@0 145 do_check_eq(Svc.Prefs.get("steam.lastSync"), undefined);
michael@0 146 do_check_eq(Svc.Prefs.get("steam.lastSyncLocal"), undefined);
michael@0 147 do_check_eq(engine.toFetch.length, 0);
michael@0 148
michael@0 149 engine.lastSync = 123.45;
michael@0 150 engine.lastSyncLocal = 67890;
michael@0 151 engine.toFetch = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
michael@0 152 engine.previousFailed = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
michael@0 153
michael@0 154 engine.resetClient();
michael@0 155 do_check_eq(engine.lastSync, 0);
michael@0 156 do_check_eq(engine.lastSyncLocal, 0);
michael@0 157 do_check_eq(engine.toFetch.length, 0);
michael@0 158 do_check_eq(engine.previousFailed.length, 0);
michael@0 159 } finally {
michael@0 160 Svc.Prefs.resetBranch("");
michael@0 161 }
michael@0 162 }
michael@0 163
michael@0 164 function test_wipeServer() {
michael@0 165 _("SyncEngine.wipeServer deletes server data and resets the client.");
michael@0 166 let engine = makeSteamEngine();
michael@0 167
michael@0 168 const PAYLOAD = 42;
michael@0 169 let steamCollection = new ServerWBO("steam", PAYLOAD);
michael@0 170 let server = httpd_setup({
michael@0 171 "/1.1/foo/storage/steam": steamCollection.handler()
michael@0 172 });
michael@0 173 let syncTesting = new SyncTestingInfrastructure(server);
michael@0 174 do_test_pending();
michael@0 175
michael@0 176 try {
michael@0 177 // Some data to reset.
michael@0 178 engine.lastSync = 123.45;
michael@0 179 engine.toFetch = [Utils.makeGUID(), Utils.makeGUID(), Utils.makeGUID()];
michael@0 180
michael@0 181 _("Wipe server data and reset client.");
michael@0 182 engine.wipeServer();
michael@0 183 do_check_eq(steamCollection.payload, undefined);
michael@0 184 do_check_eq(engine.lastSync, 0);
michael@0 185 do_check_eq(engine.toFetch.length, 0);
michael@0 186
michael@0 187 } finally {
michael@0 188 server.stop(do_test_finished);
michael@0 189 Svc.Prefs.resetBranch("");
michael@0 190 }
michael@0 191 }
michael@0 192
michael@0 193 function run_test() {
michael@0 194 server = httpd_setup({});
michael@0 195 test_url_attributes();
michael@0 196 test_syncID();
michael@0 197 test_lastSync();
michael@0 198 test_toFetch();
michael@0 199 test_previousFailed();
michael@0 200 test_resetClient();
michael@0 201 test_wipeServer();
michael@0 202
michael@0 203 server.stop(run_next_test);
michael@0 204 }

mercurial