Repository navigation
Expand file tree
/
Copy pathdotify-test.js
More file actions
419 lines (364 loc) · 15.2 KB
/
Copy pathdotify-test.js
File metadata and controls
419 lines (364 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* Copyright 2013 LinkedIn, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
var assert = require('./assert'),
dotify = require('../../lib/trace/dotify'),
CDigraph = require('graphlib').CDigraph;
describe('dotify', function() {
describe('escapes', function() {
['<', '>', '|', '{', '}'].forEach(function(specialChar) {
it('escapes `' + specialChar + '` for the Mrecord shape', function() {
var g = new CDigraph();
g.addNode(1, {
name: 'before' + specialChar + 'after',
startNanos: 0,
endNanos: 0
});
assert.include(dotify(g).node(1).label, 'before\\' + specialChar + 'after');
});
});
});
describe('style', function() {
it('styles nodes', function() {
var g = new CDigraph();
g.addNode(1, {
name: 'foo task',
startNanos: 0,
pendingNanos: 250 * 1000 * 1000,
endNanos: 500 * 1000 * 1000
});
var d = dotify(g);
assert.equal(d.node(1).label, 'foo task\\l|{@0.000\\r|250.000\\r|+500.000\\r}');
assert.equal(d.node(1).shape, 'Mrecord');
assert.equal(d.node(1).style, 'filled');
});
it('styles composed tasks', function() {
var g = new CDigraph();
g.addNode('parent', {
name: 'parent task',
startNanos: 0,
pendingNanos: 250 * 1000 * 1000,
endNanos: 500 * 1000 * 1000
});
g.addNode('child', {
startNanos: 300 * 1000 * 1000,
pendingNanos: 400 * 1000 * 1000,
endNanos: 450 * 1000 * 1000
});
g.parent('child', 'parent');
var d = dotify(g);
assert.propertyVal(d.node('cluster_parent'), 'label', 'parent task (@0.000, 250.000, +500.000)');
assert.propertyVal(d.node('cluster_parent'), 'labeljust', 'l');
assert.propertyVal(d.node('cluster_parent'), 'style', 'dashed');
assert.propertyVal(d.node('cluster_parent'), 'color', '#aaaaaa');
});
it('styles source nodes', function() {
var g = new CDigraph();
g.addNode('parent');
g.addNode('child');
g.parent('child', 'parent');
var d = dotify(g);
assert.propertyVal(d.node('source_parent'), 'shape', 'circle');
assert.propertyVal(d.node('source_parent'), 'style', 'filled');
assert.propertyVal(d.node('source_parent'), 'label', ' ');
});
it('styles sink nodes', function() {
var g = new CDigraph();
g.addNode('parent');
g.addNode('child');
g.parent('child', 'parent');
var d = dotify(g);
assert.propertyVal(d.node('sink_parent'), 'shape', 'doublecircle');
assert.propertyVal(d.node('sink_parent'), 'style', 'filled');
assert.propertyVal(d.node('sink_parent'), 'label', ' ');
});
describe('colors', function() {
var COLOR_MAP = {
SUCCESS: '#e0ffe0',
ERROR: '#ffe0e0',
EARLY_FINISH: '#fffacd',
ALL_OTHERS: '#aaaaaa'
};
Object.keys(COLOR_MAP).forEach(function(type) {
it('colors a ' + type + ' task ' + COLOR_MAP[type], function() {
var g = new CDigraph();
g.addNode(1, {
resultType: type
});
assert.equal(dotify(g).node(1).fillcolor, COLOR_MAP[type]);
});
it('colors a ' + type + ' sink node ' + COLOR_MAP[type], function() {
var g = new CDigraph();
g.addNode('parent', { resultType: type });
g.addNode('child');
g.parent('child', 'parent');
assert.equal(dotify(g).node('sink_parent').fillcolor, COLOR_MAP[type]);
});
});
});
});
describe('handles edges', function() {
var g,
root = 'root',
rootCluster = 'cluster_' + root,
rootSource = 'source_' + root,
rootSink = 'sink_' + root;
beforeEach(function() {
g = new CDigraph();
g.addNode(root, { startNanos: 0, pendingNanos: 0, endNanos: 0 });
});
it('adds source and sink nodes to composed tasks', function() {
g.addNode('t1');
g.parent('t1', root);
assert.sameMembers(dotify(g).children(rootCluster),
[rootSource, 't1', rootSink]);
});
it('adds a solid edge from the source node to source tasks', function() {
g.addNode('t1');
g.parent('t1', root);
assertEdgesFrom(dotify(g), rootSource, { t1: 'solid' });
});
it('does not add any edges to the source node', function() {
g.addNode('t1');
g.parent('t1', root);
assert.lengthOf(dotify(g).predecessors(rootSource), 0);
});
it('adds a solid edge from sink tasks to the sink node', function() {
g.addNode('t1');
g.parent('t1', root);
assertEdgesFrom(dotify(g), 't1', { 'sink_root': 'solid' });
});
it('adds a dashed edge from EARLY_FINISH sink tasks to the sink node', function() {
g.addNode('t1', { resultType: 'EARLY_FINISH' });
g.parent('t1', root);
assertEdgesFrom(dotify(g), 't1', { 'sink_root': 'dashed' });
});
it('adds a dashed edge from EARLY_FINISH sink nodes to successor tasks', function() {
g.parent(g.addNode('parent', { resultType: 'EARLY_FINISH' }), root);
g.parent(g.addNode('t1'), 'parent');
g.parent(g.addNode('t2'), root);
g.addEdge(null, 'parent', 't2', { relationship: 'SUCCESSOR' });
assertEdgesFrom(dotify(g), 'sink_parent', { 't2': 'dashed' });
});
it('does not add any edges from the sink node', function() {
g.addNode('t1');
g.parent('t1', root);
assert.lengthOf(dotify(g).successors(rootSink), 0);
});
it('adds a solid edge between nodes with a successor relationship', function() {
g.parent(g.addNode('t1'), root);
g.parent(g.addNode('t2'), root);
g.addEdge(null, 't1', 't2');
assertEdgesFrom(dotify(g), 't1', { t2: 'solid' });
});
it('does not add an edge to the sink node from a non-sink target', function() {
g.addNode('t1');
g.parent('t1', root);
g.addNode('t2');
g.parent('t2', root);
g.addEdge(null, 't1', 't2');
assert.notInclude(dotify(g).predecessors(rootSink), ['t1']);
});
it('does not add an edge from the source node to a non-source target', function() {
g.addNode('t1');
g.parent('t1', root);
g.addNode('t2');
g.parent('t2', root);
g.addEdge(null, 't1', 't2');
assert.notInclude(dotify(g).successors(rootSource), ['t2']);
});
it('points edges that end in a compound task at the task\'s source', function() {
g.addNode('t1');
g.addNode('t2');
g.parent('t2', 'root');
g.addEdge(null, 't1', 'root');
assertEdgesFrom(dotify(g), 't1', { source_root: 'solid' });
});
it('starts edges that start at a compound task from the task\'s sink', function() {
g.addNode('t1');
g.addNode('t2');
g.parent('t2', 'root');
g.addEdge(null, 'root', 't1');
assertEdgesFrom(dotify(g), rootSink, { t1: 'solid' });
});
it('creates a dashed line for potential successor edges', function() {
g.addNode('t1');
g.addNode('t2');
g.addEdge(null, 't1', 't2', { relationship: 'POTENTIAL_SUCCESSOR' });
assertEdgesFrom(dotify(g), 't1', { t2: 'dashed' });
});
it('creates a dashed edge from a potential parent\'s source if there is no path from it\'s children to a task', function() {
g.parent(g.addNode('t1'), root);
g.addNode('pp');
g.parent(g.addNode('t2'), 'pp');
g.addEdge(null, 't1', 'pp', { relationship: 'POTENTIAL_PARENT' });
assertEdge(dotify(g), 'source_pp', 't1', 'dashed');
});
it('creates a solid edge to a potential parent\'s sink if there is no path to it\'s children from a task', function() {
g.parent(g.addNode('t1'), root);
g.addNode('pp');
g.parent(g.addNode('t2'), 'pp');
g.addEdge(null, 't1', 'pp', { relationship: 'POTENTIAL_PARENT' });
assertEdge(dotify(g), 't1', 'sink_pp', 'solid');
});
it('creates a dashed edge to a potential parent\'s sink if the task\'s result was EARLY_FINISH', function() {
g.parent(g.addNode('t1', { resultType: 'EARLY_FINISH' }), root);
g.addNode('pp');
g.parent(g.addNode('t2'), 'pp');
g.addEdge(null, 't1', 'pp', { relationship: 'POTENTIAL_PARENT' });
assertEdge(dotify(g), 't1', 'sink_pp', 'dashed');
});
it('creates an invisible edge if the path to a task\'s closest sibling involves a non-sibling node', function() {
g.parent(g.addNode('t1'), root);
g.parent(g.addNode('t2'), root);
g.addNode('p1');
g.parent(g.addNode('t3'), 'p1');
g.addEdge(null, 't1', 't3');
g.addEdge(null, 't3', 't2');
var d = dotify(g);
assert.includeMembers(d.successors('t1'), ['t2']);
assertEdge(d, 't1', 't2', 'invis');
});
it('creates edges with 2 level nested task', function() {
g.parent(g.addNode('t1'), root);
g.parent(g.addNode('t3'), root);
g.parent(g.addNode('parent'), root);
g.parent(g.addNode('t2'), 'parent');
g.addEdge(null, 't1', 'parent');
g.addEdge(null, 'parent', 't3');
var d = dotify(g);
assertEdgesFrom(d, rootSource, { t1: 'solid' });
assertEdgesFrom(d, 't1', { source_parent: 'solid' });
assertEdgesFrom(d, 'source_parent', { t2: 'solid' });
assertEdgesFrom(d, 't2', { 'sink_parent': 'solid' });
assertEdgesFrom(d, 'sink_parent', { 't3': 'solid' });
assertEdgesFrom(d, 't3', { sink_root: 'solid' });
});
it('creates an edge to the source of an implicitly sequenced nested task', function() {
g.parent(g.addNode('parent'), root);
g.parent(g.addNode('t1'), 'parent');
assertEdgesFrom(dotify(g), rootSource, { source_parent: 'solid' });
});
it('creates an edge from the sink of an implicitly sequenced nested task', function() {
g.parent(g.addNode('parent'), root);
g.parent(g.addNode('t1'), 'parent');
assertEdgesFrom(dotify(g), 'sink_parent', { sink_root: 'solid' });
});
it('creates an invisible edge from a source to child with a shortest path through another parent', function() {
g.parent(g.addNode('p1'), root);
g.parent(g.addNode('t1'), 'p1');
g.parent(g.addNode('p2'), root);
g.parent(g.addNode('t2'), 'p2');
g.addEdge(null, 't2', 't1');
g.addEdge(null, 't2', 'p1', { relationship: 'POTENTIAL_PARENT' });
assertEdgesFrom(dotify(g), 'source_p1', { t1: 'invis', t2: 'dashed' });
});
it('creates an invisible edge to a sink from a child with a shortest path through another parent', function() {
g.parent(g.addNode('p1'), root);
g.parent(g.addNode('t1'), 'p1');
g.parent(g.addNode('p2'), root);
g.parent(g.addNode('t2'), 'p2');
g.addEdge(null, 't1', 't2');
g.addEdge(null, 't2', 'p1', { relationship: 'POTENTIAL_PARENT' });
assertEdgesFrom(dotify(g), 't1', { sink_p1: 'invis', t2: 'solid' });
});
it('handles cases where a potential parent has no actual children', function() {
// In this case the potential parent should still get a source and a sink node.
g.parent(g.addNode('p1'), root);
g.parent(g.addNode('t1'), root);
g.addEdge(null, 't1', 'p1', { relationship: 'POTENTIAL_PARENT' });
var d = dotify(g);
assert.isTrue(d.hasNode('source_p1'));
assert.isTrue(d.hasNode('sink_p1'));
assertEdgesFrom(d, 'source_p1', { t1: 'dashed', sink_p1: 'invis' });
assertEdge(d, 't1', 'sink_p1', 'solid');
});
it('passes old graphviz test for possible predecessors', function() {
g.parent(g.addNode('t1'), root);
g.parent(g.addNode('t2'), root);
g.parent(g.addNode('t3'), root);
g.addEdge(null, 't1', 't2');
g.addEdge(null, 't2', 't3');
g.addEdge(null, 't1', 't3', { relationship: 'POTENTIAL_SUCCESSOR' });
var d = dotify(g);
assertEdgesFrom(d, rootSource, { t1: 'solid' });
assertEdgesFrom(d, 't1', { t2: 'solid', t3: 'dashed' });
assertEdgesFrom(d, 't2', { t3: 'solid' });
assertEdgesFrom(d, 't3', { sink_root: 'solid' });
});
it('passes old graphviz test for potential parents', function() {
g.parent(g.addNode('p1'), root);
g.parent(g.addNode('t1'), 'p1');
g.parent(g.addNode('t2'), 'p1');
g.parent(g.addNode('p2'), root);
g.parent(g.addNode('t3'), 'p2');
g.parent(g.addNode('t4'), 'p2');
g.addEdge(null, 't1', 't2');
g.addEdge(null, 't3', 't4');
g.addEdge(null, 't4', 'p1', { relationship: 'POTENTIAL_PARENT' });
var d = dotify(g);
assertEdgesFrom(d, rootSource, { source_p1: 'solid', source_p2: 'solid' });
assertEdgesFrom(d, 'source_p1', { t1: 'solid', t4: 'dashed' });
assertEdgesFrom(d, 't1', { t2: 'solid' });
assertEdgesFrom(d, 't2', { sink_p1: 'solid' });
assertEdgesFrom(d, 'sink_p1', { sink_root: 'solid' });
assertEdgesFrom(d, 'source_p2', { t3: 'solid' });
assertEdgesFrom(d, 't3', { t4: 'solid' });
assertEdgesFrom(d, 't4', { sink_p1: 'solid', sink_p2: 'solid' });
assertEdgesFrom(d, 'sink_p2', { sink_root: 'solid' });
});
it('passes old graphviz test for potential parent with possible predecessor', function() {
g.parent(g.addNode('p1'), root);
g.parent(g.addNode('t1'), 'p1');
g.parent(g.addNode('t2'), 'p1');
g.parent(g.addNode('p2'), root);
g.parent(g.addNode('t3'), 'p2');
g.parent(g.addNode('t4'), 'p2');
g.addEdge(null, 't1', 't2');
g.addEdge(null, 't3', 't4');
g.addEdge(null, 't2', 't4', { relationship: 'POTENTIAL_SUCCESSOR' });
g.addEdge(null, 't4', 'p1', { relationship: 'POTENTIAL_PARENT' });
var d = dotify(g);
assertEdgesFrom(d, rootSource, { source_p1: 'solid', source_p2: 'solid' });
assertEdgesFrom(d, 'source_p1', { t1: 'solid' });
assertEdgesFrom(d, 't1', { t2: 'solid' });
assertEdgesFrom(d, 't2', { sink_p1: 'invis', t4: 'dashed' });
assertEdgesFrom(d, 'sink_p1', { sink_root: 'solid' });
assertEdgesFrom(d, 'source_p2', { t3: 'solid' });
assertEdgesFrom(d, 't3', { t4: 'solid' });
assertEdgesFrom(d, 't4', { sink_p1: 'solid', sink_p2: 'solid' });
assertEdgesFrom(d, 'sink_p2', { sink_root: 'solid' });
});
});
});
/*
* Asserts that the only edges from the given node are included in the expected
* map. The expected map provides a mapping between expected target and
* the expected style.
*/
function assertEdgesFrom(g, u, expected) {
assert.sameMembers(g.successors(u), Object.keys(expected));
Object.keys(expected).forEach(function(v) {
assertEdge(g, u, v, expected[v]);
});
}
/*
* Asserts that there is an edge from u to v and that it has the given style.
*/
function assertEdge(g, u, v, style) {
assert.lengthOf(g.outEdges(u, v), 1);
var e = g.outEdges(u, v)[0];
assert.propertyVal(g.edge(e), 'style', style, 'Edge ' + u + ' -> ' + v);
}