Skip to content

Commit

Permalink
[test minor] Unwrap style madness in file-open-test.js and file-stres…
Browse files Browse the repository at this point in the history
…s-test.js
  • Loading branch information
indexzero committed Oct 7, 2014
1 parent d1d146e commit 8e22257
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 102 deletions.
88 changes: 42 additions & 46 deletions test/transports/file-open-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,52 @@
*
*/

!function (assert, fs, os, path, vows, winston) {
'use strict';
var assert = require('assert'),
fs = require('fs'),
os = require('os'),
path = require('path'),
vows = require('vows'),
winston = require('../../lib/winston');

vows.describe('winston/transports/file').addBatch({
'An instance of the File Transport': {
topic: function () {
var callback = this.callback.bind(this),
logPath = path.resolve(__dirname, '../fixtures/logs/file-open-test.log');
vows.describe('winston/transports/file').addBatch({
'An instance of the File Transport': {
topic: function () {
var callback = this.callback.bind(this),
logPath = path.resolve(__dirname, '../fixtures/logs/file-open-test.log');

try {
fs.unlinkSync(logPath);
} catch (ex) {
if (ex && ex.code !== 'ENOENT') { return callback(ex); }
}
try {
fs.unlinkSync(logPath);
} catch (ex) {
if (ex && ex.code !== 'ENOENT') { return callback(ex); }
}

var fileTransport = new (winston.transports.File)({
filename: logPath
}),
logger = new (winston.Logger)({
transports: [fileTransport]
}),
timeline = {};
var fileTransport = new (winston.transports.File)({
filename: logPath
}),
logger = new (winston.Logger)({
transports: [fileTransport]
}),
timeline = {};

fileTransport.open(function () {
timeline.open = Date.now();
fileTransport.open(function () {
timeline.open = Date.now();

setTimeout(function () {
logger.info('Hello, World!', function () {
timeline.logged = Date.now();
});
}, 100);
setTimeout(function () {
logger.info('Hello, World!', function () {
timeline.logged = Date.now();
});
}, 100);

setTimeout(function () {
callback(null, timeline);
}, 1000);
});
},
'should fire "open" event': function (results) {
assert.isTrue(!!results.open);
},
'should fire "logged" event': function (results) {
assert.isTrue(!!results.logged);
}
setTimeout(function () {
callback(null, timeline);
}, 1000);
});
},
'should fire "open" event': function (results) {
assert.isTrue(!!results.open);
},
'should fire "logged" event': function (results) {
assert.isTrue(!!results.logged);
}
}).export(module);
}(
require('assert'),
require('fs'),
require('os'),
require('path'),
require('vows'),
require('../../lib/winston')
);
}
}).export(module);
108 changes: 52 additions & 56 deletions test/transports/file-stress-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,67 @@
*
*/

!function (assert, fs, os, path, vows, winston) {
'use strict';
var assert = require('assert'),
fs = require('fs'),
os = require('os'),
path = require('path'),
vows = require('vows'),
winston = require('../../lib/winston');

vows.describe('winston/transports/file').addBatch({
'A stressed instance of the File Transport': {
topic: function () {
var callback = this.callback.bind(this),
logPath = path.resolve(__dirname, '../fixtures/logs/file-stress-test.log');
vows.describe('winston/transports/file').addBatch({
'A stressed instance of the File Transport': {
topic: function () {
var callback = this.callback.bind(this),
logPath = path.resolve(__dirname, '../fixtures/logs/file-stress-test.log');

try {
fs.unlinkSync(logPath);
} catch (ex) {
if (ex && ex.code !== 'ENOENT') { return callback(ex); }
}
try {
fs.unlinkSync(logPath);
} catch (ex) {
if (ex && ex.code !== 'ENOENT') { return callback(ex); }
}

var fileTransport = new (winston.transports.File)({
filename: logPath
}),
logger = new (winston.Logger)({
transports: [fileTransport]
});
var fileTransport = new (winston.transports.File)({
filename: logPath
}),
logger = new (winston.Logger)({
transports: [fileTransport]
});

fileTransport.on('open', function () {
setTimeout(function () {
clearInterval(interval);
fileTransport.on('open', function () {
setTimeout(function () {
clearInterval(interval);

logger.query({ order: 'asc' }, function (err, results) {
callback(null, results);
});
}, 100);
});
logger.query({ order: 'asc' }, function (err, results) {
callback(null, results);
});
}, 100);
});

var logIndex = 0,
interval = setInterval(function () {
logger.info(++logIndex);
stress(200);
}, 0);
var logIndex = 0,
interval = setInterval(function () {
logger.info(++logIndex);
stress(200);
}, 0);

logger.info(++logIndex);
stress(200);
logger.info(++logIndex);
stress(200);

function stress(duration) {
var startTime = Date.now();
function stress(duration) {
var startTime = Date.now();

while (Date.now() - startTime < duration) {
Math.sqrt(Math.PI);
}
while (Date.now() - startTime < duration) {
Math.sqrt(Math.PI);
}
},
'should not skip any log lines': function (results) {
var testIndex = 0;

results.file.forEach(function (log) {
if (+log.message !== ++testIndex) {
throw new Error('Number skipped');
}
});
}
},
'should not skip any log lines': function (results) {
var testIndex = 0;

results.file.forEach(function (log) {
if (+log.message !== ++testIndex) {
throw new Error('Number skipped');
}
});
}
}).export(module);
}(
require('assert'),
require('fs'),
require('os'),
require('path'),
require('vows'),
require('../../lib/winston')
);
}
}).export(module);

0 comments on commit 8e22257

Please sign in to comment.