Skip to content

Commit

Permalink
Unit test Dispatcher's use of WorkerPool
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Aug 11, 2016
1 parent f9622cf commit afbdafd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion js/util/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function Dispatcher(length, parent) {
this.currentActor = 0;
this.id = util.uniqueId();
var workers = Dispatcher.workerPool.acquire(this.id, length);
for (var i = 0; i < length; i++) {
for (var i = 0; i < workers.length; i++) {
var worker = workers[i];
var actor = new Actor(worker, parent, this.id);
actor.name = "Worker " + i;
Expand Down
39 changes: 39 additions & 0 deletions test/js/util/dispatcher.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

var test = require('tap').test;
var Dispatcher = require('../../../js/util/dispatcher');
var WebWorker = require('../../../js/util/web_worker');
var originalWorkerPool = Dispatcher.workerPool;

test('Dispatcher', function (t) {
t.test('requests and releases workers from pool', function (t) {
var dispatcher;
var workers = [new WebWorker(), new WebWorker()];

var releaseCalled = [];
Dispatcher.workerPool = {
acquire: function (id, count) {
t.equal(count, 2);
return workers;
},
release: function (id) {
releaseCalled.push(id);
}
};

dispatcher = new Dispatcher(2, {});
t.same(dispatcher.actors.map(function (actor) { return actor.target; }), workers);
dispatcher.remove();
t.equal(dispatcher.actors.length, 0, 'actors discarded');
t.same(releaseCalled, [dispatcher.id]);
t.end();
});

t.end();
});

test('after', function (t) {
Dispatcher.workerPool = originalWorkerPool;
t.end();
});

0 comments on commit afbdafd

Please sign in to comment.