Skip to content

Commit

Permalink
Merge pull request #16609 from emberjs/container-tracking
Browse files Browse the repository at this point in the history
Add container leak tracking
  • Loading branch information
rwjblue authored May 4, 2018
2 parents 0f0842e + 4472ce6 commit 2501d75
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/container/lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@ import { OWNER, setOwner } from 'ember-owner';
import { assign } from '@ember/polyfills';
import { dictionary, HAS_NATIVE_PROXY } from 'ember-utils';

let leakTracking, containers;
if (DEBUG) {
// requires v8
// chrome --js-flags="--allow-natives-syntax --expose-gc"
// node --allow-natives-syntax --expose-gc
try {
/* globals gc, WeakSet */
if (typeof gc === 'function') {
leakTracking = (() => {
// avoid syntax errors when --allow-natives-syntax not present
let GetWeakSetValues = new Function('weakSet', 'return %GetWeakSetValues(weakSet, 0)');
containers = new WeakSet();
return {
hasContainers() {
gc();
return GetWeakSetValues(containers).length > 0;
},
reset() {
let values = GetWeakSetValues(containers);
for (let i = 0; i < values.length; i++) {
containers.delete(values[i]);
}
},
};
})();
}
} catch (e) {
// ignore
}
}

/**
A container used to instantiate and cache objects.
Expand All @@ -29,6 +60,9 @@ export default class Container {

if (DEBUG) {
this.validationCache = dictionary(options.validationCache || null);
if (containers !== undefined) {
containers.add(this);
}
}
}

Expand Down Expand Up @@ -160,6 +194,10 @@ export default class Container {
}
}

if (DEBUG) {
Container._leakTracking = leakTracking;
}

/*
* Wrap a factory manager in a proxy which will not permit properties to be
* set on the manager.
Expand Down
34 changes: 34 additions & 0 deletions packages/internal-test-helpers/lib/ember-dev/containers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Container } from 'container';

function ContainersAssert(env) {
this.env = env;
}

const { _leakTracking: containerLeakTracking } = Container;

ContainersAssert.prototype = {
reset: function() {},
inject: function() {},
assert: function() {
if (containerLeakTracking === undefined) return;
let { config } = QUnit;
let { testName, testId, module: { name: moduleName }, finish: originalFinish } = config.current;
config.current.finish = function() {
originalFinish.call(this);
originalFinish = undefined;
config.queue.unshift(function() {
if (containerLeakTracking.hasContainers()) {
containerLeakTracking.reset();
// eslint-disable-next-line no-console
console.assert(
false,
`Leaked container after test ${moduleName}: ${testName} testId=${testId}`
);
}
});
};
},
restore: function() {},
};

export default ContainersAssert;
2 changes: 2 additions & 0 deletions packages/internal-test-helpers/lib/ember-dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import WarningAssert from './warning';
import AssertionAssert from './assertion';
import RunLoopAssert from './run-loop';
import NamespacesAssert from './namespaces';
import ContainersAssert from './containers';

import { buildCompositeAssert } from './utils';

Expand All @@ -12,6 +13,7 @@ var EmberDevTestHelperAssert = buildCompositeAssert([
AssertionAssert,
RunLoopAssert,
NamespacesAssert,
ContainersAssert,
]);

export default EmberDevTestHelperAssert;

0 comments on commit 2501d75

Please sign in to comment.