test_TelemetryLockCount.js (1873B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ 3 */ 4 /* A testcase to make sure reading the failed profile lock count works. */ 5 6 const LOCK_FILE_NAME = "Telemetry.FailedProfileLocks.txt"; 7 const N_FAILED_LOCKS = 10; 8 9 // Constants from prio.h for nsIFileOutputStream.init 10 const PR_WRONLY = 0x2; 11 const PR_CREATE_FILE = 0x8; 12 const PR_TRUNCATE = 0x20; 13 const RW_OWNER = parseInt("0600", 8); 14 15 function write_string_to_file(file, contents) { 16 let ostream = Cc[ 17 "@mozilla.org/network/safe-file-output-stream;1" 18 ].createInstance(Ci.nsIFileOutputStream); 19 ostream.init( 20 file, 21 PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 22 RW_OWNER, 23 ostream.DEFER_OPEN 24 ); 25 ostream.write(contents, contents.length); 26 ostream.QueryInterface(Ci.nsISafeOutputStream).finish(); 27 ostream.close(); 28 } 29 30 function construct_file() { 31 let profileDirectory = Services.dirsvc.get("ProfD", Ci.nsIFile); 32 let file = profileDirectory.clone(); 33 file.append(LOCK_FILE_NAME); 34 return file; 35 } 36 37 function run_test() { 38 do_get_profile(); 39 Services.fog.initializeFOG(); 40 41 Assert.equal(Services.telemetry.failedProfileLockCount, 0); 42 Assert.equal(Glean.profileLock.failedLockCount.testGetValue(), null); 43 44 write_string_to_file(construct_file(), N_FAILED_LOCKS.toString()); 45 46 // Make sure that we're not eagerly reading the count now that the 47 // file exists. 48 Assert.equal(Services.telemetry.failedProfileLockCount, 0); 49 Assert.equal(Glean.profileLock.failedLockCount.testGetValue(), null); 50 51 do_test_pending(); 52 Services.telemetry.asyncFetchTelemetryData(actual_test); 53 } 54 55 function actual_test() { 56 Assert.equal(Services.telemetry.failedProfileLockCount, N_FAILED_LOCKS); 57 Assert.equal( 58 Glean.profileLock.failedLockCount.testGetValue(), 59 N_FAILED_LOCKS 60 ); 61 Assert.ok(!construct_file().exists()); 62 do_test_finished(); 63 }