summaryrefslogtreecommitdiff
path: root/test/pummel/test-fs-watch-file-slow.js
blob: 508de487f6dc23ae104ad2b615ebf7f6a6161606 (plain)
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
'use strict';
var common = require('../common');
var assert = require('assert');
var path = require('path');
var fs = require('fs');

var FILENAME = path.join(common.tmpDir, 'watch-me');
var TIMEOUT = 1300;

var nevents = 0;

try {
  fs.unlinkSync(FILENAME);
} catch (e) {
  // swallow
}

fs.watchFile(FILENAME, {interval: TIMEOUT - 250}, function(curr, prev) {
  console.log([curr, prev]);
  switch (++nevents) {
    case 1:
      assert.equal(common.fileExists(FILENAME), false);
      break;
    case 2:
    case 3:
      assert.equal(common.fileExists(FILENAME), true);
      break;
    case 4:
      assert.equal(common.fileExists(FILENAME), false);
      fs.unwatchFile(FILENAME);
      break;
    default:
      assert(0);
  }
});

process.on('exit', function() {
  assert.equal(nevents, 4);
});

setTimeout(createFile, TIMEOUT);

function createFile() {
  console.log('creating file');
  fs.writeFileSync(FILENAME, 'test');
  setTimeout(touchFile, TIMEOUT);
}

function touchFile() {
  console.log('touch file');
  fs.writeFileSync(FILENAME, 'test');
  setTimeout(removeFile, TIMEOUT);
}

function removeFile() {
  console.log('remove file');
  fs.unlinkSync(FILENAME);
}