Skip to content

Commit

Permalink
unit tests for resource timing fallback method
Browse files Browse the repository at this point in the history
  • Loading branch information
sbma44 committed May 23, 2018
1 parent 60353be commit c654f50
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/unit/source/geojson_worker_source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,38 @@ test('resourceTiming', (t) => {
});
});

t.test('loadData - url (resourceTiming fallback method)', (t) => {
const sampleMarks = [100, 350];
const marks = {};
const measures = {};
t.stub(perf, 'getEntriesByName').callsFake((name) => { return measures[name] || []; });
t.stub(perf, 'mark').callsFake((name) => {
marks[name] = sampleMarks.shift();
return null;
});
t.stub(perf, 'measure').callsFake((name, start, end) => {
measures[name] = measures[name] || [];
measures[name].push({
duration: marks[end] - marks[start],
entryType: 'measure',
name: name,
startTime: marks[start]
});
return null;
});
t.stub(perf, 'clearMarks').callsFake(() => { return null; });
t.stub(perf, 'clearMeasures').callsFake(() => { return null; });

const layerIndex = new StyleLayerIndex(layers);
const source = new GeoJSONWorkerSource(null, layerIndex, (params, callback) => { return callback(null, geoJson); });

source.loadData({ source: 'testSource', request: { url: 'http://localhost/nonexistent', collectResourceTiming: true } }, (err, result) => {
t.equal(err, null);
t.deepEquals(result.resourceTiming.testSource, [{"duration": 250, "entryType": "measure", "name": "http://localhost/nonexistent", "startTime": 100 }], 'got expected resource timing');
t.end();
});
});

t.test('loadData - data', (t) => {
const layerIndex = new StyleLayerIndex(layers);
const source = new GeoJSONWorkerSource(null, layerIndex);
Expand Down
54 changes: 54 additions & 0 deletions test/unit/source/vector_tile_worker_source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,57 @@ test('VectorTileWorkerSource provides resource timing information', (t) => {
t.end();
});
});

test('VectorTileWorkerSource provides resource timing information (fallback method)', (t) => {
const rawTileData = fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf'));

function loadVectorData(params, callback) {
return callback(null, {
vectorTile: new vt.VectorTile(new Protobuf(rawTileData)),
rawData: rawTileData,
cacheControl: null,
expires: null
});
}

const layerIndex = new StyleLayerIndex([{
id: 'test',
source: 'source',
'source-layer': 'test',
type: 'fill'
}]);

const source = new VectorTileWorkerSource(null, layerIndex, loadVectorData);

const sampleMarks = [100, 350];
const marks = {};
const measures = {};
t.stub(perf, 'getEntriesByName').callsFake((name) => { return measures[name] || []; });
t.stub(perf, 'mark').callsFake((name) => {
marks[name] = sampleMarks.shift();
return null;
});
t.stub(perf, 'measure').callsFake((name, start, end) => {
measures[name] = measures[name] || [];
measures[name].push({
duration: marks[end] - marks[start],
entryType: 'measure',
name: name,
startTime: marks[start]
});
return null;
});
t.stub(perf, 'clearMarks').callsFake(() => { return null; });
t.stub(perf, 'clearMeasures').callsFake(() => { return null; });

source.loadTile({
source: 'source',
uid: 0,
tileID: { overscaledZ: 0, wrap: 0, canonical: {x: 0, y: 0, z: 0, w: 0} },
request: { url: 'http://localhost:2900/faketile.pbf', collectResourceTiming: true }
}, (err, res) => {
t.false(err);
t.deepEquals(res.resourceTiming[0], {"duration": 250, "entryType": "measure", "name": "http://localhost:2900/faketile.pbf", "startTime": 100 }, 'resourceTiming resp is expected');
t.end();
});
});

0 comments on commit c654f50

Please sign in to comment.