Blame view

node_modules/graceful-fs/test/max-open.js 1.49 KB
2a09d1a4   liuqimichale   添加宜春 天水 宣化
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
  var test = require('tap').test
  var fs = require('../')
  
  test('open lots of stuff', function (t) {
    // Get around EBADF from libuv by making sure that stderr is opened
    // Otherwise Darwin will refuse to give us a FD for stderr!
    process.stderr.write('')
  
    // How many parallel open()'s to do
    var n = 1024
    var opens = 0
    var fds = []
    var going = true
    var closing = false
    var doneCalled = 0
  
    for (var i = 0; i < n; i++) {
      go()
    }
  
    function go() {
      opens++
      fs.open(__filename, 'r', function (er, fd) {
        if (er) throw er
        fds.push(fd)
        if (going) go()
      })
    }
  
    // should hit ulimit pretty fast
    setTimeout(function () {
      going = false
      t.equal(opens - fds.length, n)
      done()
    }, 100)
  
  
    function done () {
      if (closing) return
      doneCalled++
  
      if (fds.length === 0) {
        console.error('done called %d times', doneCalled)
        // First because of the timeout
        // Then to close the fd's opened afterwards
        // Then this time, to complete.
        // Might take multiple passes, depending on CPU speed
        // and ulimit, but at least 3 in every case.
        t.ok(doneCalled >= 2)
        return t.end()
      }
  
      closing = true
      setTimeout(function () {
        // console.error('do closing again')
        closing = false
        done()
      }, 100)
  
      // console.error('closing time')
      var closes = fds.slice(0)
      fds.length = 0
      closes.forEach(function (fd) {
        fs.close(fd, function (er) {
          if (er) throw er
        })
      })
    }
  })