Skip to content

Commit

Permalink
Use LoopbackPort in the "message_handler" unit-tests
Browse files Browse the repository at this point in the history
There's no good reason, as far as I can tell, to duplicate the functionality of the `LoopbackPort` in the unit-tests. The only difference between the implementations is that `LoopbackPort` mimics the (native) structured cloning, however that shouldn't matter here since the tests are only sending "simple" data (strings respectively arrays with numbers).

Furthermore the patch also changes `LoopbackPort` to default to using "structured cloning" and deferred invocation of the listeners, since native typed array support is now a requirement for using the PDF.js library.
  • Loading branch information
Snuffleupagus committed Jun 4, 2018
1 parent 1ab9d3e commit c8dafd2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
})();

class LoopbackPort {
constructor(defer) {
constructor(defer = true) {
this._listeners = [];
this._defer = defer;
this._deferred = Promise.resolve(undefined);
Expand Down
45 changes: 8 additions & 37 deletions test/unit/message_handler_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,10 @@
*/

import { createPromiseCapability } from '../../src/shared/util';
import { LoopbackPort } from '../../src/display/api';
import { MessageHandler } from '../../src/shared/message_handler';

describe('message_handler', function () {
// Temporary fake port for sending messages between main and worker.
class FakePort {
constructor() {
this._listeners = [];
this._deferred = Promise.resolve(undefined);
}

postMessage(obj) {
let event = { data: obj, };
this._deferred.then(() => {
this._listeners.forEach(function (listener) {
listener.call(this, event);
}, this);
});
}

addEventListener(name, listener) {
this._listeners.push(listener);
}

removeEventListener(name, listener) {
let i = this._listeners.indexOf(listener);
this._listeners.splice(i, 1);
}

terminate() {
this._listeners = [];
}
}

// Sleep function to wait for sometime, similar to setTimeout but faster.
function sleep(ticks) {
return Promise.resolve().then(() => {
Expand All @@ -56,7 +27,7 @@ describe('message_handler', function () {

describe('sendWithStream', function () {
it('should return a ReadableStream', function () {
let port = new FakePort();
let port = new LoopbackPort();
let messageHandler1 = new MessageHandler('main', 'worker', port);
let readable = messageHandler1.sendWithStream('fakeHandler');
// Check if readable is an instance of ReadableStream.
Expand All @@ -66,7 +37,7 @@ describe('message_handler', function () {

it('should read using a reader', function (done) {
let log = '';
let port = new FakePort();
let port = new LoopbackPort();
let messageHandler1 = new MessageHandler('main', 'worker', port);
let messageHandler2 = new MessageHandler('worker', 'main', port);
messageHandler2.on('fakeHandler', (data, sink) => {
Expand Down Expand Up @@ -110,7 +81,7 @@ describe('message_handler', function () {

it('should not read any data when cancelled', function (done) {
let log = '';
let port = new FakePort();
let port = new LoopbackPort();
let messageHandler2 = new MessageHandler('worker', 'main', port);
messageHandler2.on('fakeHandler', (data, sink) => {
sink.onPull = function () {
Expand Down Expand Up @@ -162,7 +133,7 @@ describe('message_handler', function () {

it('should not read when errored', function(done) {
let log = '';
let port = new FakePort();
let port = new LoopbackPort();
let messageHandler2 = new MessageHandler('worker', 'main', port);
messageHandler2.on('fakeHandler', (data, sink) => {
sink.onPull = function () {
Expand Down Expand Up @@ -205,7 +176,7 @@ describe('message_handler', function () {

it('should read data with blocking promise', function (done) {
let log = '';
let port = new FakePort();
let port = new LoopbackPort();
let messageHandler2 = new MessageHandler('worker', 'main', port);
messageHandler2.on('fakeHandler', (data, sink) => {
sink.onPull = function () {
Expand Down Expand Up @@ -266,7 +237,7 @@ describe('message_handler', function () {
it('should read data with blocking promise and buffer whole data' +
' into stream', function (done) {
let log = '';
let port = new FakePort();
let port = new LoopbackPort();
let messageHandler2 = new MessageHandler('worker', 'main', port);
messageHandler2.on('fakeHandler', (data, sink) => {
sink.onPull = function () {
Expand Down Expand Up @@ -326,7 +297,7 @@ describe('message_handler', function () {

it('should ignore any pull after close is called', function (done) {
let log = '';
let port = new FakePort();
let port = new LoopbackPort();
let capability = createPromiseCapability();
let messageHandler2 = new MessageHandler('worker', 'main', port);
messageHandler2.on('fakeHandler', (data, sink) => {
Expand Down

0 comments on commit c8dafd2

Please sign in to comment.