This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(trackingZone): Keep track of tasks to see outstanding tasks.
- Loading branch information
Showing
4 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* A `TaskTrackingZoneSpec` allows one to track all outstanding Tasks. | ||
* | ||
* This is useful in tests. For example to see which tasks are preventing a test from completing | ||
* or an automated way of releasing all of the event listeners at the end of the test. | ||
*/ | ||
class TaskTrackingZoneSpec implements ZoneSpec { | ||
name = 'TaskTrackingZone'; | ||
microTasks: Task[] = []; | ||
macroTasks: Task[] = []; | ||
eventTasks: Task[] = []; | ||
properties: {[key: string]: any} = {'TaskTrackingZone': this}; | ||
|
||
static get() { | ||
return Zone.current.get('TaskTrackingZone'); | ||
} | ||
|
||
private getTasksFor(type: string): Task [] { | ||
switch (type) { | ||
case 'microTask': return this.microTasks; | ||
case 'macroTask': return this.macroTasks; | ||
case 'eventTask': return this.eventTasks; | ||
} | ||
throw new Error('Unknown task format: ' + type); | ||
} | ||
|
||
onScheduleTask(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): Task { | ||
task['creationLocation'] = new Error(`Task '${task.type}' from '${task.source}'.`); | ||
const tasks = this.getTasksFor(task.type); | ||
tasks.push(task); | ||
return parentZoneDelegate.scheduleTask(targetZone, task); | ||
} | ||
|
||
onCancelTask(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): any { | ||
const tasks = this.getTasksFor(task.type); | ||
for(var i = 0; i < tasks.length; i++) { | ||
if (tasks[i] == task) { | ||
tasks.splice(i, 1); | ||
break; | ||
} | ||
} | ||
return parentZoneDelegate.cancelTask(targetZone, task); | ||
} | ||
|
||
onInvokeTask(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, | ||
task: Task, applyThis: any, applyArgs: any): any | ||
{ | ||
if (task.type === 'eventTask') return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs); | ||
const tasks = this.getTasksFor(task.type); | ||
for(var i = 0; i < tasks.length; i++) { | ||
if (tasks[i] == task) { | ||
tasks.splice(i, 1); | ||
break; | ||
} | ||
} | ||
return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs); | ||
} | ||
|
||
clearEvents() { | ||
while (this.eventTasks.length) { | ||
Zone.current.cancelTask(this.eventTasks[0]); | ||
} | ||
} | ||
} | ||
|
||
// Export the class so that new instances can be created with proper | ||
// constructor params. | ||
Zone['TaskTrackingZoneSpec'] = TaskTrackingZoneSpec; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import '../../lib/zone-spec/task-tracking'; | ||
|
||
describe('TaskTrackingZone', function() { | ||
let _TaskTrackingZoneSpec: typeof TaskTrackingZoneSpec = Zone['TaskTrackingZoneSpec']; | ||
let taskTrackingZoneSpec: TaskTrackingZoneSpec = null; | ||
let taskTrackingZone: Zone; | ||
|
||
beforeEach(() => { | ||
taskTrackingZoneSpec = new _TaskTrackingZoneSpec(); | ||
taskTrackingZone = Zone.current.fork(taskTrackingZoneSpec); | ||
}); | ||
|
||
it('should track tasks', (done: Function) => { | ||
taskTrackingZone.run(() => { | ||
const microTask = taskTrackingZone.scheduleMicroTask('test1', () => {}); | ||
expect(taskTrackingZoneSpec.microTasks.length).toBe(1); | ||
expect(taskTrackingZoneSpec.microTasks[0].source).toBe('test1'); | ||
|
||
const macroTask = setTimeout(() => {}) as any as Task; | ||
expect(taskTrackingZoneSpec.macroTasks.length).toBe(1); | ||
expect(taskTrackingZoneSpec.macroTasks[0].source).toBe('setTimeout'); | ||
taskTrackingZone.cancelTask(macroTask); | ||
expect(taskTrackingZoneSpec.macroTasks.length).toBe(0); | ||
|
||
setTimeout(() => { | ||
// assert on execution it is null | ||
expect(taskTrackingZoneSpec.macroTasks.length).toBe(0); | ||
expect(taskTrackingZoneSpec.microTasks.length).toBe(0); | ||
|
||
const xhr = new XMLHttpRequest(); | ||
xhr.open('get', '/', true); | ||
xhr.onreadystatechange = () => { | ||
if (xhr.readyState == 4) { | ||
// clear current event tasks using setTimeout | ||
setTimeout(() => { | ||
expect(taskTrackingZoneSpec.macroTasks.length).toBe(0); | ||
expect(taskTrackingZoneSpec.microTasks.length).toBe(0); | ||
expect(taskTrackingZoneSpec.eventTasks.length).toBe(2); | ||
taskTrackingZoneSpec.clearEvents(); | ||
expect(taskTrackingZoneSpec.eventTasks.length).toBe(0); | ||
done(); | ||
}); | ||
} | ||
}; | ||
xhr.send(); | ||
expect(taskTrackingZoneSpec.macroTasks.length).toBe(1); | ||
expect(taskTrackingZoneSpec.macroTasks[0].source).toBe('XMLHttpRequest.send'); | ||
|
||
expect(taskTrackingZoneSpec.eventTasks.length).toBe(2); | ||
// one for me | ||
expect(taskTrackingZoneSpec.eventTasks[0].source).toBe('XMLHttpRequest.addEventListener:readystatechange'); | ||
// one for internall tracking of XHRs. | ||
expect(taskTrackingZoneSpec.eventTasks[1].source).toBe('XMLHttpRequest.addEventListener:readystatechange'); | ||
}); | ||
|
||
}); | ||
}); | ||
|
||
it('should capture task creation stacktrace', (done) => { | ||
taskTrackingZone.run(() => { | ||
const task = setTimeout(() => { | ||
done(); | ||
}) as any as Task; | ||
expect(task['creationLocation']).toBeTruthy(); | ||
}); | ||
}); | ||
}); | ||
|
||
export var __something__; |