Skip to content

Commit

Permalink
Add isDataLoaded and tilecache rtree for testing
Browse files Browse the repository at this point in the history
(re: #2248, #5938, maybe others)
  • Loading branch information
bhousel committed Apr 8, 2019
1 parent 207cbd3 commit 2660a85
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 40 deletions.
31 changes: 18 additions & 13 deletions modules/services/osm.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var oauth = osmAuth({
});

var _blacklists = ['.*\.google(apis)?\..*/(vt|kh)[\?/].*([xyz]=.*){3}.*'];
var _tileCache = { loaded: {}, inflight: {}, seen: {} };
var _tileCache = { loaded: {}, inflight: {}, seen: {}, rtree: rbush() };
var _noteCache = { loaded: {}, inflight: {}, inflightPost: {}, note: {}, closed: {}, rtree: rbush() };
var _userCache = { toLoad: {}, user: {} };
var _changeset = {};
Expand Down Expand Up @@ -365,7 +365,7 @@ export default {
Object.values(_noteCache.inflightPost).forEach(abortRequest);
if (_changeset.inflight) abortRequest(_changeset.inflight);

_tileCache = { loaded: {}, inflight: {}, seen: {} };
_tileCache = { loaded: {}, inflight: {}, seen: {}, rtree: rbush() };
_noteCache = { loaded: {}, inflight: {}, inflightPost: {}, note: {}, closed: {}, rtree: rbush() };
_userCache = { toLoad: {}, user: {} };
_changeset = {};
Expand Down Expand Up @@ -801,6 +801,9 @@ export default {
delete _tileCache.inflight[tile.id];
if (!err) {
_tileCache.loaded[tile.id] = true;
var bbox = tile.extent.bbox();
bbox.id = tile.id;
_tileCache.rtree.insert(bbox);
}
if (callback) {
callback(err, Object.assign({ data: parsed }, tile));
Expand All @@ -819,6 +822,12 @@ export default {
},


isDataLoaded: function(loc) {
var bbox = { minX: loc[0], minY: loc[1], maxX: loc[0], maxY: loc[1] };
return _tileCache.rtree.collides(bbox);
},


// Load notes from the API in tiles
// GET /api/0.6/notes?bbox=
loadNotes: function(projection, noteOptions) {
Expand Down Expand Up @@ -992,32 +1001,28 @@ export default {
// This is used to save/restore the state when entering/exiting the walkthrough
// Also used for testing purposes.
caches: function(obj) {
function cloneDeep(source) {
return JSON.parse(JSON.stringify(source));
}

function cloneNoteCache(source) {
function cloneCache(source) {
var target = {};
Object.keys(source).forEach(function(k) {
if (k === 'rtree') {
target.rtree = rbush().fromJSON(source.rtree.toJSON());
target.rtree = rbush().fromJSON(source.rtree.toJSON()); // clone rbush
} else if (k === 'note') {
target.note = {};
Object.keys(source.note).forEach(function(id) {
target.note[id] = osmNote(source.note[id]);
target.note[id] = osmNote(source.note[id]); // copy notes
});
} else {
target[k] = cloneDeep(source[k]);
target[k] = JSON.parse(JSON.stringify(source[k])); // clone deep
}
});
return target;
}

if (!arguments.length) {
return {
tile: cloneDeep(_tileCache),
note: cloneNoteCache(_noteCache),
user: cloneDeep(_userCache)
tile: cloneCache(_tileCache),
note: cloneCache(_noteCache),
user: cloneCache(_userCache)
};
}

Expand Down
93 changes: 66 additions & 27 deletions test/spec/services/osm.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,62 @@ describe('iD.serviceOsm', function () {
});


describe('#loadTiles', function() {
var tileXML = '<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6">' +
' <bounds minlat="40.6681396" minlon="-74.0478516" maxlat="40.6723060" maxlon="-74.0423584"/>' +
' <node id="368395606" visible="true" version="3" changeset="28924294" timestamp="2015-02-18T04:25:04Z" user="peace2" uid="119748" lat="40.6694299" lon="-74.0444216">' +
' <tag k="addr:state" v="NJ"/>' +
' <tag k="ele" v="0"/>' +
' <tag k="gnis:county_name" v="Hudson"/>' +
' <tag k="gnis:feature_id" v="881377"/>' +
' <tag k="gnis:feature_type" v="Bay"/>' +
' <tag k="name" v="Upper Bay"/>' +
' <tag k="natural" v="bay"/>' +
' </node>' +
'</osm>';

beforeEach(function() {
var dimensions = [64, 64];
context.projection
.scale(iD.geoZoomToScale(20))
.translate([55212042.434589595, 33248879.510193843]) // -74.0444216, 40.6694299
.clipExtent([[0,0], dimensions]);
});

it('calls callback when data tiles are loaded', function() {
var spy = sinon.spy();
connection.loadTiles(context.projection, spy);

server.respondWith('GET', /map\?bbox/,
[200, { 'Content-Type': 'text/xml' }, tileXML]);
server.respond();

expect(spy).to.have.been.calledOnce;
});

it('#isDataLoaded', function() {
expect(connection.isDataLoaded([-74.0444216, 40.6694299])).to.be.not.ok;

connection.loadTiles(context.projection);
server.respondWith('GET', /map\?bbox/,
[200, { 'Content-Type': 'text/xml' }, tileXML]);
server.respond();

expect(connection.isDataLoaded([-74.0444216, 40.6694299])).to.be.ok;
});
});

describe('#loadEntity', function () {
var nodeXML = '<?xml version="1.0" encoding="UTF-8"?><osm>' +
'<node id="1" version="1" changeset="1" lat="0" lon="0" visible="true" timestamp="2009-03-07T03:26:33Z"></node>' +
'</osm>';
var wayXML = '<?xml version="1.0" encoding="UTF-8"?><osm>' +
'<node id="1" version="1" changeset="2817006" lat="0" lon="0" visible="true" timestamp="2009-10-11T18:03:23Z"/>' +
'<way id="1" visible="true" timestamp="2008-01-03T05:24:43Z" version="1" changeset="522559"><nd ref="1"/></way>' +
'</osm>';
var nodeXML = '<?xml version="1.0" encoding="UTF-8"?>' +
'<osm>' +
'<node id="1" version="1" changeset="1" lat="0" lon="0" visible="true" timestamp="2009-03-07T03:26:33Z"></node>' +
'</osm>';
var wayXML = '<?xml version="1.0" encoding="UTF-8"?>' +
'<osm>' +
'<node id="1" version="1" changeset="2817006" lat="0" lon="0" visible="true" timestamp="2009-10-11T18:03:23Z"/>' +
'<way id="1" visible="true" timestamp="2008-01-03T05:24:43Z" version="1" changeset="522559"><nd ref="1"/></way>' +
'</osm>';

beforeEach(function() {
server = sinon.fakeServer.create();
Expand Down Expand Up @@ -357,12 +405,14 @@ describe('iD.serviceOsm', function () {


describe('#loadEntityVersion', function () {
var nodeXML = '<?xml version="1.0" encoding="UTF-8"?><osm>' +
'<node id="1" version="1" changeset="1" lat="0" lon="0" visible="true" timestamp="2009-03-07T03:26:33Z"></node>' +
'</osm>';
var wayXML = '<?xml version="1.0" encoding="UTF-8"?><osm>' +
'<way id="1" visible="true" timestamp="2008-01-03T05:24:43Z" version="1" changeset="522559"><nd ref="1"/></way>' +
'</osm>';
var nodeXML = '<?xml version="1.0" encoding="UTF-8"?>' +
'<osm>' +
'<node id="1" version="1" changeset="1" lat="0" lon="0" visible="true" timestamp="2009-03-07T03:26:33Z"></node>' +
'</osm>';
var wayXML = '<?xml version="1.0" encoding="UTF-8"?>' +
'<osm>' +
'<way id="1" visible="true" timestamp="2008-01-03T05:24:43Z" version="1" changeset="522559"><nd ref="1"/></way>' +
'</osm>';

beforeEach(function() {
server = sinon.fakeServer.create();
Expand Down Expand Up @@ -539,21 +589,10 @@ describe('iD.serviceOsm', function () {

describe('#caches', function() {
it('loads reset caches', function (done) {
var resetCaches = {
tile: {
inflight: {}, loaded: {}, seen: {}
},
note: {
loaded: {}, inflight: {}, inflightPost: {}, note: {} // not including rtree
},
user: {
toLoad: {}, user: {}
}
};
var caches = connection.caches();
expect(caches.tile).to.eql(resetCaches.tile);
expect(caches.note.loaded).to.eql(resetCaches.note.loaded);
expect(caches.user).to.eql(resetCaches.user);
expect(caches.tile).to.have.all.keys(['loaded','inflight','seen','rtree']);
expect(caches.note).to.have.all.keys(['loaded','inflight','inflightPost','note','closed','rtree']);
expect(caches.user).to.have.all.keys(['toLoad','user']);
done();
});

Expand Down

0 comments on commit 2660a85

Please sign in to comment.