Initial commit
[yaffs-website] / node_modules / globule / node_modules / graceful-fs / test / ulimit.js
1 var test = require('tap').test
2
3 // simulated ulimit
4 // this is like graceful-fs, but in reverse
5 var fs_ = require('fs')
6 var fs = require('../graceful-fs.js')
7 var files = fs.readdirSync(__dirname)
8
9 // Ok, no more actual file reading!
10
11 var fds = 0
12 var nextFd = 60
13 var limit = 8
14 fs_.open = function (path, flags, mode, cb) {
15   process.nextTick(function() {
16     ++fds
17     if (fds >= limit) {
18       --fds
19       var er = new Error('EMFILE Curses!')
20       er.code = 'EMFILE'
21       er.path = path
22       return cb(er)
23     } else {
24       cb(null, nextFd++)
25     }
26   })
27 }
28
29 fs_.openSync = function (path, flags, mode) {
30   if (fds >= limit) {
31     var er = new Error('EMFILE Curses!')
32     er.code = 'EMFILE'
33     er.path = path
34     throw er
35   } else {
36     ++fds
37     return nextFd++
38   }
39 }
40
41 fs_.close = function (fd, cb) {
42   process.nextTick(function () {
43     --fds
44     cb()
45   })
46 }
47
48 fs_.closeSync = function (fd) {
49   --fds
50 }
51
52 fs_.readdir = function (path, cb) {
53   process.nextTick(function() {
54     if (fds >= limit) {
55       var er = new Error('EMFILE Curses!')
56       er.code = 'EMFILE'
57       er.path = path
58       return cb(er)
59     } else {
60       ++fds
61       process.nextTick(function () {
62         --fds
63         cb(null, [__filename, "some-other-file.js"])
64       })
65     }
66   })
67 }
68
69 fs_.readdirSync = function (path) {
70   if (fds >= limit) {
71     var er = new Error('EMFILE Curses!')
72     er.code = 'EMFILE'
73     er.path = path
74     throw er
75   } else {
76     return [__filename, "some-other-file.js"]
77   }
78 }
79
80
81 test('open emfile autoreduce', function (t) {
82   fs.MIN_MAX_OPEN = 4
83   t.equal(fs.MAX_OPEN, 1024)
84
85   var max = 12
86   for (var i = 0; i < max; i++) {
87     fs.open(__filename, 'r', next(i))
88   }
89
90   var phase = 0
91
92   var expect =
93       [ [ 0, 60, null, 1024, 4, 12, 1 ],
94         [ 1, 61, null, 1024, 4, 12, 2 ],
95         [ 2, 62, null, 1024, 4, 12, 3 ],
96         [ 3, 63, null, 1024, 4, 12, 4 ],
97         [ 4, 64, null, 1024, 4, 12, 5 ],
98         [ 5, 65, null, 1024, 4, 12, 6 ],
99         [ 6, 66, null, 1024, 4, 12, 7 ],
100         [ 7, 67, null, 6, 4, 5, 1 ],
101         [ 8, 68, null, 6, 4, 5, 2 ],
102         [ 9, 69, null, 6, 4, 5, 3 ],
103         [ 10, 70, null, 6, 4, 5, 4 ],
104         [ 11, 71, null, 6, 4, 5, 5 ] ]
105
106   var actual = []
107
108   function next (i) { return function (er, fd) {
109     if (er)
110       throw er
111     actual.push([i, fd, er, fs.MAX_OPEN, fs.MIN_MAX_OPEN, fs._curOpen, fds])
112
113     if (i === max - 1) {
114       t.same(actual, expect)
115       t.ok(fs.MAX_OPEN < limit)
116       t.end()
117     }
118
119     fs.close(fd)
120   } }
121 })
122
123 test('readdir emfile autoreduce', function (t) {
124   fs.MAX_OPEN = 1024
125   var max = 12
126   for (var i = 0; i < max; i ++) {
127     fs.readdir(__dirname, next(i))
128   }
129
130   var expect =
131       [ [0,[__filename,"some-other-file.js"],null,7,4,7,7],
132         [1,[__filename,"some-other-file.js"],null,7,4,7,6],
133         [2,[__filename,"some-other-file.js"],null,7,4,7,5],
134         [3,[__filename,"some-other-file.js"],null,7,4,7,4],
135         [4,[__filename,"some-other-file.js"],null,7,4,7,3],
136         [5,[__filename,"some-other-file.js"],null,7,4,6,2],
137         [6,[__filename,"some-other-file.js"],null,7,4,5,1],
138         [7,[__filename,"some-other-file.js"],null,7,4,4,0],
139         [8,[__filename,"some-other-file.js"],null,7,4,3,3],
140         [9,[__filename,"some-other-file.js"],null,7,4,2,2],
141         [10,[__filename,"some-other-file.js"],null,7,4,1,1],
142         [11,[__filename,"some-other-file.js"],null,7,4,0,0] ]
143
144   var actual = []
145
146   function next (i) { return function (er, files) {
147     if (er)
148       throw er
149     var line = [i, files, er, fs.MAX_OPEN, fs.MIN_MAX_OPEN, fs._curOpen, fds ]
150     actual.push(line)
151
152     if (i === max - 1) {
153       t.ok(fs.MAX_OPEN < limit)
154       t.same(actual, expect)
155       t.end()
156     }
157   } }
158 })