fixed tests to cover writestream

This commit is contained in:
Gareth Jones
2011-07-17 20:49:39 +10:00
parent 3d27140a9d
commit 71fe001278
2 changed files with 90 additions and 72 deletions

View File

@@ -289,7 +289,7 @@ function fileAppender (file, layout, logSize, numBackups, filePollInterval) {
setupLogRolling(logFile, file, logSize, numBackups || 5, (filePollInterval * 1000) || 30000); setupLogRolling(logFile, file, logSize, numBackups || 5, (filePollInterval * 1000) || 30000);
} }
//close the file on process exit, otherwise the process won't die. //close the file on process exit.
process.on('exit', function() { process.on('exit', function() {
logFile.end(); logFile.end();
logFile.destroySoon(); logFile.destroySoon();

View File

@@ -57,19 +57,23 @@ vows.describe('log4js').addBatch({
'fileAppender': { 'fileAppender': {
topic: function() { topic: function() {
var appender, logmessages = [], thing = "thing", fakeFS = { var appender
openSync: function() { , logmessages = []
, thing = "thing"
, fakeFS = {
createWriteStream: function() {
assert.equal(arguments[0], './tmp-tests.log'); assert.equal(arguments[0], './tmp-tests.log');
assert.equal(arguments[1], 'a'); assert.isObject(arguments[1]);
assert.equal(arguments[2], 0644); assert.equal(arguments[1].flags, 'a');
return thing; assert.equal(arguments[1].mode, 0644);
}, assert.equal(arguments[1].encoding, 'utf8');
write: function() { return {
assert.equal(arguments[0], thing); write: function(message) {
assert.isString(arguments[1]); logmessages.push(message);
assert.isNull(arguments[2]); }
assert.equal(arguments[3], "utf8"); , end: function() {}
logmessages.push(arguments[1]); , destroySoon: function() {}
};
}, },
watchFile: function() { watchFile: function() {
throw new Error("watchFile should not be called if logSize is not defined"); throw new Error("watchFile should not be called if logSize is not defined");
@@ -103,7 +107,8 @@ vows.describe('log4js').addBatch({
topic: function() { topic: function() {
var watchCb, var watchCb,
filesOpened = [], filesOpened = [],
filesClosed = [], filesEnded = [],
filesDestroyedSoon = [],
filesRenamed = [], filesRenamed = [],
newFilenames = [], newFilenames = [],
existingFiles = ['tests.log'], existingFiles = ['tests.log'],
@@ -119,10 +124,17 @@ vows.describe('log4js').addBatch({
assert.isFunction(callback); assert.isFunction(callback);
watchCb = callback; watchCb = callback;
}, },
openSync: function(file) { createWriteStream: function(file) {
assert.equal(file, 'tests.log'); assert.equal(file, 'tests.log');
filesOpened.push(file); filesOpened.push(file);
return file; return {
end: function() {
filesEnded.push(file);
},
destroySoon: function() {
filesDestroyedSoon.push(file);
}
};
}, },
statSync: function(file) { statSync: function(file) {
if (existingFiles.indexOf(file) < 0) { if (existingFiles.indexOf(file) < 0) {
@@ -134,78 +146,84 @@ vows.describe('log4js').addBatch({
renameSync: function(oldFile, newFile) { renameSync: function(oldFile, newFile) {
filesRenamed.push(oldFile); filesRenamed.push(oldFile);
existingFiles.push(newFile); existingFiles.push(newFile);
},
closeSync: function(file) {
//it should always be closing tests.log
assert.equal(file, 'tests.log');
filesClosed.push(file);
} }
} }
} }
} }
); );
var appender = log4js.fileAppender('tests.log', log4js.messagePassThroughLayout, 1024, 2, 30); var appender = log4js.fileAppender('tests.log', log4js.messagePassThroughLayout, 1024, 2, 30);
return [watchCb, filesOpened, filesClosed, filesRenamed, existingFiles]; return [watchCb, filesOpened, filesEnded, filesDestroyedSoon, filesRenamed, existingFiles];
}, },
'should close current log file, rename all old ones, open new one on rollover': function(args) { 'should close current log file, rename all old ones, open new one on rollover': function(args) {
var watchCb = args[0], filesOpened = args[1], filesClosed = args[2], filesRenamed = args[3], existingFiles = args[4]; var watchCb = args[0]
assert.isFunction(watchCb); , filesOpened = args[1]
//tell the watchCb that the file is below the threshold , filesEnded = args[2]
watchCb({ size: 891 }, { size: 0 }); , filesDestroyedSoon = args[3]
//filesOpened should still be the first one. , filesRenamed = args[4]
assert.length(filesOpened, 1); , existingFiles = args[5];
//tell the watchCb that the file is now over the threshold assert.isFunction(watchCb);
watchCb({ size: 1053 }, { size: 891 }); //tell the watchCb that the file is below the threshold
//it should have closed the first log file. watchCb({ size: 891 }, { size: 0 });
assert.length(filesClosed, 1); //filesOpened should still be the first one.
//it should have renamed the previous log file assert.length(filesOpened, 1);
assert.length(filesRenamed, 1); //tell the watchCb that the file is now over the threshold
//and we should have two files now watchCb({ size: 1053 }, { size: 891 });
assert.length(existingFiles, 2); //it should have closed the first log file.
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.1']); assert.length(filesEnded, 1);
//and opened a new log file. assert.length(filesDestroyedSoon, 1);
assert.length(filesOpened, 2); //it should have renamed the previous log file
assert.length(filesRenamed, 1);
//and we should have two files now
assert.length(existingFiles, 2);
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.1']);
//and opened a new log file.
assert.length(filesOpened, 2);
//now tell the watchCb that we've flipped over the threshold again //now tell the watchCb that we've flipped over the threshold again
watchCb({ size: 1025 }, { size: 123 }); watchCb({ size: 1025 }, { size: 123 });
//it should have closed the old file //it should have closed the old file
assert.length(filesClosed, 2); assert.length(filesEnded, 2);
//it should have renamed both the old log file, and the previous '.1' file assert.length(filesDestroyedSoon, 2);
assert.length(filesRenamed, 3); //it should have renamed both the old log file, and the previous '.1' file
assert.deepEqual(filesRenamed, ['tests.log', 'tests.log.1', 'tests.log' ]); assert.length(filesRenamed, 3);
//it should have renamed 2 more file assert.deepEqual(filesRenamed, ['tests.log', 'tests.log.1', 'tests.log' ]);
assert.length(existingFiles, 4); //it should have renamed 2 more file
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.1', 'tests.log.2', 'tests.log.1']); assert.length(existingFiles, 4);
//and opened a new log file assert.deepEqual(existingFiles, ['tests.log', 'tests.log.1', 'tests.log.2', 'tests.log.1']);
assert.length(filesOpened, 3); //and opened a new log file
assert.length(filesOpened, 3);
//tell the watchCb we've flipped again. //tell the watchCb we've flipped again.
watchCb({ size: 1024 }, { size: 234 }); watchCb({ size: 1024 }, { size: 234 });
//close the old one again. //close the old one again.
assert.length(filesClosed, 3); assert.length(filesEnded, 3);
//it should have renamed the old log file and the 2 backups, with the last one being overwritten. assert.length(filesDestroyedSoon, 3);
assert.length(filesRenamed, 5); //it should have renamed the old log file and the 2 backups, with the last one being overwritten.
assert.deepEqual(filesRenamed, ['tests.log', 'tests.log.1', 'tests.log', 'tests.log.1', 'tests.log' ]); assert.length(filesRenamed, 5);
//it should have renamed 2 more files assert.deepEqual(filesRenamed, ['tests.log', 'tests.log.1', 'tests.log', 'tests.log.1', 'tests.log' ]);
assert.length(existingFiles, 6); //it should have renamed 2 more files
assert.deepEqual(existingFiles, ['tests.log', 'tests.log.1', 'tests.log.2', 'tests.log.1', 'tests.log.2', 'tests.log.1']); assert.length(existingFiles, 6);
//and opened a new log file assert.deepEqual(existingFiles, ['tests.log', 'tests.log.1', 'tests.log.2', 'tests.log.1', 'tests.log.2', 'tests.log.1']);
assert.length(filesOpened, 4); //and opened a new log file
} assert.length(filesOpened, 4);
}
}, },
'configure' : { 'configure' : {
topic: function() { topic: function() {
var messages = {}, fakeFS = { var messages = {}, fakeFS = {
openSync: function(file) { createWriteStream: function(file) {
return file; return {
}, write: function(message) {
write: function(file, message) { if (!messages.hasOwnProperty(file)) {
if (!messages.hasOwnProperty(file)) { messages[file] = [];
messages[file] = []; }
} messages[file].push(message);
messages[file].push(message); }
, end: function() {}
, destroySoon: function() {}
};
}, },
readFileSync: function(file, encoding) { readFileSync: function(file, encoding) {
return require('fs').readFileSync(file, encoding); return require('fs').readFileSync(file, encoding);