ulimit.js
3.6 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
var test = require('tap').test
// simulated ulimit
// this is like graceful-fs, but in reverse
var fs_ = require('fs')
var fs = require('../graceful-fs.js')
var files = fs.readdirSync(__dirname)
// Ok, no more actual file reading!
var fds = 0
var nextFd = 60
var limit = 8
fs_.open = function (path, flags, mode, cb) {
process.nextTick(function() {
++fds
if (fds >= limit) {
--fds
var er = new Error('EMFILE Curses!')
er.code = 'EMFILE'
er.path = path
return cb(er)
} else {
cb(null, nextFd++)
}
})
}
fs_.openSync = function (path, flags, mode) {
if (fds >= limit) {
var er = new Error('EMFILE Curses!')
er.code = 'EMFILE'
er.path = path
throw er
} else {
++fds
return nextFd++
}
}
fs_.close = function (fd, cb) {
process.nextTick(function () {
--fds
cb()
})
}
fs_.closeSync = function (fd) {
--fds
}
fs_.readdir = function (path, cb) {
process.nextTick(function() {
if (fds >= limit) {
var er = new Error('EMFILE Curses!')
er.code = 'EMFILE'
er.path = path
return cb(er)
} else {
++fds
process.nextTick(function () {
--fds
cb(null, [__filename, "some-other-file.js"])
})
}
})
}
fs_.readdirSync = function (path) {
if (fds >= limit) {
var er = new Error('EMFILE Curses!')
er.code = 'EMFILE'
er.path = path
throw er
} else {
return [__filename, "some-other-file.js"]
}
}
test('open emfile autoreduce', function (t) {
fs.MIN_MAX_OPEN = 4
t.equal(fs.MAX_OPEN, 1024)
var max = 12
for (var i = 0; i < max; i++) {
fs.open(__filename, 'r', next(i))
}
var phase = 0
var expect =
[ [ 0, 60, null, 1024, 4, 12, 1 ],
[ 1, 61, null, 1024, 4, 12, 2 ],
[ 2, 62, null, 1024, 4, 12, 3 ],
[ 3, 63, null, 1024, 4, 12, 4 ],
[ 4, 64, null, 1024, 4, 12, 5 ],
[ 5, 65, null, 1024, 4, 12, 6 ],
[ 6, 66, null, 1024, 4, 12, 7 ],
[ 7, 67, null, 6, 4, 5, 1 ],
[ 8, 68, null, 6, 4, 5, 2 ],
[ 9, 69, null, 6, 4, 5, 3 ],
[ 10, 70, null, 6, 4, 5, 4 ],
[ 11, 71, null, 6, 4, 5, 5 ] ]
var actual = []
function next (i) { return function (er, fd) {
if (er)
throw er
actual.push([i, fd, er, fs.MAX_OPEN, fs.MIN_MAX_OPEN, fs._curOpen, fds])
if (i === max - 1) {
t.same(actual, expect)
t.ok(fs.MAX_OPEN < limit)
t.end()
}
fs.close(fd)
} }
})
test('readdir emfile autoreduce', function (t) {
fs.MAX_OPEN = 1024
var max = 12
for (var i = 0; i < max; i ++) {
fs.readdir(__dirname, next(i))
}
var expect =
[ [0,[__filename,"some-other-file.js"],null,7,4,7,7],
[1,[__filename,"some-other-file.js"],null,7,4,7,6],
[2,[__filename,"some-other-file.js"],null,7,4,7,5],
[3,[__filename,"some-other-file.js"],null,7,4,7,4],
[4,[__filename,"some-other-file.js"],null,7,4,7,3],
[5,[__filename,"some-other-file.js"],null,7,4,6,2],
[6,[__filename,"some-other-file.js"],null,7,4,5,1],
[7,[__filename,"some-other-file.js"],null,7,4,4,0],
[8,[__filename,"some-other-file.js"],null,7,4,3,3],
[9,[__filename,"some-other-file.js"],null,7,4,2,2],
[10,[__filename,"some-other-file.js"],null,7,4,1,1],
[11,[__filename,"some-other-file.js"],null,7,4,0,0] ]
var actual = []
function next (i) { return function (er, files) {
if (er)
throw er
var line = [i, files, er, fs.MAX_OPEN, fs.MIN_MAX_OPEN, fs._curOpen, fds ]
actual.push(line)
if (i === max - 1) {
t.ok(fs.MAX_OPEN < limit)
t.same(actual, expect)
t.end()
}
} }
})