Merge pull request #6904 from timvandermeij/cleanup-workers

Destroy workers when they are no longer needed in the unit tests
This commit is contained in:
Yury Delendik 2016-02-03 14:46:22 -06:00
commit 4c59712606
2 changed files with 80 additions and 32 deletions

View File

@ -1034,6 +1034,11 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
function operatorListChanged() { function operatorListChanged() {
if (intentState.operatorList.lastChunk) { if (intentState.operatorList.lastChunk) {
intentState.opListReadCapability.resolve(intentState.operatorList); intentState.opListReadCapability.resolve(intentState.operatorList);
var i = intentState.renderTasks.indexOf(opListTask);
if (i >= 0) {
intentState.renderTasks.splice(i, 1);
}
} }
} }
@ -1042,9 +1047,10 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
this.intentStates[renderingIntent] = Object.create(null); this.intentStates[renderingIntent] = Object.create(null);
} }
var intentState = this.intentStates[renderingIntent]; var intentState = this.intentStates[renderingIntent];
var opListTask;
if (!intentState.opListReadCapability) { if (!intentState.opListReadCapability) {
var opListTask = {}; opListTask = {};
opListTask.operatorListChanged = operatorListChanged; opListTask.operatorListChanged = operatorListChanged;
intentState.receivingOperatorList = true; intentState.receivingOperatorList = true;
intentState.opListReadCapability = createPromiseCapability(); intentState.opListReadCapability = createPromiseCapability();
@ -1087,6 +1093,10 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
var waitOn = []; var waitOn = [];
Object.keys(this.intentStates).forEach(function(intent) { Object.keys(this.intentStates).forEach(function(intent) {
if (intent === 'oplist') {
// Avoid errors below, since the renderTasks are just stubs.
return;
}
var intentState = this.intentStates[intent]; var intentState = this.intentStates[intent];
intentState.renderTasks.forEach(function(renderTask) { intentState.renderTasks.forEach(function(renderTask) {
var renderCompleted = renderTask.capability.promise. var renderCompleted = renderTask.capability.promise.

View File

@ -1,7 +1,7 @@
/* globals PDFJS, expect, it, describe, Promise, combineUrl, waitsFor, /* globals PDFJS, expect, it, describe, Promise, combineUrl, waitsFor,
InvalidPDFException, MissingPDFException, StreamType, FontType, InvalidPDFException, MissingPDFException, StreamType, FontType,
PDFDocumentProxy, PasswordException, PasswordResponses, PDFDocumentProxy, PasswordException, PasswordResponses,
PDFPageProxy, createPromiseCapability, afterEach */ PDFPageProxy, createPromiseCapability, beforeEach, afterEach */
'use strict'; 'use strict';
@ -62,6 +62,7 @@ describe('api', function() {
expect((data[0].loaded / data[0].total) > 0).toEqual(true); expect((data[0].loaded / data[0].total) > 0).toEqual(true);
expect(data[1] instanceof PDFDocumentProxy).toEqual(true); expect(data[1] instanceof PDFDocumentProxy).toEqual(true);
expect(loadingTask).toEqual(data[1].loadingTask); expect(loadingTask).toEqual(data[1].loadingTask);
loadingTask.destroy();
}); });
}); });
it('creates pdf doc from URL and aborts before worker initialized', it('creates pdf doc from URL and aborts before worker initialized',
@ -114,26 +115,29 @@ describe('api', function() {
// Sanity check to make sure that we fetched the entire PDF file. // Sanity check to make sure that we fetched the entire PDF file.
expect(typedArrayPdf.length).toEqual(basicApiFileLength); expect(typedArrayPdf.length).toEqual(basicApiFileLength);
var promise = PDFJS.getDocument(typedArrayPdf); var loadingTask = PDFJS.getDocument(typedArrayPdf);
waitsForPromiseResolved(promise, function(data) { waitsForPromiseResolved(loadingTask.promise, function(data) {
expect(data instanceof PDFDocumentProxy).toEqual(true); expect(data instanceof PDFDocumentProxy).toEqual(true);
loadingTask.destroy();
}); });
}); });
it('creates pdf doc from invalid PDF file', function() { it('creates pdf doc from invalid PDF file', function() {
// A severely corrupt PDF file (even Adobe Reader fails to open it). // A severely corrupt PDF file (even Adobe Reader fails to open it).
var url = combineUrl(window.location.href, '../pdfs/bug1020226.pdf'); var url = combineUrl(window.location.href, '../pdfs/bug1020226.pdf');
var promise = PDFJS.getDocument(url); var loadingTask = PDFJS.getDocument(url);
waitsForPromiseRejected(promise, function (error) { waitsForPromiseRejected(loadingTask.promise, function (error) {
expect(error instanceof InvalidPDFException).toEqual(true); expect(error instanceof InvalidPDFException).toEqual(true);
loadingTask.destroy();
}); });
}); });
it('creates pdf doc from non-existent URL', function() { it('creates pdf doc from non-existent URL', function() {
var nonExistentUrl = combineUrl(window.location.href, var nonExistentUrl = combineUrl(window.location.href,
'../pdfs/non-existent.pdf'); '../pdfs/non-existent.pdf');
var promise = PDFJS.getDocument(nonExistentUrl); var loadingTask = PDFJS.getDocument(nonExistentUrl);
waitsForPromiseRejected(promise, function(error) { waitsForPromiseRejected(loadingTask.promise, function(error) {
expect(error instanceof MissingPDFException).toEqual(true); expect(error instanceof MissingPDFException).toEqual(true);
loadingTask.destroy();
}); });
}); });
it('creates pdf doc from PDF file protected with user and owner password', it('creates pdf doc from PDF file protected with user and owner password',
@ -176,33 +180,40 @@ describe('api', function() {
]; ];
waitsForPromiseResolved(Promise.all(promises), function (data) { waitsForPromiseResolved(Promise.all(promises), function (data) {
expect(data[2] instanceof PDFDocumentProxy).toEqual(true); expect(data[2] instanceof PDFDocumentProxy).toEqual(true);
loadingTask.destroy();
}); });
}); });
it('creates pdf doc from PDF file protected with only a user password', it('creates pdf doc from PDF file protected with only a user password',
function () { function () {
var url = combineUrl(window.location.href, '../pdfs/pr6531_2.pdf'); var url = combineUrl(window.location.href, '../pdfs/pr6531_2.pdf');
var passwordNeededPromise = PDFJS.getDocument({ var passwordNeededLoadingTask = PDFJS.getDocument({
url: url, password: '', url: url, password: '',
}); });
waitsForPromiseRejected(passwordNeededPromise, function (data) { waitsForPromiseRejected(passwordNeededLoadingTask.promise,
function (data) {
expect(data instanceof PasswordException).toEqual(true); expect(data instanceof PasswordException).toEqual(true);
expect(data.code).toEqual(PasswordResponses.NEED_PASSWORD); expect(data.code).toEqual(PasswordResponses.NEED_PASSWORD);
passwordNeededLoadingTask.destroy();
}); });
var passwordIncorrectPromise = PDFJS.getDocument({ var passwordIncorrectLoadingTask = PDFJS.getDocument({
url: url, password: 'qwerty', url: url, password: 'qwerty',
}); });
waitsForPromiseRejected(passwordIncorrectPromise, function (data) { waitsForPromiseRejected(passwordIncorrectLoadingTask.promise,
function (data) {
expect(data instanceof PasswordException).toEqual(true); expect(data instanceof PasswordException).toEqual(true);
expect(data.code).toEqual(PasswordResponses.INCORRECT_PASSWORD); expect(data.code).toEqual(PasswordResponses.INCORRECT_PASSWORD);
passwordIncorrectLoadingTask.destroy();
}); });
var passwordAcceptedPromise = PDFJS.getDocument({ var passwordAcceptedLoadingTask = PDFJS.getDocument({
url: url, password: 'asdfasdf', url: url, password: 'asdfasdf',
}); });
waitsForPromiseResolved(passwordAcceptedPromise, function (data) { waitsForPromiseResolved(passwordAcceptedLoadingTask.promise,
function (data) {
expect(data instanceof PDFDocumentProxy).toEqual(true); expect(data instanceof PDFDocumentProxy).toEqual(true);
passwordAcceptedLoadingTask.destroy();
}); });
}); });
}); });
@ -275,11 +286,20 @@ describe('api', function() {
}); });
}); });
describe('PDFDocument', function() { describe('PDFDocument', function() {
var promise = PDFJS.getDocument(basicApiUrl); var loadingTask;
var doc; var doc;
waitsForPromiseResolved(promise, function(data) {
beforeEach(function() {
loadingTask = PDFJS.getDocument(basicApiUrl);
waitsForPromiseResolved(loadingTask.promise, function(data) {
doc = data; doc = data;
}); });
});
afterEach(function() {
loadingTask.destroy();
});
it('gets number of pages', function() { it('gets number of pages', function() {
expect(doc.numPages).toEqual(3); expect(doc.numPages).toEqual(3);
}); });
@ -386,24 +406,28 @@ describe('api', function() {
it('gets javascript with printing instructions (Print action)', function() { it('gets javascript with printing instructions (Print action)', function() {
// PDF document with "Print" Named action in OpenAction // PDF document with "Print" Named action in OpenAction
var pdfUrl = combineUrl(window.location.href, '../pdfs/bug1001080.pdf'); var pdfUrl = combineUrl(window.location.href, '../pdfs/bug1001080.pdf');
var promise = PDFJS.getDocument(pdfUrl).then(function(doc) { var loadingTask = PDFJS.getDocument(pdfUrl);
var promise = loadingTask.promise.then(function(doc) {
return doc.getJavaScript(); return doc.getJavaScript();
}); });
waitsForPromiseResolved(promise, function (data) { waitsForPromiseResolved(promise, function (data) {
expect(data).toEqual(['print({});']); expect(data).toEqual(['print({});']);
expect(data[0]).toMatch(viewerPrintRegExp); expect(data[0]).toMatch(viewerPrintRegExp);
loadingTask.destroy();
}); });
}); });
it('gets javascript with printing instructions (JS action)', function() { it('gets javascript with printing instructions (JS action)', function() {
// PDF document with "JavaScript" action in OpenAction // PDF document with "JavaScript" action in OpenAction
var pdfUrl = combineUrl(window.location.href, '../pdfs/issue6106.pdf'); var pdfUrl = combineUrl(window.location.href, '../pdfs/issue6106.pdf');
var promise = PDFJS.getDocument(pdfUrl).then(function(doc) { var loadingTask = PDFJS.getDocument(pdfUrl);
var promise = loadingTask.promise.then(function(doc) {
return doc.getJavaScript(); return doc.getJavaScript();
}); });
waitsForPromiseResolved(promise, function (data) { waitsForPromiseResolved(promise, function (data) {
expect(data).toEqual( expect(data).toEqual(
['this.print({bUI:true,bSilent:false,bShrinkToFit:true});']); ['this.print({bUI:true,bSilent:false,bShrinkToFit:true});']);
expect(data[0]).toMatch(viewerPrintRegExp); expect(data[0]).toMatch(viewerPrintRegExp);
loadingTask.destroy();
}); });
}); });
it('gets outline', function() { it('gets outline', function() {
@ -486,25 +510,30 @@ describe('api', function() {
expect(fingerprint2.length > 0).toEqual(true); expect(fingerprint2.length > 0).toEqual(true);
expect(fingerprint1).not.toEqual(fingerprint2); expect(fingerprint1).not.toEqual(fingerprint2);
loadingTask1.destroy();
loadingTask2.destroy();
}); });
}); });
}); });
describe('Page', function() { describe('Page', function() {
var resolvePromise; var loadingTask;
var promise = new Promise(function (resolve) { var pdfDocument, page;
resolvePromise = resolve;
}); beforeEach(function() {
var pdfDocument; loadingTask = PDFJS.getDocument(basicApiUrl);
PDFJS.getDocument(basicApiUrl).then(function(doc) { waitsForPromiseResolved(loadingTask.promise, function(doc) {
doc.getPage(1).then(function(data) {
resolvePromise(data);
});
pdfDocument = doc; pdfDocument = doc;
}); waitsForPromiseResolved(pdfDocument.getPage(1), function(data) {
var page;
waitsForPromiseResolved(promise, function(data) {
page = data; page = data;
}); });
});
});
afterEach(function() {
loadingTask.destroy();
});
it('gets page number', function () { it('gets page number', function () {
expect(page.pageNumber).toEqual(1); expect(page.pageNumber).toEqual(1);
}); });
@ -592,12 +621,15 @@ describe('api', function() {
var pdf2 = combineUrl(window.location.href, '../pdfs/TAMReview.pdf'); var pdf2 = combineUrl(window.location.href, '../pdfs/TAMReview.pdf');
// A PDF using the Arial font. // A PDF using the Arial font.
var pdf3 = combineUrl(window.location.href, '../pdfs/issue6068.pdf'); var pdf3 = combineUrl(window.location.href, '../pdfs/issue6068.pdf');
var loadingTasks = [];
var pdfDocuments = []; var pdfDocuments = [];
// Render the first page of the given PDF file. // Render the first page of the given PDF file.
// Fulfills the promise with the base64-encoded version of the PDF. // Fulfills the promise with the base64-encoded version of the PDF.
function renderPDF(filename) { function renderPDF(filename) {
return PDFJS.getDocument(filename) var loadingTask = PDFJS.getDocument(filename);
loadingTasks.push(loadingTask);
return loadingTask.promise
.then(function(pdf) { .then(function(pdf) {
pdfDocuments.push(pdf); pdfDocuments.push(pdf);
return pdf.getPage(1); return pdf.getPage(1);
@ -623,6 +655,12 @@ describe('api', function() {
return pdfDocument.destroy(); return pdfDocument.destroy();
}); });
waitsForPromiseResolved(Promise.all(destroyPromises), function() {}); waitsForPromiseResolved(Promise.all(destroyPromises), function() {});
// Destroy the workers.
destroyPromises = loadingTasks.map(function(loadingTask) {
return loadingTask.destroy();
});
waitsForPromiseResolved(Promise.all(destroyPromises), function() {});
}); });
it('should correctly render PDFs in parallel', function() { it('should correctly render PDFs in parallel', function() {