MCPcopy Index your code
hub / github.com/nodejs/node / validateByRetainingPathFromNodes

Function validateByRetainingPathFromNodes

test/common/heap.js:241–297  ·  view source on GitHub ↗

* A alternative heap snapshot validator that can be used to verify cppgc-managed nodes. * Modified from * https://chromium.googlesource.com/v8/v8/+/b00e995fb212737802810384ba2b868d0d92f7e5/test/unittests/heap/cppgc-js/unified-heap-snapshot-unittest.cc#134 * @param {object[]} nodes Snapshot nodes

(nodes, rootName, retainingPath, allowEmpty = false)

Source from the content-addressed store, hash-verified

239 * assertion error.
240 */
241function validateByRetainingPathFromNodes(nodes, rootName, retainingPath, allowEmpty = false) {
242 let haystack = nodes.filter((n) => n.name === rootName && n.type !== 'string');
243
244 for (let i = 0; i < retainingPath.length; ++i) {
245 const expected = retainingPath[i];
246 const newHaystack = [];
247
248 for (const parent of haystack) {
249 for (let j = 0; j < parent.outgoingEdges.length; j++) {
250 const edge = parent.outgoingEdges[j];
251 // The strings are represented as { type: 'string', name: '<string content>' } in the snapshot.
252 // Ignore them or we'll poke into strings that are just referenced as names of real nodes,
253 // unless the caller is specifically looking for string nodes via `node_type`.
254 let match = (edge.to.type !== 'string');
255 if (expected.node_type) {
256 match = (edge.to.type === expected.node_type);
257 }
258 if (expected.node_name && edge.to.name !== expected.node_name) {
259 match = false;
260 }
261 if (expected.edge_name && edge.name !== expected.edge_name) {
262 match = false;
263 }
264 if (expected.edge_type && edge.type !== expected.type) {
265 match = false;
266 }
267 if (match) {
268 newHaystack.push(edge.to);
269 }
270 }
271 }
272
273 if (newHaystack.length === 0) {
274 if (allowEmpty) {
275 return [];
276 }
277 const format = (val) => util.inspect(val, { breakLength: 128, depth: 3 });
278 console.error('#');
279 console.error('# Retaining path to search for:');
280 for (let j = 0; j < retainingPath.length; ++j) {
281 console.error(`# - '${format(retainingPath[j])}'${i === j ? '\t<--- not found' : ''}`);
282 }
283 console.error('#\n');
284 console.error('# Nodes found in the last step include:');
285 for (let j = 0; j < haystack.length; ++j) {
286 console.error(`# - '${format(haystack[j])}`);
287 }
288
289 assert.fail(`Could not find target edge ${format(expected)} in the heap snapshot.`);
290
291 }
292
293 haystack = newHaystack;
294 }
295
296 return haystack;
297}
298

Calls 5

formatFunction · 0.70
filterMethod · 0.65
pushMethod · 0.45
errorMethod · 0.45
failMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…