toolkit/components/telemetry/tests/unit/test_TelemetryLockCount.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 /* A testcase to make sure reading the failed profile lock count works. */
michael@0 5
michael@0 6 const Cc = Components.classes;
michael@0 7 const Ci = Components.interfaces;
michael@0 8 const Cu = Components.utils;
michael@0 9 const Cr = Components.results;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/Services.jsm", this);
michael@0 12
michael@0 13 const Telemetry = Cc["@mozilla.org/base/telemetry;1"].getService(Ci.nsITelemetry);
michael@0 14
michael@0 15 const LOCK_FILE_NAME = "Telemetry.FailedProfileLocks.txt";
michael@0 16 const N_FAILED_LOCKS = 10;
michael@0 17
michael@0 18 // Constants from prio.h for nsIFileOutputStream.init
michael@0 19 const PR_WRONLY = 0x2;
michael@0 20 const PR_CREATE_FILE = 0x8;
michael@0 21 const PR_TRUNCATE = 0x20;
michael@0 22 const RW_OWNER = 0600;
michael@0 23
michael@0 24 function write_string_to_file(file, contents) {
michael@0 25 let ostream = Cc["@mozilla.org/network/safe-file-output-stream;1"]
michael@0 26 .createInstance(Ci.nsIFileOutputStream);
michael@0 27 ostream.init(file, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
michael@0 28 RW_OWNER, ostream.DEFER_OPEN);
michael@0 29 ostream.write(contents, contents.length);
michael@0 30 ostream.QueryInterface(Ci.nsISafeOutputStream).finish();
michael@0 31 ostream.close();
michael@0 32 }
michael@0 33
michael@0 34 function construct_file() {
michael@0 35 let profileDirectory = Services.dirsvc.get("ProfD", Ci.nsIFile);
michael@0 36 let file = profileDirectory.clone();
michael@0 37 file.append(LOCK_FILE_NAME);
michael@0 38 return file;
michael@0 39 }
michael@0 40
michael@0 41 function run_test() {
michael@0 42 do_get_profile();
michael@0 43
michael@0 44 do_check_eq(Telemetry.failedProfileLockCount, 0);
michael@0 45
michael@0 46 write_string_to_file(construct_file(), N_FAILED_LOCKS.toString());
michael@0 47
michael@0 48 // Make sure that we're not eagerly reading the count now that the
michael@0 49 // file exists.
michael@0 50 do_check_eq(Telemetry.failedProfileLockCount, 0);
michael@0 51
michael@0 52 do_test_pending();
michael@0 53 Telemetry.asyncFetchTelemetryData(actual_test);
michael@0 54 }
michael@0 55
michael@0 56 function actual_test() {
michael@0 57 do_check_eq(Telemetry.failedProfileLockCount, N_FAILED_LOCKS);
michael@0 58 do_check_false(construct_file().exists());
michael@0 59 do_test_finished();
michael@0 60 }

mercurial