michael@0: let {OS} = Components.utils.import("resource://gre/modules/osfile.jsm", {}); michael@0: let {Services} = Components.utils.import("resource://gre/modules/Services.jsm", {}); michael@0: michael@0: /** michael@0: * Test optional duration reporting that can be used for telemetry. michael@0: */ michael@0: add_task(function* duration() { michael@0: Services.prefs.setBoolPref("toolkit.osfile.log", true); michael@0: // Options structure passed to a OS.File copy method. michael@0: let copyOptions = { michael@0: // This field should be overridden with the actual duration michael@0: // measurement. michael@0: outExecutionDuration: null michael@0: }; michael@0: let currentDir = yield OS.File.getCurrentDirectory(); michael@0: let pathSource = OS.Path.join(currentDir, "test_duration.js"); michael@0: let copyFile = pathSource + ".bak"; michael@0: function testOptions(options, name) { michael@0: do_print("Checking outExecutionDuration for operation: " + name); michael@0: do_print(name + ": Gathered method duration time: " + michael@0: options.outExecutionDuration + "ms"); michael@0: // Making sure that duration was updated. michael@0: do_check_eq(typeof options.outExecutionDuration, "number"); michael@0: do_check_true(options.outExecutionDuration >= 0); michael@0: }; michael@0: // Testing duration of OS.File.copy. michael@0: yield OS.File.copy(pathSource, copyFile, copyOptions); michael@0: testOptions(copyOptions, "OS.File.copy"); michael@0: yield OS.File.remove(copyFile); michael@0: michael@0: // Trying an operation where options are cloned. michael@0: let pathDest = OS.Path.join(OS.Constants.Path.tmpDir, michael@0: "osfile async test read writeAtomic.tmp"); michael@0: let tmpPath = pathDest + ".tmp"; michael@0: let readOptions = { michael@0: outExecutionDuration: null michael@0: }; michael@0: let contents = yield OS.File.read(pathSource, undefined, readOptions); michael@0: testOptions(readOptions, "OS.File.read"); michael@0: // Options structure passed to a OS.File writeAtomic method. michael@0: let writeAtomicOptions = { michael@0: // This field should be first initialized with the actual michael@0: // duration measurement then progressively incremented. michael@0: outExecutionDuration: null, michael@0: tmpPath: tmpPath michael@0: }; michael@0: yield OS.File.writeAtomic(pathDest, contents, writeAtomicOptions); michael@0: testOptions(writeAtomicOptions, "OS.File.writeAtomic"); michael@0: yield OS.File.remove(pathDest); michael@0: michael@0: do_print("Ensuring that we can use outExecutionDuration to accumulate durations"); michael@0: michael@0: let ARBITRARY_BASE_DURATION = 5; michael@0: copyOptions = { michael@0: // This field should now be incremented with the actual duration michael@0: // measurement. michael@0: outExecutionDuration: ARBITRARY_BASE_DURATION michael@0: }; michael@0: let backupDuration = ARBITRARY_BASE_DURATION; michael@0: // Testing duration of OS.File.copy. michael@0: yield OS.File.copy(pathSource, copyFile, copyOptions); michael@0: michael@0: do_check_true(copyOptions.outExecutionDuration >= backupDuration); michael@0: michael@0: backupDuration = copyOptions.outExecutionDuration; michael@0: yield OS.File.remove(copyFile, copyOptions); michael@0: do_check_true(copyOptions.outExecutionDuration >= backupDuration); michael@0: michael@0: // Trying an operation where options are cloned. michael@0: // Options structure passed to a OS.File writeAtomic method. michael@0: writeAtomicOptions = { michael@0: // This field should be overridden with the actual duration michael@0: // measurement. michael@0: outExecutionDuration: copyOptions.outExecutionDuration, michael@0: tmpPath: tmpPath michael@0: }; michael@0: backupDuration = writeAtomicOptions.outExecutionDuration; michael@0: michael@0: yield OS.File.writeAtomic(pathDest, contents, writeAtomicOptions); michael@0: do_check_true(copyOptions.outExecutionDuration >= backupDuration); michael@0: OS.File.remove(pathDest); michael@0: michael@0: // Testing an operation that doesn't take arguments at all michael@0: let file = yield OS.File.open(pathSource); michael@0: yield file.stat(); michael@0: yield file.close(); michael@0: }); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: }