-
Notifications
You must be signed in to change notification settings - Fork 1
/
memoryManagement.spec.js
92 lines (80 loc) · 2.89 KB
/
memoryManagement.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import LeakDetector from 'jest-leak-detector'
import iniettore from '../../src'
import { CONSTRUCTOR, LAZY, PROVIDER, SINGLETON, TRANSIENT } from '../../src'
describe('Given a context', () => {
describe('with a TRANSIENT, SINGLETON mapping', () => {
describe('when releasing an instance generated with such mapping', () => {
it('should remove any reference to the instance so that memory can be freed correctly', () => {
class Foo {}
let detector
const rootContext = iniettore.create(map => {
map('foo')
.to(() => {
const instance = new Foo()
detector = new LeakDetector(instance)
return instance
})
.as(TRANSIENT, SINGLETON, PROVIDER)
})
rootContext.get('foo')
rootContext.release('foo')
expect(detector.isLeaking()).toBe(false)
})
})
})
describe('with a SINGLETON mapping', () => {
describe('when disposing the context', () => {
it('should remove any reference to instances create with such mapping so that memory can be freed correctly', () => {
class Foo {}
let detector
const rootContext = iniettore.create(map => {
map('foo')
.to(() => {
const instance = new Foo()
detector = new LeakDetector(instance)
return instance
})
.as(LAZY, SINGLETON, PROVIDER)
})
rootContext.get('foo')
rootContext.dispose()
expect(detector.isLeaking()).toBe(false)
})
})
})
describe('with a child context that has some SINGLETON instances instatiantes', () => {
describe('when disposing the child context', () => {
it('should remove any reference to such instances so that memory can be freed correctly', () => {
class Foo {}
let detector
const rootContext = iniettore.create(() => void 0)
const childContext = rootContext.createChild(map => {
map('foo')
.to(() => {
const instance = new Foo()
detector = new LeakDetector(instance)
return instance
})
.as(LAZY, SINGLETON, PROVIDER)
})
childContext.get('foo')
childContext.dispose()
expect(detector.isLeaking()).toBe(false)
})
it('should remove any internal reference to the child context itself so that memory can be freed correctly', () => {
class Foo {}
const rootContext = iniettore.create(() => void 0)
let childContext = rootContext.createChild(map => {
map('foo')
.to(Foo)
.as(LAZY, SINGLETON, CONSTRUCTOR)
})
const detector = new LeakDetector(childContext)
childContext.get('foo')
childContext.dispose()
childContext = null
expect(detector.isLeaking()).toBe(false)
})
})
})
})