Fixed the file appender tests

This commit is contained in:
Gareth Jones 2011-12-20 08:49:21 +11:00
parent 78de73a274
commit a999d8fc00
4 changed files with 47 additions and 31 deletions

View File

@ -20,13 +20,18 @@ function fileAppender (file, layout, logSize, numBackups) {
numBackups = numBackups === 0 ? 1 : numBackups;
function openTheStream(file, fileSize, numFiles) {
var stream = new streams.BufferedWriteStream(
new streams.RollingFileStream(
file,
fileSize,
numFiles
)
);
var stream;
if (fileSize) {
stream = new streams.BufferedWriteStream(
new streams.RollingFileStream(
file,
fileSize,
numFiles
)
);
} else {
stream = new streams.BufferedWriteStream(fs.createWriteStream(file, { encoding: "utf8", mode: 0644, flags: 'a' }));
}
stream.on("error", function (err) {
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
});

View File

@ -84,11 +84,12 @@ function RollingFileStream (filename, size, backups, options) {
this.options = options || { encoding: "utf8", mode: 0644, flags: 'a' };
this.rolling = false;
this.writesWhileRolling = [];
this.bytesQueued = 0;
throwErrorIfArgumentsAreNotValid();
RollingFileStream.super_.call(this, this.filename, this.options);
this.bytesWritten = currentFileSize(this.filename);
this.bytesQueued = currentFileSize(this.filename);
function currentFileSize(file) {
var fileSize = 0;
@ -114,9 +115,9 @@ RollingFileStream.prototype.write = function(data, encoding) {
return false;
} else {
var canWrite = RollingFileStream.super_.prototype.write.call(this, data, encoding);
//this.bytesWritten += data.length;
console.log("bytesWritten: %d, max: %d", this.bytesWritten, this.size);
if (this.bytesWritten >= this.size) {
console.log("bytesQueued: %d, max: %d", this.bytesQueued, this.size);
this.bytesQueued += data.length;
if (this.bytesQueued >= this.size) {
this.roll();
}
return canWrite;
@ -185,6 +186,7 @@ RollingFileStream.prototype.roll = function () {
that.fd = fd;
that.writable = true;
fs.close(oldLogFileFD, function() {
that.bytesQueued = 0;
that.bytesWritten = 0;
cb();
});
@ -197,9 +199,10 @@ RollingFileStream.prototype.roll = function () {
var toWrite;
while ((toWrite = that.writesWhileRolling.shift())) {
RollingFileStream.super_.prototype.write.call(that, toWrite.data, toWrite.encoding);
that.bytesWritten += toWrite.data.length;
that.bytesQueued += toWrite.data.length;
}
that.rolling = false;
that.flush();
cb();
}

View File

@ -44,8 +44,8 @@ vows.describe('log4js logLevelFilter').addBatch({
logger.error('both');
logger.warn('both');
logger.debug('main');
return logger;
//wait for the file system to catch up
setTimeout(this.callback, 100);
},
'tmp-tests.log': {
topic: function() {

View File

@ -90,28 +90,36 @@ vows.describe('RollingFileStream').addBatch({
remove(__dirname + "/test-rolling-file-stream-write-more");
remove(__dirname + "/test-rolling-file-stream-write-more.1");
var that = this, stream = new RollingFileStream(__dirname + "/test-rolling-file-stream-write-more", 45);
stream.on("open", function() { that.callback(null, stream); });
},
'(when open)': {
topic: function(stream) {
var that = this;
stream.on("open", function() {
for (var i=0; i < 7; i++) {
stream.write(i +".cheese\n", "utf8");
}
stream.end(function (err) {
fs.readFile(__dirname + "/test-rolling-file-stream-write-more", "utf8", that.callback);
});
//wait for the file system to catch up with us
setTimeout(that.callback, 100);
});
},
'the number of files': {
topic: function() {
fs.readdir(__dirname, this.callback);
},
'should write to the file': function(contents) {
assert.equal(contents, "5.cheese\n6.cheese\n");
'should be two': function(files) {
assert.equal(files.filter(function(file) { return file.indexOf('test-rolling-file-stream-write-more') > -1; }).length, 2);
}
},
'the first file': {
topic: function() {
fs.readFile(__dirname + "/test-rolling-file-stream-write-more", "utf8", this.callback);
},
'the number of files': {
topic: function() {
fs.readdir(__dirname, this.callback);
},
'should be two': function(files) {
assert.equal(files.filter(function(file) { return file.indexOf('test-rolling-file-stream-write-more') > -1; }).length, 2);
}
'should contain the last two log messages': function(contents) {
assert.equal(contents, '5.cheese\n6.cheese\n');
}
},
'the second file': {
topic: function() {
fs.readFile(__dirname + '/test-rolling-file-stream-write-more.1', "utf8", this.callback);
},
'should contain the first five log messages': function(contents) {
assert.equal(contents, '0.cheese\n1.cheese\n2.cheese\n3.cheese\n4.cheese\n');
}
}
}