Permalink
Browse files

[fix] windows test bug - by using setTimeout in FileWatcher

  • Loading branch information...
1 parent 6caf88b commit 7b35d23905377f84653b0e1be67fdd065b52875a @AndreasMadsen committed May 1, 2012
Showing with 43 additions and 23 deletions.
  1. +2 −2 README.md
  2. +41 −21 test/watchers/filewatch.js
View
4 README.md
@@ -13,8 +13,8 @@
| | **Mac OS X** | **Linux** | **Windows** |
|-------------:|:-------------------:|:-------------------:|:------------------:|
-| **node 0.7** | testsuite: 187/187 | testsuite: 187/187 | testsuite: 166/187 |
-| **node 0.6** | testsuite: 187/187* | testsuite: 187/187* | testsuite: 166/187 |
+| **node 0.7** | testsuite: 187/187 | testsuite: 187/187 | testsuite: 182/187 |
+| **node 0.6** | testsuite: 187/187* | testsuite: 187/187* | testsuite: 182/187 |
| **node 0.4** | manual confirmed* | manual confirmed* | never |
> Please note that the testsuite in node 0.4 don't work because of the module dependencies
View
62 test/watchers/filewatch.js
@@ -76,23 +76,29 @@
self.updateStat(function () {
// Start file a watcher
- self.stream = fs.watch(self.filepath, { persistent: true }, function (event) {
- if (event === 'change') {
-
- // push to query
- self.query += 1;
-
- // update stat
- if (self.reading === false) {
- self.updateStat();
+ if (process.platform !== 'win32') {
+ self.stream = fs.watch(self.filepath, { persistent: true }, function (event) {
+ if (event === 'change') {
+ self.pushQuery();
}
- }
- });
+ });
+ }
+
});
});
});
};
+ FileWatcher.prototype.pushQuery = function (callback) {
+ // push to query
+ this.query += 1;
+
+ // update stat
+ if (this.reading === false) {
+ this.updateStat();
+ }
+ };
+
FileWatcher.prototype.updateStat = function (callback) {
var self = this;
@@ -126,30 +132,37 @@
fs.fstat(this.fd, function (error, stat) {
if (error) return self.emit('error', error);
- var current = self.stat;
self.stat = stat;
-
- // save as old and new if this is the first read
- if (current === null) {
- return handle();
- }
-
- // if the file has been modified update stats and execute callback
return handle();
});
};
// store lines in cache
FileWatcher.prototype.pause = function () {
this.paused = true;
+
+ if (process.platform === 'win32') {
+ clearTimeout(this.stream);
+ }
};
// drain cache and discontinue line storeing
FileWatcher.prototype.resume = function () {
+ var self = this;
+
this.paused = false;
while (this.cache.length !== 0 && this.paused === false) {
this.cache.splice(0, 1)[0]();
}
+
+ if (process.platform === 'win32') {
+ self.stream = setTimeout(function check(event) {
+ if (self.fd) {
+ self.pushQuery();
+ setTimeout(check, 150);
+ }
+ }, 150);
+ }
};
// Stop file watcher
@@ -159,7 +172,11 @@
this.stoped = true;
if (this.stream) {
- this.stream.close();
+ if (process.platform = 'win32') {
+ clearTimeout(this.stream);
+ } else {
+ this.stream.close();
+ }
this.stream = null;
}
if (this.fd) {
@@ -208,7 +225,10 @@
// read by each line
while ((i = self.buffer.indexOf('\n', start)) >= 0) {
- var line = self.buffer.slice(start, i);
+
+ var returnChar = (self.buffer[i - 1] === '\r') ? 1 : 0;
+
+ var line = self.buffer.slice(start, i - returnChar);
// emit line event or add to cache
callback(self.emit.bind(self, 'line', line));

0 comments on commit 7b35d23

Please sign in to comment.