|
| 1 | +const {Node, TreeBuilder} = require('../js/skew'); |
| 2 | +const should = require('chai').should(); |
| 3 | + |
| 4 | +// originally zipkin2.internal.NodeTest.java |
| 5 | +describe('Node', () => { |
| 6 | + it('should construct without a value', () => { |
| 7 | + const value = {traceId: '1', id: '1'}; |
| 8 | + const node = new Node(value); |
| 9 | + |
| 10 | + expect(node.value).to.equal(value); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should construct without a value', () => { |
| 14 | + const node = new Node(); |
| 15 | + |
| 16 | + should.equal(node.value, undefined); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should not allow setting an undefined value', () => { |
| 20 | + const node = new Node(); |
| 21 | + |
| 22 | + expect(() => node.setValue()).to.throw('newValue was undefined'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should not allow creating a cycle', () => { |
| 26 | + const fake = new Node(); |
| 27 | + |
| 28 | + expect(() => fake.addChild(fake)).to.throw('circular dependency on Node()'); |
| 29 | + |
| 30 | + const node = new Node({traceId: '1', id: '1'}); |
| 31 | + |
| 32 | + expect(() => node.addChild(node)) |
| 33 | + .to.throw('circular dependency on Node({"traceId":"1","id":"1"})'); |
| 34 | + }); |
| 35 | + |
| 36 | + /* |
| 37 | + * The following tree should traverse in alphabetical order |
| 38 | + * |
| 39 | + * a |
| 40 | + * / | \ |
| 41 | + * b c d |
| 42 | + * /|\ \ |
| 43 | + * e f g h |
| 44 | + */ |
| 45 | + it('should traverse breadth first', () => { |
| 46 | + const a = new Node('a'); |
| 47 | + const b = new Node('b'); |
| 48 | + const c = new Node('c'); |
| 49 | + const d = new Node('d'); |
| 50 | + // root(a) has children b, c, d |
| 51 | + a.addChild(b); |
| 52 | + a.addChild(c); |
| 53 | + a.addChild(d); |
| 54 | + const e = new Node('e'); |
| 55 | + const f = new Node('f'); |
| 56 | + const g = new Node('g'); |
| 57 | + // child(b) has children e, f, g |
| 58 | + b.addChild(e); |
| 59 | + b.addChild(f); |
| 60 | + b.addChild(g); |
| 61 | + const h = new Node('h'); |
| 62 | + // f has no children |
| 63 | + // child(g) has child h |
| 64 | + g.addChild(h); |
| 65 | + |
| 66 | + expect(a.traverse()).to.deep.equal([ |
| 67 | + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' |
| 68 | + ]); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('TreeBuilder', () => { |
| 73 | + // Makes sure that the trace tree is constructed based on parent-child, not by parameter order. |
| 74 | + it('should construct a trace tree', () => { |
| 75 | + const trace = [ |
| 76 | + {traceId: 'a', id: 'a'}, |
| 77 | + {traceId: 'a', parentId: 'a', id: 'b'}, |
| 78 | + {traceId: 'a', parentId: 'b', id: 'c'}, |
| 79 | + ]; |
| 80 | + |
| 81 | + const treeBuilder = new TreeBuilder({traceId: 'a'}); |
| 82 | + |
| 83 | + // TRACE is sorted with root span first, lets reverse them to make |
| 84 | + // sure the trace is stitched together by id. |
| 85 | + trace.slice(0).reverse().forEach((span) => treeBuilder.addNode(span.parentId, span.id, span)); |
| 86 | + |
| 87 | + const root = treeBuilder.build(); |
| 88 | + expect(root.value).to.equal(trace[0]); |
| 89 | + expect(root.children.map(n => n.value)).to.deep.equal([trace[1]]); |
| 90 | + |
| 91 | + const child = root.children[0]; |
| 92 | + expect(child.children.map(n => n.value)).to.deep.equal([trace[2]]); |
| 93 | + }); |
| 94 | + |
| 95 | + // input should be merged, but this ensures we are fine anyway |
| 96 | + it('should dedupe while constructing a trace tree', () => { |
| 97 | + const trace = [ |
| 98 | + {traceId: 'a', id: 'a'}, |
| 99 | + {traceId: 'a', id: 'a'}, |
| 100 | + {traceId: 'a', id: 'a'} |
| 101 | + ]; |
| 102 | + |
| 103 | + const treeBuilder = new TreeBuilder({traceId: 'a'}); |
| 104 | + trace.forEach((span) => treeBuilder.addNode(span.parentId, span.id, span)); |
| 105 | + const root = treeBuilder.build(); |
| 106 | + |
| 107 | + expect(root.value).to.equal(trace[0]); |
| 108 | + expect(root.children.length).to.equal(0); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should allocate spans missing parents to root', () => { |
| 112 | + const trace = [ |
| 113 | + {traceId: 'a', id: 'b'}, |
| 114 | + {traceId: 'a', parentId: 'b', id: 'c'}, |
| 115 | + {traceId: 'a', parentId: 'b', id: 'd'}, |
| 116 | + {traceId: 'a', id: 'e'}, |
| 117 | + {traceId: 'a', id: 'f'} |
| 118 | + ]; |
| 119 | + |
| 120 | + const treeBuilder = new TreeBuilder({traceId: 'a'}); |
| 121 | + trace.forEach((span) => treeBuilder.addNode(span.parentId, span.id, span)); |
| 122 | + const root = treeBuilder.build(); |
| 123 | + |
| 124 | + expect(root.traverse()).to.deep.equal(trace); |
| 125 | + expect(root.children.map(n => n.value)).to.deep.equal(trace.slice(1)); |
| 126 | + }); |
| 127 | + |
| 128 | + // spans are often reported depth-first, so it is possible to not have a root yet |
| 129 | + it('should construct a trace missing a root span', () => { |
| 130 | + const trace = [ |
| 131 | + {traceId: 'a', parentId: 'a', id: 'b'}, |
| 132 | + {traceId: 'a', parentId: 'a', id: 'c'}, |
| 133 | + {traceId: 'a', parentId: 'a', id: 'd'} |
| 134 | + ]; |
| 135 | + |
| 136 | + const treeBuilder = new TreeBuilder({traceId: 'a'}); |
| 137 | + trace.forEach((span) => treeBuilder.addNode(span.parentId, span.id, span)); |
| 138 | + const root = treeBuilder.build(); |
| 139 | + |
| 140 | + should.equal(root.value, undefined); |
| 141 | + expect(root.traverse()).to.deep.equal(trace); |
| 142 | + }); |
| 143 | + |
| 144 | + // input should be well formed, but this ensures we are fine anyway |
| 145 | + it('should skip on cycle', () => { |
| 146 | + const treeBuilder = new TreeBuilder({traceId: 'a'}); |
| 147 | + expect(treeBuilder.addNode('b', 'b', {traceId: 'a', parentId: 'b', id: 'b'})) |
| 148 | + .to.equal(false); |
| 149 | + }); |
| 150 | +}); |
0 commit comments