|
1 let {OS} = Components.utils.import("resource://gre/modules/osfile.jsm", {}); |
|
2 let {Services} = Components.utils.import("resource://gre/modules/Services.jsm", {}); |
|
3 |
|
4 /** |
|
5 * Test optional duration reporting that can be used for telemetry. |
|
6 */ |
|
7 add_task(function* duration() { |
|
8 Services.prefs.setBoolPref("toolkit.osfile.log", true); |
|
9 // Options structure passed to a OS.File copy method. |
|
10 let copyOptions = { |
|
11 // This field should be overridden with the actual duration |
|
12 // measurement. |
|
13 outExecutionDuration: null |
|
14 }; |
|
15 let currentDir = yield OS.File.getCurrentDirectory(); |
|
16 let pathSource = OS.Path.join(currentDir, "test_duration.js"); |
|
17 let copyFile = pathSource + ".bak"; |
|
18 function testOptions(options, name) { |
|
19 do_print("Checking outExecutionDuration for operation: " + name); |
|
20 do_print(name + ": Gathered method duration time: " + |
|
21 options.outExecutionDuration + "ms"); |
|
22 // Making sure that duration was updated. |
|
23 do_check_eq(typeof options.outExecutionDuration, "number"); |
|
24 do_check_true(options.outExecutionDuration >= 0); |
|
25 }; |
|
26 // Testing duration of OS.File.copy. |
|
27 yield OS.File.copy(pathSource, copyFile, copyOptions); |
|
28 testOptions(copyOptions, "OS.File.copy"); |
|
29 yield OS.File.remove(copyFile); |
|
30 |
|
31 // Trying an operation where options are cloned. |
|
32 let pathDest = OS.Path.join(OS.Constants.Path.tmpDir, |
|
33 "osfile async test read writeAtomic.tmp"); |
|
34 let tmpPath = pathDest + ".tmp"; |
|
35 let readOptions = { |
|
36 outExecutionDuration: null |
|
37 }; |
|
38 let contents = yield OS.File.read(pathSource, undefined, readOptions); |
|
39 testOptions(readOptions, "OS.File.read"); |
|
40 // Options structure passed to a OS.File writeAtomic method. |
|
41 let writeAtomicOptions = { |
|
42 // This field should be first initialized with the actual |
|
43 // duration measurement then progressively incremented. |
|
44 outExecutionDuration: null, |
|
45 tmpPath: tmpPath |
|
46 }; |
|
47 yield OS.File.writeAtomic(pathDest, contents, writeAtomicOptions); |
|
48 testOptions(writeAtomicOptions, "OS.File.writeAtomic"); |
|
49 yield OS.File.remove(pathDest); |
|
50 |
|
51 do_print("Ensuring that we can use outExecutionDuration to accumulate durations"); |
|
52 |
|
53 let ARBITRARY_BASE_DURATION = 5; |
|
54 copyOptions = { |
|
55 // This field should now be incremented with the actual duration |
|
56 // measurement. |
|
57 outExecutionDuration: ARBITRARY_BASE_DURATION |
|
58 }; |
|
59 let backupDuration = ARBITRARY_BASE_DURATION; |
|
60 // Testing duration of OS.File.copy. |
|
61 yield OS.File.copy(pathSource, copyFile, copyOptions); |
|
62 |
|
63 do_check_true(copyOptions.outExecutionDuration >= backupDuration); |
|
64 |
|
65 backupDuration = copyOptions.outExecutionDuration; |
|
66 yield OS.File.remove(copyFile, copyOptions); |
|
67 do_check_true(copyOptions.outExecutionDuration >= backupDuration); |
|
68 |
|
69 // Trying an operation where options are cloned. |
|
70 // Options structure passed to a OS.File writeAtomic method. |
|
71 writeAtomicOptions = { |
|
72 // This field should be overridden with the actual duration |
|
73 // measurement. |
|
74 outExecutionDuration: copyOptions.outExecutionDuration, |
|
75 tmpPath: tmpPath |
|
76 }; |
|
77 backupDuration = writeAtomicOptions.outExecutionDuration; |
|
78 |
|
79 yield OS.File.writeAtomic(pathDest, contents, writeAtomicOptions); |
|
80 do_check_true(copyOptions.outExecutionDuration >= backupDuration); |
|
81 OS.File.remove(pathDest); |
|
82 |
|
83 // Testing an operation that doesn't take arguments at all |
|
84 let file = yield OS.File.open(pathSource); |
|
85 yield file.stat(); |
|
86 yield file.close(); |
|
87 }); |
|
88 |
|
89 function run_test() { |
|
90 run_next_test(); |
|
91 } |