This repository has been archived by the owner on Apr 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
252 lines (206 loc) · 5.93 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
"use strict";
var assert, cache;
assert = require("assert");
cache = require("./");
describe("cache", function() {
var inc, n;
inc = function(callback) {
process.nextTick(function() {
callback(null, n++);
});
};
beforeEach(function() {
n = 0;
});
describe("once", function() {
it("should only call the backing cache function once", function(done) {
var cinc = cache.once(inc);
cinc(function(err, n) {
assert.ifError(err);
assert.equal(n, 0);
cinc(function(err, n) {
assert.ifError(err);
assert.equal(n, 0);
cinc(function(err, n) {
assert.ifError(err);
assert.equal(n, 0);
done(null);
});
});
});
});
it("should call the backing cache function once even if simultaneous " +
"calls are made", function(done) {
var cinc = cache.once(inc),
m = 0;
cinc(function(err, n) {
assert.ifError(err);
assert.equal(n, 0);
if(++m === 3) {
done(null);
}
});
cinc(function(err, n) {
assert.ifError(err);
assert.equal(n, 0);
if(++m === 3) {
done(null);
}
});
cinc(function(err, n) {
assert.ifError(err);
assert.equal(n, 0);
if(++m === 3) {
done(null);
}
});
});
});
describe("timeBasedWithGrace", function() {
it("should only update the cache as appropriate", function(done) {
var cinc = cache.timeBasedWithGrace(inc, 100, 1000);
/* First datapoint. */
cinc(0, function(err, n) {
assert.ifError(err);
assert.deepEqual(n, 0);
/* Read cached. */
cinc(50, function(err, n) {
assert.ifError(err);
assert.deepEqual(n, 0);
/* Soft timeout: return cached but update in the background. */
cinc(200, function(err, n) {
assert.ifError(err);
assert.deepEqual(n, 0);
/* Read cached. */
cinc(210, function(err, n) {
assert.ifError(err);
assert.deepEqual(n, 1);
/* Hard timeout: update n before returning. */
cinc(2500, function(err, n) {
assert.ifError(err);
assert.deepEqual(n, 2);
done(null);
});
});
});
});
});
});
it("should only call the callback once if called multiple times " +
"simultaneously", function(done) {
var cinc, m;
cinc = cache.timeBasedWithGrace(inc, 100, 1000);
m = 0;
cinc(0, function(err, n) {
assert.ifError(err);
assert.strictEqual(n, 0);
if(++m === 3) {
done(null);
}
});
cinc(0, function(err, n) {
assert.ifError(err);
assert.strictEqual(n, 0);
if(++m === 3) {
done(null);
}
});
cinc(0, function(err, n) {
assert.ifError(err);
assert.strictEqual(n, 0);
if(++m === 3) {
done(null);
}
});
});
it("should only call the callback once if called multiple times " +
"simultaneously in soft timeout state", function(done) {
var cinc;
cinc = cache.timeBasedWithGrace(inc, 100, 1000);
cinc(0, function(err, n) {
var m;
assert.ifError(err);
assert.strictEqual(n, 0);
m = 0;
cinc(200, function(err, n) {
assert.ifError(err);
assert.strictEqual(n, 0);
if(++m === 3) {
done(null);
}
});
/* We assume that by the time this gets executed, the background update
* succeeded... */
cinc(200, function(err, n) {
assert.ifError(err);
assert.strictEqual(n, 1);
if(++m === 3) {
done(null);
}
});
cinc(200, function(err, n) {
assert.ifError(err);
assert.strictEqual(n, 1);
if(++m === 3) {
done(null);
}
});
});
});
/* FIXME: Ensure that calling the function a bajillion times causes each
* callback to get called. */
});
describe("sizeBasedKeyValue", function() {
var inc, n;
inc = function(key, callback) {
process.nextTick(function() {
callback(null, key ^ n++);
});
};
beforeEach(function() {
n = 0;
});
it("should cache for each key, until you fill the space", function(done) {
var f = cache.sizeBasedKeyValue(inc, 2);
f(20, function(err, value) {
assert.ifError(err);
assert.equal(value, 20);
f(20, function(err, value) {
assert.ifError(err);
assert.equal(value, 20);
f(40, function(err, value) {
assert.ifError(err);
assert.equal(value, 41);
f(20, function(err, value) {
assert.ifError(err);
assert.equal(value, 20);
f(40, function(err, value) {
assert.ifError(err);
assert.equal(value, 41);
f(30, function(err, value) {
assert.ifError(err);
assert.equal(value, 28);
f(40, function(err, value) {
assert.ifError(err);
assert.equal(value, 41);
f(20, function(err, value) {
assert.ifError(err);
assert.equal(value, 23);
f(40, function(err, value) {
assert.ifError(err);
assert.equal(value, 41);
done(null);
});
});
});
});
});
});
});
});
});
});
/* FIXME: Ensure that calling the function a bajillion times causes each
* callback to get called. */
});
});