From 963a382055c2fa8863e1e6b55cc8e967d25cb4a4 Mon Sep 17 00:00:00 2001 From: mattjstar Date: Mon, 9 Jan 2017 20:04:26 -0500 Subject: [PATCH 1/2] add garbage collection tests, and fix scrubFields --- .travis.yml | 10 +++++++ package.json | 6 +++-- src/client.js | 6 ++--- src/node.js | 6 ++--- src/util/common/scrub.js | 5 ++++ test/browser.index.js | 4 +-- test/specs/{ => common}/logger.spec.js | 5 ++-- test/specs/{ => common}/requestLogger.spec.js | 8 +++--- test/specs/leakage/logger.spec.js | 27 +++++++++++++++++++ 9 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 src/util/common/scrub.js rename test/specs/{ => common}/logger.spec.js (98%) rename test/specs/{ => common}/requestLogger.spec.js (96%) create mode 100644 test/specs/leakage/logger.spec.js diff --git a/.travis.yml b/.travis.yml index 54b0c39..f85299e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,16 @@ language: node_js node_js: - "6.*" +# https://github.com/andywer/leakage#travis-ci +addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-4.8 +before_install: + - if [[ $TRAVIS_OS_NAME == "linux" ]]; then export CXX=g++-4.8; fi + branches: only: - master diff --git a/package.json b/package.json index ef3923c..231376c 100644 --- a/package.json +++ b/package.json @@ -19,14 +19,15 @@ "main": "dist/node.js", "browser": "dist/client.js", "scripts": { - "test": "npm run test:browser && npm run test:node", + "test": "npm run test:browser && npm run test:node && npm run test:leakage", "tdd": "npm-run-all --parallel test:*:tdd", "test:ci": "npm run test:browser && npm run test:node:ci", "test:browser": "NODE_ENV=test karma start test/karma.conf.js", "test:browser:tdd": "npm run test:browser -- --auto-watch --no-single-run", - "test:node": "NODE_ENV=test nyc mocha test/runner test/specs/**/*.js", + "test:node": "NODE_ENV=test nyc mocha test/runner test/specs/common/**/*.js", "test:node:tdd": "npm run test:node -- --cache --watch", "test:node:ci": "npm run test:node && nyc report --reporter=text-lcov | coveralls", + "test:leakage": "NODE_ENV=test nyc mocha test/runner test/specs/leakage/**/*.js", "test:bundle:tdd": "npm-run-all -l --parallel 'bundle:* -- --watch'", "lint": "eslint --format ./node_modules/eslint-friendly-formatter --cache src test", "clean": "rimraf dist && mkdirp dist", @@ -104,6 +105,7 @@ "karma-sauce-launcher": "1.1.0", "karma-sourcemap-loader": "0.3.7", "karma-webpack": "1.8.1", + "leakage": "0.2.0", "mkdirp": "0.5.1", "mocha": "3.2.0", "np": "2.12.0", diff --git a/src/client.js b/src/client.js index 2aab163..6738825 100644 --- a/src/client.js +++ b/src/client.js @@ -1,7 +1,7 @@ import bunyan from 'bunyan'; -import hideSecrets from 'hide-secrets'; -import { get, forEach } from 'lodash'; +import { forEach } from 'lodash'; +import scrub from './util/common/scrub'; import { assembleConfig, toBunyanConfig, BUNYAN_LOGGER_LEVELS } from './util/common/config'; import ClientConsoleLogger from './util/client/consoleLogger'; @@ -35,7 +35,7 @@ bunyan.prototype.child = function(options, simple) { forEach(BUNYAN_LOGGER_LEVELS, (type) => { const original = bunyan.prototype[type]; bunyan.prototype[type] = function(...args) { - const newArgs = args.map((arg) => hideSecrets(arg, { badWords: get(this.config, 'scrubFields', []) })); + const newArgs = args.map((arg) => scrub(arg, this.config)); return original.apply(this, newArgs); } }); diff --git a/src/node.js b/src/node.js index f425485..97a10fa 100644 --- a/src/node.js +++ b/src/node.js @@ -1,8 +1,8 @@ import bunyan from 'bunyan'; -import hideSecrets from 'hide-secrets'; -import { get, forEach } from 'lodash'; +import { forEach } from 'lodash'; import { assembleConfig, toBunyanConfig, BUNYAN_LOGGER_LEVELS } from './util/common/config'; +import scrub from './util/common/scrub'; import Rollbar from 'rollbar'; import bunyanFormat from 'bunyan-format'; @@ -39,7 +39,7 @@ bunyan.prototype.child = function(options, simple) { forEach(BUNYAN_LOGGER_LEVELS, (type) => { const original = bunyan.prototype[type]; bunyan.prototype[type] = function(...args) { - const newArgs = args.map((arg) => hideSecrets(arg, { badWords: get(this.config, 'scrubFields', []) })); + const newArgs = args.map((arg) => scrub(arg, this.config)); return original.apply(this, newArgs); } }); diff --git a/src/util/common/scrub.js b/src/util/common/scrub.js new file mode 100644 index 0000000..6f799a4 --- /dev/null +++ b/src/util/common/scrub.js @@ -0,0 +1,5 @@ +import hideSecrets from 'hide-secrets'; + +export default function(obj, config = {}) { + return hideSecrets(obj, { badWords: config.scrubFields }); +} diff --git a/test/browser.index.js b/test/browser.index.js index e02b8d7..31a6d22 100644 --- a/test/browser.index.js +++ b/test/browser.index.js @@ -17,5 +17,5 @@ global.Rollbar = { }; // require all `/test/specs/**/*.js` -const testsContext = require.context('./specs/', true, /\.js$/); -testsContext.keys().forEach(testsContext); \ No newline at end of file +const testsContext = require.context('./specs/common', true, /\.js$/); +testsContext.keys().forEach(testsContext); diff --git a/test/specs/logger.spec.js b/test/specs/common/logger.spec.js similarity index 98% rename from test/specs/logger.spec.js rename to test/specs/common/logger.spec.js index cb94154..7095791 100644 --- a/test/specs/logger.spec.js +++ b/test/specs/common/logger.spec.js @@ -1,7 +1,7 @@ // Require the package. For client tests, webpack should // resolve to the browser version automatically. -import Logger from '../../'; -import TestLogger from '../testLogger'; +import Logger from '../../../'; +import TestLogger from '../../testLogger'; // Logentries validates the token it is passed, // here is a fake one in an acceptable format @@ -13,7 +13,6 @@ describe('we-js-logger', () => { expect(new Logger()).to.be.ok; }); - describe('options', () => { it('accepts a name', () => { const name = 'WeTest!'; diff --git a/test/specs/requestLogger.spec.js b/test/specs/common/requestLogger.spec.js similarity index 96% rename from test/specs/requestLogger.spec.js rename to test/specs/common/requestLogger.spec.js index 6c25acd..57a6124 100644 --- a/test/specs/requestLogger.spec.js +++ b/test/specs/common/requestLogger.spec.js @@ -1,7 +1,7 @@ -import Logger from '../../'; -import TestLogger from '../testLogger'; +import Logger from '../../../'; +import TestLogger from '../../testLogger'; -import createRequestLogger from '../../src/util/server/requestLogger'; +import createRequestLogger from '../../../src/util/server/requestLogger'; // Only run these tests in node // FIXME find a better way to do this. Should we use `env-universal`? @@ -135,4 +135,4 @@ if (typeof document === 'undefined') { }); }); }); -} \ No newline at end of file +} diff --git a/test/specs/leakage/logger.spec.js b/test/specs/leakage/logger.spec.js new file mode 100644 index 0000000..83439a9 --- /dev/null +++ b/test/specs/leakage/logger.spec.js @@ -0,0 +1,27 @@ +import { iterate } from 'leakage'; +import Logger from '../../../'; + + +describe('Heap Tests - we-js-logger', function() { + let log; + + beforeEach(() => { + log = new Logger({ stdout: false }); + }); + + it('does not leak when logging 1k times', function() { + this.timeout(20000); + iterate(1000, () => { + log.info('log time'); + }); + }); + + it('does not leak when building and logging child 1k times', function() { + this.timeout(20000); + iterate(1000, () => { + const child = log.child(); + child.info('child time'); + }); + }); +}); + From 47cde2a9a1e87b3775ea245d0d102f1911aab68a Mon Sep 17 00:00:00 2001 From: mattjstar Date: Tue, 10 Jan 2017 11:50:00 -0500 Subject: [PATCH 2/2] Remove nyc from leakage target --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 231376c..cfe2c31 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "test:node": "NODE_ENV=test nyc mocha test/runner test/specs/common/**/*.js", "test:node:tdd": "npm run test:node -- --cache --watch", "test:node:ci": "npm run test:node && nyc report --reporter=text-lcov | coveralls", - "test:leakage": "NODE_ENV=test nyc mocha test/runner test/specs/leakage/**/*.js", + "test:leakage": "NODE_ENV=test mocha test/runner test/specs/leakage/**/*.js", "test:bundle:tdd": "npm-run-all -l --parallel 'bundle:* -- --watch'", "lint": "eslint --format ./node_modules/eslint-friendly-formatter --cache src test", "clean": "rimraf dist && mkdirp dist",