-
Notifications
You must be signed in to change notification settings - Fork 17
/
test.js
264 lines (207 loc) · 7.48 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
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
'use strict';
const path = require('path');
const Cache = require('./');
const Metric = require('./lib/metric');
const fs = require('fs');
const chai = require('chai');
const expect = chai.expect;
const RSVP = require('rsvp');
const Mode = require('stat-mode');
const crypto = require('crypto');
const heimdall = require('heimdalljs');
const MODE = process.platform === 'win32' ? '-rw-rw-rw-' : '-rw-------';
describe('cache', function() {
let cache;
const key = 'path/to/file.js';
const value = 'Some test value';
const longKey = 'GET|https://api.example.com/lorem/ipsum/dolor/sit/amet/consectetur/adipiscing/elit?donec=in&consequat=nibh&mauris=condimentum&turpis=at&lacus=finibus&ut=rutrum&lorem=dictum&morbi=dictum&ac=lectus&et=porttitor&donec=vel&dolor=ex&cras=aliquam&risus=in&tellus=mollis&elementum=pellentesque&lobortis=a&ex=nec&egestas=nunc&nec=feugiat&ante=integer&sit=amet&nibh=id&nisi=vulputate&condimentum=aliquam&lacinia=dignissim';
const keyHash = crypto.createHash('sha1').update(key).digest('hex');
const longKeyHash = crypto.createHash('sha1').update(longKey).digest('hex');
beforeEach(function() {
cache = new Cache();
});
afterEach(function() {
return cache.clear();
});
it('has expected default root', function() {
let os = require('os');
let tmpdir = os.tmpdir();
let username = require('username-sync')();
let descriptiveName = 'if-you-need-to-delete-this-open-an-issue-async-disk-cache';
let defaultKey = 'default-disk-cache';
expect(cache.root).to.eql(path.join(tmpdir, username, descriptiveName, defaultKey));
});
it('pathFor', function() {
expect(cache.pathFor(key)).to.be.equal(path.join(cache.root, keyHash));
expect(cache.pathFor(longKey)).to.be.equal(path.join(cache.root, longKeyHash));
});
it('set', async function() {
let filePath = await cache.set(key, value);
// credit @jgable
let mode = new Mode(fs.statSync(filePath));
expect(mode.toString()).to.equal(MODE);
expect(fs.readFileSync(filePath).toString()).equal(value);
});
it('get (doesn\'t exist)', async function() {
expect((await cache.get(key)).isCached).be.false;
});
it('get (does exist)', async function() {
let filePath = await cache.set(key, value);
let details = await cache.get(key);
expect(details.isCached).be.true;
expect(details.value).equal(value);
expect(details.key).equal(filePath);
});
it('has (doesn\'t exist)', async function() {
expect(await cache.has(key)).be.false;
});
it('has (does exist)', async function() {
await cache.set(key, value);
expect(await cache.has(key)).be.true;
});
it('has (does exist) (long key)', async function() {
await cache.set(longKey, value)
expect(await cache.has(longKey)).be.true;
});
it('remove', async function() {
await cache.set(key, value)
expect(await cache.has(key)).be.true;
await cache.remove(key);
expect(await cache.has(key)).be.false;
});
it('handles concurrent operations', async function() {
await RSVP.Promise.all([
cache.get(key).then(details => expect(details.isCached).be.false),
cache.get(key).then(details => expect(details.isCached).be.false)
]);
});
it('properly stops metrics when an error occurs', function() {
expect(() => cache.pathFor()).to.throw();
expect(heimdall.statsFor('async-disk-cache').pathFor.startTime).to.be.undefined;
});
});
const zlib = require('zlib');
const inflate = RSVP.denodeify(zlib.inflate);
const gunzip = RSVP.denodeify(zlib.gunzip);
const inflateRaw = RSVP.denodeify(zlib.inflateRaw);
describe('cache compress: [ deflate ]', function() {
let cache;
let key = 'path/to/file.js';
let value = 'Some test value';
beforeEach(function() {
cache = new Cache('my-testing-cache', {
compression: 'deflate'
});
});
afterEach(function() {
return cache.clear();
});
it('set', async function() {
let filePath = await cache.set(key, value);
let mode = new Mode(fs.statSync(filePath));
expect(mode.toString()).to.equal(MODE);
let result = await inflate(fs.readFileSync(filePath));
result = result.toString();
expect(result).equal(value);
let detail = await cache.get(key);
expect(detail.value).equal(value);
});
});
describe('cache compress: [ gzip ]', function() {
let cache;
let key = 'path/to/file.js';
let value = 'Some test value';
beforeEach(function() {
cache = new Cache('my-testing-cache', {
compression: 'gzip'
});
});
afterEach(function() {
return cache.clear();
});
it('set', async function() {
let filePath = await cache.set(key, value);
let result = await gunzip(fs.readFileSync(filePath));
result = result.toString();
expect(result).equal(value);
let detail = await cache.get(key);
expect(detail.value).equal(value);
});
});
describe('cache compress: [ deflateRaw ]', function() {
let cache;
let key = 'path/to/file.js';
let value = 'Some test value';
beforeEach(function() {
cache = new Cache('my-testing-cache', {
compression: 'deflateRaw'
});
});
afterEach(function() {
return cache.clear();
});
it('set', async function() {
let filePath = await cache.set(key, value);
let mode = new Mode(fs.statSync(filePath));
expect(mode.toString()).to.equal(MODE);
let result = await inflateRaw(fs.readFileSync(filePath));
result = result.toString();
expect(result).equal(value);
let detail = await cache.get(key);
expect(detail.value).equal(value);
});
});
describe('buffer support', function() {
let key = 'buffer_fixed';
let value = fs.readFileSync('./common/bufferdemo.png');
let cache = new Cache('my-testing-cache', { supportBuffer: true });
it('set', async function() {
// set file to cache
await cache.set(key, value)
// get file from cache
const cacheEntry = await cache.get(key);
fs.writeFileSync('./common/bufferdemo_fromcache.png', cacheEntry.value);
let oldFile = fs.readFileSync('./common/bufferdemo.png');
let newFile = fs.readFileSync('./common/bufferdemo_fromcache.png');
if (oldFile.toString('binary') !== newFile.toString('binary')) {
throw new Error('Files didn\'t match!');
}
});
});
describe('buffer support disabled', function() {
let key = 'buffer_fixed';
let value = fs.readFileSync('./common/bufferdemo.png');
let cache = new Cache('my-testing-cache');
it('set', async function() {
await cache.set(key, value);
// get file from cache
const cacheEntry = await cache.get(key);
fs.writeFileSync('./common/bufferdemo_fromcache.png', cacheEntry.value);
let oldFile = fs.readFileSync('./common/bufferdemo.png');
let newFile = fs.readFileSync('./common/bufferdemo_fromcache.png');
if (oldFile.toString('binary') !== newFile.toString('binary')) {
} else {
throw new Error('Files still matches, looks like nodejs community has fixed Buffer -> to string -> to buffer conversion bug. Applaud!');
}
});
});
describe('metric', function() {
it('throws error if stop called more than start', function() {
const metric = new Metric();
expect(() => {
metric.stop();
}).to.throw('Called stop more times than start was called');
});
it('can safely call start and stop multiple times', function() {
const metric = new Metric();
metric.start();
metric.start();
metric.stop();
metric.start();
metric.stop();
metric.stop();
const json = metric.toJSON();
expect(json.count).to.equal(3);
expect(json.time).to.be.greaterThan(0);
});
});