Skip to content

Commit

Permalink
Fix circular references in JSON test log serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiosa committed Mar 4, 2024
1 parent 52ef815 commit 47419a0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
2 changes: 0 additions & 2 deletions src/tiledimage.js
Expand Up @@ -2107,8 +2107,6 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag
}

this._setTileLoaded(tile, data, null, tileRequest, dataType);

//TODO aiosa missing timeout might damage the cache system
},

/**
Expand Down
36 changes: 29 additions & 7 deletions test/helpers/test.js
Expand Up @@ -180,22 +180,44 @@
}
};

// OSD has circular references, if a console log tries to serialize
// certain object, remove these references from a clone (do not delete prop
// on the original object).
// NOTE: this does not work if someone replaces the original class with
// a mock object! Try to mock functions only, or ensure mock objects
// do not hold circular references.
const circularOSDReferences = {
'Tile': 'tiledImage',
'World': 'viewer',
'DrawerBase': ['viewer', 'viewport'],
'CanvasDrawer': ['viewer', 'viewport'],
'WebGLDrawer': ['viewer', 'viewport'],
'TiledImage': ['viewer', '_drawer'],
};
for ( var i in testLog ) {
if ( testLog.hasOwnProperty( i ) && testLog[i].push ) {
//Tile.tiledImage creates circular reference, copy object to avoid and allow JSON serialization
const tileCircularStructureReplacer = function (key, value) {
if (value instanceof OpenSeadragon.Tile) {
var instance = {};
Object.assign(instance, value);
delete value.tiledImage;
// Circular reference removal
const osdCircularStructureReplacer = function (key, value) {
for (let ClassType in circularOSDReferences) {
if (value instanceof OpenSeadragon[ClassType]) {
const instance = {};
Object.assign(instance, value);

let circProps = circularOSDReferences[ClassType];
if (!Array.isArray(circProps)) circProps = [circProps];
for (let prop of circProps) {
instance[prop] = '__circular_reference__';
}
return instance;
}
}
return value;
};

testConsole[i] = ( function ( arr ) {
return function () {
var args = Array.prototype.slice.call( arguments, 0 ); // Coerce to true Array
arr.push( JSON.stringify( args, tileCircularStructureReplacer ) ); // Store as JSON to avoid tedious array-equality tests
arr.push( JSON.stringify( args, osdCircularStructureReplacer ) ); // Store as JSON to avoid tedious array-equality tests
};
} )( testLog[i] );

Expand Down
27 changes: 10 additions & 17 deletions test/modules/tilesource-dynamic-url.js
Expand Up @@ -8,7 +8,7 @@
var DYNAMIC_URL = "";
var viewer = null;
var OriginalAjax = OpenSeadragon.makeAjaxRequest;
var OriginalTile = OpenSeadragon.Tile;
var OriginalTileGetUrl = OpenSeadragon.Tile.prototype.getUrl;
// These variables allow tracking when the first request for data has finished
var firstUrlPromise = null;
var isFirstUrlPromiseResolved = false;
Expand Down Expand Up @@ -115,22 +115,15 @@
return request;
};

// Override Tile to ensure getUrl is called successfully.
var Tile = function(...params) {
OriginalTile.apply(this, params);
};

OpenSeadragon.extend( Tile.prototype, OpenSeadragon.Tile.prototype, {
getUrl: function() {
// if ASSERT is still truthy, call ASSERT.ok. If the viewer
// has already been destroyed and ASSERT has set to null, ignore this
if(ASSERT){
ASSERT.ok(true, 'Tile.getUrl called');
}
return OriginalTile.prototype.getUrl.apply(this);
// Override Tile::getUrl to ensure getUrl is called successfully.
OpenSeadragon.Tile.prototype.getUrl = function () {
// if ASSERT is still truthy, call ASSERT.ok. If the viewer
// has already been destroyed and ASSERT has set to null, ignore this
if (ASSERT) {
ASSERT.ok(true, 'Tile.getUrl called');
}
});
OpenSeadragon.Tile = Tile;
return OriginalTileGetUrl.apply(this, arguments);
};
},

afterEach: function () {
Expand All @@ -143,7 +136,7 @@
viewer = null;

OpenSeadragon.makeAjaxRequest = OriginalAjax;
OpenSeadragon.Tile = OriginalTile;
OpenSeadragon.Tile.prototype.getUrl = OriginalTileGetUrl;
}
});

Expand Down

0 comments on commit 47419a0

Please sign in to comment.