Skip to content

Commit

Permalink
tests: better fatal trace logic error coverage (#7959)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Apr 4, 2019
1 parent b8ac221 commit c140802
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lighthouse-core/computed/main-thread-tasks.js
Expand Up @@ -148,11 +148,16 @@ class MainThreadTasks {

/**
* @param {TaskNode} task
* @param {TaskNode|undefined} parent
* @return {number}
*/
static _computeRecursiveSelfTime(task) {
static _computeRecursiveSelfTime(task, parent) {
if (parent && task.endTime > parent.endTime) {
throw new Error('Fatal trace logic error - child cannot end after parent');
}

const childTime = task.children
.map(MainThreadTasks._computeRecursiveSelfTime)
.map(child => MainThreadTasks._computeRecursiveSelfTime(child, task))
.reduce((sum, child) => sum + child, 0);
task.duration = task.endTime - task.startTime;
task.selfTime = task.duration - childTime;
Expand Down Expand Up @@ -237,7 +242,7 @@ class MainThreadTasks {
for (const task of tasks) {
if (task.parent) continue;

MainThreadTasks._computeRecursiveSelfTime(task);
MainThreadTasks._computeRecursiveSelfTime(task, undefined);
MainThreadTasks._computeRecursiveAttributableURLs(task, [], priorTaskData);
MainThreadTasks._computeRecursiveTaskGroup(task);
}
Expand Down
41 changes: 41 additions & 0 deletions lighthouse-core/test/computed/main-thread-tasks-test.js
Expand Up @@ -195,4 +195,45 @@ describe('MainResource computed artifact', () => {
group: taskGroups.other,
});
});

const invalidEventSets = [
[
// TaskA overlaps with TaskB, X first
{ph: 'X', name: 'TaskA', pid, tid, ts: baseTs, dur: 100e3, args},
{ph: 'B', name: 'TaskB', pid, tid, ts: baseTs + 5e3, args},
{ph: 'E', name: 'TaskB', pid, tid, ts: baseTs + 115e3, args},
],
[
// TaskA overlaps with TaskB, B first
{ph: 'B', name: 'TaskA', pid, tid, ts: baseTs, args},
{ph: 'X', name: 'TaskB', pid, tid, ts: baseTs + 5e3, dur: 100e3, args},
{ph: 'E', name: 'TaskA', pid, tid, ts: baseTs + 90e3, args},
],
[
// TaskA is missing a B event
{ph: 'E', name: 'TaskA', pid, tid, ts: baseTs, args},
{ph: 'B', name: 'TaskB', pid, tid, ts: baseTs + 5e3, args},
{ph: 'E', name: 'TaskB', pid, tid, ts: baseTs + 115e3, args},
],
[
// TaskB is missing a B event after an X
{ph: 'X', name: 'TaskA', pid, tid, ts: baseTs, dur: 100e3, args},
{ph: 'E', name: 'TaskB', pid, tid, ts: baseTs + 10e3, args},
],
];

for (const invalidEvents of invalidEventSets) {
it('should throw on invalid task input', async () => {
const traceEvents = [
...boilerplateTrace,
...invalidEvents,
];

traceEvents.forEach(evt => Object.assign(evt, {cat: 'devtools.timeline'}));

const context = {computedCache: new Map()};
const promise = MainThreadTasks.request({traceEvents}, context);
await expect(promise).rejects.toBeTruthy();
});
}
});

0 comments on commit c140802

Please sign in to comment.