Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

backup vows test

  • Loading branch information...
commit a4048562c59830ae90e82b44b8dbccb0f5c4878e 1 parent ea2428b
@sdolard authored
View
7 lib/logtofile.js
@@ -154,7 +154,10 @@ var LogToFile = function (config){ // ctor
return;
}
this.filePath = path.normalize(this.directory + '/' + this.fileName);
- this.writeDelay = Math.max(config.writeDelay || DEFAULT_WRITE_DELAY, 10); // Buffer is flushed every 200 ms, min 10
+
+ this.writeDelay = config.writeDelay === undefined ? DEFAULT_WRITE_DELAY : config.writeDelay;
+ this.writeDelay = Math.max(this.writeDelay, 10); // Buffer is flushed every 200 ms, min 10
+
this.bufferSize = Math.max(config.bufferSize || 65536, 4096); // Buffer blocks size, min 4096o
this.fileMaxSize = config.fileMaxSize || 1024 * 1024 * 5;
//this.fileMaxSize = Math.max(config.fileMaxSize || 1024 * 1024 * 5, 1024 * 1024); // 5MB, min 1MB
@@ -209,7 +212,7 @@ LogToFile.prototype._createWriteStream = function(delay) {
me._rotationPending = false;
me._waitDrain = false;
- //Writable stream
+ // Writable stream
me._writableStream = fs.createWriteStream(me.filePath, {
flags: 'a'
});
View
5 package.json
@@ -16,7 +16,7 @@
"main": "lib/logtofile",
"license": "MIT",
"scripts": {
- "test": "node test/run_test.js"
+ "test": "vows test/vows*.js --spec"
},
"engines": {
"node": ">= 0.6.0"
@@ -30,9 +30,6 @@
"url": "https://raw.github.com/sdolard/node-logtofile/master/LICENSE"
}
],
- "scripts": {
- "test": "vows test/vows* --spec"
- },
"dependencies": {
"vows": ">= 0.6.2"
},
View
140 test/logtofile-backup.js
@@ -1,140 +0,0 @@
-/*
-Copyright © 2011 by Sebastien Dolard (sdolard@gmail.com)
-
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-*/
-
-var
-assert = require('assert'),
-path = require('path'),
-util = require('util'),
-fs = require('fs'),
-logToFile = require('../lib/logtofile'),
-dataTest = [
- '1234567890AZERTYUIOPQSDFGHJKLMWXCVBN',
- '&é"(§è!çà)-azertyuiop^qsdfghjklmù`xcvbn,;:='
-].join(''),
-endData = '',
-rs,
-log,
-endEvent = 0,
-closeEventUnlink = 0,
-readStreamErrorEvent = 0,
-errorEvent = 0,
-writtingEvent = 0,
-writeEvent = 0,
-writtenEvent = 0,
-backupedEvent = 0,
-gzippingEvent = 0,
-gzippedEvent = 0;
-
-
-log = logToFile.create({
- directory: __dirname,
- fileName: path.basename(__filename) + '.test.txt',
- gzipBackupFile: false,
- fileMaxSize: dataTest.length
-});
-
-log.on('error', function(err){
- errorEvent++;
- console.log("Error: ", err);
-});
-
-log.on('writting', function(){
- writtingEvent++;
-});
-
-log.on('write', function(){
- writeEvent++;
-});
-
-log.on('written', function(fileName){
- writtenEvent++;
-});
-
-log.on('backuped', function (filePath, newFilePath) {
- backupedEvent++;
- rs = fs.createReadStream(filePath + '.00', {
- encoding: 'utf8'
- });
-
- rs.on('data', function (data) {
- endData += data;
- });
- rs.on('end', function () {
- endEvent++;
- });
- rs.on('error', function (exception) {
- readStreamErrorEvent++;
- });
- rs.on('close', function () {
- // Clean up
- fs.unlink(filePath, function (err) {
- if (err) {
- throw err;
- }
- closeEventUnlink++;
-
- });
- // Clean up
- fs.unlink(filePath + '.00', function (err) {
- if (err) {
- throw err;
- }
- closeEventUnlink++;
-
- });
- assert.equal(endData, dataTest);
- });
-});
-
-log.on('gzipping', function (filePath, newFilePath) {
- gzippingEvent++;
-});
-
-log.on('gzipped', function (filePath, newFilePath) {
- gzippedEvent++;
-});
-
-log.write(dataTest);
-
-
-process.on('uncaughtException', function (err) {
- console.log('Caught exception: ' + err);
-});
-
-process.on('exit', function () {
- // Event
- assert.strictEqual(errorEvent, 0, 'errorEvent');
- assert.strictEqual(writtingEvent, 1, 'writtingEvent');
- assert.strictEqual(writeEvent, 1, 'writeEvent');
- assert.strictEqual(writtenEvent, 1, 'writtenEvent');
- assert.strictEqual(backupedEvent, 1, 'backupedEvent');
- assert.strictEqual(gzippingEvent, 0, 'gzippingEvent');
- assert.strictEqual(gzippedEvent, 0, 'gzippedEvent');
-
- // Cleanup
- assert.strictEqual(endEvent, 1, 'endEvent');
- assert.strictEqual(closeEventUnlink, 2, 'closeEventUnlink ');
-});
-
-
-
-
View
75 test/vows-logtofile-backup.js
@@ -0,0 +1,75 @@
+var
+vows = require('vows'),
+assert = require('assert'),
+util = require('util'),
+events = require("events"),
+path = require("path"),
+fs = require("fs"),
+
+logToFile = require('../lib/logtofile'),
+dataTest = [
+ '1234567890AZERTYUIOPQSDFGHJKLMWXCVBN',
+ '&é"(§è!çà)-azertyuiop^qsdfghjklmù`xcvbn,;:='
+].join(''),
+testFileName = 'tmpTest-backup.txt',
+testFilePath = path.normalize(__dirname + '/' + testFileName);
+
+exports.suite1 = vows.describe('logtofile backup').addBatch({
+ 'When calling write method with a fileMaxSize set to data size': {
+ topic: function() {
+ var
+ promise = new events.EventEmitter(),
+ log = logToFile.create({
+ directory: __dirname,
+ fileName: testFileName,
+ gzipBackupFile: false,
+ fileMaxSize: dataTest.length
+ });
+ log.on('backuped', function (oldFilePath, newFilePath) {
+ promise.emit('success', oldFilePath, newFilePath);
+ });
+ log.write(dataTest);
+ return promise;
+
+ },
+ "It creates a backup file": function (log, oldFilePath, newFilePath) {
+ assert.isTrue(path.existsSync(newFilePath));
+ },
+ "Backup content is correct": function (log, oldFilePath, newFilePath) {
+ assert.strictEqual(fs.readFileSync(newFilePath, 'utf8'), dataTest);
+ },
+ "It removes old file": function (log, oldFilePath, newFilePath) {
+ assert.isFalse(path.existsSync(oldFilePath));
+ }
+ }
+}).
+addBatch({
+ 'Then': {
+ topic: function() {
+ setTimeout(this.callback, 100); // must be async
+ },
+ "it creates a new empty file": function () {
+ assert.isTrue(path.existsSync(testFilePath));
+ }
+ }
+}).
+addBatch({
+ 'Finnaly, we delete tmpTest-backup.txt.00': {
+ topic: function() {
+ fs.unlink(testFilePath+'.00', this.callback);
+ },
+ "Ok": function (err) {
+ assert.isUndefined(err);
+ }
+ }
+}).
+addBatch({
+ 'and we delete tmpTest-backup.txt': {
+ topic: function() {
+ fs.unlink(testFilePath, this.callback);
+ },
+ "Ok": function (err) {
+ assert.isUndefined(err);
+ }
+ }
+});
View
140 test/vows-logtofile.js
@@ -13,16 +13,16 @@ dataTest = [
].join(''),
testFileName = 'tmpTest.txt',
testInvalidFileName = ' /tmpTest.txt',
-testFilePath = path.normalize(__dirname + '/' + testFileName);
+testFilePath = path.normalize(__dirname + '/' + testFileName),
testInvalidFilePath = path.normalize(__dirname + '/' + testInvalidFileName);
-exports.suite1 = vows.describe('logtofile').addBatch({
+exports.suite1 = vows.describe('logtofile basic').addBatch({
'When creating a logtofile instance without any config': {
topic: function() {
return logToFile.create();
},
- 'It throw an exception (Error)': function (err) {
- assert.isTrue(err instanceof Error);
+ 'It throws an exception (Error)': function (err) {
+ assert.instanceOf(err, Error);
},
'Exception code is "EEMPTYFILENAME"': function (err) {
assert.strictEqual(err.code, 'EEMPTYFILENAME');
@@ -37,8 +37,8 @@ exports.suite1 = vows.describe('logtofile').addBatch({
fileName: testFileName
});
},
- 'It throw an exception (Error)': function (err) {
- assert.isTrue(err instanceof Error);
+ 'It throws an exception (Error)': function (err) {
+ assert.instanceOf(err, Error);
},
'Exception code is "EEMPTYDIRECTORY"': function (err) {
assert.strictEqual(err.code, 'EEMPTYDIRECTORY');
@@ -51,8 +51,8 @@ exports.suite1 = vows.describe('logtofile').addBatch({
fileName: testFileName
});
},
- 'It throw an exception (Error)': function (err) {
- assert.isTrue(err instanceof Error);
+ 'It throws an exception (Error)': function (err) {
+ assert.instanceOf(err, Error);
},
'Exception code is "EDIRNOTFOUND"': function (err) {
assert.strictEqual(err.code, 'EDIRNOTFOUND');
@@ -71,8 +71,8 @@ exports.suite1 = vows.describe('logtofile').addBatch({
});
return promise;
},
- "It throw(emit) an exception": function (err, log) {
- assert.isTrue(err instanceof Error);
+ "It throws(emit) an exception": function (err, log) {
+ assert.instanceOf(err, Error);
},
'Exception code is "ENOENT"': function (err, log) {
@@ -97,7 +97,8 @@ exports.suite1 = vows.describe('logtofile').addBatch({
},
"It writes in a new file": function (p) {
assert.strictEqual(p, testFilePath);
- assert.isFalse(p instanceof Error);
+ assert.isTrue(path.existsSync(p));
+
},
"Written data are correct": function (p) {
assert.strictEqual(fs.readFileSync(p, 'utf8'), dataTest);
@@ -147,7 +148,7 @@ addBatch({
}
}).
addBatch({
- 'Finnaly, we clean up test dir': {
+ 'Finnaly, we remove tmpTest.txt': {
topic: function() {
fs.unlink(testFilePath, this.callback);
},
@@ -155,117 +156,4 @@ addBatch({
assert.isUndefined(err);
}
}
-});
-/*.
-addBatch({
-'We cleanup previous test': {
-topic: function() {
-var log = logToFile.create();
-return log;
-},
-'It throw an exception (Error)': function (r) {
-assert.isTrue(r instanceof Error);
-},
-'Exception code is "EEMPTYFILENAME"': function (r) {
-assert.strictEqual(r.code, 'EEMPTYFILENAME');
-},
-'Stack is attached to this exception': function (r) {
-assert.isTrue(r.hasOwnProperty('stack'));
-}
-}
-});*/
-
-/*
-exports.suite1 = vows.describe('ping').addBatch({
-'When we ping localhost': {
-topic: function() {
-var promise = new events.EventEmitter();
-
-ping.check({
-host: 'localhost'
-}, function (err, r) {
-if (err) {
-promise.emit('error', err, r);
-} else {
-promise.emit('success', r);
-}
-});
-return promise;
-},
-'It succeed': function (r) {
-assert.equal(r.host, 'localhost');
-assert.equal(r.exitCode, 0);
-}
-},
-'When we ping nohost': {
-topic: function() {
-var promise = new events.EventEmitter();
-
-ping.check({
-host: 'nohost'
-}, function (err, r) {
-if (err) {
-promise.emit('error', err, r);
-} else {
-promise.emit('success', r);
-}
-});
-return promise;
-},
-'It failed': function (err, r) {
-//console.log(util.inspect(r));
-assert.equal(err.code, 'EPINGFAILED');
-assert.notEqual(r.exitCode, 0);
-}
-},
-'When we ping ::1': {
-topic: function() {
-var promise = new events.EventEmitter();
-
-ping.check({
-host: '::1',
-ipV6: true
-}, function (err, r) {
-if (err) {
-promise.emit('error', err, r);
-} else {
-promise.emit('success', r);
-}
-});
-return promise;
-},
-'It succeed': function (r) {
-assert.equal(r.host, '::1');
-assert.equal(r.exitCode, 0);
-
-}
-},
-'When we ping 1.1.1.1 with timeout set to 1s': {
-topic: function() {
-var
-promise = new events.EventEmitter();
-start = new Date();
-
-ping.check({
-host: '1.1.1.1',
-timeout: 1
-}, function (err, r) {
-end = new Date();
-if (err) {
-promise.emit('error', err, r);
-} else {
-promise.emit('success', r);
-}
-
-});
-return promise;
-},
-'It takes 1s to return': function (r) {
-assert.equal(r.host, '1.1.1.1');
-assert.notEqual(r.exitCode, 0);
-assert.equal(end.getTime() - start.getTime() >= 1000, true);
-}
-}
-});
-
-*/
+});
Please sign in to comment.
Something went wrong with that request. Please try again.