Wait for previous pdfDocument(s) to be destroyed before running the next reference test

Refactors `Driver._cleanup` to return a `Promise` which is resolved once all opened documents have been destroyed.
This is then used in `Driver._nextTask` to ensure that we wait for everything to be cleaned up, such that the tests run sequentially.
This commit is contained in:
Jonas Jenwald 2017-05-11 12:54:48 +02:00
parent 52998c5fe1
commit b5775af716

View File

@ -334,66 +334,67 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
}, this.delay); }, this.delay);
}, },
_nextTask: function Driver_nextTask() { _nextTask() {
var self = this; let failure = '';
var failure = '';
this._cleanup(); this._cleanup().then(() => {
if (this.currentTask === this.manifest.length) {
this._done();
return;
}
let task = this.manifest[this.currentTask];
task.round = 0;
task.pageNum = task.firstPage || 1;
task.stats = { times: [] };
if (this.currentTask === this.manifest.length) { this._log('Loading file "' + task.file + '"\n');
this._done();
return;
}
var task = this.manifest[this.currentTask];
task.round = 0;
task.pageNum = task.firstPage || 1;
task.stats = { times: [] };
this._log('Loading file "' + task.file + '"\n'); let absoluteUrl = new URL(task.file, window.location).href;
PDFJS.disableRange = task.disableRange;
var absoluteUrl = new URL(task.file, window.location).href; PDFJS.disableAutoFetch = !task.enableAutoFetch;
PDFJS.disableRange = task.disableRange; try {
PDFJS.disableAutoFetch = !task.enableAutoFetch; PDFJS.getDocument({
try { url: absoluteUrl,
PDFJS.getDocument({ password: task.password,
url: absoluteUrl, }).then((doc) => {
password: task.password task.pdfDoc = doc;
}).then(function(doc) { this._nextPage(task, failure);
task.pdfDoc = doc; }, (err) => {
self._nextPage(task, failure); failure = 'Loading PDF document: ' + err;
}, function(e) { this._nextPage(task, failure);
failure = 'Loading PDF document: ' + e; });
self._nextPage(task, failure); return;
}); } catch (e) {
return; failure = 'Loading PDF document: ' + this._exceptionToString(e);
} catch (e) { }
failure = 'Loading PDF document: ' + this._exceptionToString(e); this._nextPage(task, failure);
} });
this._nextPage(task, failure);
}, },
_cleanup: function Driver_cleanup() { _cleanup() {
// Clear out all the stylesheets since a new one is created for each font. // Clear out all the stylesheets since a new one is created for each font.
while (document.styleSheets.length > 0) { while (document.styleSheets.length > 0) {
var styleSheet = document.styleSheets[0]; let styleSheet = document.styleSheets[0];
while (styleSheet.cssRules.length > 0) { while (styleSheet.cssRules.length > 0) {
styleSheet.deleteRule(0); styleSheet.deleteRule(0);
} }
var ownerNode = styleSheet.ownerNode; let ownerNode = styleSheet.ownerNode;
ownerNode.parentNode.removeChild(ownerNode); ownerNode.parentNode.removeChild(ownerNode);
} }
var body = document.body; let body = document.body;
while (body.lastChild !== this.end) { while (body.lastChild !== this.end) {
body.removeChild(body.lastChild); body.removeChild(body.lastChild);
} }
let destroyedPromises = [];
// Wipe out the link to the pdfdoc so it can be GC'ed. // Wipe out the link to the pdfdoc so it can be GC'ed.
for (var i = 0; i < this.manifest.length; i++) { for (let i = 0; i < this.manifest.length; i++) {
if (this.manifest[i].pdfDoc) { if (this.manifest[i].pdfDoc) {
this.manifest[i].pdfDoc.destroy(); destroyedPromises.push(this.manifest[i].pdfDoc.destroy());
delete this.manifest[i].pdfDoc; delete this.manifest[i].pdfDoc;
} }
} }
return Promise.all(destroyedPromises);
}, },
_exceptionToString: function Driver_exceptionToString(e) { _exceptionToString: function Driver_exceptionToString(e) {