Fix #1478 - Project files are not being flushed to disk after being written (#1479)

* Call fsync() before closing file

* Fix race condition in tests due to incorrect stub.

The startFlows() function wasn't really being stubbed, so it was still being called. But there was no corresponding call to stopFlows().

In later tests, the check in Flows.init() was throwing the "Cannot init without a stop" error.

* Test coverage for fsync() calls

For issue #1478

* Revert "Fix race condition in tests due to incorrect stub."

This reverts commit 4f71d7851b.

* Fix race condition in tests due to incorrect stub.

The startFlows() function wasn't really being stubbed, so it was still being called. But there was no corresponding call to stopFlows().

In later tests, the check in Flows.init() was throwing the "Cannot init without a stop" error.

* Fix intermittent test failure in Exec node.

Occasionally, the error text on stderr will come in more than one piece. The test only worked correctly if a single message was received.
This commit is contained in:
Jim Turner
2017-11-17 09:29:33 -08:00
committed by Nick O'Leary
parent f39d9d6f1b
commit 6baedf909d
5 changed files with 60 additions and 6 deletions

View File

@@ -26,11 +26,11 @@ var registry = require("../../../../red/runtime/nodes/registry");
describe("red/nodes/index", function() {
before(function() {
sinon.stub(flows,"startFlows");
sinon.stub(index,"startFlows");
process.env.NODE_RED_HOME = path.resolve(path.join(__dirname,"..","..","..",".."))
});
after(function() {
flows.startFlows.restore();
index.startFlows.restore();
delete process.env.NODE_RED_HOME;
});

View File

@@ -17,8 +17,10 @@
var should = require("should");
var fs = require('fs-extra');
var path = require('path');
var sinon = require('sinon');
var localfilesystem = require("../../../../red/runtime/storage/localfilesystem");
var log = require("../../../../red/runtime/log");
describe('LocalFileSystem', function() {
var userDir = path.join(__dirname,".testUserHome");
@@ -279,6 +281,45 @@ describe('LocalFileSystem', function() {
});
});
it('should fsync the flows file',function(done) {
var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile);
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}).then(function() {
sinon.spy(fs,"fsync");
localfilesystem.saveFlows(testFlow).then(function() {
fs.fsync.callCount.should.eql(1);
fs.fsync.restore();
done();
}).otherwise(function(err) {
done(err);
});
}).otherwise(function(err) {
done(err);
});
});
it('should log fsync errors and continue',function(done) {
var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile);
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}).then(function() {
sinon.stub(fs,"fsync", function(fd, cb) {
cb(new Error());
});
sinon.spy(log,"warn");
localfilesystem.saveFlows(testFlow).then(function() {
log.warn.callCount.should.eql(1);
log.warn.restore();
fs.fsync.callCount.should.eql(1);
fs.fsync.restore();
done();
}).otherwise(function(err) {
done(err);
});
}).otherwise(function(err) {
done(err);
});
});
it('should backup the flows file', function(done) {
var defaultFlowFile = 'flows_'+require('os').hostname()+'.json';
var defaultFlowFilePath = path.join(userDir,defaultFlowFile);