chore: upgrade to new Zone.js API v0.6.2

BREAKING CHANGE

Removed deprecated API from NgZone
- `NgZone.overrideOnTurnStart`
- `NgZone.overrideOnTurnDone`
- `NgZone.overrideOnEventDone`
- `NgZone.overrideOnErrorHandler`

Rename NgZone API
- `NgZone.onTurnStart` => `NgZone.onUnstable`
- `NgZone.onTurnDone` => `NgZone.onMicrotaskEmpty`
- `NgZone.onEventDone` => `NgZone.onStable`

Closes #7345
This commit is contained in:
Misko Hevery
2016-02-25 14:24:17 -08:00
committed by Miško Hevery
parent f9fb72fb0e
commit 310620fd12
42 changed files with 866 additions and 1826 deletions
@@ -93,7 +93,7 @@ export function main() {
TimerWrapper.setTimeout(() => {
expect(ref.spy('markForCheck')).toHaveBeenCalled();
async.done();
}, 0)
}, 10)
}));
});
@@ -121,7 +121,7 @@ export function main() {
var completer: PromiseCompleter<any>;
var ref: SpyChangeDetectorRef;
// adds longer timers for passing tests in IE
var timer = (!isBlank(DOM) && browserDetection.isIE) ? 50 : 0;
var timer = (!isBlank(DOM) && browserDetection.isIE) ? 50 : 10;
beforeEach(() => {
completer = PromiseWrapper.completer();
@@ -174,11 +174,12 @@ export function main() {
it("should request a change detection check upon receiving a new value",
inject([AsyncTestCompleter], (async) => {
var markForCheck = ref.spy('markForCheck');
pipe.transform(completer.promise);
completer.resolve(message);
TimerWrapper.setTimeout(() => {
expect(ref.spy('markForCheck')).toHaveBeenCalled();
expect(markForCheck).toHaveBeenCalled();
async.done();
}, timer)
}));
@@ -20,6 +20,8 @@ import {Injector, Provider, APP_INITIALIZER} from "angular2/core";
import {ChangeDetectorRef_} from "angular2/src/core/change_detection/change_detector_ref";
import {PromiseWrapper, PromiseCompleter, TimerWrapper} from "angular2/src/facade/async";
import {ListWrapper} from "angular2/src/facade/collection";
import {ExceptionHandler} from 'angular2/src/facade/exception_handler';
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
export function main() {
describe("ApplicationRef", () => {
@@ -33,6 +35,8 @@ export function main() {
});
describe("PlatformRef", () => {
var exceptionHandler =
new Provider(ExceptionHandler, {useValue: new ExceptionHandler(DOM, true)});
describe("asyncApplication", () => {
function expectProviders(injector: Injector, providers: Array<any>): void {
for (let i = 0; i < providers.length; i++) {
@@ -44,7 +48,7 @@ export function main() {
it("should merge syncronous and asyncronous providers",
inject([AsyncTestCompleter, Injector], (async, injector) => {
let ref = new PlatformRef_(injector, null);
let ASYNC_PROVIDERS = [new Provider(Foo, {useValue: new Foo()})];
let ASYNC_PROVIDERS = [new Provider(Foo, {useValue: new Foo()}), exceptionHandler];
let SYNC_PROVIDERS = [new Provider(Bar, {useValue: new Bar()})];
ref.asyncApplication((zone) => PromiseWrapper.resolve(ASYNC_PROVIDERS), SYNC_PROVIDERS)
.then((appRef) => {
@@ -57,7 +61,7 @@ export function main() {
it("should allow function to be null",
inject([AsyncTestCompleter, Injector], (async, injector) => {
let ref = new PlatformRef_(injector, null);
let SYNC_PROVIDERS = [new Provider(Bar, {useValue: new Bar()})];
let SYNC_PROVIDERS = [new Provider(Bar, {useValue: new Bar()}), exceptionHandler];
ref.asyncApplication(null, SYNC_PROVIDERS)
.then((appRef) => {
expectProviders(appRef.injector, SYNC_PROVIDERS);
@@ -86,7 +90,7 @@ export function main() {
new Provider(APP_INITIALIZER,
{useValue: mockAsyncAppInitializer(completer), multi: true})
];
ref.asyncApplication(null, SYNC_PROVIDERS)
ref.asyncApplication(null, [SYNC_PROVIDERS, exceptionHandler])
.then((appRef) => {
expectProviders(appRef.injector,
SYNC_PROVIDERS.slice(0, SYNC_PROVIDERS.length - 1));
@@ -109,7 +113,8 @@ export function main() {
deps: [Injector]
})
];
ref.asyncApplication((zone) => PromiseWrapper.resolve(ASYNC_PROVIDERS), SYNC_PROVIDERS)
ref.asyncApplication((zone) => PromiseWrapper.resolve(ASYNC_PROVIDERS),
[SYNC_PROVIDERS, exceptionHandler])
.then((appRef) => {
expectProviders(appRef.injector,
SYNC_PROVIDERS.slice(0, SYNC_PROVIDERS.length - 1));
@@ -123,7 +128,7 @@ export function main() {
let ref = new PlatformRef_(injector, null);
let appInitializer = new Provider(
APP_INITIALIZER, {useValue: () => PromiseWrapper.resolve([]), multi: true});
expect(() => ref.application([appInitializer]))
expect(() => ref.application([appInitializer, exceptionHandler]))
.toThrowError(
"Cannot use asyncronous app initializers with application. Use asyncApplication instead.");
}));
@@ -128,7 +128,7 @@ export function main() {
}));
});
it('should handle swapping element', () => {
it('should handle incremental swapping element', () => {
let l = ['a', 'b', 'c'];
differ.check(l);
@@ -14,36 +14,43 @@ import {
} from 'angular2/testing_internal';
import {Testability} from 'angular2/src/core/testability/testability';
import {NgZone} from 'angular2/src/core/zone/ng_zone';
import {normalizeBlank} from 'angular2/src/facade/lang';
import {normalizeBlank, scheduleMicroTask} from 'angular2/src/facade/lang';
import {PromiseWrapper, EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
// Schedules a microtasks (using a resolved promise .then())
function microTask(fn: Function): void {
PromiseWrapper.resolve(null).then((_) => { fn(); });
scheduleMicroTask(() => {
// We do double dispatch so that we can wait for scheduleMicrotas in the Testability when
// NgZone becomes stable.
scheduleMicroTask(fn);
});
}
@Injectable()
class MockNgZone extends NgZone {
_onTurnStartStream: EventEmitter<any>;
get onTurnStart() { return this._onTurnStartStream; }
_onUnstableStream: EventEmitter<any>;
get onUnstable() { return this._onUnstableStream; }
_onEventDoneStream: EventEmitter<any>;
get onEventDone() { return this._onEventDoneStream; }
_onStableStream: EventEmitter<any>;
get onStable() { return this._onStableStream; }
constructor() {
super({enableLongStackTrace: false});
this._onTurnStartStream = new EventEmitter(false);
this._onEventDoneStream = new EventEmitter(false);
this._onUnstableStream = new EventEmitter(false);
this._onStableStream = new EventEmitter(false);
}
start(): void { ObservableWrapper.callEmit(this._onTurnStartStream, null); }
unstable(): void { ObservableWrapper.callEmit(this._onUnstableStream, null); }
finish(): void { ObservableWrapper.callEmit(this._onEventDoneStream, null); }
stable(): void { ObservableWrapper.callEmit(this._onStableStream, null); }
}
export function main() {
describe('Testability', () => {
var testability, execute, execute2, ngZone;
var testability: Testability;
var execute: any;
var execute2: any;
var ngZone: MockNgZone;
beforeEach(() => {
ngZone = new MockNgZone();
@@ -142,13 +149,10 @@ export function main() {
});
describe('NgZone callback logic', () => {
it('should start being ready',
() => { expect(testability.isAngularEventPending()).toEqual(false); });
it('should fire whenstable callback if event is already finished',
inject([AsyncTestCompleter], (async) => {
ngZone.start();
ngZone.finish();
ngZone.unstable();
ngZone.stable();
testability.whenStable(execute);
microTask(() => {
@@ -158,8 +162,8 @@ export function main() {
}));
it('should not fire whenstable callbacks synchronously if event is already finished', () => {
ngZone.start();
ngZone.finish();
ngZone.unstable();
ngZone.stable();
testability.whenStable(execute);
expect(execute).not.toHaveBeenCalled();
@@ -167,12 +171,12 @@ export function main() {
it('should fire whenstable callback when event finishes',
inject([AsyncTestCompleter], (async) => {
ngZone.start();
ngZone.unstable();
testability.whenStable(execute);
microTask(() => {
expect(execute).not.toHaveBeenCalled();
ngZone.finish();
ngZone.stable();
microTask(() => {
expect(execute).toHaveBeenCalled();
@@ -182,16 +186,16 @@ export function main() {
}));
it('should not fire whenstable callbacks synchronously when event finishes', () => {
ngZone.start();
ngZone.unstable();
testability.whenStable(execute);
ngZone.finish();
ngZone.stable();
expect(execute).not.toHaveBeenCalled();
});
it('should not fire whenstable callback when event did not finish',
inject([AsyncTestCompleter], (async) => {
ngZone.start();
ngZone.unstable();
testability.increasePendingRequestCount();
testability.whenStable(execute);
@@ -201,7 +205,7 @@ export function main() {
microTask(() => {
expect(execute).not.toHaveBeenCalled();
ngZone.finish();
ngZone.stable();
microTask(() => {
expect(execute).toHaveBeenCalled();
@@ -213,14 +217,14 @@ export function main() {
it('should not fire whenstable callback when there are pending counts',
inject([AsyncTestCompleter], (async) => {
ngZone.start();
ngZone.unstable();
testability.increasePendingRequestCount();
testability.increasePendingRequestCount();
testability.whenStable(execute);
microTask(() => {
expect(execute).not.toHaveBeenCalled();
ngZone.finish();
ngZone.stable();
microTask(() => {
expect(execute).not.toHaveBeenCalled();
@@ -241,8 +245,8 @@ export function main() {
it('should fire whenstable callback with didWork if event is already finished',
inject([AsyncTestCompleter], (async) => {
ngZone.start();
ngZone.finish();
ngZone.unstable();
ngZone.stable();
testability.whenStable(execute);
microTask(() => {
@@ -258,11 +262,11 @@ export function main() {
it('should fire whenstable callback with didwork when event finishes',
inject([AsyncTestCompleter], (async) => {
ngZone.start();
ngZone.unstable();
testability.whenStable(execute);
microTask(() => {
ngZone.finish();
ngZone.stable();
microTask(() => {
expect(execute).toHaveBeenCalledWith(true);
@@ -1,655 +0,0 @@
// TODO(yjbanov): this file tests the deprecated NgZone API. Delete it when
// the old API is cleaned up.
import {
AsyncTestCompleter,
beforeEach,
ddescribe,
describe,
expect,
iit,
inject,
it,
xdescribe,
xit,
Log,
isInInnerZone,
browserDetection
} from 'angular2/testing_internal';
import {PromiseCompleter, PromiseWrapper, TimerWrapper} from 'angular2/src/facade/async';
import {BaseException} from 'angular2/src/facade/exceptions';
import {NgZone} from 'angular2/src/core/zone/ng_zone';
var needsLongerTimers = browserDetection.isSlow || browserDetection.isEdge;
var resultTimer = 1000;
var testTimeout = browserDetection.isEdge ? 1200 : 100;
// Schedules a macrotask (using a timer)
function macroTask(fn: (...args: any[]) => void, timer = 1): void {
// adds longer timers for passing tests in IE and Edge
_zone.runOutsideAngular(() => TimerWrapper.setTimeout(fn, needsLongerTimers ? timer : 1));
}
// Schedules a microtasks (using a resolved promise .then())
function microTask(fn: Function): void {
PromiseWrapper.resolve(null).then((_) => { fn(); });
}
var _log;
var _errors: any[];
var _traces: any[];
var _zone;
function logError(error, stackTrace) {
_errors.push(error);
_traces.push(stackTrace);
}
export function main() {
describe("NgZone", () => {
function createZone(enableLongStackTrace) {
var zone = new NgZone({enableLongStackTrace: enableLongStackTrace});
zone.overrideOnTurnStart(_log.fn('onTurnStart'));
zone.overrideOnTurnDone(_log.fn('onTurnDone'));
return zone;
}
beforeEach(() => {
_log = new Log();
_errors = [];
_traces = [];
});
describe('long stack trace', () => {
beforeEach(() => { _zone = createZone(true); });
commonTests();
it('should produce long stack traces', inject([AsyncTestCompleter], (async) => {
macroTask(() => {
_zone.overrideOnErrorHandler(logError);
var c: PromiseCompleter<any> = PromiseWrapper.completer();
_zone.run(() => {
TimerWrapper.setTimeout(() => {
TimerWrapper.setTimeout(() => {
c.resolve(null);
throw new BaseException('ccc');
}, 0);
}, 0);
});
c.promise.then((_) => {
expect(_traces.length).toBe(1);
expect(_traces[0].length).toBeGreaterThan(1);
async.done();
});
});
}), testTimeout);
it('should produce long stack traces (when using microtasks)',
inject([AsyncTestCompleter], (async) => {
macroTask(() => {
_zone.overrideOnErrorHandler(logError);
var c: PromiseCompleter<any> = PromiseWrapper.completer();
_zone.run(() => {
microTask(() => {
microTask(() => {
c.resolve(null);
throw new BaseException("ddd");
});
});
});
c.promise.then((_) => {
expect(_traces.length).toBe(1);
expect(_traces[0].length).toBeGreaterThan(1);
async.done();
});
});
}), testTimeout);
});
describe('short stack trace', () => {
beforeEach(() => { _zone = createZone(false); });
commonTests();
it('should disable long stack traces', inject([AsyncTestCompleter], (async) => {
macroTask(() => {
_zone.overrideOnErrorHandler(logError);
var c: PromiseCompleter<any> = PromiseWrapper.completer();
_zone.run(() => {
TimerWrapper.setTimeout(() => {
TimerWrapper.setTimeout(() => {
c.resolve(null);
throw new BaseException('ccc');
}, 0);
}, 0);
});
c.promise.then((_) => {
expect(_traces.length).toBe(1);
expect(_traces[0].length).toEqual(1);
async.done();
});
});
}), testTimeout);
});
});
}
function commonTests() {
describe('isInInnerZone',
() => {it('should return whether the code executes in the inner zone', () => {
expect(isInInnerZone()).toEqual(false);
_zone.run(() => { expect(isInInnerZone()).toEqual(true); });
}, testTimeout)});
describe('run', () => {
it('should return the body return value from run', inject([AsyncTestCompleter], (async) => {
macroTask(() => { expect(_zone.run(() => { return 6; })).toEqual(6); });
macroTask(() => { async.done(); });
}), testTimeout);
it('should call onTurnStart and onTurnDone', inject([AsyncTestCompleter], (async) => {
macroTask(() => { _zone.run(_log.fn('run')); });
macroTask(() => {
expect(_log.result()).toEqual('onTurnStart; run; onTurnDone');
async.done();
});
}), testTimeout);
it('should call onEventDone once at the end of event', inject([AsyncTestCompleter], (async) => {
// The test is set up in a way that causes the zone loop to run onTurnDone twice
// then verified that onEventDone is only called once at the end
_zone.overrideOnTurnStart(null);
_zone.overrideOnEventDone(() => { _log.add('onEventDone'); });
var times = 0;
_zone.overrideOnTurnDone(() => {
times++;
_log.add(`onTurnDone ${times}`);
if (times < 2) {
// Scheduling a microtask causes a second digest
microTask(() => {});
}
});
macroTask(() => { _zone.run(_log.fn('run')); });
macroTask(() => {
expect(_log.result()).toEqual('run; onTurnDone 1; onTurnDone 2; onEventDone');
async.done();
}, resultTimer);
}), testTimeout);
it('should call standalone onEventDone', inject([AsyncTestCompleter], (async) => {
_zone.overrideOnTurnStart(null);
_zone.overrideOnEventDone(() => { _log.add('onEventDone'); });
_zone.overrideOnTurnDone(null);
macroTask(() => { _zone.run(_log.fn('run')); });
macroTask(() => {
expect(_log.result()).toEqual('run; onEventDone');
async.done();
}, resultTimer);
}), testTimeout);
it('should not allow onEventDone to cause further digests',
inject([AsyncTestCompleter], (async) => {
_zone.overrideOnTurnStart(null);
var eventDone = false;
_zone.overrideOnEventDone(() => {
if (eventDone) throw 'Should not call this more than once';
_log.add('onEventDone');
// If not implemented correctly, this microtask will cause another digest,
// which is not what we want.
microTask(() => {});
eventDone = true;
});
_zone.overrideOnTurnDone(() => { _log.add('onTurnDone'); });
macroTask(() => { _zone.run(_log.fn('run')); });
macroTask(() => {
expect(_log.result()).toEqual('run; onTurnDone; onEventDone');
async.done();
}, resultTimer);
}), testTimeout);
it('should run async tasks scheduled inside onEventDone outside Angular zone',
inject([AsyncTestCompleter], (async) => {
_zone.overrideOnTurnStart(null);
_zone.overrideOnEventDone(() => {
_log.add('onEventDone');
// If not implemented correctly, this time will cause another digest,
// which is not what we want.
TimerWrapper.setTimeout(() => { _log.add('asyncTask'); }, 5);
});
_zone.overrideOnTurnDone(() => { _log.add('onTurnDone'); });
macroTask(() => { _zone.run(_log.fn('run')); });
macroTask(() => {
TimerWrapper.setTimeout(() => {
expect(_log.result()).toEqual('run; onTurnDone; onEventDone; asyncTask');
async.done();
}, 50);
});
}), testTimeout);
it('should call onTurnStart once before a turn and onTurnDone once after the turn',
inject([AsyncTestCompleter], (async) => {
macroTask(() => {
_zone.run(() => {
_log.add('run start');
microTask(_log.fn('async'));
_log.add('run end');
});
});
macroTask(() => {
// The microtask (async) is executed after the macrotask (run)
expect(_log.result()).toEqual('onTurnStart; run start; run end; async; onTurnDone');
async.done();
}, resultTimer);
}), testTimeout);
it('should not run onTurnStart and onTurnDone for nested Zone.run',
inject([AsyncTestCompleter], (async) => {
macroTask(() => {
_zone.run(() => {
_log.add('start run');
_zone.run(() => {
_log.add('nested run');
microTask(_log.fn('nested run microtask'));
});
_log.add('end run');
});
});
macroTask(() => {
expect(_log.result())
.toEqual(
'onTurnStart; start run; nested run; end run; nested run microtask; onTurnDone');
async.done();
}, resultTimer);
}), testTimeout);
it('should not run onTurnStart and onTurnDone for nested Zone.run invoked from onTurnDone',
inject([AsyncTestCompleter], (async) => {
_zone.overrideOnTurnStart(null);
_zone.overrideOnTurnDone(() => {
_log.add('onTurnDone:started');
_zone.run(() => _log.add('nested run'));
_log.add('onTurnDone:finished');
});
macroTask(() => { _zone.run(() => { _log.add('start run'); }); });
macroTask(() => {
expect(_log.result())
.toEqual('start run; onTurnDone:started; nested run; onTurnDone:finished');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart and onTurnDone before and after each top-level run',
inject([AsyncTestCompleter], (async) => {
macroTask(() => { _zone.run(_log.fn('run1')); });
macroTask(() => { _zone.run(_log.fn('run2')); });
macroTask(() => {
expect(_log.result())
.toEqual('onTurnStart; run1; onTurnDone; onTurnStart; run2; onTurnDone');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart and onTurnDone before and after each turn',
inject([AsyncTestCompleter], (async) => {
var a: PromiseCompleter<string>;
var b: PromiseCompleter<string>;
macroTask(() => {
_zone.run(() => {
a = PromiseWrapper.completer();
b = PromiseWrapper.completer();
_log.add('run start');
a.promise.then(_log.fn('a then'));
b.promise.then(_log.fn('b then'));
});
});
macroTask(() => {
_zone.run(() => {
a.resolve('a');
b.resolve('b');
});
});
macroTask(() => {
expect(_log.result())
.toEqual(
'onTurnStart; run start; onTurnDone; onTurnStart; a then; b then; onTurnDone');
async.done();
}, resultTimer);
}), testTimeout);
it('should run a function outside of the angular zone',
inject([AsyncTestCompleter], (async) => {
macroTask(() => { _zone.runOutsideAngular(_log.fn('run')); });
macroTask(() => {
expect(_log.result()).toEqual('run');
async.done()
});
}), testTimeout);
it('should call onTurnStart and onTurnDone when an inner microtask is scheduled from outside angular',
inject([AsyncTestCompleter], (async) => {
var completer: PromiseCompleter<any>;
macroTask(
() => { _zone.runOutsideAngular(() => { completer = PromiseWrapper.completer(); }); });
macroTask(
() => { _zone.run(() => { completer.promise.then(_log.fn('executedMicrotask')); }); });
macroTask(() => {
_zone.runOutsideAngular(() => {
_log.add('scheduling a microtask');
completer.resolve(null);
});
});
macroTask(() => {
expect(_log.result())
.toEqual(
// First VM turn => setup Promise then
'onTurnStart; onTurnDone; ' +
// Second VM turn (outside of anguler)
'scheduling a microtask; ' +
// Third VM Turn => execute the microtask (inside angular)
'onTurnStart; executedMicrotask; onTurnDone');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart before executing a microtask scheduled in onTurnDone as well as ' +
'onTurnDone after executing the task',
inject([AsyncTestCompleter], (async) => {
var ran = false;
_zone.overrideOnTurnStart(_log.fn('onTurnStart'));
_zone.overrideOnTurnDone(() => {
_log.add('onTurnDone(begin)');
if (!ran) {
microTask(() => {
ran = true;
_log.add('executedMicrotask');
});
}
_log.add('onTurnDone(end)');
});
macroTask(() => { _zone.run(_log.fn('run')); });
macroTask(() => {
expect(_log.result())
.toEqual(
// First VM turn => 'run' macrotask
'onTurnStart; run; onTurnDone(begin); onTurnDone(end); ' +
// Second VM Turn => microtask enqueued from onTurnDone
'onTurnStart; executedMicrotask; onTurnDone(begin); onTurnDone(end)');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart and onTurnDone for a scheduleMicrotask in onTurnDone triggered by ' +
'a scheduleMicrotask in run',
inject([AsyncTestCompleter], (async) => {
var ran = false;
_zone.overrideOnTurnStart(_log.fn('onTurnStart'));
_zone.overrideOnTurnDone(() => {
_log.add('onTurnDone(begin)');
if (!ran) {
_log.add('onTurnDone(scheduleMicrotask)');
microTask(() => {
ran = true;
_log.add('onTurnDone(executeMicrotask)');
});
}
_log.add('onTurnDone(end)');
});
macroTask(() => {
_zone.run(() => {
_log.add('scheduleMicrotask');
microTask(_log.fn('run(executeMicrotask)'));
});
});
macroTask(() => {
expect(_log.result())
.toEqual(
// First VM Turn => a macrotask + the microtask it enqueues
'onTurnStart; scheduleMicrotask; run(executeMicrotask); onTurnDone(begin); onTurnDone(scheduleMicrotask); onTurnDone(end); ' +
// Second VM Turn => the microtask enqueued from onTurnDone
'onTurnStart; onTurnDone(executeMicrotask); onTurnDone(begin); onTurnDone(end)');
async.done();
}, resultTimer);
}), testTimeout);
it('should execute promises scheduled in onTurnStart before promises scheduled in run',
inject([AsyncTestCompleter], (async) => {
var donePromiseRan = false;
var startPromiseRan = false;
_zone.overrideOnTurnStart(() => {
_log.add('onTurnStart(begin)');
if (!startPromiseRan) {
_log.add('onTurnStart(schedulePromise)');
microTask(_log.fn('onTurnStart(executePromise)'));
startPromiseRan = true;
}
_log.add('onTurnStart(end)');
});
_zone.overrideOnTurnDone(() => {
_log.add('onTurnDone(begin)');
if (!donePromiseRan) {
_log.add('onTurnDone(schedulePromise)');
microTask(_log.fn('onTurnDone(executePromise)'));
donePromiseRan = true;
}
_log.add('onTurnDone(end)');
});
macroTask(() => {
_zone.run(() => {
_log.add('run start');
PromiseWrapper.resolve(null)
.then((_) => {
_log.add('promise then');
PromiseWrapper.resolve(null).then(_log.fn('promise foo'));
return PromiseWrapper.resolve(null);
})
.then(_log.fn('promise bar'));
_log.add('run end');
});
});
macroTask(() => {
expect(_log.result())
.toEqual(
// First VM turn: enqueue a microtask in onTurnStart
'onTurnStart(begin); onTurnStart(schedulePromise); onTurnStart(end); ' +
// First VM turn: execute the macrotask which enqueues microtasks
'run start; run end; ' +
// First VM turn: execute enqueued microtasks
'onTurnStart(executePromise); promise then; promise foo; promise bar; ' +
// First VM turn: onTurnEnd, enqueue a microtask
'onTurnDone(begin); onTurnDone(schedulePromise); onTurnDone(end); ' +
// Second VM turn: execute the microtask from onTurnEnd
'onTurnStart(begin); onTurnStart(end); onTurnDone(executePromise); onTurnDone(begin); onTurnDone(end)');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart and onTurnDone before and after each turn, respectively',
inject([AsyncTestCompleter], (async) => {
var completerA: PromiseCompleter<any>;
var completerB: PromiseCompleter<any>;
macroTask(() => {
_zone.run(() => {
completerA = PromiseWrapper.completer();
completerB = PromiseWrapper.completer();
completerA.promise.then(_log.fn('a then'));
completerB.promise.then(_log.fn('b then'));
_log.add('run start');
});
});
macroTask(() => { _zone.run(() => { completerA.resolve(null); }); }, 20);
macroTask(() => { _zone.run(() => { completerB.resolve(null); }); }, 500);
macroTask(() => {
expect(_log.result())
.toEqual(
// First VM turn
'onTurnStart; run start; onTurnDone; ' +
// Second VM turn
'onTurnStart; a then; onTurnDone; ' +
// Third VM turn
'onTurnStart; b then; onTurnDone');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart and onTurnDone before and after (respectively) all turns in a chain',
inject([AsyncTestCompleter], (async) => {
macroTask(() => {
_zone.run(() => {
_log.add('run start');
microTask(() => {
_log.add('async1');
microTask(_log.fn('async2'));
});
_log.add('run end');
});
});
macroTask(() => {
expect(_log.result())
.toEqual('onTurnStart; run start; run end; async1; async2; onTurnDone');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart and onTurnDone for promises created outside of run body',
inject([AsyncTestCompleter], (async) => {
var promise: Promise<any>;
macroTask(() => {
_zone.runOutsideAngular(() => {
promise = PromiseWrapper.resolve(4).then((x) => PromiseWrapper.resolve(x));
});
_zone.run(() => {
promise.then(_log.fn('promise then'));
_log.add('zone run');
});
});
macroTask(() => {
expect(_log.result())
.toEqual('onTurnStart; zone run; onTurnDone; onTurnStart; promise then; onTurnDone');
async.done();
}, resultTimer);
}), testTimeout);
});
describe('exceptions', () => {
it('should call the on error callback when it is defined',
inject([AsyncTestCompleter], (async) => {
macroTask(() => {
_zone.overrideOnErrorHandler(logError);
var exception = new BaseException('sync');
_zone.run(() => { throw exception; });
expect(_errors.length).toBe(1);
expect(_errors[0]).toBe(exception);
async.done();
});
}), testTimeout);
it('should call onError for errors from microtasks', inject([AsyncTestCompleter], (async) => {
_zone.overrideOnErrorHandler(logError);
var exception = new BaseException('async');
macroTask(() => { _zone.run(() => { microTask(() => { throw exception; }); }); });
macroTask(() => {
expect(_errors.length).toBe(1);
expect(_errors[0]).toEqual(exception);
async.done();
}, resultTimer);
}), testTimeout);
it('should call onError when onTurnDone throws and the zone is sync',
inject([AsyncTestCompleter], (async) => {
var exception = new BaseException('fromOnTurnDone');
_zone.overrideOnErrorHandler(logError);
_zone.overrideOnTurnDone(() => { throw exception; });
macroTask(() => { _zone.run(() => {}); });
macroTask(() => {
expect(_errors.length).toBe(1);
expect(_errors[0]).toEqual(exception);
async.done();
}, resultTimer);
}), testTimeout);
it('should call onError when onTurnDone throws and the zone is async',
inject([AsyncTestCompleter], (async) => {
var asyncRan = false;
var exception = new BaseException('fromOnTurnDone');
_zone.overrideOnErrorHandler(logError);
_zone.overrideOnTurnDone(() => { throw exception; });
macroTask(() => { _zone.run(() => { microTask(() => { asyncRan = true; }); }); });
macroTask(() => {
expect(asyncRan).toBe(true);
expect(_errors.length).toBe(1);
expect(_errors[0]).toEqual(exception);
async.done();
}, resultTimer);
}), testTimeout);
});
}
+231 -246
View File
@@ -10,7 +10,6 @@ import {
xdescribe,
xit,
Log,
isInInnerZone,
browserDetection
} from 'angular2/testing_internal';
@@ -18,31 +17,26 @@ import {
PromiseCompleter,
PromiseWrapper,
TimerWrapper,
ObservableWrapper,
EventEmitter
ObservableWrapper
} from 'angular2/src/facade/async';
import {BaseException} from 'angular2/src/facade/exceptions';
import {IS_DART, scheduleMicroTask, isPresent} from 'angular2/src/facade/lang';
import {NgZone, NgZoneError} from 'angular2/src/core/zone/ng_zone';
var needsLongerTimers = browserDetection.isSlow || browserDetection.isEdge;
var resultTimer = 1000;
var testTimeout = browserDetection.isEdge ? 1200 : 100;
var testTimeout = browserDetection.isEdge ? 1200 : 500;
// Schedules a macrotask (using a timer)
function macroTask(fn: (...args: any[]) => void, timer = 1): void {
// adds longer timers for passing tests in IE and Edge
_zone.runOutsideAngular(() => TimerWrapper.setTimeout(fn, needsLongerTimers ? timer : 1));
TimerWrapper.setTimeout(fn, needsLongerTimers ? timer : 1);
}
// Schedules a microtasks (using a resolved promise .then())
function microTask(fn: Function): void {
PromiseWrapper.resolve(null).then((_) => { fn(); });
}
var _log;
var _log: Log;
var _errors: any[];
var _traces: any[];
var _zone;
var _zone: NgZone;
function logOnError() {
ObservableWrapper.subscribe(_zone.onError, (ngErr: NgZoneError) => {
@@ -51,16 +45,26 @@ function logOnError() {
});
}
function logOnTurnStart() {
ObservableWrapper.subscribe(_zone.onTurnStart, _log.fn('onTurnStart'));
function logOnUnstable() {
ObservableWrapper.subscribe(_zone.onUnstable, _log.fn('onUnstable'));
}
function logOnTurnDone() {
ObservableWrapper.subscribe(_zone.onTurnDone, _log.fn('onTurnDone'));
function logOnMicrotaskEmpty() {
ObservableWrapper.subscribe(_zone.onMicrotaskEmpty, _log.fn('onMicrotaskEmpty'));
}
function logOnEventDone() {
ObservableWrapper.subscribe(_zone.onEventDone, _log.fn('onEventDone'));
function logOnStable() {
ObservableWrapper.subscribe(_zone.onStable, _log.fn('onStable'));
}
function runNgZoneNoLog(fn: () => any) {
var length = _log.logItems.length;
try {
return _zone.run(fn);
} finally {
// delete anything which may have gotten logged.
_log.logItems.length = length;
}
}
export function main() {
@@ -77,13 +81,18 @@ export function main() {
});
describe('long stack trace', () => {
beforeEach(() => { _zone = createZone(true); });
beforeEach(() => {
_zone = createZone(true);
logOnUnstable();
logOnMicrotaskEmpty();
logOnStable();
logOnError();
});
commonTests();
it('should produce long stack traces', inject([AsyncTestCompleter], (async) => {
macroTask(() => {
logOnError();
var c: PromiseCompleter<any> = PromiseWrapper.completer();
_zone.run(() => {
@@ -106,12 +115,11 @@ export function main() {
it('should produce long stack traces (when using microtasks)',
inject([AsyncTestCompleter], (async) => {
macroTask(() => {
logOnError();
var c: PromiseCompleter<any> = PromiseWrapper.completer();
_zone.run(() => {
microTask(() => {
microTask(() => {
scheduleMicroTask(() => {
scheduleMicroTask(() => {
c.resolve(null);
throw new BaseException("ddd");
});
@@ -128,13 +136,18 @@ export function main() {
});
describe('short stack trace', () => {
beforeEach(() => { _zone = createZone(false); });
beforeEach(() => {
_zone = createZone(false);
logOnUnstable();
logOnMicrotaskEmpty();
logOnStable();
logOnError();
});
commonTests();
it('should disable long stack traces', inject([AsyncTestCompleter], (async) => {
macroTask(() => {
logOnError();
var c: PromiseCompleter<any> = PromiseWrapper.completer();
_zone.run(() => {
@@ -148,7 +161,10 @@ export function main() {
c.promise.then((_) => {
expect(_traces.length).toBe(1);
expect(_traces[0].length).toEqual(1);
if (isPresent(_traces[0])) {
// some browsers don't have stack traces.
expect(_traces[0].indexOf('---')).toEqual(-1);
}
async.done();
});
});
@@ -162,38 +178,38 @@ function commonTests() {
it('should be false', () => { expect(_zone.hasPendingMicrotasks).toBe(false); });
it('should be true', () => {
_zone.run(() => { microTask(() => {}); });
runNgZoneNoLog(() => { scheduleMicroTask(() => {}); });
expect(_zone.hasPendingMicrotasks).toBe(true);
});
});
describe('hasPendingTimers', () => {
it('should be false', () => { expect(_zone.hasPendingTimers).toBe(false); });
it('should be false', () => { expect(_zone.hasPendingMacrotasks).toBe(false); });
it('should be true', () => {
_zone.run(() => { TimerWrapper.setTimeout(() => {}, 0); });
expect(_zone.hasPendingTimers).toBe(true);
runNgZoneNoLog(() => { TimerWrapper.setTimeout(() => {}, 0); });
expect(_zone.hasPendingMacrotasks).toBe(true);
});
});
describe('hasPendingAsyncTasks', () => {
it('should be false', () => { expect(_zone.hasPendingAsyncTasks).toBe(false); });
it('should be false', () => { expect(_zone.hasPendingMicrotasks).toBe(false); });
it('should be true when microtask is scheduled', () => {
_zone.run(() => { microTask(() => {}); });
expect(_zone.hasPendingAsyncTasks).toBe(true);
runNgZoneNoLog(() => { scheduleMicroTask(() => {}); });
expect(_zone.hasPendingMicrotasks).toBe(true);
});
it('should be true when timer is scheduled', () => {
_zone.run(() => { TimerWrapper.setTimeout(() => {}, 0); });
expect(_zone.hasPendingAsyncTasks).toBe(true);
runNgZoneNoLog(() => { TimerWrapper.setTimeout(() => {}, 0); });
expect(_zone.hasPendingMacrotasks).toBe(true);
});
});
describe('isInInnerZone', () => {
it('should return whether the code executes in the inner zone', () => {
expect(isInInnerZone()).toEqual(false);
_zone.run(() => { expect(isInInnerZone()).toEqual(true); });
expect(NgZone.isInAngularZone()).toEqual(false);
runNgZoneNoLog(() => { expect(NgZone.isInAngularZone()).toEqual(true); });
}, testTimeout);
});
@@ -204,47 +220,43 @@ function commonTests() {
macroTask(() => { async.done(); });
}), testTimeout);
it('should call onTurnStart and onTurnDone', inject([AsyncTestCompleter], (async) => {
logOnTurnStart();
logOnTurnDone();
macroTask(() => { _zone.run(_log.fn('run')); });
it('should call onUnstable and onMicrotaskEmpty', inject([AsyncTestCompleter], (async) => {
runNgZoneNoLog(() => macroTask(_log.fn('run')));
macroTask(() => {
expect(_log.result()).toEqual('onTurnStart; run; onTurnDone');
expect(_log.result()).toEqual('onUnstable; run; onMicrotaskEmpty; onStable');
async.done();
});
}), testTimeout);
it('should call onEventDone once at the end of event', inject([AsyncTestCompleter], (async) => {
// The test is set up in a way that causes the zone loop to run onTurnDone twice
// then verified that onEventDone is only called once at the end
logOnEventDone();
it('should call onStable once at the end of event', inject([AsyncTestCompleter], (async) => {
// The test is set up in a way that causes the zone loop to run onMicrotaskEmpty twice
// then verified that onStable is only called once at the end
runNgZoneNoLog(() => macroTask(_log.fn('run')));
var times = 0;
ObservableWrapper.subscribe(_zone.onTurnDone, (_) => {
ObservableWrapper.subscribe(_zone.onMicrotaskEmpty, (_) => {
times++;
_log.add(`onTurnDone ${times}`);
_log.add(`onMicrotaskEmpty ${times}`);
if (times < 2) {
// Scheduling a microtask causes a second digest
_zone.run(() => { microTask(() => {}); });
runNgZoneNoLog(() => { scheduleMicroTask(() => {}); });
}
});
macroTask(() => { _zone.run(_log.fn('run')); });
macroTask(() => {
expect(_log.result()).toEqual('run; onTurnDone 1; onTurnDone 2; onEventDone');
expect(_log.result())
.toEqual('onUnstable; run; onMicrotaskEmpty; onMicrotaskEmpty 1; ' +
'onMicrotaskEmpty; onMicrotaskEmpty 2; onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should call standalone onEventDone', inject([AsyncTestCompleter], (async) => {
logOnEventDone();
macroTask(() => { _zone.run(_log.fn('run')); });
it('should call standalone onStable', inject([AsyncTestCompleter], (async) => {
runNgZoneNoLog(() => macroTask(_log.fn('run')));
macroTask(() => {
expect(_log.result()).toEqual('run; onEventDone');
expect(_log.result()).toEqual('onUnstable; run; onMicrotaskEmpty; onStable');
async.done();
}, resultTimer);
}), testTimeout);
@@ -255,108 +267,101 @@ function commonTests() {
// then verifies that those microtasks do not cause additional digests.
var turnStart = false;
ObservableWrapper.subscribe(_zone.onTurnStart, (_) => {
ObservableWrapper.subscribe(_zone.onUnstable, (_) => {
if (turnStart) throw 'Should not call this more than once';
_log.add('onTurnStart');
microTask(() => {});
_log.add('onUnstable');
scheduleMicroTask(() => {});
turnStart = true;
});
var turnDone = false;
ObservableWrapper.subscribe(_zone.onTurnDone, (_) => {
ObservableWrapper.subscribe(_zone.onMicrotaskEmpty, (_) => {
if (turnDone) throw 'Should not call this more than once';
_log.add('onTurnDone');
microTask(() => {});
_log.add('onMicrotaskEmpty');
scheduleMicroTask(() => {});
turnDone = true;
});
var eventDone = false;
ObservableWrapper.subscribe(_zone.onEventDone, (_) => {
ObservableWrapper.subscribe(_zone.onStable, (_) => {
if (eventDone) throw 'Should not call this more than once';
_log.add('onEventDone');
microTask(() => {});
_log.add('onStable');
scheduleMicroTask(() => {});
eventDone = true;
});
macroTask(() => { _zone.run(_log.fn('run')); });
macroTask(() => {
expect(_log.result()).toEqual('onTurnStart; run; onTurnDone; onEventDone');
expect(_log.result()).toEqual('onUnstable; run; onMicrotaskEmpty; onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should run subscriber listeners in the subscription zone (inside)',
inject([AsyncTestCompleter], (async) => {
runNgZoneNoLog(() => macroTask(_log.fn('run')));
// the only practical use-case to run a callback inside the zone is
// change detection after "onTurnDone". That's the only case tested.
// change detection after "onMicrotaskEmpty". That's the only case tested.
var turnDone = false;
ObservableWrapper.subscribe(_zone.onTurnDone, (_) => {
_log.add('onTurnDone');
ObservableWrapper.subscribe(_zone.onMicrotaskEmpty, (_) => {
_log.add('onMyMicrotaskEmpty');
if (turnDone) return;
_zone.run(() => { microTask(() => {}); });
_zone.run(() => { scheduleMicroTask(() => {}); });
turnDone = true;
});
macroTask(() => { _zone.run(_log.fn('run')); });
macroTask(() => {
expect(_log.result()).toEqual('run; onTurnDone; onTurnDone');
expect(_log.result())
.toEqual('onUnstable; run; onMicrotaskEmpty; onMyMicrotaskEmpty; ' +
'onMicrotaskEmpty; onMyMicrotaskEmpty; onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should run async tasks scheduled inside onEventDone outside Angular zone',
it('should run async tasks scheduled inside onStable outside Angular zone',
inject([AsyncTestCompleter], (async) => {
ObservableWrapper.subscribe(_zone.onEventDone, (_) => {
_log.add('onEventDone');
// If not implemented correctly, this time will cause another digest,
// which is not what we want.
TimerWrapper.setTimeout(() => { _log.add('asyncTask'); }, 5);
runNgZoneNoLog(() => macroTask(_log.fn('run')));
ObservableWrapper.subscribe(_zone.onStable, (_) => {
NgZone.assertNotInAngularZone();
_log.add('onMyTaskDone');
});
logOnTurnDone();
macroTask(() => { _zone.run(_log.fn('run')); });
macroTask(() => {
TimerWrapper.setTimeout(() => {
expect(_log.result()).toEqual('run; onTurnDone; onEventDone; asyncTask');
async.done();
}, 50);
expect(_log.result())
.toEqual('onUnstable; run; onMicrotaskEmpty; onStable; onMyTaskDone');
async.done();
});
}), testTimeout);
it('should call onTurnStart once before a turn and onTurnDone once after the turn',
it('should call onUnstable once before a turn and onMicrotaskEmpty once after the turn',
inject([AsyncTestCompleter], (async) => {
logOnTurnStart();
logOnTurnDone();
macroTask(() => {
_zone.run(() => {
runNgZoneNoLog(() => {
macroTask(() => {
_log.add('run start');
microTask(_log.fn('async'));
scheduleMicroTask(_log.fn('async'));
_log.add('run end');
});
});
macroTask(() => {
// The microtask (async) is executed after the macrotask (run)
expect(_log.result()).toEqual('onTurnStart; run start; run end; async; onTurnDone');
expect(_log.result())
.toEqual('onUnstable; run start; run end; async; onMicrotaskEmpty; onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should not run onTurnStart and onTurnDone for nested Zone.run',
it('should not run onUnstable and onMicrotaskEmpty for nested Zone.run',
inject([AsyncTestCompleter], (async) => {
logOnTurnStart();
logOnTurnDone();
macroTask(() => {
_zone.run(() => {
runNgZoneNoLog(() => {
macroTask(() => {
_log.add('start run');
_zone.run(() => {
_log.add('nested run');
microTask(_log.fn('nested run microtask'));
scheduleMicroTask(_log.fn('nested run microtask'));
});
_log.add('end run');
});
@@ -365,54 +370,49 @@ function commonTests() {
macroTask(() => {
expect(_log.result())
.toEqual(
'onTurnStart; start run; nested run; end run; nested run microtask; onTurnDone');
'onUnstable; start run; nested run; end run; nested run microtask; onMicrotaskEmpty; onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should not run onTurnStart and onTurnDone for nested Zone.run invoked from onTurnDone',
it('should not run onUnstable and onMicrotaskEmpty for nested Zone.run invoked from onMicrotaskEmpty',
inject([AsyncTestCompleter], (async) => {
ObservableWrapper.subscribe(_zone.onTurnDone, (_) => {
_log.add('onTurnDone:started');
runNgZoneNoLog(() => macroTask(_log.fn('start run')));
ObservableWrapper.subscribe(_zone.onMicrotaskEmpty, (_) => {
_log.add('onMicrotaskEmpty:started');
_zone.run(() => _log.add('nested run'));
_log.add('onTurnDone:finished');
_log.add('onMicrotaskEmpty:finished');
});
macroTask(() => { _zone.run(() => { _log.add('start run'); }); });
macroTask(() => {
expect(_log.result())
.toEqual('start run; onTurnDone:started; nested run; onTurnDone:finished');
.toEqual(
'onUnstable; start run; onMicrotaskEmpty; onMicrotaskEmpty:started; nested run; onMicrotaskEmpty:finished; onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart and onTurnDone before and after each top-level run',
it('should call onUnstable and onMicrotaskEmpty before and after each top-level run',
inject([AsyncTestCompleter], (async) => {
logOnTurnStart();
logOnTurnDone();
macroTask(() => { _zone.run(_log.fn('run1')); });
macroTask(() => { _zone.run(_log.fn('run2')); });
runNgZoneNoLog(() => macroTask(_log.fn('run1')));
runNgZoneNoLog(() => macroTask(_log.fn('run2')));
macroTask(() => {
expect(_log.result())
.toEqual('onTurnStart; run1; onTurnDone; onTurnStart; run2; onTurnDone');
.toEqual(
'onUnstable; run1; onMicrotaskEmpty; onStable; onUnstable; run2; onMicrotaskEmpty; onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart and onTurnDone before and after each turn',
it('should call onUnstable and onMicrotaskEmpty before and after each turn',
inject([AsyncTestCompleter], (async) => {
logOnTurnStart();
logOnTurnDone();
var a: PromiseCompleter<string>;
var b: PromiseCompleter<string>;
macroTask(() => {
_zone.run(() => {
runNgZoneNoLog(() => {
macroTask(() => {
a = PromiseWrapper.completer();
b = PromiseWrapper.completer();
@@ -422,8 +422,8 @@ function commonTests() {
});
});
macroTask(() => {
_zone.run(() => {
runNgZoneNoLog(() => {
macroTask(() => {
a.resolve('a');
b.resolve('b');
});
@@ -432,16 +432,13 @@ function commonTests() {
macroTask(() => {
expect(_log.result())
.toEqual(
'onTurnStart; run start; onTurnDone; onTurnStart; a then; b then; onTurnDone');
'onUnstable; run start; onMicrotaskEmpty; onStable; onUnstable; a then; b then; onMicrotaskEmpty; onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should run a function outside of the angular zone',
inject([AsyncTestCompleter], (async) => {
logOnTurnStart();
logOnTurnDone();
macroTask(() => { _zone.runOutsideAngular(_log.fn('run')); });
macroTask(() => {
@@ -450,138 +447,114 @@ function commonTests() {
});
}), testTimeout);
it('should call onTurnStart and onTurnDone when an inner microtask is scheduled from outside angular',
it('should call onUnstable and onMicrotaskEmpty when an inner microtask is scheduled from outside angular',
inject([AsyncTestCompleter], (async) => {
logOnTurnStart();
logOnTurnDone();
var completer: PromiseCompleter<any>;
macroTask(
() => { _zone.runOutsideAngular(() => { completer = PromiseWrapper.completer(); }); });
macroTask(() => {
NgZone.assertNotInAngularZone();
completer = PromiseWrapper.completer();
});
macroTask(
() => { _zone.run(() => { completer.promise.then(_log.fn('executedMicrotask')); }); });
runNgZoneNoLog(() => {
macroTask(() => {
NgZone.assertInAngularZone();
completer.promise.then(_log.fn('executedMicrotask'));
});
});
macroTask(() => {
_zone.runOutsideAngular(() => {
_log.add('scheduling a microtask');
completer.resolve(null);
});
NgZone.assertNotInAngularZone();
_log.add('scheduling a microtask');
completer.resolve(null);
});
macroTask(() => {
expect(_log.result())
.toEqual(
// First VM turn => setup Promise then
'onTurnStart; onTurnDone; ' +
// Second VM turn (outside of anguler)
'scheduling a microtask; ' +
'onUnstable; onMicrotaskEmpty; onStable; ' +
// Second VM turn (outside of angular)
'scheduling a microtask; onUnstable; ' +
// Third VM Turn => execute the microtask (inside angular)
'onTurnStart; executedMicrotask; onTurnDone');
// No onUnstable; because we don't own the task which started the turn.
'executedMicrotask; onMicrotaskEmpty; onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart before executing a microtask scheduled in onTurnDone as well as ' +
'onTurnDone after executing the task',
it('should call onUnstable only before executing a microtask scheduled in onMicrotaskEmpty ' +
'and not onMicrotaskEmpty after executing the task',
inject([AsyncTestCompleter], (async) => {
var ran = false;
logOnTurnStart();
runNgZoneNoLog(() => macroTask(_log.fn('run')));
ObservableWrapper.subscribe(_zone.onTurnDone, (_) => {
_log.add('onTurnDone(begin)');
var ran = false;
ObservableWrapper.subscribe(_zone.onMicrotaskEmpty, (_) => {
_log.add('onMicrotaskEmpty(begin)');
if (!ran) {
_zone.run(() => {
microTask(() => {
scheduleMicroTask(() => {
ran = true;
_log.add('executedMicrotask');
});
});
}
_log.add('onTurnDone(end)');
_log.add('onMicrotaskEmpty(end)');
});
macroTask(() => { _zone.run(_log.fn('run')); });
macroTask(() => {
expect(_log.result())
.toEqual(
// First VM turn => 'run' macrotask
'onTurnStart; run; onTurnDone(begin); onTurnDone(end); ' +
// Second VM Turn => microtask enqueued from onTurnDone
'onTurnStart; executedMicrotask; onTurnDone(begin); onTurnDone(end)');
'onUnstable; run; onMicrotaskEmpty; onMicrotaskEmpty(begin); onMicrotaskEmpty(end); ' +
// Second microtaskDrain Turn => microtask enqueued from onMicrotaskEmpty
'executedMicrotask; onMicrotaskEmpty; onMicrotaskEmpty(begin); onMicrotaskEmpty(end); onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart and onTurnDone for a scheduleMicrotask in onTurnDone triggered by ' +
'a scheduleMicrotask in run',
it('should call onUnstable and onMicrotaskEmpty for a scheduleMicroTask in onMicrotaskEmpty triggered by ' +
'a scheduleMicroTask in run',
inject([AsyncTestCompleter], (async) => {
var ran = false;
logOnTurnStart();
runNgZoneNoLog(() => {
macroTask(() => {
_log.add('scheduleMicroTask');
scheduleMicroTask(_log.fn('run(executeMicrotask)'));
});
});
ObservableWrapper.subscribe(_zone.onTurnDone, (_) => {
_log.add('onTurnDone(begin)');
var ran = false;
ObservableWrapper.subscribe(_zone.onMicrotaskEmpty, (_) => {
_log.add('onMicrotaskEmpty(begin)');
if (!ran) {
_log.add('onTurnDone(scheduleMicrotask)');
_log.add('onMicrotaskEmpty(scheduleMicroTask)');
_zone.run(() => {
microTask(() => {
scheduleMicroTask(() => {
ran = true;
_log.add('onTurnDone(executeMicrotask)');
_log.add('onMicrotaskEmpty(executeMicrotask)');
});
});
}
_log.add('onTurnDone(end)');
});
macroTask(() => {
_zone.run(() => {
_log.add('scheduleMicrotask');
microTask(_log.fn('run(executeMicrotask)'));
});
_log.add('onMicrotaskEmpty(end)');
});
macroTask(() => {
expect(_log.result())
.toEqual(
// First VM Turn => a macrotask + the microtask it enqueues
'onTurnStart; scheduleMicrotask; run(executeMicrotask); onTurnDone(begin); onTurnDone(scheduleMicrotask); onTurnDone(end); ' +
// Second VM Turn => the microtask enqueued from onTurnDone
'onTurnStart; onTurnDone(executeMicrotask); onTurnDone(begin); onTurnDone(end)');
'onUnstable; scheduleMicroTask; run(executeMicrotask); onMicrotaskEmpty; onMicrotaskEmpty(begin); onMicrotaskEmpty(scheduleMicroTask); onMicrotaskEmpty(end); ' +
// Second VM Turn => the microtask enqueued from onMicrotaskEmpty
'onMicrotaskEmpty(executeMicrotask); onMicrotaskEmpty; onMicrotaskEmpty(begin); onMicrotaskEmpty(end); onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should execute promises scheduled in onTurnStart before promises scheduled in run',
it('should execute promises scheduled in onUnstable before promises scheduled in run',
inject([AsyncTestCompleter], (async) => {
var donePromiseRan = false;
var startPromiseRan = false;
ObservableWrapper.subscribe(_zone.onTurnStart, (_) => {
_log.add('onTurnStart(begin)');
if (!startPromiseRan) {
_log.add('onTurnStart(schedulePromise)');
_zone.run(() => { microTask(_log.fn('onTurnStart(executePromise)')); });
startPromiseRan = true;
}
_log.add('onTurnStart(end)');
});
ObservableWrapper.subscribe(_zone.onTurnDone, (_) => {
_log.add('onTurnDone(begin)');
if (!donePromiseRan) {
_log.add('onTurnDone(schedulePromise)');
_zone.run(() => { microTask(_log.fn('onTurnDone(executePromise)')); });
donePromiseRan = true;
}
_log.add('onTurnDone(end)');
});
macroTask(() => {
_zone.run(() => {
runNgZoneNoLog(() => {
macroTask(() => {
_log.add('run start');
PromiseWrapper.resolve(null)
.then((_) => {
@@ -594,33 +567,53 @@ function commonTests() {
});
});
var donePromiseRan = false;
var startPromiseRan = false;
ObservableWrapper.subscribe(_zone.onUnstable, (_) => {
_log.add('onUnstable(begin)');
if (!startPromiseRan) {
_log.add('onUnstable(schedulePromise)');
_zone.run(() => { scheduleMicroTask(_log.fn('onUnstable(executePromise)')); });
startPromiseRan = true;
}
_log.add('onUnstable(end)');
});
ObservableWrapper.subscribe(_zone.onMicrotaskEmpty, (_) => {
_log.add('onMicrotaskEmpty(begin)');
if (!donePromiseRan) {
_log.add('onMicrotaskEmpty(schedulePromise)');
_zone.run(() => { scheduleMicroTask(_log.fn('onMicrotaskEmpty(executePromise)')); });
donePromiseRan = true;
}
_log.add('onMicrotaskEmpty(end)');
});
macroTask(() => {
expect(_log.result())
.toEqual(
// First VM turn: enqueue a microtask in onTurnStart
'onTurnStart(begin); onTurnStart(schedulePromise); onTurnStart(end); ' +
// First VM turn: enqueue a microtask in onUnstable
'onUnstable; onUnstable(begin); onUnstable(schedulePromise); onUnstable(end); ' +
// First VM turn: execute the macrotask which enqueues microtasks
'run start; run end; ' +
// First VM turn: execute enqueued microtasks
'onTurnStart(executePromise); promise then; promise foo; promise bar; ' +
'onUnstable(executePromise); promise then; promise foo; promise bar; onMicrotaskEmpty; ' +
// First VM turn: onTurnEnd, enqueue a microtask
'onTurnDone(begin); onTurnDone(schedulePromise); onTurnDone(end); ' +
'onMicrotaskEmpty(begin); onMicrotaskEmpty(schedulePromise); onMicrotaskEmpty(end); ' +
// Second VM turn: execute the microtask from onTurnEnd
'onTurnStart(begin); onTurnStart(end); onTurnDone(executePromise); onTurnDone(begin); onTurnDone(end)');
'onMicrotaskEmpty(executePromise); onMicrotaskEmpty; onMicrotaskEmpty(begin); onMicrotaskEmpty(end); onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart and onTurnDone before and after each turn, respectively',
it('should call onUnstable and onMicrotaskEmpty before and after each turn, respectively',
inject([AsyncTestCompleter], (async) => {
var completerA: PromiseCompleter<any>;
var completerB: PromiseCompleter<any>;
logOnTurnStart();
logOnTurnDone();
macroTask(() => {
_zone.run(() => {
runNgZoneNoLog(() => {
macroTask(() => {
completerA = PromiseWrapper.completer();
completerB = PromiseWrapper.completer();
completerA.promise.then(_log.fn('a then'));
@@ -629,34 +622,31 @@ function commonTests() {
});
});
macroTask(() => { _zone.run(() => { completerA.resolve(null); }); }, 20);
runNgZoneNoLog(() => { macroTask(() => { completerA.resolve(null); }, 10); });
macroTask(() => { _zone.run(() => { completerB.resolve(null); }); }, 500);
runNgZoneNoLog(() => { macroTask(() => { completerB.resolve(null); }, 20); });
macroTask(() => {
expect(_log.result())
.toEqual(
// First VM turn
'onTurnStart; run start; onTurnDone; ' +
'onUnstable; run start; onMicrotaskEmpty; onStable; ' +
// Second VM turn
'onTurnStart; a then; onTurnDone; ' +
'onUnstable; a then; onMicrotaskEmpty; onStable; ' +
// Third VM turn
'onTurnStart; b then; onTurnDone');
'onUnstable; b then; onMicrotaskEmpty; onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart and onTurnDone before and after (respectively) all turns in a chain',
it('should call onUnstable and onMicrotaskEmpty before and after (respectively) all turns in a chain',
inject([AsyncTestCompleter], (async) => {
logOnTurnStart();
logOnTurnDone();
macroTask(() => {
_zone.run(() => {
runNgZoneNoLog(() => {
macroTask(() => {
_log.add('run start');
microTask(() => {
scheduleMicroTask(() => {
_log.add('async1');
microTask(_log.fn('async2'));
scheduleMicroTask(_log.fn('async2'));
});
_log.add('run end');
});
@@ -664,24 +654,22 @@ function commonTests() {
macroTask(() => {
expect(_log.result())
.toEqual('onTurnStart; run start; run end; async1; async2; onTurnDone');
.toEqual(
'onUnstable; run start; run end; async1; async2; onMicrotaskEmpty; onStable');
async.done();
}, resultTimer);
}), testTimeout);
it('should call onTurnStart and onTurnDone for promises created outside of run body',
it('should call onUnstable and onMicrotaskEmpty for promises created outside of run body',
inject([AsyncTestCompleter], (async) => {
logOnTurnStart();
logOnTurnDone();
var promise: Promise<any>;
macroTask(() => {
_zone.runOutsideAngular(() => {
promise = PromiseWrapper.resolve(4).then((x) => PromiseWrapper.resolve(x));
});
runNgZoneNoLog(() => {
macroTask(() => {
_zone.runOutsideAngular(() => {
promise = PromiseWrapper.resolve(4).then((x) => PromiseWrapper.resolve(x));
});
_zone.run(() => {
promise.then(_log.fn('promise then'));
_log.add('zone run');
});
@@ -689,7 +677,8 @@ function commonTests() {
macroTask(() => {
expect(_log.result())
.toEqual('onTurnStart; zone run; onTurnDone; onTurnStart; promise then; onTurnDone');
.toEqual('onUnstable; zone run; onMicrotaskEmpty; onStable; ' +
'onUnstable; promise then; onMicrotaskEmpty; onStable');
async.done();
}, resultTimer);
}), testTimeout);
@@ -699,8 +688,6 @@ function commonTests() {
it('should call the on error callback when it is defined',
inject([AsyncTestCompleter], (async) => {
macroTask(() => {
logOnError();
var exception = new BaseException('sync');
_zone.run(() => { throw exception; });
@@ -712,11 +699,9 @@ function commonTests() {
}), testTimeout);
it('should call onError for errors from microtasks', inject([AsyncTestCompleter], (async) => {
logOnError();
var exception = new BaseException('async');
macroTask(() => { _zone.run(() => { microTask(() => { throw exception; }); }); });
macroTask(() => { _zone.run(() => { scheduleMicroTask(() => { throw exception; }); }); });
macroTask(() => {
expect(_errors.length).toBe(1);
@@ -20,10 +20,10 @@ import {Component, Directive, OnDestroy, platform} from 'angular2/core';
import {BROWSER_PROVIDERS, BROWSER_APP_PROVIDERS} from 'angular2/platform/browser';
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
import {DOCUMENT} from 'angular2/src/platform/dom/dom_tokens';
import {PromiseWrapper} from 'angular2/src/facade/async';
import {PromiseWrapper, TimerWrapper} from 'angular2/src/facade/async';
import {provide, Inject, Injector, PLATFORM_INITIALIZER, APP_INITIALIZER} from 'angular2/core';
import {disposePlatform} from 'angular2/src/core/application_ref';
import {ExceptionHandler} from 'angular2/src/facade/exceptions';
import {ExceptionHandler, BaseException} from 'angular2/src/facade/exceptions';
import {Testability, TestabilityRegistry} from 'angular2/src/core/testability/testability';
import {ComponentRef_, ComponentRef} from "angular2/src/core/linker/dynamic_component_loader";
@@ -119,13 +119,12 @@ export function main() {
`Could not compile '${stringify(HelloRootDirectiveIsNotCmp)}' because it is not a component.`);
expect(logger.res.join("")).toContain("Could not compile");
async.done();
return null;
});
}));
it('should throw if no element is found', inject([AsyncTestCompleter], (async) => {
var logger = new _ArrayLogger();
var exceptionHandler = new ExceptionHandler(logger, !IS_DART);
var exceptionHandler = new ExceptionHandler(logger, false);
var refPromise =
bootstrap(HelloRootCmp, [provide(ExceptionHandler, {useValue: exceptionHandler})]);
@@ -137,10 +136,25 @@ export function main() {
}));
if (DOM.supportsDOMEvents()) {
it('should forward the error to promise when bootstrap fails',
inject([AsyncTestCompleter], (async) => {
// Skip for dart since it causes a confusing error message in console when test passes.
var logger = new _ArrayLogger();
var exceptionHandler = new ExceptionHandler(logger, false);
var refPromise =
bootstrap(HelloRootCmp, [provide(ExceptionHandler, {useValue: exceptionHandler})]);
PromiseWrapper.then(refPromise, null, (reason: BaseException) => {
expect(reason.message)
.toContain('The selector "hello-app" did not match any elements');
async.done();
});
}));
it('should invoke the default exception handler when bootstrap fails',
inject([AsyncTestCompleter], (async) => {
var logger = new _ArrayLogger();
var exceptionHandler = new ExceptionHandler(logger, !IS_DART);
var exceptionHandler = new ExceptionHandler(logger, false);
var refPromise =
bootstrap(HelloRootCmp, [provide(ExceptionHandler, {useValue: exceptionHandler})]);
-2
View File
@@ -162,7 +162,6 @@ var NG_CORE = [
'DirectiveResolver',
'DynamicComponentLoader',
'ElementRef',
'ErrorHandlingFn:dart',
'Output',
'EmbeddedViewRef',
'EventEmitter',
@@ -244,7 +243,6 @@ var NG_CORE = [
'ViewResolver',
'WrappedException',
'WrappedValue',
'ZeroArgFunction:dart',
'asNativeElements',
'bind',
'provide',
@@ -51,7 +51,7 @@ export function main() {
el = DOM.createElement('app-cmp', fakeDoc);
DOM.appendChild(fakeDoc.body, el);
var logger = new _ArrayLogger();
var exceptionHandler = new ExceptionHandler(logger, !IS_DART);
var exceptionHandler = new ExceptionHandler(logger, false);
testBindings = [
ROUTER_PROVIDERS,
provide(LocationStrategy, {useClass: MockLocationStrategy}),