Files
elasticsearch-js/test/unit/specs/stream_logger.js
Dominykas Blyžė f1de944809 Upgrade to lodash v4 (#660)
* npm install lodash-2

Someone handily published a lodash-2 v4.17.4 - it is exactly the same as lodash v4.17.4, so it is safe to use during the migration.

* use lodash-2 in tests

* update tests to split utils vs lodash

* remove Utils.nextTick usage

Utils.nextTick with a single argument is the same as process.nextTick

* lowercase utils

Because it seems that this is the coding style in this repo

* upgrade lodash in grunt/*

* keep lodash-2 as a dev dep for now

* use lodash-2 in scripts

* use snakeCase from utils

It was a mistake in my previous commit to not update this usage

* fix naming gruntUtils vs utils

As all three - gruntUtils, utils and lodash (_) are getting passed into templates, it makes sense to keep the naming consistent

* fix naming gruntUtils vs utils

As all three - gruntUtils, utils and lodash (_) are getting passed into templates, it makes sense to keep the naming consistent

* split utils vs lodash in scripts/generate

Also use lodash-2 where it is easy to do so

* use utils.get until lodash upgrade

* remove lodash.isempty; lodash-2 now used in prod (in src/lib/apis/ code)

* unbundle lodash from utils

* upgrade to lodash 4

* remove lodash.get and lodash.trimEnd

* clean out unused code

* clean out unused code

* fix a breaking change listed under "notable changes" rather than under "breaking changes"...
2018-05-14 11:37:23 -07:00

92 lines
2.7 KiB
JavaScript

describe('Stream Logger', function () {
var Log = require('../../../src/lib/log');
var StreamLogger = require('../../../src/lib/loggers/stream');
var MockWritableStream = require('../../mocks/writable_stream');
var once = require('events').EventEmitter.prototype.once;
var stream = new MockWritableStream();
var _ = require('lodash');
var utils = require('../../../src/lib/utils');
var expect = require('expect.js');
var parentLog;
var stub = require('../../utils/auto_release_stub').make();
beforeEach(function () {
stub(stream, 'write');
stub(stream, 'end');
parentLog = new Log();
});
afterEach(function () {
parentLog.close();
utils.clearWriteStreamBuffer(stream);
});
function makeLogger(parent, levels) {
parent = parent || parentLog;
var config = {
levels: Log.parseLevels(levels || 'trace'),
stream: stream
};
return new StreamLogger(parent, config);
}
require('../generic_logger_tests')(makeLogger);
describe('buffer flush', function () {
if (require('stream').Writable) {
it('writes everything in the buffer to console.error', function () {
var logger = makeLogger();
var line = 'This string is written 10 times to create buffered output\n';
// get the last handler for process's "exit" event
var exitHandlers = process._events.exit;
var exitHandler = _.isArray(exitHandlers) ? _.last(exitHandlers) : exitHandlers;
// allow the logger to acctually write to the stream
stream.write.restore();
// write the line 10 times
_.times(10, function () {
logger.onDebug(line);
});
// collect everything that is written to console.error
var flushedOutput = '';
stub(console, 'error', function (str) {
flushedOutput += str;
});
// call the event handler
exitHandler.call(process);
// restore console.error asap.
console.error.restore();
// the first line is sent immediately to _write and there is nothing we are not going to worry about it
expect(flushedOutput).to.match(new RegExp(line));
expect(flushedOutput.match(new RegExp(line, 'g'))).to.have.length(9);
});
} else {
it('does not fall apart with non streams2 streams', function () {
var exitHandler;
stub(process, 'once', function (event, handler) {
if (event === 'exit') {
exitHandler = handler;
}
once.call(process, event, handler);
});
var logger = makeLogger();
expect(function () {
// call the event handler
exitHandler.call(process);
}).to.not.throwError();
});
}
});
});