Skip to content

Commit f35773a

Browse files
committed
Add broken test for require cache working
require is caching the compiled code but still stating the filenames. https://groups.google.com/d/topic/nodejs-dev/QGGlrvLDHVs/discussion
1 parent d9087c1 commit f35773a

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// We've experienced a regression where the module loader stats a bunch of
2+
// directories on require() even if it's been called before. The require()
3+
// should caching the request.
4+
var common = require('../common');
5+
var fs = require('fs');
6+
var assert = require('assert');
7+
8+
var counter = 0;
9+
10+
// Switch out the two stat implementations so that they increase a counter
11+
// each time they are called.
12+
13+
var _statSync = fs.statSync;
14+
var _stat = fs.stat;
15+
16+
fs.statSync = function() {
17+
counter++;
18+
return _statSync.apply(this, arguments);
19+
};
20+
21+
fs.stat = function() {
22+
counter++;
23+
return _stat.apply(this, arguments);
24+
};
25+
26+
// Load the module a.js once. It should become cached.
27+
28+
var m = common.fixturesDir + '/a.js';
29+
require(m);
30+
31+
console.log("counterBefore = %d", counter);
32+
var counterBefore = counter;
33+
34+
// Now load the module a bunch of times.
35+
// stat should not be called.
36+
for (var i = 0; i < 100; i++) {
37+
require(m);
38+
}
39+
40+
console.log("counterAfter = %d", counter);
41+
var counterAfter = counter;
42+
43+
assert.equal(counterBefore, counterAfter);
44+

0 commit comments

Comments
 (0)