Skip to content

Commit

Permalink
WorkerPool.WORKER_COUNT => mapboxgl.workerCount
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Aug 21, 2016
1 parent 05b7e69 commit 2f00368
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
4 changes: 4 additions & 0 deletions js/mapbox-gl.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
'use strict';

var browser = require('./util/browser');

// jshint -W079
var mapboxgl = module.exports = {};

mapboxgl.version = require('../package.json').version;
mapboxgl.workerCount = Math.max(browser.hardwareConcurrency - 1, 1);


mapboxgl.Map = require('./ui/map');
mapboxgl.Control = require('./ui/control/control');
Expand Down
17 changes: 11 additions & 6 deletions js/util/worker_pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@

var assert = require('assert');
var WebWorker = require('./web_worker');
var browser = require('./browser');

module.exports = WorkerPool;

WorkerPool.WORKER_COUNT = Math.max(browser.hardwareConcurrency - 1, 1);

/**
* Constructs a worker pool.
* @private
*/
function WorkerPool() {
this.workerCount = WorkerPool.WORKER_COUNT;
this.active = {};
}

WorkerPool.prototype = {
acquire: function (mapId) {
if (!this.workers) {
assert(typeof this.workerCount === 'number');
// Lazily look up the value of mapboxgl.workerCount. This allows
// client code a chance to set it while circumventing cyclic
// dependency problems
var workerCount = require('../mapbox-gl').workerCount;
assert(typeof workerCount === 'number' && workerCount < Infinity);

this.workers = [];
while (this.workers.length < this.workerCount) {
while (this.workers.length < workerCount) {
this.workers.push(new WebWorker());
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/js/mapbox-gl.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
'use strict';

var test = require('tap').test;
var proxyquire = require('proxyquire');
var mapboxgl = require('../../../js/mapbox-gl');

test('mapboxgl', function(t) {
t.test('version', function(t) {
t.ok(mapboxgl.version);
t.end();
});

t.test('.workerCount defaults to hardwareConcurrency - 1', function (t) {
var mapboxgl = proxyquire('../../../js/mapbox-gl', {
'./util/browser': { hardwareConcurrency: 15 }
});
t.equal(mapboxgl.workerCount, 14);
t.end();
});
t.end();
});
10 changes: 4 additions & 6 deletions test/js/util/dispatcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var test = require('tap').test;
var proxyquire = require('proxyquire');
var Dispatcher = require('../../../js/util/dispatcher');
var WebWorker = require('../../../js/util/web_worker');
var WorkerPool = require('../../../js/util/worker_pool');

test('Dispatcher', function (t) {
t.test('requests and releases workers from pool', function (t) {
Expand All @@ -31,19 +30,18 @@ test('Dispatcher', function (t) {
});

test('creates Actors with unique map id', function (t) {
var Dispatcher = proxyquire('../../../js/util/dispatcher', { './actor': Actor });
var Dispatcher = proxyquire('../../../js/util/dispatcher', {'./actor': Actor });
var WorkerPool = proxyquire('../../../js/util/worker_pool', {
'../mapbox-gl': { workerCount: 1 }
});

var ids = [];
function Actor (target, parent, mapId) { ids.push(mapId); }

var previousWorkerCount = WorkerPool.WORKER_COUNT;
WorkerPool.WORKER_COUNT = 1;

var workerPool = new WorkerPool();
var dispatchers = [new Dispatcher(workerPool, {}), new Dispatcher(workerPool, {})];
t.same(ids, dispatchers.map(function (d) { return d.id; }));

WorkerPool.WORKER_COUNT = previousWorkerCount;
t.end();
});

Expand Down
24 changes: 9 additions & 15 deletions test/js/util/worker_pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,31 @@

var test = require('tap').test;
var proxyquire = require('proxyquire');
var WorkerPool = require('../../../js/util/worker_pool');

test('WorkerPool', function (t) {
t.test('.WORKER_COUNT', function (t) {
t.test('#acquire', function (t) {
var WorkerPool = proxyquire('../../../js/util/worker_pool', {
'./browser': { hardwareConcurrency: 15 }
'../mapbox-gl': { workerCount: 4 }
});
t.equal(WorkerPool.WORKER_COUNT, 14);

WorkerPool.WORKER_COUNT = 4;
t.end();
});

t.test('#acquire', function (t) {
// make sure we're actually creating some workers
t.ok(WorkerPool.WORKER_COUNT > 0);

var pool = new WorkerPool();

t.notOk(pool.workers);
var workers1 = pool.acquire('map-1');
var workers2 = pool.acquire('map-2');
t.equal(workers1.length, WorkerPool.WORKER_COUNT);
t.equal(workers2.length, WorkerPool.WORKER_COUNT);
t.equal(workers1.length, 4);
t.equal(workers2.length, 4);

// check that the two different dispatchers' workers arrays correspond
workers1.forEach(function (w, i) { t.equal(w, workers2[i]); });
t.end();
});

t.test('#release', function (t) {
var WorkerPool = proxyquire('../../../js/util/worker_pool', {
'../mapbox-gl': { workerCount: 4 }
});

var pool = new WorkerPool();
pool.acquire('map-1');
var workers = pool.acquire('map-2');
Expand All @@ -48,7 +42,7 @@ test('WorkerPool', function (t) {

t.comment('terminates workers if no dispatchers are active');
pool.release('map-1');
t.equal(terminated, WorkerPool.WORKER_COUNT);
t.equal(terminated, 4);
t.notOk(pool.workers);

t.end();
Expand Down

0 comments on commit 2f00368

Please sign in to comment.