forked from cornerstonejs/cornerstone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageCache_test.js
More file actions
306 lines (238 loc) · 8.67 KB
/
Copy pathimageCache_test.js
File metadata and controls
306 lines (238 loc) · 8.67 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { assert } from 'chai'; // eslint-disable-line import/extensions
import { default as imageCache,
setMaximumSizeBytes,
putImageLoadObject,
getImageLoadObject,
removeImageLoadObject,
getCacheInfo,
// changeImageIdCacheSize,
purgeCache } from '../src/imageCache.js';
import { events } from '../src/events.js';
describe('Set maximum cache size', function () {
it('should allow setting of cache size', function () {
// Arrange
const maximumSizeInBytes = 1024 * 1024 * 1024;
// Act
setMaximumSizeBytes(maximumSizeInBytes);
// Assert
const cacheInfo = getCacheInfo();
assert.equal(cacheInfo.maximumSizeInBytes, maximumSizeInBytes);
});
it('should fail if numBytes is not defined', function () {
assert.throws(() => setMaximumSizeBytes(undefined));
});
it('should fail if numBytes is not a number', function () {
assert.throws(() => setMaximumSizeBytes('10000'));
});
});
describe('Store, retrieve, and remove imagePromises from the cache', function () {
before(function () {
// Act
purgeCache();
});
beforeEach(function () {
// Arrange
this.image = {
imageId: 'anImageId',
sizeInBytes: 100
};
this.imageLoadObject = {
promise: new Promise((resolve) => {
resolve(this.image);
}),
cancelFn: undefined
};
});
afterEach(function () {
purgeCache();
});
it('should allow image promises to be added to the cache (putImageLoadObject)', function (done) {
const image = this.image;
const imageLoadObject = this.imageLoadObject;
// Act
putImageLoadObject(image.imageId, imageLoadObject);
imageLoadObject.promise.then(() => {
// Assert
const cacheInfo = getCacheInfo();
assert.equal(cacheInfo.numberOfImagesCached, 1);
assert.equal(cacheInfo.cacheSizeInBytes, this.image.sizeInBytes);
done();
});
});
it('should not change cache size if sizeInBytes is undefined (putImagePromise)', function (done) {
// Arrange
this.image.sizeInBytes = undefined;
putImageLoadObject(this.image.imageId, this.imageLoadObject);
// Act
this.imageLoadObject.promise.then(() => {
const cacheInfo = getCacheInfo();
// Assert
assert.equal(cacheInfo.numberOfImagesCached, 1);
assert.equal(cacheInfo.cacheSizeInBytes, 0);
done();
});
});
it('should not change cache size if sizeInBytes is not a number (putImagePromise)', function (done) {
// Arrange
this.image.sizeInBytes = '10000';
putImageLoadObject(this.image.imageId, this.imageLoadObject);
// Act
this.imageLoadObject.promise.then(() => {
const cacheInfo = getCacheInfo();
// Assert
assert.equal(cacheInfo.numberOfImagesCached, 1);
assert.equal(cacheInfo.cacheSizeInBytes, 0);
done();
});
});
it('should throw an error if imageId is not defined (putImageLoadObject)', function () {
// Assert
assert.throws(() => putImageLoadObject(undefined, this.imageLoadObject));
});
it('should throw an error if imagePromise is not defined (putImageLoadObject)', function () {
// Assert
assert.throws(() => putImageLoadObject(this.image.imageId, undefined));
});
it('should throw an error if imageId is already in the cache (putImageLoadObject)', function () {
// Arrange
putImageLoadObject(this.image.imageId, this.imageLoadObject);
// Assert
assert.throws(() => putImageLoadObject(this.image.imageId, this.imageLoadObject));
});
it('should allow image promises to be retrieved from the cache (getImageLoadObject()', function () {
const image = this.image;
const imageLoadObject = this.imageLoadObject;
// Act
putImageLoadObject(image.imageId, imageLoadObject);
// Assert
const retrievedImageLoadObject = getImageLoadObject(image.imageId);
assert.equal(imageLoadObject, retrievedImageLoadObject);
});
it('should throw an error if imageId is not defined (getImageLoadObject()', function () {
// Assert
assert.throws(() => getImageLoadObject(undefined));
});
it('should fail silently to retrieve a promise for an imageId not in the cache', function () {
// Act
const retrievedImageLoadObject = getImageLoadObject('AnImageIdNotInCache');
// Assert
assert.isUndefined(retrievedImageLoadObject, undefined);
});
it('should allow image promises to be removed from the cache (removeImagePromise)', function () {
const image = this.image;
const imageLoadObject = this.imageLoadObject;
// Arrange
putImageLoadObject(image.imageId, imageLoadObject);
// Act
removeImageLoadObject(image.imageId);
// Assert
imageLoadObject.promise.then(() => {
// Fail if the Promise is resolved.
assert.equal(true, false);
});
// Make sure that the cache is now empty
const cacheInfo = getCacheInfo();
assert.equal(cacheInfo.numberOfImagesCached, 0);
assert.equal(cacheInfo.cacheSizeInBytes, 0);
});
it('should fail if imageId is not defined (removeImagePromise)', function () {
assert.throws(() => removeImageLoadObject(undefined));
});
it('should fail if imageId is not in cache (removeImagePromise)', function () {
assert.throws(() => removeImageLoadObject('RandomImageId'));
});
/* it('should allow image promises to have their cache size changed', function () {
const image = this.image;
const imageLoadObject = this.imageLoadObject;
const newCacheSize = 500;
// Arrange
putImageLoadObject(image.imageId, imageLoadObject);
// Act
changeImageIdCacheSize(image.imageId, newCacheSize);
const cacheInfo = getCacheInfo();
assert.equal(cacheInfo.numberOfImagesCached, 1);
assert.equal(image.sizeInBytes, newCacheSize);
assert.equal(cacheInfo.cacheSizeInBytes, newCacheSize);
}); */
it('should be able to purge the entire cache', function (done) {
const image = this.image;
const imageLoadObject = this.imageLoadObject;
// Arrange
putImageLoadObject(image.imageId, imageLoadObject);
imageLoadObject.promise.then(() => {
// Act
purgeCache();
// Make sure that the cache is now empty
const cacheInfo = getCacheInfo();
assert.equal(cacheInfo.numberOfImagesCached, 0);
assert.equal(cacheInfo.cacheSizeInBytes, 0);
assert.isEmpty(imageCache.imageCache);
done();
});
});
it('should be able to kick the oldest image out of the cache', function (done) {
// Arrange
const maxCacheSize = 1000;
const promises = [];
setMaximumSizeBytes(maxCacheSize);
for (let i = 0; i < 10; i++) {
// Create the image
const image = {
imageId: `imageId-${i}`,
sizeInBytes: 100
};
image.decache = () => console.log('decaching image');
const imageLoadObject = {
promise: new Promise((resolve) => {
resolve(image);
}),
cancelFn: undefined
};
// Add it to the cache
putImageLoadObject(image.imageId, imageLoadObject);
promises.push(imageLoadObject.promise);
}
// Retrieve a few of the imagePromises in order to bump their timestamps
getImageLoadObject('imageId-5');
getImageLoadObject('imageId-4');
getImageLoadObject('imageId-6');
// Setup event listeners to check that the promise removed and cache full events have fired properly
events.addEventListener('cornerstoneimagecachepromiseremoved', (event) => {
const imageId = event.detail.imageId;
// Detect that the earliest image added has been removed
// TODO: Figure out how to change the test setup to ensure the same
// image is always kicked out of the cache. It looks like timestamps
// are not in the expected order, probably since handling the promise
// resolving is async
// assert.equal(imageId, 'imageId-0');
assert.isDefined(imageId);
done();
});
events.addEventListener('cornerstoneimagecachefull', (event) => {
assert.equal(event.detail.numberOfImagesCached, 10);
assert.equal(event.detail.cacheSizeInBytes, maxCacheSize);
done();
});
// Act
// Create another image which will push us over the cache limit
const extraImage = {
imageId: 'imageId-11',
sizeInBytes: 100
};
const extraImageLoadObject = {
promise: new Promise((resolve) => {
resolve(extraImage);
}),
cancelFn: undefined
};
Promise.all(promises).then(() => {
// Add it to the cache
putImageLoadObject(extraImage.imageId, extraImageLoadObject);
// Make sure that the cache has pushed out the first image
const cacheInfo = getCacheInfo();
assert.equal(cacheInfo.numberOfImagesCached, 10);
assert.equal(cacheInfo.cacheSizeInBytes, 1000);
done();
});
});
});