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 {classes: Cc, interfaces: Ci, utils: Cu} = Components; |
michael@0 | 7 | |
michael@0 | 8 | Cu.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 9 | Cu.import("resource://gre/modules/Metrics.jsm"); |
michael@0 | 10 | Cu.import("resource://testing-common/services/metrics/mocks.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | const PULL_ONLY_TESTING_CATEGORY = "testing-only-pull-only-providers"; |
michael@0 | 13 | |
michael@0 | 14 | function run_test() { |
michael@0 | 15 | let cm = Cc["@mozilla.org/categorymanager;1"] |
michael@0 | 16 | .getService(Ci.nsICategoryManager); |
michael@0 | 17 | cm.addCategoryEntry(PULL_ONLY_TESTING_CATEGORY, "DummyProvider", |
michael@0 | 18 | "resource://testing-common/services/metrics/mocks.jsm", |
michael@0 | 19 | false, true); |
michael@0 | 20 | cm.addCategoryEntry(PULL_ONLY_TESTING_CATEGORY, "DummyConstantProvider", |
michael@0 | 21 | "resource://testing-common/services/metrics/mocks.jsm", |
michael@0 | 22 | false, true); |
michael@0 | 23 | |
michael@0 | 24 | run_next_test(); |
michael@0 | 25 | }; |
michael@0 | 26 | |
michael@0 | 27 | add_task(function test_constructor() { |
michael@0 | 28 | let storage = yield Metrics.Storage("constructor"); |
michael@0 | 29 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 30 | |
michael@0 | 31 | yield storage.close(); |
michael@0 | 32 | }); |
michael@0 | 33 | |
michael@0 | 34 | add_task(function test_register_provider() { |
michael@0 | 35 | let storage = yield Metrics.Storage("register_provider"); |
michael@0 | 36 | |
michael@0 | 37 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 38 | let dummy = new DummyProvider(); |
michael@0 | 39 | |
michael@0 | 40 | yield manager.registerProvider(dummy); |
michael@0 | 41 | do_check_eq(manager._providers.size, 1); |
michael@0 | 42 | yield manager.registerProvider(dummy); |
michael@0 | 43 | do_check_eq(manager._providers.size, 1); |
michael@0 | 44 | do_check_eq(manager.getProvider(dummy.name), dummy); |
michael@0 | 45 | |
michael@0 | 46 | let failed = false; |
michael@0 | 47 | try { |
michael@0 | 48 | manager.registerProvider({}); |
michael@0 | 49 | } catch (ex) { |
michael@0 | 50 | do_check_true(ex.message.startsWith("Provider is not valid")); |
michael@0 | 51 | failed = true; |
michael@0 | 52 | } finally { |
michael@0 | 53 | do_check_true(failed); |
michael@0 | 54 | failed = false; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | manager.unregisterProvider(dummy.name); |
michael@0 | 58 | do_check_eq(manager._providers.size, 0); |
michael@0 | 59 | do_check_null(manager.getProvider(dummy.name)); |
michael@0 | 60 | |
michael@0 | 61 | yield storage.close(); |
michael@0 | 62 | }); |
michael@0 | 63 | |
michael@0 | 64 | add_task(function test_register_providers_from_category_manager() { |
michael@0 | 65 | const category = "metrics-providers-js-modules"; |
michael@0 | 66 | |
michael@0 | 67 | let cm = Cc["@mozilla.org/categorymanager;1"] |
michael@0 | 68 | .getService(Ci.nsICategoryManager); |
michael@0 | 69 | cm.addCategoryEntry(category, "DummyProvider", |
michael@0 | 70 | "resource://testing-common/services/metrics/mocks.jsm", |
michael@0 | 71 | false, true); |
michael@0 | 72 | |
michael@0 | 73 | let storage = yield Metrics.Storage("register_providers_from_category_manager"); |
michael@0 | 74 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 75 | try { |
michael@0 | 76 | do_check_eq(manager._providers.size, 0); |
michael@0 | 77 | yield manager.registerProvidersFromCategoryManager(category); |
michael@0 | 78 | do_check_eq(manager._providers.size, 1); |
michael@0 | 79 | } finally { |
michael@0 | 80 | yield storage.close(); |
michael@0 | 81 | } |
michael@0 | 82 | }); |
michael@0 | 83 | |
michael@0 | 84 | add_task(function test_collect_constant_data() { |
michael@0 | 85 | let storage = yield Metrics.Storage("collect_constant_data"); |
michael@0 | 86 | let errorCount = 0; |
michael@0 | 87 | let manager= new Metrics.ProviderManager(storage); |
michael@0 | 88 | manager.onProviderError = function () { errorCount++; } |
michael@0 | 89 | let provider = new DummyProvider(); |
michael@0 | 90 | yield manager.registerProvider(provider); |
michael@0 | 91 | |
michael@0 | 92 | do_check_eq(provider.collectConstantCount, 0); |
michael@0 | 93 | |
michael@0 | 94 | yield manager.collectConstantData(); |
michael@0 | 95 | do_check_eq(provider.collectConstantCount, 1); |
michael@0 | 96 | |
michael@0 | 97 | do_check_true(manager._providers.get("DummyProvider").constantsCollected); |
michael@0 | 98 | |
michael@0 | 99 | yield storage.close(); |
michael@0 | 100 | do_check_eq(errorCount, 0); |
michael@0 | 101 | }); |
michael@0 | 102 | |
michael@0 | 103 | add_task(function test_collect_constant_throws() { |
michael@0 | 104 | let storage = yield Metrics.Storage("collect_constant_throws"); |
michael@0 | 105 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 106 | let errors = []; |
michael@0 | 107 | manager.onProviderError = function (error) { errors.push(error); }; |
michael@0 | 108 | |
michael@0 | 109 | let provider = new DummyProvider(); |
michael@0 | 110 | provider.throwDuringCollectConstantData = "Fake error during collect"; |
michael@0 | 111 | yield manager.registerProvider(provider); |
michael@0 | 112 | |
michael@0 | 113 | yield manager.collectConstantData(); |
michael@0 | 114 | do_check_eq(errors.length, 1); |
michael@0 | 115 | do_check_true(errors[0].contains(provider.throwDuringCollectConstantData)); |
michael@0 | 116 | |
michael@0 | 117 | yield storage.close(); |
michael@0 | 118 | }); |
michael@0 | 119 | |
michael@0 | 120 | add_task(function test_collect_constant_populate_throws() { |
michael@0 | 121 | let storage = yield Metrics.Storage("collect_constant_populate_throws"); |
michael@0 | 122 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 123 | let errors = []; |
michael@0 | 124 | manager.onProviderError = function (error) { errors.push(error); }; |
michael@0 | 125 | |
michael@0 | 126 | let provider = new DummyProvider(); |
michael@0 | 127 | provider.throwDuringConstantPopulate = "Fake error during constant populate"; |
michael@0 | 128 | yield manager.registerProvider(provider); |
michael@0 | 129 | |
michael@0 | 130 | yield manager.collectConstantData(); |
michael@0 | 131 | |
michael@0 | 132 | do_check_eq(errors.length, 1); |
michael@0 | 133 | do_check_true(errors[0].contains(provider.throwDuringConstantPopulate)); |
michael@0 | 134 | do_check_false(manager._providers.get(provider.name).constantsCollected); |
michael@0 | 135 | |
michael@0 | 136 | yield storage.close(); |
michael@0 | 137 | }); |
michael@0 | 138 | |
michael@0 | 139 | add_task(function test_collect_constant_onetime() { |
michael@0 | 140 | let storage = yield Metrics.Storage("collect_constant_onetime"); |
michael@0 | 141 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 142 | let provider = new DummyProvider(); |
michael@0 | 143 | yield manager.registerProvider(provider); |
michael@0 | 144 | |
michael@0 | 145 | yield manager.collectConstantData(); |
michael@0 | 146 | do_check_eq(provider.collectConstantCount, 1); |
michael@0 | 147 | |
michael@0 | 148 | yield manager.collectConstantData(); |
michael@0 | 149 | do_check_eq(provider.collectConstantCount, 1); |
michael@0 | 150 | |
michael@0 | 151 | yield storage.close(); |
michael@0 | 152 | }); |
michael@0 | 153 | |
michael@0 | 154 | add_task(function test_collect_multiple() { |
michael@0 | 155 | let storage = yield Metrics.Storage("collect_multiple"); |
michael@0 | 156 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 157 | |
michael@0 | 158 | for (let i = 0; i < 10; i++) { |
michael@0 | 159 | yield manager.registerProvider(new DummyProvider("provider" + i)); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | do_check_eq(manager._providers.size, 10); |
michael@0 | 163 | |
michael@0 | 164 | yield manager.collectConstantData(); |
michael@0 | 165 | |
michael@0 | 166 | yield storage.close(); |
michael@0 | 167 | }); |
michael@0 | 168 | |
michael@0 | 169 | add_task(function test_collect_daily() { |
michael@0 | 170 | let storage = yield Metrics.Storage("collect_daily"); |
michael@0 | 171 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 172 | |
michael@0 | 173 | let provider1 = new DummyProvider("DP1"); |
michael@0 | 174 | let provider2 = new DummyProvider("DP2"); |
michael@0 | 175 | |
michael@0 | 176 | yield manager.registerProvider(provider1); |
michael@0 | 177 | yield manager.registerProvider(provider2); |
michael@0 | 178 | |
michael@0 | 179 | yield manager.collectDailyData(); |
michael@0 | 180 | do_check_eq(provider1.collectDailyCount, 1); |
michael@0 | 181 | do_check_eq(provider2.collectDailyCount, 1); |
michael@0 | 182 | |
michael@0 | 183 | yield manager.collectDailyData(); |
michael@0 | 184 | do_check_eq(provider1.collectDailyCount, 2); |
michael@0 | 185 | do_check_eq(provider2.collectDailyCount, 2); |
michael@0 | 186 | |
michael@0 | 187 | yield storage.close(); |
michael@0 | 188 | }); |
michael@0 | 189 | |
michael@0 | 190 | add_task(function test_pull_only_not_initialized() { |
michael@0 | 191 | let storage = yield Metrics.Storage("pull_only_not_initialized"); |
michael@0 | 192 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 193 | yield manager.registerProvidersFromCategoryManager(PULL_ONLY_TESTING_CATEGORY); |
michael@0 | 194 | do_check_eq(manager.providers.length, 1); |
michael@0 | 195 | do_check_eq(manager.providers[0].name, "DummyProvider"); |
michael@0 | 196 | yield storage.close(); |
michael@0 | 197 | }); |
michael@0 | 198 | |
michael@0 | 199 | add_task(function test_pull_only_registration() { |
michael@0 | 200 | let storage = yield Metrics.Storage("pull_only_registration"); |
michael@0 | 201 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 202 | yield manager.registerProvidersFromCategoryManager(PULL_ONLY_TESTING_CATEGORY); |
michael@0 | 203 | do_check_eq(manager.providers.length, 1); |
michael@0 | 204 | |
michael@0 | 205 | // Simple registration and unregistration. |
michael@0 | 206 | yield manager.ensurePullOnlyProvidersRegistered(); |
michael@0 | 207 | do_check_eq(manager.providers.length, 2); |
michael@0 | 208 | do_check_neq(manager.getProvider("DummyConstantProvider"), null); |
michael@0 | 209 | yield manager.ensurePullOnlyProvidersUnregistered(); |
michael@0 | 210 | do_check_eq(manager.providers.length, 1); |
michael@0 | 211 | do_check_null(manager.getProvider("DummyConstantProvider")); |
michael@0 | 212 | |
michael@0 | 213 | // Multiple calls to register work. |
michael@0 | 214 | yield manager.ensurePullOnlyProvidersRegistered(); |
michael@0 | 215 | do_check_eq(manager.providers.length, 2); |
michael@0 | 216 | yield manager.ensurePullOnlyProvidersRegistered(); |
michael@0 | 217 | do_check_eq(manager.providers.length, 2); |
michael@0 | 218 | |
michael@0 | 219 | // Unregister with 2 requests for registration should not unregister. |
michael@0 | 220 | yield manager.ensurePullOnlyProvidersUnregistered(); |
michael@0 | 221 | do_check_eq(manager.providers.length, 2); |
michael@0 | 222 | |
michael@0 | 223 | // But the 2nd one will. |
michael@0 | 224 | yield manager.ensurePullOnlyProvidersUnregistered(); |
michael@0 | 225 | do_check_eq(manager.providers.length, 1); |
michael@0 | 226 | |
michael@0 | 227 | yield storage.close(); |
michael@0 | 228 | }); |
michael@0 | 229 | |
michael@0 | 230 | add_task(function test_pull_only_register_while_registering() { |
michael@0 | 231 | let storage = yield Metrics.Storage("pull_only_register_will_registering"); |
michael@0 | 232 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 233 | yield manager.registerProvidersFromCategoryManager(PULL_ONLY_TESTING_CATEGORY); |
michael@0 | 234 | |
michael@0 | 235 | manager.ensurePullOnlyProvidersRegistered(); |
michael@0 | 236 | manager.ensurePullOnlyProvidersRegistered(); |
michael@0 | 237 | yield manager.ensurePullOnlyProvidersRegistered(); |
michael@0 | 238 | do_check_eq(manager.providers.length, 2); |
michael@0 | 239 | |
michael@0 | 240 | manager.ensurePullOnlyProvidersUnregistered(); |
michael@0 | 241 | manager.ensurePullOnlyProvidersUnregistered(); |
michael@0 | 242 | yield manager.ensurePullOnlyProvidersUnregistered(); |
michael@0 | 243 | do_check_eq(manager.providers.length, 1); |
michael@0 | 244 | |
michael@0 | 245 | yield storage.close(); |
michael@0 | 246 | }); |
michael@0 | 247 | |
michael@0 | 248 | add_task(function test_pull_only_unregister_while_registering() { |
michael@0 | 249 | let storage = yield Metrics.Storage("pull_only_unregister_while_registering"); |
michael@0 | 250 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 251 | yield manager.registerProvidersFromCategoryManager(PULL_ONLY_TESTING_CATEGORY); |
michael@0 | 252 | |
michael@0 | 253 | manager.ensurePullOnlyProvidersRegistered(); |
michael@0 | 254 | yield manager.ensurePullOnlyProvidersUnregistered(); |
michael@0 | 255 | do_check_eq(manager.providers.length, 1); |
michael@0 | 256 | |
michael@0 | 257 | yield storage.close(); |
michael@0 | 258 | }); |
michael@0 | 259 | |
michael@0 | 260 | add_task(function test_pull_only_register_while_unregistering() { |
michael@0 | 261 | let storage = yield Metrics.Storage("pull_only_register_while_unregistering"); |
michael@0 | 262 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 263 | yield manager.registerProvidersFromCategoryManager(PULL_ONLY_TESTING_CATEGORY); |
michael@0 | 264 | |
michael@0 | 265 | yield manager.ensurePullOnlyProvidersRegistered(); |
michael@0 | 266 | manager.ensurePullOnlyProvidersUnregistered(); |
michael@0 | 267 | yield manager.ensurePullOnlyProvidersRegistered(); |
michael@0 | 268 | do_check_eq(manager.providers.length, 2); |
michael@0 | 269 | |
michael@0 | 270 | yield storage.close(); |
michael@0 | 271 | }); |
michael@0 | 272 | |
michael@0 | 273 | // Re-use database for perf reasons. |
michael@0 | 274 | const REGISTRATION_ERRORS_DB = "registration_errors"; |
michael@0 | 275 | |
michael@0 | 276 | add_task(function test_category_manager_registration_error() { |
michael@0 | 277 | let storage = yield Metrics.Storage(REGISTRATION_ERRORS_DB); |
michael@0 | 278 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 279 | |
michael@0 | 280 | let cm = Cc["@mozilla.org/categorymanager;1"] |
michael@0 | 281 | .getService(Ci.nsICategoryManager); |
michael@0 | 282 | cm.addCategoryEntry("registration-errors", "DummyThrowOnInitProvider", |
michael@0 | 283 | "resource://testing-common/services/metrics/mocks.jsm", |
michael@0 | 284 | false, true); |
michael@0 | 285 | |
michael@0 | 286 | let deferred = Promise.defer(); |
michael@0 | 287 | let errorCount = 0; |
michael@0 | 288 | |
michael@0 | 289 | manager.onProviderError = function (msg) { |
michael@0 | 290 | errorCount++; |
michael@0 | 291 | deferred.resolve(msg); |
michael@0 | 292 | }; |
michael@0 | 293 | |
michael@0 | 294 | yield manager.registerProvidersFromCategoryManager("registration-errors"); |
michael@0 | 295 | do_check_eq(manager.providers.length, 0); |
michael@0 | 296 | do_check_eq(errorCount, 1); |
michael@0 | 297 | |
michael@0 | 298 | let msg = yield deferred.promise; |
michael@0 | 299 | do_check_true(msg.contains("Provider error: DummyThrowOnInitProvider: " |
michael@0 | 300 | + "Error registering provider from category manager: " |
michael@0 | 301 | + "Error: Dummy Error")); |
michael@0 | 302 | |
michael@0 | 303 | yield storage.close(); |
michael@0 | 304 | }); |
michael@0 | 305 | |
michael@0 | 306 | add_task(function test_pull_only_registration_error() { |
michael@0 | 307 | let storage = yield Metrics.Storage(REGISTRATION_ERRORS_DB); |
michael@0 | 308 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 309 | |
michael@0 | 310 | let deferred = Promise.defer(); |
michael@0 | 311 | let errorCount = 0; |
michael@0 | 312 | |
michael@0 | 313 | manager.onProviderError = function (msg) { |
michael@0 | 314 | errorCount++; |
michael@0 | 315 | deferred.resolve(msg); |
michael@0 | 316 | }; |
michael@0 | 317 | |
michael@0 | 318 | yield manager.registerProviderFromType(DummyPullOnlyThrowsOnInitProvider); |
michael@0 | 319 | do_check_eq(errorCount, 0); |
michael@0 | 320 | |
michael@0 | 321 | yield manager.ensurePullOnlyProvidersRegistered(); |
michael@0 | 322 | do_check_eq(errorCount, 1); |
michael@0 | 323 | |
michael@0 | 324 | let msg = yield deferred.promise; |
michael@0 | 325 | do_check_true(msg.contains("Provider error: DummyPullOnlyThrowsOnInitProvider: " + |
michael@0 | 326 | "Error registering pull-only provider: Error: Dummy Error")); |
michael@0 | 327 | |
michael@0 | 328 | yield storage.close(); |
michael@0 | 329 | }); |
michael@0 | 330 | |
michael@0 | 331 | add_task(function test_error_during_shutdown() { |
michael@0 | 332 | let storage = yield Metrics.Storage(REGISTRATION_ERRORS_DB); |
michael@0 | 333 | let manager = new Metrics.ProviderManager(storage); |
michael@0 | 334 | |
michael@0 | 335 | let deferred = Promise.defer(); |
michael@0 | 336 | let errorCount = 0; |
michael@0 | 337 | |
michael@0 | 338 | manager.onProviderError = function (msg) { |
michael@0 | 339 | errorCount++; |
michael@0 | 340 | deferred.resolve(msg); |
michael@0 | 341 | }; |
michael@0 | 342 | |
michael@0 | 343 | yield manager.registerProviderFromType(DummyThrowOnShutdownProvider); |
michael@0 | 344 | yield manager.registerProviderFromType(DummyProvider); |
michael@0 | 345 | do_check_eq(errorCount, 0); |
michael@0 | 346 | do_check_eq(manager.providers.length, 1); |
michael@0 | 347 | |
michael@0 | 348 | yield manager.ensurePullOnlyProvidersRegistered(); |
michael@0 | 349 | do_check_eq(errorCount, 0); |
michael@0 | 350 | yield manager.ensurePullOnlyProvidersUnregistered(); |
michael@0 | 351 | do_check_eq(errorCount, 1); |
michael@0 | 352 | let msg = yield deferred.promise; |
michael@0 | 353 | do_check_true(msg.contains("Provider error: DummyThrowOnShutdownProvider: " + |
michael@0 | 354 | "Error when shutting down provider: Error: Dummy shutdown error")); |
michael@0 | 355 | |
michael@0 | 356 | yield storage.close(); |
michael@0 | 357 | }); |