test: fix memory leak when running test campaign (#11072)

This commit is contained in:
Marc Laval
2016-08-25 23:37:46 +02:00
committed by Victor Berchet
parent 566d4361e2
commit d7c82f5c0f
6 changed files with 92 additions and 67 deletions
@@ -57,6 +57,7 @@ export class ComponentFixture<T> {
private _autoDetect: boolean;
private _isStable: boolean = true;
private _isDestroyed: boolean = false;
private _resolve: (result: any) => void;
private _promise: Promise<any> = null;
private _onUnstableSubscription: any /** TODO #9100 */ = null;
@@ -178,22 +179,25 @@ export class ComponentFixture<T> {
* Trigger component destruction.
*/
destroy(): void {
this.componentRef.destroy();
if (this._onUnstableSubscription != null) {
this._onUnstableSubscription.unsubscribe();
this._onUnstableSubscription = null;
}
if (this._onStableSubscription != null) {
this._onStableSubscription.unsubscribe();
this._onStableSubscription = null;
}
if (this._onMicrotaskEmptySubscription != null) {
this._onMicrotaskEmptySubscription.unsubscribe();
this._onMicrotaskEmptySubscription = null;
}
if (this._onErrorSubscription != null) {
this._onErrorSubscription.unsubscribe();
this._onErrorSubscription = null;
if (!this._isDestroyed) {
this.componentRef.destroy();
if (this._onUnstableSubscription != null) {
this._onUnstableSubscription.unsubscribe();
this._onUnstableSubscription = null;
}
if (this._onStableSubscription != null) {
this._onStableSubscription.unsubscribe();
this._onStableSubscription = null;
}
if (this._onMicrotaskEmptySubscription != null) {
this._onMicrotaskEmptySubscription.unsubscribe();
this._onMicrotaskEmptySubscription = null;
}
if (this._onErrorSubscription != null) {
this._onErrorSubscription.unsubscribe();
this._onErrorSubscription = null;
}
this._isDestroyed = true;
}
}
}
+7 -1
View File
@@ -11,6 +11,7 @@ import {ListWrapper} from '../src/facade/collection';
import {BaseException} from '../src/facade/exceptions';
import {FunctionWrapper, stringify} from '../src/facade/lang';
import {Type} from '../src/type';
import {AsyncTestCompleter} from './async_test_completer';
import {ComponentFixture} from './component_fixture';
import {MetadataOverride} from './metadata_override';
@@ -155,6 +156,7 @@ export class TestBed implements Injector {
private _declarations: Array<Type<any>|any[]|any> = [];
private _imports: Array<Type<any>|any[]|any> = [];
private _schemas: Array<SchemaMetadata|any[]> = [];
private _activeFixtures: ComponentFixture<any>[] = [];
/**
* Initialize the environment for testing with a compiler factory, a PlatformRef, and an
@@ -203,6 +205,8 @@ export class TestBed implements Injector {
this._imports = [];
this._schemas = [];
this._instantiated = false;
this._activeFixtures.forEach((fixture) => fixture.destroy());
this._activeFixtures = [];
}
platform: PlatformRef = null;
@@ -355,7 +359,9 @@ export class TestBed implements Injector {
return new ComponentFixture<T>(componentRef, ngZone, autoDetect);
};
return ngZone == null ? initComponent() : ngZone.run(initComponent);
const fixture = ngZone == null ? initComponent() : ngZone.run(initComponent);
this._activeFixtures.push(fixture);
return fixture;
}
}