Skip to content

Commit

Permalink
add tests to detect memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Aug 10, 2018
1 parent d3cfb25 commit 6477af9
Show file tree
Hide file tree
Showing 16 changed files with 344 additions and 8 deletions.
12 changes: 6 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
lint:
docker:
- image: node:alpine
- image: node
working_directory: ~/dd-trace-js
steps:
- checkout
Expand All @@ -17,7 +17,7 @@ jobs:
command: npm run lint
build-node-base: &node-base
docker:
- image: node:alpine
- image: node
- &postgres
image: postgres:9.5
- &mysql
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
build-node-4:
<<: *node-base
docker:
- image: node:4-alpine
- image: node:4
- *postgres
- *mysql
- *redis
Expand All @@ -63,7 +63,7 @@ jobs:
build-node-6:
<<: *node-base
docker:
- image: node:6-alpine
- image: node:6
- *postgres
- *mysql
- *redis
Expand All @@ -73,7 +73,7 @@ jobs:
build-node-8:
<<: *node-base
docker:
- image: node:8-alpine
- image: node:8
- *postgres
- *mysql
- *redis
Expand All @@ -83,7 +83,7 @@ jobs:
build-node-latest:
<<: *node-base
docker:
- image: node:alpine
- image: node
- *postgres
- *mysql
- *redis
Expand Down
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ dev,retry,MIT,Copyright 2011 Tim Koschützki Felix Geisendörfer
dev,semver,ISC,Copyright Isaac Z. Schlueter and Contributors
dev,sinon,BSD-3-Clause,Copyright 2010-2017 Christian Johansen
dev,sinon-chai,WTFPL and BSD-2-Clause,Copyright 2004 Sam Hocevar 2012–2017 Domenic Denicola
dev,tape,MIT,Copyright James Halliday
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
version: '2'
services:
server:
build: test/server
ports:
- 127.0.0.1:3030:3030
postgres:
image: postgres:9.5-alpine
ports:
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"jsdoc:watch": "gulp jsdoc:watch",
"lint": "eslint . && node scripts/check_licenses.js",
"tdd": "mocha --watch",
"test": "nyc --reporter text --reporter lcov mocha 'test/**/*.spec.js'"
"test": "nyc --reporter text --reporter lcov mocha 'test/**/*.spec.js'",
"leak": "node --no-warnings ./node_modules/.bin/tape 'test/leak/**/*.js'"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -52,6 +53,7 @@
"url-parse": "^1.2.0"
},
"devDependencies": {
"@airbnb/node-memwatch": "^1.0.2",
"amqplib": "^0.5.2",
"axios": "^0.18.0",
"benchmark": "^2.1.4",
Expand Down Expand Up @@ -83,6 +85,7 @@
"redis": "^2.8.0",
"retry": "^0.10.1",
"sinon": "^4.2.1",
"sinon-chai": "^2.14.0"
"sinon-chai": "^2.14.0",
"tape": "^4.9.1"
}
}
40 changes: 40 additions & 0 deletions test/leak/plugins/amqplib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

require('../../..')
.init({ plugins: false, sampleRate: 0 })
.use('amqplib')

const test = require('tape')
const profile = require('../../profile')

test('amqplib plugin should not leak when using callbacks', t => {
require('amqplib/callback_api')
.connect((err, conn) => {
if (err) return t.fail(err)

conn.createChannel((err, ch) => {
if (err) return t.fail(err)

profile(t, operation).then(() => conn.close())

function operation (done) {
ch.assertQueue('test', {}, done)
}
})
})
})

test('amqplib plugin should not leak when using promises', t => {
require('amqplib').connect()
.then(conn => {
return conn.createChannel()
.then(ch => {
profile(t, operation).then(() => conn.close())

function operation (done) {
ch.assertQueue('test', {}).then(done)
}
})
})
.catch(t.fail)
})
25 changes: 25 additions & 0 deletions test/leak/plugins/graphql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

require('../../..')
.init({ plugins: false, sampleRate: 0 })
.use('graphql')

const test = require('tape')
const graphql = require('graphql')
const profile = require('../../profile')

test('graphql plugin should not leak', t => {
const schema = graphql.buildSchema(`
type Query {
hello: String
}
`)

const source = `{ hello }`

profile(t, operation)

function operation (done) {
graphql.graphql(schema, source).then(done)
}
})
29 changes: 29 additions & 0 deletions test/leak/plugins/mongodb-core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

require('../../..')
.init({ plugins: false, sampleRate: 0 })
.use('mongodb-core')

const test = require('tape')
const mongo = require('mongodb-core')
const profile = require('../../profile')

test('mongodb-core plugin should not leak', t => {
const server = new mongo.Server({
host: 'localhost',
port: 27017,
reconnect: false
})

server.on('connect', () => {
profile(t, operation).then(() => server.destroy())
})

server.on('error', t.fail)

server.connect()

function operation (done) {
server.insert(`test.1234`, [{ a: 1 }], {}, done)
}
})
28 changes: 28 additions & 0 deletions test/leak/plugins/mysql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

require('../../..')
.init({ plugins: false, sampleRate: 0 })
.use('mysql')

const test = require('tape')
const mysql = require('mysql')
const profile = require('../../profile')

test('mysql plugin should not leak', t => {
const connection = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'userpass',
database: 'db'
})

connection.connect(err => {
if (err) return t.fail(err)

profile(t, operation).then(() => connection.end())
})

function operation (done) {
connection.query('SELECT 1 + 1 AS solution', done)
}
})
28 changes: 28 additions & 0 deletions test/leak/plugins/mysql2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

require('../../..')
.init({ plugins: false, sampleRate: 0 })
.use('mysql2')

const test = require('tape')
const mysql2 = require('mysql2')
const profile = require('../../profile')

test('mysql2 plugin should not leak', t => {
const connection = mysql2.createConnection({
host: 'localhost',
user: 'user',
password: 'userpass',
database: 'db'
})

connection.connect(err => {
if (err) return t.fail(err)

profile(t, operation).then(() => connection.end())
})

function operation (done) {
connection.query('SELECT 1 + 1 AS solution', done)
}
})
28 changes: 28 additions & 0 deletions test/leak/plugins/pg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

require('../../..')
.init({ plugins: false, sampleRate: 0 })
.use('pg')

const test = require('tape')
const pg = require('pg')
const profile = require('../../profile')

test('pg plugin should not leak', t => {
const client = new pg.Client({
user: 'postgres',
password: 'postgres',
database: 'postgres',
application_name: 'test'
})

client.connect(err => {
if (err) return t.fail(err)

profile(t, operation).then(() => client.end())
})

function operation (done) {
client.query('SELECT 1 + 1 AS solution', done)
}
})
19 changes: 19 additions & 0 deletions test/leak/plugins/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

require('../../..')
.init({ plugins: false, sampleRate: 0 })
.use('redis')

const test = require('tape')
const redis = require('redis')
const profile = require('../../profile')

test('redis plugin should not leak', t => {
const client = redis.createClient()

profile(t, operation).then(() => client.quit())

function operation (done) {
client.get('foo', done)
}
})
14 changes: 14 additions & 0 deletions test/leak/scope_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

require('../..').init()

const test = require('tape')
const profile = require('../profile')

test('ScopeManager should destroy executions even if their context is already destroyed', t => {
profile(t, operation)

function operation (done) {
Promise.resolve().then(done)
}
})
15 changes: 15 additions & 0 deletions test/leak/tracer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const tracer = require('../..').init()

const test = require('tape')
const profile = require('../profile')

test('Tracer should not keep unfinished spans in memory if they are no longer needed', t => {
profile(t, operation)

function operation (done) {
tracer.startSpan('test')
done()
}
})

0 comments on commit 6477af9

Please sign in to comment.