From c654f506e59a90d3a75b7eee662735c4f0eead57 Mon Sep 17 00:00:00 2001 From: Tom Lee Date: Wed, 23 May 2018 14:38:44 -0400 Subject: [PATCH] unit tests for resource timing fallback method --- .../unit/source/geojson_worker_source.test.js | 32 +++++++++++ .../source/vector_tile_worker_source.test.js | 54 +++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/test/unit/source/geojson_worker_source.test.js b/test/unit/source/geojson_worker_source.test.js index e5256ba42e0..eda04e2d39f 100644 --- a/test/unit/source/geojson_worker_source.test.js +++ b/test/unit/source/geojson_worker_source.test.js @@ -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); diff --git a/test/unit/source/vector_tile_worker_source.test.js b/test/unit/source/vector_tile_worker_source.test.js index 90787a68bc7..9ec24190113 100644 --- a/test/unit/source/vector_tile_worker_source.test.js +++ b/test/unit/source/vector_tile_worker_source.test.js @@ -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(); + }); +}); \ No newline at end of file