/** * Returns a reference to a temporary file that is guaranteed not to exist and * is cleaned up later. See FileTestUtils.getTempFile for details.
*/ function getTempFile(leafName) { return FileTestUtils.getTempFile(leafName);
}
add_task(async function test_load_with_dataPostProcessor() {
let storeForSave = new JSONFile({
path: getTempFile(TEST_STORE_FILE_NAME).path,
});
await storeForSave.load();
Object.assign(storeForSave.data, TEST_DATA);
await storeForSave._save();
let random = Math.random();
let storeForLoad = new JSONFile({
path: storeForSave.path,
dataPostProcessor: data => { Assert.deepEqual(data, TEST_DATA);
data.test = random; return data;
},
});
await storeForLoad.load();
Assert.equal(storeForLoad.data.test, random);
});
add_task(async function test_load_with_dataPostProcessor_fails() {
let store = new JSONFile({
path: getTempFile(TEST_STORE_FILE_NAME).path,
dataPostProcessor: () => { thrownew Error("dataPostProcessor fails.");
},
});
add_task(async function test_load_sync_with_dataPostProcessor_fails() {
let store = new JSONFile({
path: getTempFile(TEST_STORE_FILE_NAME).path,
dataPostProcessor: () => { thrownew Error("dataPostProcessor fails.");
},
});
/** * Loads data from a string in a predefined format. The purpose of this test is * to verify that the JSON format used in previous versions can be loaded.
*/
add_task(async function test_load_string_predefined() {
let store = new JSONFile({
path: getTempFile(TEST_STORE_FILE_NAME).path,
});
let string = '{"number":123,"string":"test","object":{"prop1":1,"prop2":2}}';
/** * Loads data from a malformed JSON string.
*/
add_task(async function test_load_string_malformed() {
let store = new JSONFile({
path: getTempFile(TEST_STORE_FILE_NAME).path,
});
let string = '{"number":123,"string":"test","object":{"prop1":1,';
// A backup file should have been created. Assert.ok(await IOUtils.exists(store.path + ".corrupt"));
await IOUtils.remove(store.path + ".corrupt");
// The store should be ready to accept new data. Assert.ok(store.dataReady); Assert.deepEqual(store.data, {});
});
/** * Loads data from a malformed JSON string, using the synchronous initialization * path.
*/
add_task(async function test_load_string_malformed_sync() {
let store = new JSONFile({
path: getTempFile(TEST_STORE_FILE_NAME).path,
});
let string = '{"number":123,"string":"test","object":{"prop1":1,';
add_task(async function test_beforeSave() {
let store;
let promiseBeforeSave = new Promise(resolve => {
store = new JSONFile({
path: getTempFile(TEST_STORE_FILE_NAME).path,
beforeSave: resolve,
saveDelayMs: 250,
});
});
store.saveSoon();
await promiseBeforeSave;
});
add_task(async function test_beforeSave_rejects() {
let storeForSave = new JSONFile({
path: getTempFile(TEST_STORE_FILE_NAME).path,
beforeSave() { return Promise.reject(new Error("oops"));
},
saveDelayMs: 250,
});
let promiseSave = new Promise((resolve, reject) => {
let save = storeForSave._save.bind(storeForSave);
storeForSave._save = () => {
save().then(resolve, reject);
};
storeForSave.saveSoon();
});
add_task(async function test_finalize() {
let path = getTempFile(TEST_STORE_FILE_NAME).path;
let barrier = new AsyncShutdown.Barrier("test-auto-finalize");
let storeForSave = new JSONFile({
path,
saveDelayMs: 2000,
finalizeAt: barrier.client,
});
await storeForSave.load();
storeForSave.data = TEST_DATA;
storeForSave.saveSoon();
let promiseFinalize = storeForSave.finalize();
await Assert.rejects(storeForSave.finalize(), /has already been finalized$/);
await promiseFinalize; Assert.ok(!storeForSave.dataReady);
// Finalization removes the blocker, so waiting should not log an unhandled // error even though the object has been explicitly finalized.
await barrier.wait();
let storeForLoad = new JSONFile({ path });
await storeForLoad.load(); Assert.deepEqual(storeForLoad.data, TEST_DATA);
});
add_task(async function test_finalize_on_shutdown() {
let path = getTempFile(TEST_STORE_FILE_NAME).path;
let barrier = new AsyncShutdown.Barrier("test-finalize-shutdown");
let storeForSave = new JSONFile({
path,
saveDelayMs: 2000,
finalizeAt: barrier.client,
});
await storeForSave.load();
storeForSave.data = TEST_DATA; // Arm the saver, then simulate shutdown and ensure the file is // automatically finalized.
storeForSave.saveSoon();
await barrier.wait(); // It's possible for `finalize` to reject when called concurrently with // shutdown. We don't distinguish between explicit `finalize` calls and // finalization on shutdown because we expect most consumers to rely on the // latter. However, this behavior can be safely changed if needed.
await Assert.rejects(storeForSave.finalize(), /has already been finalized$/); Assert.ok(!storeForSave.dataReady);
let storeForLoad = new JSONFile({ path });
await storeForLoad.load(); Assert.deepEqual(storeForLoad.data, TEST_DATA);
});
Messung V0.5
¤ Dauer der Verarbeitung: 0.1 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.