Skip to content

Commit

Permalink
Start adding tests (SourceCache, Tile, util.parseCacheControl)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauren Budorick committed Jan 20, 2017
1 parent 4273e76 commit e1bc72b
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
70 changes: 70 additions & 0 deletions test/js/source/source_cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ function MockSourceType(id, sourceOptions, _dispatcher, eventedParent) {
this.setEventedParent(eventedParent);
}
loadTile(tile, callback) {
if (sourceOptions.expires) {
tile.setExpiryData({
expires: sourceOptions.expires
});
}
setTimeout(callback, 0);
}
onAdd() {
Expand Down Expand Up @@ -112,6 +117,52 @@ test('SourceCache#addTile', (t) => {
t.end();
});

t.test('moves timers when adding tile from cache', (t) => {
const coord = new TileCoord(0, 0, 0);
const time = new Date();
time.setSeconds(time.getSeconds() + 5);

const sourceCache = createSourceCache();
sourceCache._setTimers = (id) => {
sourceCache._timers[id] = setTimeout(() => {}, 0);
};
sourceCache._setCacheTimers = (id) => {
sourceCache._cacheTimers[id] = setTimeout(() => {}, 0);
};
sourceCache.loadTile = (tile, callback) => {
tile.state = 'loaded';
tile.getExpiry = () => time;
sourceCache._setTimers(coord.id, tile);
callback();
};

const tr = new Transform();
tr.width = 512;
tr.height = 512;
sourceCache.updateCacheSize(tr);

const id = coord.id;
t.notOk(sourceCache._timers[id]);
t.notOk(sourceCache._cacheTimers[id]);

sourceCache.addTile(coord);

t.ok(sourceCache._timers[id]);
t.notOk(sourceCache._cacheTimers[id]);

sourceCache.removeTile(coord.id);

t.notOk(sourceCache._timers[id]);
t.ok(sourceCache._cacheTimers[id]);

sourceCache.addTile(coord);

t.ok(sourceCache._timers[id]);
t.notOk(sourceCache._cacheTimers[id]);

t.end();
});

t.test('reuses wrapped tile', (t) => {
const coord = new TileCoord(0, 0, 0);
let load = 0,
Expand Down Expand Up @@ -777,3 +828,22 @@ test('SourceCache#reload', (t) => {

t.end();
});

test('SourceCache reloads expiring tiles', (t) => {
t.test('calls reloadTile when tile expires', (t) => {
const coord = new TileCoord(1, 0, 0);

const expiryDate = new Date();
expiryDate.setMilliseconds(expiryDate.getMilliseconds() + 5);
const sourceCache = createSourceCache({ expires: expiryDate });

sourceCache.reloadTile = (id, state) => {
t.equal(state, 'expired');
t.end();
};

sourceCache.addTile(coord);
});

t.end();
});
48 changes: 48 additions & 0 deletions test/js/source/tile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,54 @@ test('querySourceFeatures', (t) => {
t.end();
});

test('expiring tiles', (t) => {
t.test('regular tiles do not expire', (t) => {
const tile = new Tile(new TileCoord(1, 1, 1));
tile.state = 'loaded';
tile.timeAdded = Date.now();

t.notOk(tile.cacheControl);
t.notOk(tile.expires);

t.end();
});

t.test('set, get expiry', (t) => {
const tile = new Tile(new TileCoord(1, 1, 1));
tile.state = 'loaded';
tile.timeAdded = Date.now();

t.notOk(tile.cacheControl, 'no cache-control set');
t.notOk(tile.expires, 'no expires set');

tile.setExpiryData({
cacheControl: 'max-age=60'
});

t.equal(tile.cacheControl, 'max-age=60', 'set cache-control');
t.equal(tile.getExpiry(), tile.timeAdded + 60000, 'cache-control parsed as expected');

const date = new Date();
date.setMinutes(date.getMinutes() + 10);
date.setMilliseconds(0);

tile.setExpiryData({
expires: date.toString()
});

// this shouldn't happen, but if both expiry data are set, cacheControl takes precedence
t.equal(tile.getExpiry(), tile.timeAdded + 60000, 'cache-control takes precedence over expires');

delete tile.cacheControl;

t.equal(tile.getExpiry(), date.getTime(), 'expires header set date as expected');

t.end();
});

t.end();
});

function createRawTileData() {
return fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf'));
}
Expand Down
20 changes: 20 additions & 0 deletions test/js/util/lru_cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ test('LRUCache - duplicate add', (t) => {
t.end();
});

test('LRUCache - remove', (t) => {
const cache = new LRUCache(10, () => {});

cache.add('washington', 'dc');
cache.add('baltimore', 'md');
cache.add('richmond', 'va');

t.deepEqual(cache.keys(), ['washington', 'baltimore', 'richmond']);
t.ok(cache.has('baltimore'));

cache.remove('baltimore');

t.deepEqual(cache.keys(), ['washington', 'richmond']);
t.notOk(cache.has('baltimore'));

t.ok(cache.remove('baltimore'));

t.end();
});

test('LRUCache - overflow', (t) => {
const cache = new LRUCache(1, (removed) => {
t.equal(removed, 'b');
Expand Down
33 changes: 33 additions & 0 deletions test/js/util/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,38 @@ test('util', (t) => {
t.end();
});

t.test('parseCacheControl', (t) => {
t.test('max-age', (t) => {
t.deepEqual(util.parseCacheControl('max-age=123456789'), {
'max-age': 123456789
}, 'returns valid max-age header');

t.deepEqual(util.parseCacheControl('max-age=1000'), {
'max-age': 1000
}, 'returns valid max-age header');

t.deepEqual(util.parseCacheControl('max-age=null'), {}, 'does not return invalid max-age header');

t.end();
});

t.test('s-maxage', (t) => {
t.deepEqual(util.parseCacheControl('s-maxage=200'), {
's-maxage': 200
}, 'returns valid s-maxage header');

t.deepEqual(util.parseCacheControl('max-age=43200,s-maxage=300'), {
'max-age': 43200,
's-maxage': 300
}, 'returns valid headers');

t.deepEqual(util.parseCacheControl('s-maxage=something'), {}, 'does not return invalid s-maxage header');

t.end();
});

t.end();
});

t.end();
});

0 comments on commit e1bc72b

Please sign in to comment.