file-stress-test.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * file-stress-test.js: Tests for stressing File transport
  3. *
  4. * (C) 2014 William Wong
  5. * MIT LICENSE
  6. *
  7. */
  8. var assert = require('assert'),
  9. fs = require('fs'),
  10. os = require('os'),
  11. path = require('path'),
  12. vows = require('vows'),
  13. winston = require('../../lib/winston');
  14. vows.describe('winston/transports/file').addBatch({
  15. 'A stressed instance of the File Transport': {
  16. topic: function () {
  17. var callback = this.callback.bind(this),
  18. logPath = path.resolve(__dirname, '../fixtures/logs/file-stress-test.log');
  19. try {
  20. fs.unlinkSync(logPath);
  21. } catch (ex) {
  22. if (ex && ex.code !== 'ENOENT') { return callback(ex); }
  23. }
  24. var fileTransport = new (winston.transports.File)({
  25. filename: logPath
  26. }),
  27. logger = new (winston.Logger)({
  28. transports: [fileTransport]
  29. });
  30. fileTransport.on('open', function () {
  31. setTimeout(function () {
  32. clearInterval(interval);
  33. logger.query({ order: 'asc' }, function (err, results) {
  34. callback(null, results);
  35. });
  36. }, 100);
  37. });
  38. var logIndex = 0,
  39. interval = setInterval(function () {
  40. logger.info(++logIndex);
  41. stress(200);
  42. }, 0);
  43. logger.info(++logIndex);
  44. stress(200);
  45. function stress(duration) {
  46. var startTime = Date.now();
  47. while (Date.now() - startTime < duration) {
  48. Math.sqrt(Math.PI);
  49. }
  50. }
  51. },
  52. 'should not skip any log lines': function (results) {
  53. var testIndex = 0;
  54. results.file.forEach(function (log) {
  55. if (+log.message !== ++testIndex) {
  56. throw new Error('Number skipped');
  57. }
  58. });
  59. }
  60. }
  61. }).export(module);