-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
170 lines (145 loc) · 5.11 KB
/
test.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict';
/* eslint-disable import/no-extraneous-dependencies */
const {expect} = require('chai');
const contextor = require('./index');
function executeInSpecificAsyncContext(execute, done) {
setTimeout(() => {
setTimeout(execute, 0);
if (done) {
setTimeout(done, 10);
}
}, 0);
}
function buildMultiDone(done) {
let id = 0;
const executions = [];
return () => {
const executionId = id++;
executions.push(executionId);
return () => {
executions.splice(executions.indexOf(executionId), 1);
if (executions.length === 0) {
done();
}
};
};
}
describe('contextor', () => {
it('should create a context allowing to set and get value', (done) => {
executeInSpecificAsyncContext(() => {
expect(contextor.create().set('foo', 'bar').get('foo')).to.equal('bar');
}, done);
});
it('should failed to set and get value on missing context', (done) => {
executeInSpecificAsyncContext(() => {
expect(() => contextor.set('foo', 'bar')).to.throw(
ReferenceError,
'No current context found; use \'create\' method to create one'
);
expect(() => contextor.get('foo')).to.throw(
ReferenceError,
'No current context found; use \'create\' method to create one'
);
}, done);
});
it('should failed to get a not defined value on the current context', (done) => {
executeInSpecificAsyncContext(() => {
expect(() => contextor.create().get('foo')).to.throw(
ReferenceError,
'No value found in context for \'foo\' key'
);
}, done);
});
it('should succeed to get a not defined value on the current context if a default value is given', (done) => {
executeInSpecificAsyncContext(() => {
expect(contextor.create().get('foo', 'bar')).to.equal('bar');
}, done);
});
it('should failed to get a value on a missing context if allowed but no default value is given', (done) => {
executeInSpecificAsyncContext(() => {
expect(() => contextor.get('foo', undefined, true)).to.throw(
ReferenceError,
'No current context found; use \'create\' method to create one'
);
}, done);
});
it('should succeed to get a value on a missing context if allowed and a default value is given', (done) => {
executeInSpecificAsyncContext(() => {
expect(contextor.get('foo', 'bar', true)).to.equal('bar');
}, done);
});
it('should pass context along asynchronous resource chain', (done) => {
const multiDone = buildMultiDone(done);
executeInSpecificAsyncContext(() => {
expect(contextor.create().set('plop', 'plip').get('plop')).to.equal('plip');
(new Promise((resolve) => {
executeInSpecificAsyncContext(() => {
expect(contextor.get('plop')).to.equal('plip');
expect(contextor.set('plop', 'plap').get('plop')).to.equal('plap');
resolve();
}, multiDone());
})).then(() => {
executeInSpecificAsyncContext(() => {
expect(contextor.get('plop')).to.equal('plap');
}, multiDone());
});
}, multiDone());
});
it('should handle embedded contexts', (done) => {
const multiDone = buildMultiDone(done);
executeInSpecificAsyncContext(() => {
expect(contextor.create().set('plop', 'plip').get('plop')).to.equal('plip');
executeInSpecificAsyncContext(() => {
expect(contextor.create().set('plop', 'plap').get('plop')).to.equal('plap');
executeInSpecificAsyncContext(() => {
expect(contextor.get('plop')).to.equal('plap');
}, multiDone());
}, multiDone());
executeInSpecificAsyncContext(() => {
expect(contextor.get('plop')).to.equal('plip');
}, multiDone());
}, multiDone());
});
it('should be debuggable', () => {
const memoryUsage = contextor.getMemoryUsage();
expect(Object.getOwnPropertyNames(memoryUsage)).to.deep.equal(['processMemory', 'sizes', 'contents']);
expect(memoryUsage.processMemory).to.haveOwnProperty('heapUsed');
expect(Object.getOwnPropertyNames(memoryUsage.sizes)).to.deep.equal([
'resourceTree',
'contexts',
'childResources',
'parentResources',
'destroyedResources'
]);
expect(Object.getOwnPropertyNames(memoryUsage.contents)).to.deep.equal([
'resourceTree',
'contexts',
'childResources',
'parentResources',
'destroyedResources'
]);
});
it('should clean expired contexts', (done) => {
let memoryUsage = contextor.getMemoryUsage();
expect(memoryUsage.sizes.contexts).to.be.greaterThan(1);
setTimeout(() => {
contextor.create();
contextor.create();
memoryUsage = contextor.getMemoryUsage();
expect(memoryUsage.sizes.contexts).equal(1);
setTimeout(() => {
contextor.create();
contextor.create();
memoryUsage = contextor.getMemoryUsage();
expect(memoryUsage.sizes.contexts).equal(1);
done();
}, 400);
}, 400);
setTimeout(() => {
contextor.create();
contextor.create();
memoryUsage = contextor.getMemoryUsage();
expect(memoryUsage.sizes.contexts).equal(2);
}, 500);
});
});