ulimit.js 3.6 KB

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