Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: test/filterStorage_readwrite.js

Issue 29984577: Issue 6959 - Use async/await in test/filterStorage_readwrite.js (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Jan. 17, 2019, 8:32 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/filterStorage_readwrite.js
===================================================================
--- a/test/filterStorage_readwrite.js
+++ b/test/filterStorage_readwrite.js
@@ -86,26 +86,27 @@
return -1;
else if (a.key > b.key)
return 1;
return 0;
});
return sections;
}
-function testReadWrite(test, withExternal, withEmptySpecial)
+async function testReadWrite(test, withExternal, withEmptySpecial)
{
test.ok(!filterStorage.initialized, "Uninitialized before the first load");
- return testData.then(data =>
+ try
{
+ let data = await testData;
+
IO._setFileContents(filterStorage.sourceFile, data);
- return filterStorage.loadFromDisk();
- }).then(() =>
- {
+ await filterStorage.loadFromDisk();
+
test.ok(filterStorage.initialized, "Initialize after the first load");
test.equal(filterStorage.fileProperties.version, filterStorage.formatVersion, "File format version");
if (withExternal)
{
{
let subscription = new ExternalSubscription("~external~external subscription ID", "External subscription");
subscription.addFilter(Filter.fromText("foo"));
@@ -129,22 +130,29 @@
filterStorage.removeFilter(Filter.fromText("!foo"), specialSubscription);
test.equal(specialSubscription.filterCount, 0,
"No filters in special subscription");
test.ok(new Set(filterStorage.subscriptions()).has(specialSubscription),
"Empty special subscription still in storage");
}
- return filterStorage.saveToDisk();
- }).then(() => testData).then(expected =>
+ await filterStorage.saveToDisk();
+
+ let expected = await testData;
+
+ test.deepEqual(canonize(IO._getFileContents(filterStorage.sourceFile)),
+ canonize(expected), "Read/write result");
+ }
+ catch (error)
{
- test.deepEqual(canonize(IO._getFileContents(filterStorage.sourceFile)),
- canonize(expected), "Read/write result");
- }).catch(unexpectedError.bind(test)).then(() => test.done());
+ unexpectedError.call(test, error);
+ }
+
+ test.done();
}
exports.testReadAndSaveToFile = function(test)
{
testReadWrite(test, false);
};
exports.testReadAndSaveToFileWithExternalSubscription = function(test)
@@ -152,122 +160,143 @@
testReadWrite(test, true);
};
exports.testReadAndSaveToFileWithEmptySpecial = function(test)
{
testReadWrite(test, false, true);
};
-exports.testImportExport = function(test)
+exports.testImportExport = async function(test)
{
- testData.then(lines =>
+ try
{
+ let lines = await testData;
+
if (lines.length && lines[lines.length - 1] == "")
lines.pop();
let importer = filterStorage.importData();
for (let line of lines)
importer(line);
importer(null);
test.equal(filterStorage.fileProperties.version, filterStorage.formatVersion, "File format version");
let exported = Array.from(filterStorage.exportData());
test.deepEqual(canonize(exported), canonize(lines), "Import/export result");
- }).catch(unexpectedError.bind(test)).then(() => test.done());
+ }
+ catch (error)
+ {
+ unexpectedError.call(test, error);
+ }
+
+ test.done();
};
-exports.testSavingWithoutBackups = function(test)
+exports.testSavingWithoutBackups = async function(test)
{
Prefs.patternsbackups = 0;
Prefs.patternsbackupinterval = 24;
- filterStorage.saveToDisk().then(() =>
+ try
{
- return filterStorage.saveToDisk();
- }).then(() =>
- {
+ await filterStorage.saveToDisk();
+ await filterStorage.saveToDisk();
+
test.ok(!IO._getFileContents(filterStorage.getBackupName(1)),
"Backup shouldn't be created");
- }).catch(unexpectedError.bind(test)).then(() => test.done());
+ }
+ catch (error)
+ {
+ unexpectedError.call(test, error);
+ }
+
+ test.done();
};
-exports.testSavingWithBackups = function(test)
+exports.testSavingWithBackups = async function(test)
{
Prefs.patternsbackups = 2;
Prefs.patternsbackupinterval = 24;
let backupFile = filterStorage.getBackupName(1);
let backupFile2 = filterStorage.getBackupName(2);
let backupFile3 = filterStorage.getBackupName(3);
let oldModifiedTime;
- filterStorage.saveToDisk().then(() =>
+ try
{
+ await filterStorage.saveToDisk();
+
// Save again immediately
- return filterStorage.saveToDisk();
- }).then(() =>
- {
+ await filterStorage.saveToDisk();
+
test.ok(IO._getFileContents(backupFile), "First backup created");
oldModifiedTime = IO._getModifiedTime(backupFile) - 10000;
IO._setModifiedTime(backupFile, oldModifiedTime);
- return filterStorage.saveToDisk();
- }).then(() =>
- {
+ await filterStorage.saveToDisk();
+
test.equal(IO._getModifiedTime(backupFile), oldModifiedTime, "Backup not overwritten if it is only 10 seconds old");
oldModifiedTime -= 40 * 60 * 60 * 1000;
IO._setModifiedTime(backupFile, oldModifiedTime);
- return filterStorage.saveToDisk();
- }).then(() =>
- {
+ await filterStorage.saveToDisk();
+
test.notEqual(IO._getModifiedTime(backupFile), oldModifiedTime, "Backup overwritten if it is 40 hours old");
test.ok(IO._getFileContents(backupFile2), "Second backup created when first backup is overwritten");
IO._setModifiedTime(backupFile, IO._getModifiedTime(backupFile) - 20000);
oldModifiedTime = IO._getModifiedTime(backupFile2);
- return filterStorage.saveToDisk();
- }).then(() =>
- {
+ await filterStorage.saveToDisk();
+
test.equal(IO._getModifiedTime(backupFile2), oldModifiedTime, "Second backup not overwritten if first one is only 20 seconds old");
IO._setModifiedTime(backupFile, IO._getModifiedTime(backupFile) - 25 * 60 * 60 * 1000);
oldModifiedTime = IO._getModifiedTime(backupFile2);
- return filterStorage.saveToDisk();
- }).then(() =>
- {
+ await filterStorage.saveToDisk();
+
test.notEqual(IO._getModifiedTime(backupFile2), oldModifiedTime, "Second backup overwritten if first one is 25 hours old");
test.ok(!IO._getFileContents(backupFile3), "Third backup not created with patternsbackups = 2");
- }).catch(unexpectedError.bind(test)).then(() => test.done());
+ }
+ catch (error)
+ {
+ unexpectedError.call(test, error);
+ }
+
+ test.done();
};
-exports.testRestoringBackup = function(test)
+exports.testRestoringBackup = async function(test)
{
Prefs.patternsbackups = 2;
Prefs.patternsbackupinterval = 24;
- filterStorage.saveToDisk().then(() =>
+ try
{
+ await filterStorage.saveToDisk();
+
test.equal([...filterStorage.subscriptions()][0].filterCount, 1, "Initial filter count");
filterStorage.addFilter(Filter.fromText("barfoo"));
test.equal([...filterStorage.subscriptions()][0].filterCount, 2, "Filter count after adding a filter");
- return filterStorage.saveToDisk();
- }).then(() =>
- {
- return filterStorage.loadFromDisk();
- }).then(() =>
- {
+ await filterStorage.saveToDisk();
+
+ await filterStorage.loadFromDisk();
+
test.equal([...filterStorage.subscriptions()][0].filterCount, 2, "Filter count after adding filter and reloading");
- return filterStorage.restoreBackup(1);
- }).then(() =>
- {
+ await filterStorage.restoreBackup(1);
+
test.equal([...filterStorage.subscriptions()][0].filterCount, 1, "Filter count after restoring backup");
- return filterStorage.loadFromDisk();
- }).then(() =>
+ await filterStorage.loadFromDisk();
+
+ test.equal([...filterStorage.subscriptions()][0].filterCount, 1, "Filter count after reloading");
+ }
+ catch (error)
{
- test.equal([...filterStorage.subscriptions()][0].filterCount, 1, "Filter count after reloading");
- }).catch(unexpectedError.bind(test)).then(() => test.done());
+ unexpectedError.call(test, error);
+ }
+
+ test.done();
};
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld