Thu, 22 Jan 2015 13:21:57 +0100
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 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | const {utils: Cu} = Components; |
michael@0 | 7 | |
michael@0 | 8 | Cu.import("resource://gre/modules/Metrics.jsm"); |
michael@0 | 9 | Cu.import("resource://gre/modules/Preferences.jsm"); |
michael@0 | 10 | Cu.import("resource://gre/modules/Task.jsm"); |
michael@0 | 11 | Cu.import("resource://testing-common/services/metrics/mocks.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | |
michael@0 | 14 | const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000; |
michael@0 | 15 | |
michael@0 | 16 | |
michael@0 | 17 | function getProvider(storageName) { |
michael@0 | 18 | return Task.spawn(function () { |
michael@0 | 19 | let provider = new DummyProvider(); |
michael@0 | 20 | let storage = yield Metrics.Storage(storageName); |
michael@0 | 21 | |
michael@0 | 22 | yield provider.init(storage); |
michael@0 | 23 | |
michael@0 | 24 | throw new Task.Result(provider); |
michael@0 | 25 | }); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | |
michael@0 | 29 | function run_test() { |
michael@0 | 30 | run_next_test(); |
michael@0 | 31 | }; |
michael@0 | 32 | |
michael@0 | 33 | add_test(function test_constructor() { |
michael@0 | 34 | let failed = false; |
michael@0 | 35 | try { |
michael@0 | 36 | new Metrics.Provider(); |
michael@0 | 37 | } catch(ex) { |
michael@0 | 38 | do_check_true(ex.message.startsWith("Provider must define a name")); |
michael@0 | 39 | failed = true; |
michael@0 | 40 | } |
michael@0 | 41 | finally { |
michael@0 | 42 | do_check_true(failed); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | run_next_test(); |
michael@0 | 46 | }); |
michael@0 | 47 | |
michael@0 | 48 | add_task(function test_init() { |
michael@0 | 49 | let provider = new DummyProvider(); |
michael@0 | 50 | let storage = yield Metrics.Storage("init"); |
michael@0 | 51 | |
michael@0 | 52 | yield provider.init(storage); |
michael@0 | 53 | |
michael@0 | 54 | let m = provider.getMeasurement("DummyMeasurement", 1); |
michael@0 | 55 | do_check_true(m instanceof Metrics.Measurement); |
michael@0 | 56 | do_check_eq(m.id, 1); |
michael@0 | 57 | do_check_eq(Object.keys(m._fields).length, 7); |
michael@0 | 58 | do_check_true(m.hasField("daily-counter")); |
michael@0 | 59 | do_check_false(m.hasField("does-not-exist")); |
michael@0 | 60 | |
michael@0 | 61 | yield storage.close(); |
michael@0 | 62 | }); |
michael@0 | 63 | |
michael@0 | 64 | add_task(function test_default_collectors() { |
michael@0 | 65 | let provider = new DummyProvider(); |
michael@0 | 66 | let storage = yield Metrics.Storage("default_collectors"); |
michael@0 | 67 | yield provider.init(storage); |
michael@0 | 68 | |
michael@0 | 69 | for (let property in Metrics.Provider.prototype) { |
michael@0 | 70 | if (!property.startsWith("collect")) { |
michael@0 | 71 | continue; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | let result = provider[property](); |
michael@0 | 75 | do_check_neq(result, null); |
michael@0 | 76 | do_check_eq(typeof(result.then), "function"); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | yield storage.close(); |
michael@0 | 80 | }); |
michael@0 | 81 | |
michael@0 | 82 | add_task(function test_measurement_storage_basic() { |
michael@0 | 83 | let provider = yield getProvider("measurement_storage_basic"); |
michael@0 | 84 | let m = provider.getMeasurement("DummyMeasurement", 1); |
michael@0 | 85 | |
michael@0 | 86 | let now = new Date(); |
michael@0 | 87 | let yesterday = new Date(now.getTime() - MILLISECONDS_PER_DAY); |
michael@0 | 88 | |
michael@0 | 89 | // Daily counter. |
michael@0 | 90 | let counterID = m.fieldID("daily-counter"); |
michael@0 | 91 | yield m.incrementDailyCounter("daily-counter", now); |
michael@0 | 92 | yield m.incrementDailyCounter("daily-counter", now); |
michael@0 | 93 | yield m.incrementDailyCounter("daily-counter", yesterday); |
michael@0 | 94 | let count = yield provider.storage.getDailyCounterCountFromFieldID(counterID, now); |
michael@0 | 95 | do_check_eq(count, 2); |
michael@0 | 96 | |
michael@0 | 97 | count = yield provider.storage.getDailyCounterCountFromFieldID(counterID, yesterday); |
michael@0 | 98 | do_check_eq(count, 1); |
michael@0 | 99 | |
michael@0 | 100 | yield m.incrementDailyCounter("daily-counter", now, 4); |
michael@0 | 101 | count = yield provider.storage.getDailyCounterCountFromFieldID(counterID, now); |
michael@0 | 102 | do_check_eq(count, 6); |
michael@0 | 103 | |
michael@0 | 104 | // Daily discrete numeric. |
michael@0 | 105 | let dailyDiscreteNumericID = m.fieldID("daily-discrete-numeric"); |
michael@0 | 106 | yield m.addDailyDiscreteNumeric("daily-discrete-numeric", 5, now); |
michael@0 | 107 | yield m.addDailyDiscreteNumeric("daily-discrete-numeric", 6, now); |
michael@0 | 108 | yield m.addDailyDiscreteNumeric("daily-discrete-numeric", 7, yesterday); |
michael@0 | 109 | |
michael@0 | 110 | let values = yield provider.storage.getDailyDiscreteNumericFromFieldID( |
michael@0 | 111 | dailyDiscreteNumericID, now); |
michael@0 | 112 | |
michael@0 | 113 | do_check_eq(values.size, 1); |
michael@0 | 114 | do_check_true(values.hasDay(now)); |
michael@0 | 115 | let actual = values.getDay(now); |
michael@0 | 116 | do_check_eq(actual.length, 2); |
michael@0 | 117 | do_check_eq(actual[0], 5); |
michael@0 | 118 | do_check_eq(actual[1], 6); |
michael@0 | 119 | |
michael@0 | 120 | values = yield provider.storage.getDailyDiscreteNumericFromFieldID( |
michael@0 | 121 | dailyDiscreteNumericID, yesterday); |
michael@0 | 122 | |
michael@0 | 123 | do_check_eq(values.size, 1); |
michael@0 | 124 | do_check_true(values.hasDay(yesterday)); |
michael@0 | 125 | do_check_eq(values.getDay(yesterday)[0], 7); |
michael@0 | 126 | |
michael@0 | 127 | // Daily discrete text. |
michael@0 | 128 | let dailyDiscreteTextID = m.fieldID("daily-discrete-text"); |
michael@0 | 129 | yield m.addDailyDiscreteText("daily-discrete-text", "foo", now); |
michael@0 | 130 | yield m.addDailyDiscreteText("daily-discrete-text", "bar", now); |
michael@0 | 131 | yield m.addDailyDiscreteText("daily-discrete-text", "biz", yesterday); |
michael@0 | 132 | |
michael@0 | 133 | values = yield provider.storage.getDailyDiscreteTextFromFieldID( |
michael@0 | 134 | dailyDiscreteTextID, now); |
michael@0 | 135 | |
michael@0 | 136 | do_check_eq(values.size, 1); |
michael@0 | 137 | do_check_true(values.hasDay(now)); |
michael@0 | 138 | actual = values.getDay(now); |
michael@0 | 139 | do_check_eq(actual.length, 2); |
michael@0 | 140 | do_check_eq(actual[0], "foo"); |
michael@0 | 141 | do_check_eq(actual[1], "bar"); |
michael@0 | 142 | |
michael@0 | 143 | values = yield provider.storage.getDailyDiscreteTextFromFieldID( |
michael@0 | 144 | dailyDiscreteTextID, yesterday); |
michael@0 | 145 | do_check_true(values.hasDay(yesterday)); |
michael@0 | 146 | do_check_eq(values.getDay(yesterday)[0], "biz"); |
michael@0 | 147 | |
michael@0 | 148 | // Daily last numeric. |
michael@0 | 149 | let lastDailyNumericID = m.fieldID("daily-last-numeric"); |
michael@0 | 150 | yield m.setDailyLastNumeric("daily-last-numeric", 5, now); |
michael@0 | 151 | yield m.setDailyLastNumeric("daily-last-numeric", 6, yesterday); |
michael@0 | 152 | |
michael@0 | 153 | let result = yield provider.storage.getDailyLastNumericFromFieldID( |
michael@0 | 154 | lastDailyNumericID, now); |
michael@0 | 155 | do_check_eq(result.size, 1); |
michael@0 | 156 | do_check_true(result.hasDay(now)); |
michael@0 | 157 | do_check_eq(result.getDay(now), 5); |
michael@0 | 158 | |
michael@0 | 159 | result = yield provider.storage.getDailyLastNumericFromFieldID( |
michael@0 | 160 | lastDailyNumericID, yesterday); |
michael@0 | 161 | do_check_true(result.hasDay(yesterday)); |
michael@0 | 162 | do_check_eq(result.getDay(yesterday), 6); |
michael@0 | 163 | |
michael@0 | 164 | yield m.setDailyLastNumeric("daily-last-numeric", 7, now); |
michael@0 | 165 | result = yield provider.storage.getDailyLastNumericFromFieldID( |
michael@0 | 166 | lastDailyNumericID, now); |
michael@0 | 167 | do_check_eq(result.getDay(now), 7); |
michael@0 | 168 | |
michael@0 | 169 | // Daily last text. |
michael@0 | 170 | let lastDailyTextID = m.fieldID("daily-last-text"); |
michael@0 | 171 | yield m.setDailyLastText("daily-last-text", "foo", now); |
michael@0 | 172 | yield m.setDailyLastText("daily-last-text", "bar", yesterday); |
michael@0 | 173 | |
michael@0 | 174 | result = yield provider.storage.getDailyLastTextFromFieldID( |
michael@0 | 175 | lastDailyTextID, now); |
michael@0 | 176 | do_check_eq(result.size, 1); |
michael@0 | 177 | do_check_true(result.hasDay(now)); |
michael@0 | 178 | do_check_eq(result.getDay(now), "foo"); |
michael@0 | 179 | |
michael@0 | 180 | result = yield provider.storage.getDailyLastTextFromFieldID( |
michael@0 | 181 | lastDailyTextID, yesterday); |
michael@0 | 182 | do_check_true(result.hasDay(yesterday)); |
michael@0 | 183 | do_check_eq(result.getDay(yesterday), "bar"); |
michael@0 | 184 | |
michael@0 | 185 | yield m.setDailyLastText("daily-last-text", "biz", now); |
michael@0 | 186 | result = yield provider.storage.getDailyLastTextFromFieldID( |
michael@0 | 187 | lastDailyTextID, now); |
michael@0 | 188 | do_check_eq(result.getDay(now), "biz"); |
michael@0 | 189 | |
michael@0 | 190 | // Last numeric. |
michael@0 | 191 | let lastNumericID = m.fieldID("last-numeric"); |
michael@0 | 192 | yield m.setLastNumeric("last-numeric", 1, now); |
michael@0 | 193 | result = yield provider.storage.getLastNumericFromFieldID(lastNumericID); |
michael@0 | 194 | do_check_eq(result[1], 1); |
michael@0 | 195 | do_check_true(result[0].getTime() < now.getTime()); |
michael@0 | 196 | do_check_true(result[0].getTime() > yesterday.getTime()); |
michael@0 | 197 | |
michael@0 | 198 | yield m.setLastNumeric("last-numeric", 2, now); |
michael@0 | 199 | result = yield provider.storage.getLastNumericFromFieldID(lastNumericID); |
michael@0 | 200 | do_check_eq(result[1], 2); |
michael@0 | 201 | |
michael@0 | 202 | // Last text. |
michael@0 | 203 | let lastTextID = m.fieldID("last-text"); |
michael@0 | 204 | yield m.setLastText("last-text", "foo", now); |
michael@0 | 205 | result = yield provider.storage.getLastTextFromFieldID(lastTextID); |
michael@0 | 206 | do_check_eq(result[1], "foo"); |
michael@0 | 207 | do_check_true(result[0].getTime() < now.getTime()); |
michael@0 | 208 | do_check_true(result[0].getTime() > yesterday.getTime()); |
michael@0 | 209 | |
michael@0 | 210 | yield m.setLastText("last-text", "bar", now); |
michael@0 | 211 | result = yield provider.storage.getLastTextFromFieldID(lastTextID); |
michael@0 | 212 | do_check_eq(result[1], "bar"); |
michael@0 | 213 | |
michael@0 | 214 | yield provider.storage.close(); |
michael@0 | 215 | }); |
michael@0 | 216 | |
michael@0 | 217 | add_task(function test_serialize_json_default() { |
michael@0 | 218 | let provider = yield getProvider("serialize_json_default"); |
michael@0 | 219 | |
michael@0 | 220 | let now = new Date(); |
michael@0 | 221 | let yesterday = new Date(now.getTime() - MILLISECONDS_PER_DAY); |
michael@0 | 222 | |
michael@0 | 223 | let m = provider.getMeasurement("DummyMeasurement", 1); |
michael@0 | 224 | |
michael@0 | 225 | m.incrementDailyCounter("daily-counter", now); |
michael@0 | 226 | m.incrementDailyCounter("daily-counter", now); |
michael@0 | 227 | m.incrementDailyCounter("daily-counter", yesterday); |
michael@0 | 228 | |
michael@0 | 229 | m.addDailyDiscreteNumeric("daily-discrete-numeric", 1, now); |
michael@0 | 230 | m.addDailyDiscreteNumeric("daily-discrete-numeric", 2, now); |
michael@0 | 231 | m.addDailyDiscreteNumeric("daily-discrete-numeric", 3, yesterday); |
michael@0 | 232 | |
michael@0 | 233 | m.addDailyDiscreteText("daily-discrete-text", "foo", now); |
michael@0 | 234 | m.addDailyDiscreteText("daily-discrete-text", "bar", now); |
michael@0 | 235 | m.addDailyDiscreteText("daily-discrete-text", "baz", yesterday); |
michael@0 | 236 | |
michael@0 | 237 | m.setDailyLastNumeric("daily-last-numeric", 4, now); |
michael@0 | 238 | m.setDailyLastNumeric("daily-last-numeric", 5, yesterday); |
michael@0 | 239 | |
michael@0 | 240 | m.setDailyLastText("daily-last-text", "apple", now); |
michael@0 | 241 | m.setDailyLastText("daily-last-text", "orange", yesterday); |
michael@0 | 242 | |
michael@0 | 243 | m.setLastNumeric("last-numeric", 6, now); |
michael@0 | 244 | yield m.setLastText("last-text", "hello", now); |
michael@0 | 245 | |
michael@0 | 246 | let data = yield provider.storage.getMeasurementValues(m.id); |
michael@0 | 247 | |
michael@0 | 248 | let serializer = m.serializer(m.SERIALIZE_JSON); |
michael@0 | 249 | let formatted = serializer.singular(data.singular); |
michael@0 | 250 | |
michael@0 | 251 | do_check_eq(Object.keys(formatted).length, 3); // Our keys + _v. |
michael@0 | 252 | do_check_true("last-numeric" in formatted); |
michael@0 | 253 | do_check_true("last-text" in formatted); |
michael@0 | 254 | do_check_eq(formatted["last-numeric"], 6); |
michael@0 | 255 | do_check_eq(formatted["last-text"], "hello"); |
michael@0 | 256 | do_check_eq(formatted["_v"], 1); |
michael@0 | 257 | |
michael@0 | 258 | formatted = serializer.daily(data.days.getDay(now)); |
michael@0 | 259 | do_check_eq(Object.keys(formatted).length, 6); // Our keys + _v. |
michael@0 | 260 | do_check_eq(formatted["daily-counter"], 2); |
michael@0 | 261 | do_check_eq(formatted["_v"], 1); |
michael@0 | 262 | |
michael@0 | 263 | do_check_true(Array.isArray(formatted["daily-discrete-numeric"])); |
michael@0 | 264 | do_check_eq(formatted["daily-discrete-numeric"].length, 2); |
michael@0 | 265 | do_check_eq(formatted["daily-discrete-numeric"][0], 1); |
michael@0 | 266 | do_check_eq(formatted["daily-discrete-numeric"][1], 2); |
michael@0 | 267 | |
michael@0 | 268 | do_check_true(Array.isArray(formatted["daily-discrete-text"])); |
michael@0 | 269 | do_check_eq(formatted["daily-discrete-text"].length, 2); |
michael@0 | 270 | do_check_eq(formatted["daily-discrete-text"][0], "foo"); |
michael@0 | 271 | do_check_eq(formatted["daily-discrete-text"][1], "bar"); |
michael@0 | 272 | |
michael@0 | 273 | do_check_eq(formatted["daily-last-numeric"], 4); |
michael@0 | 274 | do_check_eq(formatted["daily-last-text"], "apple"); |
michael@0 | 275 | |
michael@0 | 276 | formatted = serializer.daily(data.days.getDay(yesterday)); |
michael@0 | 277 | do_check_eq(formatted["daily-last-numeric"], 5); |
michael@0 | 278 | do_check_eq(formatted["daily-last-text"], "orange"); |
michael@0 | 279 | |
michael@0 | 280 | // Now let's turn off a field so that it's present in the DB |
michael@0 | 281 | // but not present in the output. |
michael@0 | 282 | let called = false; |
michael@0 | 283 | let excluded = "daily-last-numeric"; |
michael@0 | 284 | Object.defineProperty(m, "shouldIncludeField", { |
michael@0 | 285 | value: function fakeShouldIncludeField(field) { |
michael@0 | 286 | called = true; |
michael@0 | 287 | return field != excluded; |
michael@0 | 288 | }, |
michael@0 | 289 | }); |
michael@0 | 290 | |
michael@0 | 291 | let limited = serializer.daily(data.days.getDay(yesterday)); |
michael@0 | 292 | do_check_true(called); |
michael@0 | 293 | do_check_false(excluded in limited); |
michael@0 | 294 | do_check_eq(formatted["daily-last-text"], "orange"); |
michael@0 | 295 | |
michael@0 | 296 | yield provider.storage.close(); |
michael@0 | 297 | }); |