Last active
January 7, 2021 22:55
-
-
Save fcamblor/9d2552460766a2fe1e203fd45ddae449 to your computer and use it in GitHub Desktop.
cordova-file concurrent writes reproducer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function testingDirEntryFor(directoryName) { | |
return new Promise((resolve, reject) => { | |
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, (rootDataDir) => { | |
rootDataDir.getDirectory(directoryName, { create: true }, (dirEntry) => { | |
resolve(dirEntry); | |
}, (error) => reject(error)); | |
}, (error) => reject(error)); | |
}); | |
} | |
function fileEntry(directoryName, fileName, getFileFlags) { | |
return new Promise((resolve, reject) => { | |
testingDirEntryFor(directoryName).then(dirEntry => { | |
dirEntry.getFile(fileName, getFileFlags, (fileEntry) => { | |
resolve(fileEntry); | |
}, (error) => reject(`Error while retrieving file entry for ${dirEntry.fullPath}/${fileName}: ${error.toString()}`)); | |
}, reject); | |
}); | |
} | |
function creationFileEntry(directoryName, fileName) { | |
return fileEntry(directoryName, fileName, {create: true, exclusive: false }); | |
} | |
function readFileEntry(directoryName, fileName) { | |
return fileEntry(directoryName, fileName, {create: false }); | |
} | |
function writeFile(fileWriter, content) { | |
return new Promise((resolve, reject) => { | |
fileWriter.onwriteend = () => { | |
resolve(); | |
} | |
fileWriter.onerror = (error) => { | |
console.error(`Error while writing : ${error.toString()}`) | |
reject(error); | |
} | |
console.info(`fileWriter.write() called with position=${fileWriter.position},length=${fileWriter.length}`); | |
fileWriter.write(content); | |
}); | |
} | |
function readFileContent(directoryName, fileName) { | |
return new Promise((resolve, reject) => { | |
readFileEntry(directoryName, fileName).then((fileEntry) => { | |
if(!fileEntry) { | |
resolve(null); | |
} else { | |
fileEntry.file(file => { | |
const reader = new FileReader(); | |
reader.onloadend = function() { | |
resolve(this.result); | |
}; | |
reader.readAsText(file, "utf-8"); | |
}, (error) => { | |
if([FileError.NOT_FOUND_ERR, FileError.NOT_READABLE_ERR].includes(error.code)) { | |
resolve(null); | |
} else { | |
console.error(`Error while reading file : ${fileName} in ${directoryName}/${fileName}`) | |
reject(error); | |
} | |
}) | |
} | |
}, (error) => { | |
console.error(`Error while reading file : ${fileName} in ${directoryName}/${fileName}`) | |
reject(error); | |
}) | |
}); | |
} | |
function testWriting(numberOfTimes) { | |
var fileEntryPromises = new Array(numberOfTimes*2).fill(0).map((_) => creationFileEntry("tests", "testing")); | |
var fileWriterPromises = fileEntryPromises.map(feProm => feProm.then(fileEntry => | |
new Promise((resolve, reject) => fileEntry.createWriter(resolve, (error) => reject(`Error while creating writer for ${fileEntry.fullPath}: ${error.toString()}`))) | |
)); | |
new Array(numberOfTimes).fill(0).reduce((previousPromise, _, idx) => { | |
return previousPromise.then(({ detected, content }) => { | |
if(detected) { | |
return { detected, content }; | |
} | |
var result = { found: false, prom1Resolved: false, prom2Resolved: false }; | |
var prom1 = fileWriterPromises[idx*2].then(fw => writeFile(fw, "a")); | |
var prom2 = fileWriterPromises[idx*2+1].then(fw => writeFile(fw, "b")); | |
return Promise.all([prom1, prom2]).then(() => { | |
return readFileContent("tests", "testing").then(content => { | |
var detected = (content.length !== 1); | |
console.info(`test ${idx} => ${detected?"detected !":"not detected !"}`); | |
return { detected, content }; | |
}); | |
}); | |
}); | |
}, Promise.resolve({ detected: false, content: undefined })).then(({ detected, content }) => { | |
console.info(detected?"Thread safety write detected !":"No thread safety write issue found !", content); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testWriting(50) | |
undefined | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 0 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 1 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 2 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 3 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 4 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 5 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 6 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 7 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 8 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 9 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 10 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 11 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 12 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 13 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 14 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 15 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 16 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 17 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 18 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 19 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 20 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 21 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 22 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 23 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 24 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 25 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 26 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 27 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 28 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 29 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 30 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 31 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 32 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 33 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 34 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 35 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 36 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 37 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 38 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 39 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 40 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 41 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 42 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 43 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 44 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 45 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 46 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 47 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 48 => not detected ! | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:39 fileWriter.write() called with position=0,length=1 | |
filewriter-threadsafety.js:92 test 49 => not detected ! | |
filewriter-threadsafety.js:98 No thread safety write issue found ! b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> testWriting(50) | |
< undefined | |
[Info] fileWriter.write() called with position=0,length=2 (t.js, line 13) | |
[Info] fileWriter.write() called with position=0,length=2 (t.js, line 13) | |
[Info] test 0 => not detected ! (t.js, line 13) | |
[Info] fileWriter.write() called with position=0,length=2 (t.js, line 13) | |
[Info] fileWriter.write() called with position=0,length=2 (t.js, line 13) | |
[Info] test 1 => detected ! (t.js, line 13) | |
[Info] Thread safety write detected ! – "ab" (t.js, line 13) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment