Wait until PDFDocumentLoadingTask.destroy is resolved before calling done in the API unit-tests

Try to ensure that everything, including workers, are properly destroyed before running the next test-case.
This commit is contained in:
Jonas Jenwald 2017-05-10 00:21:09 +02:00
parent 52998c5fe1
commit 7a040c05d7

View File

@ -69,8 +69,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(); loadingTask.destroy().then(done);
done();
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });
@ -78,12 +77,13 @@ describe('api', function() {
it('creates pdf doc from URL and aborts before worker initialized', it('creates pdf doc from URL and aborts before worker initialized',
function(done) { function(done) {
var loadingTask = PDFJS.getDocument(basicApiUrl); var loadingTask = PDFJS.getDocument(basicApiUrl);
loadingTask.destroy(); let destroyed = loadingTask.destroy();
loadingTask.promise.then(function(reason) { loadingTask.promise.then(function(reason) {
done.fail('shall fail loading'); done.fail('shall fail loading');
}).catch(function (reason) { }).catch(function (reason) {
expect(true).toEqual(true); expect(true).toEqual(true);
done(); destroyed.then(done);
}); });
}); });
it('creates pdf doc from URL and aborts loading after worker initialized', it('creates pdf doc from URL and aborts loading after worker initialized',
@ -134,8 +134,7 @@ describe('api', function() {
var loadingTask = PDFJS.getDocument(typedArrayPdf); var loadingTask = PDFJS.getDocument(typedArrayPdf);
loadingTask.promise.then(function(data) { loadingTask.promise.then(function(data) {
expect(data instanceof PDFDocumentProxy).toEqual(true); expect(data instanceof PDFDocumentProxy).toEqual(true);
loadingTask.destroy(); loadingTask.destroy().then(done);
done();
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });
@ -149,8 +148,7 @@ describe('api', function() {
done.fail('shall fail loading'); done.fail('shall fail loading');
}).catch(function (error) { }).catch(function (error) {
expect(error instanceof InvalidPDFException).toEqual(true); expect(error instanceof InvalidPDFException).toEqual(true);
loadingTask.destroy(); loadingTask.destroy().then(done);
done();
}); });
}); });
it('creates pdf doc from non-existent URL', function(done) { it('creates pdf doc from non-existent URL', function(done) {
@ -161,8 +159,7 @@ describe('api', function() {
done.fail('shall fail loading'); done.fail('shall fail loading');
}).catch(function (error) { }).catch(function (error) {
expect(error instanceof MissingPDFException).toEqual(true); expect(error instanceof MissingPDFException).toEqual(true);
loadingTask.destroy(); loadingTask.destroy().then(done);
done();
}); });
}); });
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',
@ -205,8 +202,7 @@ describe('api', function() {
]; ];
Promise.all(promises).then(function (data) { Promise.all(promises).then(function (data) {
expect(data[2] instanceof PDFDocumentProxy).toEqual(true); expect(data[2] instanceof PDFDocumentProxy).toEqual(true);
loadingTask.destroy(); loadingTask.destroy().then(done);
done();
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });
@ -262,9 +258,10 @@ describe('api', function() {
url, password: 'qwerty', url, password: 'qwerty',
}); });
let passwordNeededDestroyed;
passwordNeededLoadingTask.onPassword = function (callback, reason) { passwordNeededLoadingTask.onPassword = function (callback, reason) {
if (reason === PasswordResponses.NEED_PASSWORD) { if (reason === PasswordResponses.NEED_PASSWORD) {
passwordNeededLoadingTask.destroy(); passwordNeededDestroyed = passwordNeededLoadingTask.destroy();
return; return;
} }
// Shouldn't get here. // Shouldn't get here.
@ -276,6 +273,7 @@ describe('api', function() {
}, function (reason) { }, function (reason) {
expect(reason instanceof PasswordException).toEqual(true); expect(reason instanceof PasswordException).toEqual(true);
expect(reason.code).toEqual(PasswordResponses.NEED_PASSWORD); expect(reason.code).toEqual(PasswordResponses.NEED_PASSWORD);
return passwordNeededDestroyed;
}); });
passwordIncorrectLoadingTask.onPassword = function (callback, reason) { passwordIncorrectLoadingTask.onPassword = function (callback, reason) {
@ -393,8 +391,8 @@ describe('api', function() {
}); });
}); });
afterAll(function() { afterAll(function(done) {
loadingTask.destroy(); loadingTask.destroy().then(done);
}); });
it('gets number of pages', function() { it('gets number of pages', function() {
@ -508,8 +506,7 @@ describe('api', function() {
'Page.2': [{ num: 6, gen: 0}, { name: 'XYZ' }, 0, 375, null], 'Page.2': [{ num: 6, gen: 0}, { name: 'XYZ' }, 0, 375, null],
}); });
loadingTask.destroy(); loadingTask.destroy().then(done);
done();
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });
@ -524,8 +521,7 @@ describe('api', function() {
expect(destination).toEqual([{ num: 1, gen: 0}, { name: 'XYZ' }, expect(destination).toEqual([{ num: 1, gen: 0}, { name: 'XYZ' },
0, 375, null]); 0, 375, null]);
loadingTask.destroy(); loadingTask.destroy().then(done);
done();
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });
@ -540,8 +536,7 @@ describe('api', function() {
promise.then(function (destination) { promise.then(function (destination) {
expect(destination).toEqual(null); expect(destination).toEqual(null);
loadingTask.destroy(); loadingTask.destroy().then(done);
done();
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });
@ -592,11 +587,12 @@ describe('api', function() {
expect(pageLabels[2]).toEqual(['1', '2']); expect(pageLabels[2]).toEqual(['1', '2']);
expect(pageLabels[3]).toEqual(['X3']); expect(pageLabels[3]).toEqual(['X3']);
loadingTask0.destroy(); Promise.all([
loadingTask1.destroy(); loadingTask0.destroy(),
loadingTask2.destroy(); loadingTask1.destroy(),
loadingTask3.destroy(); loadingTask2.destroy(),
done(); loadingTask3.destroy()
]).then(done);
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });
@ -623,8 +619,7 @@ describe('api', function() {
expect(attachment.content instanceof Uint8Array).toBeTruthy(); expect(attachment.content instanceof Uint8Array).toBeTruthy();
expect(attachment.content.length).toEqual(30098); expect(attachment.content.length).toEqual(30098);
loadingTask.destroy(); loadingTask.destroy().then(done);
done();
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });
@ -653,8 +648,7 @@ describe('api', function() {
promise.then(function (data) { promise.then(function (data) {
expect(data).toEqual(['print({});']); expect(data).toEqual(['print({});']);
expect(data[0]).toMatch(viewerPrintRegExp); expect(data[0]).toMatch(viewerPrintRegExp);
loadingTask.destroy(); loadingTask.destroy().then(done);
done();
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });
@ -671,8 +665,7 @@ describe('api', function() {
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(); loadingTask.destroy().then(done);
done();
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });
@ -687,8 +680,7 @@ describe('api', function() {
promise.then(function (outline) { promise.then(function (outline) {
expect(outline).toEqual(null); expect(outline).toEqual(null);
loadingTask.destroy(); loadingTask.destroy().then(done);
done();
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });
@ -739,8 +731,7 @@ describe('api', function() {
expect(outlineItemOne.italic).toEqual(true); expect(outlineItemOne.italic).toEqual(true);
expect(outlineItemOne.color).toEqual(new Uint8Array([0, 0, 0])); expect(outlineItemOne.color).toEqual(new Uint8Array([0, 0, 0]));
loadingTask.destroy(); // Cleanup the worker. loadingTask.destroy().then(done);
done();
}); });
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
@ -806,9 +797,10 @@ describe('api', function() {
expect(fingerprint1).not.toEqual(fingerprint2); expect(fingerprint1).not.toEqual(fingerprint2);
loadingTask1.destroy(); Promise.all([
loadingTask2.destroy(); loadingTask1.destroy(),
done(); loadingTask2.destroy()
]).then(done);
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });
@ -831,8 +823,8 @@ describe('api', function() {
}); });
}); });
afterAll(function() { afterAll(function(done) {
loadingTask.destroy(); loadingTask.destroy().then(done);
}); });
it('gets page number', function () { it('gets page number', function () {
@ -933,10 +925,11 @@ describe('api', function() {
expect(invalidDocBaseUrlAnnotations[0].unsafeUrl).toEqual( expect(invalidDocBaseUrlAnnotations[0].unsafeUrl).toEqual(
'../../0021/002156/215675E.pdf#nameddest=15'); '../../0021/002156/215675E.pdf#nameddest=15');
defaultLoadingTask.destroy(); Promise.all([
docBaseUrlLoadingTask.destroy(); defaultLoadingTask.destroy(),
invalidDocBaseUrlLoadingTask.destroy(); docBaseUrlLoadingTask.destroy(),
done(); invalidDocBaseUrlLoadingTask.destroy()
]).then(done);
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);
}); });