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
|
var async = require('../lib');
var expect = require('chai').expect;
var assert = require('assert');
describe("memoize", function() {
it('memoize', function(done) {
var call_order = [];
var fn = function (arg1, arg2, callback) {
async.setImmediate(function () {
call_order.push(['fn', arg1, arg2]);
callback(null, arg1 + arg2);
});
};
var fn2 = async.memoize(fn);
fn2(1, 2, function (err, result) {
assert(err === null, err + " passed instead of 'null'");
expect(result).to.equal(3);
fn2(1, 2, function (err, result) {
expect(result).to.equal(3);
fn2(2, 2, function (err, result) {
expect(result).to.equal(4);
expect(call_order).to.eql([['fn',1,2], ['fn',2,2]]);
done();
});
});
});
});
it('maintains asynchrony', function(done) {
var call_order = [];
var fn = function (arg1, arg2, callback) {
call_order.push(['fn', arg1, arg2]);
async.setImmediate(function () {
call_order.push(['cb', arg1, arg2]);
callback(null, arg1 + arg2);
});
};
var fn2 = async.memoize(fn);
fn2(1, 2, function (err, result) {
expect(result).to.equal(3);
fn2(1, 2, function (err, result) {
expect(result).to.equal(3);
async.nextTick(memoize_done);
call_order.push('tick3');
});
call_order.push('tick2');
});
call_order.push('tick1');
function memoize_done() {
var async_call_order = [
['fn',1,2], // initial async call
'tick1', // async caller
['cb',1,2], // async callback
// ['fn',1,2], // memoized // memoized async body
'tick2', // handler for first async call
// ['cb',1,2], // memoized // memoized async response body
'tick3' // handler for memoized async call
];
expect(call_order).to.eql(async_call_order);
done();
}
});
it('unmemoize', function(done) {
var call_order = [];
var fn = function (arg1, arg2, callback) {
call_order.push(['fn', arg1, arg2]);
async.setImmediate(function () {
callback(null, arg1 + arg2);
});
};
var fn2 = async.memoize(fn);
var fn3 = async.unmemoize(fn2);
fn3(1, 2, function (err, result) {
expect(result).to.equal(3);
fn3(1, 2, function (err, result) {
expect(result).to.equal(3);
fn3(2, 2, function (err, result) {
expect(result).to.equal(4);
expect(call_order).to.eql([['fn',1,2], ['fn',1,2], ['fn',2,2]]);
done();
});
});
});
});
it('unmemoize a not memoized function', function(done) {
var fn = function (arg1, arg2, callback) {
callback(null, arg1 + arg2);
};
var fn2 = async.unmemoize(fn);
fn2(1, 2, function(err, result) {
expect(result).to.equal(3);
});
done();
});
it('error', function(done) {
var testerr = new Error('test');
var fn = function (arg1, arg2, callback) {
callback(testerr, arg1 + arg2);
};
async.memoize(fn)(1, 2, function (err) {
expect(err).to.equal(testerr);
});
done();
});
it('multiple calls', function(done) {
var fn = function (arg1, arg2, callback) {
assert(true);
setTimeout(function(){
callback(null, arg1, arg2);
}, 10);
};
var fn2 = async.memoize(fn);
fn2(1, 2, function(err, result) {
expect(result).to.equal(1, 2);
});
fn2(1, 2, function(err, result) {
expect(result).to.equal(1, 2);
done();
});
});
it('custom hash function', function(done) {
var testerr = new Error('test');
var fn = function (arg1, arg2, callback) {
callback(testerr, arg1 + arg2);
};
var fn2 = async.memoize(fn, function () {
return 'custom hash';
});
fn2(1, 2, function (err, result) {
expect(result).to.equal(3);
fn2(2, 2, function (err, result) {
expect(result).to.equal(3);
done();
});
});
});
it('manually added memo value', function(done) {
var fn = async.memoize(function() {
throw new Error("Function should never be called");
});
fn.memo.foo = ["bar"];
fn("foo", function(val) {
expect(val).to.equal("bar");
done();
});
});
it('avoid constructor key return undefined', function(done) {
var fn = async.memoize(function(name, callback) {
setTimeout(function(){
callback(null, name);
}, 100);
});
fn('constructor', function(error, results) {
expect(results).to.equal('constructor');
done();
});
});
it('avoid __proto__ key return undefined', function(done) {
// Skip test if there is a Object.create bug (node 0.10 and some Chrome 30x versions)
var x = Object.create(null);
/* jshint proto: true */
x.__proto__ = 'foo';
if (x.__proto__ !== 'foo') {
return done();
}
var fn = async.memoize(function(name, callback) {
setTimeout(function(){
callback(null, name);
}, 100);
});
fn('__proto__', function(error, results) {
expect(results).to.equal('__proto__');
done();
});
});
it('allow hasOwnProperty as key', function(done) {
var fn = async.memoize(function(name, callback) {
setTimeout(function(){
callback(null, name);
}, 100);
});
fn('hasOwnProperty', function(error, results) {
expect(results).to.equal('hasOwnProperty');
done();
});
});
});
|