Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Commit

Permalink
test: add truncation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Aug 6, 2018
1 parent e414679 commit c3244b7
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"scripts": {
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"test": "standard && nyc node test/test.js"
"test": "standard && nyc tape test/*.js"
},
"engines": {
"node": "6 || 8 || 10"
Expand Down
2 changes: 1 addition & 1 deletion test/unref-client.js → test/lib/unref-client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Client = require('../')
const Client = require('../../')

const client = new Client({
serverUrl: process.argv[2],
Expand Down
17 changes: 13 additions & 4 deletions test/utils.js → test/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const zlib = require('zlib')
const semver = require('semver')
const pem = require('https-pem')
const ndjson = require('ndjson')
const pkg = require('../package')
const Client = require('../')
const pkg = require('../../package')
const Client = require('../../')

exports.APMServer = APMServer
exports.processReq = processReq
Expand Down Expand Up @@ -80,11 +80,20 @@ function assertMetadata (t, obj) {
const _process = metadata.process
t.ok(_process.pid > 0)
t.ok(_process.ppid > 0)
t.ok(/(\/node|^node)$/.test(_process.title), `process.title should match /(\\/node|^node)$/ (was: ${_process.title})`)

if (_process.title.length === 1) {
// because of truncation test
t.equal(_process.title, process.title[0])
} else {
const regex = /(\/node|^node)$/
t.ok(regex.test(_process.title), `process.title should match ${regex} (was: ${_process.title})`)
}

t.ok(Array.isArray(_process.argv), 'process.title should be an array')
t.ok(_process.argv.length >= 2, 'process.title should contain at least two elements')
t.ok(/\/node$/.test(_process.argv[0]), `process.argv[0] should match /\\/node$/ (was: ${_process.argv[0]})`)
t.ok(/\/test\/(test|unref-client)\.js$/.test(_process.argv[1]), `process.argv[1] should match /\\/test\\/(test|unref-client)\\.js$/ (was: ${_process.argv[1]})"`)
const regex = /(\/test\/(test|truncate|lib\/unref-client)\.js|node_modules\/\.bin\/tape)$/
t.ok(regex.test(_process.argv[1]), `process.argv[1] should match ${regex} (was: ${_process.argv[1]})"`)
const system = metadata.system
t.ok(typeof system.hostname, 'string')
t.ok(system.hostname.length > 0)
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const exec = require('child_process').exec
const http = require('http')
const test = require('tape')
const semver = require('semver')
const utils = require('./utils')
const utils = require('./lib/utils')
const pkg = require('../package')
const Client = require('../')

Expand Down Expand Up @@ -649,7 +649,7 @@ test('client should not hold the process open', function (t) {

server.listen(function () {
const url = 'http://localhost:' + server.address().port
const file = path.join(__dirname, 'unref-client.js')
const file = path.join(__dirname, 'lib', 'unref-client.js')
exec(`node ${file} ${url}`, function (err, stdout, stderr) {
if (err) throw err
const end = Date.now()
Expand Down
250 changes: 250 additions & 0 deletions test/truncate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
'use strict'

const test = require('tape')
const utils = require('./lib/utils')

const APMServer = utils.APMServer
const processReq = utils.processReq
const assertReq = utils.assertReq
const assertMetadata = utils.assertMetadata
const assertEvent = utils.assertEvent

const options = [
{}, // default options
{truncateKeywordsAt: 1, truncateErrorMessagesAt: 1, truncateSourceLinesAt: 1},
{truncateErrorMessagesAt: -1}
]

options.forEach(function (opts) {
const veryLong = 9999
const keywordLen = opts.truncateKeywordsAt || 1024
const errMsgLen = opts.truncateErrorMessagesAt === -1
? veryLong
: (opts.truncateErrorMessagesAt || 2048)
const lineLen = opts.truncateSourceLinesAt || 1000

test('truncate transaction', function (t) {
t.plan(assertReq.asserts + assertMetadata.asserts + assertEvent.asserts)
const datas = [
assertMetadata,
assertEvent({
transaction: {
name: genStr('a', keywordLen),
type: genStr('b', keywordLen),
result: genStr('c', keywordLen),
sampled: true,
context: {
request: {
method: genStr('d', keywordLen),
url: {
protocol: genStr('e', keywordLen),
hostname: genStr('f', keywordLen),
port: genStr('g', keywordLen),
pathname: genStr('h', keywordLen),
search: genStr('i', keywordLen),
hash: genStr('j', keywordLen),
raw: genStr('k', keywordLen),
full: genStr('l', keywordLen)
}
},
user: {
id: genStr('m', keywordLen),
email: genStr('n', keywordLen),
username: genStr('o', keywordLen)
}
}
}
})
]
const server = APMServer(function (req, res) {
assertReq(t, req)
req = processReq(req)
req.on('data', function (obj) {
datas.shift()(t, obj)
})
req.on('end', function () {
res.end()
server.close()
t.end()
})
}).client(opts, function (client) {
client.sendTransaction({
name: genStr('a', veryLong),
type: genStr('b', veryLong),
result: genStr('c', veryLong),
sampled: true,
context: {
request: {
method: genStr('d', veryLong),
url: {
protocol: genStr('e', veryLong),
hostname: genStr('f', veryLong),
port: genStr('g', veryLong),
pathname: genStr('h', veryLong),
search: genStr('i', veryLong),
hash: genStr('j', veryLong),
raw: genStr('k', veryLong),
full: genStr('l', veryLong)
}
},
user: {
id: genStr('m', veryLong),
email: genStr('n', veryLong),
username: genStr('o', veryLong)
}
}
})
client.flush()
})
})

test('truncate span', function (t) {
t.plan(assertReq.asserts + assertMetadata.asserts + assertEvent.asserts)
const datas = [
assertMetadata,
assertEvent({
span: {
name: genStr('a', keywordLen),
type: genStr('b', keywordLen),
stacktrace: [
{pre_context: [genStr('c', lineLen), genStr('d', lineLen)], context_line: genStr('e', lineLen), post_context: [genStr('f', lineLen), genStr('g', lineLen)]},
{pre_context: [genStr('h', lineLen), genStr('i', lineLen)], context_line: genStr('j', lineLen), post_context: [genStr('k', lineLen), genStr('l', lineLen)]}
]
}
})
]
const server = APMServer(function (req, res) {
assertReq(t, req)
req = processReq(req)
req.on('data', function (obj) {
datas.shift()(t, obj)
})
req.on('end', function () {
res.end()
server.close()
t.end()
})
}).client(opts, function (client) {
client.sendSpan({
name: genStr('a', veryLong),
type: genStr('b', veryLong),
stacktrace: [
{pre_context: [genStr('c', veryLong), genStr('d', veryLong)], context_line: genStr('e', veryLong), post_context: [genStr('f', veryLong), genStr('g', veryLong)]},
{pre_context: [genStr('h', veryLong), genStr('i', veryLong)], context_line: genStr('j', veryLong), post_context: [genStr('k', veryLong), genStr('l', veryLong)]}
]
})
client.flush()
})
})

test('truncate error', function (t) {
t.plan(assertReq.asserts + assertMetadata.asserts + assertEvent.asserts)
const datas = [
assertMetadata,
assertEvent({
error: {
log: {
level: genStr('a', keywordLen),
logger_name: genStr('b', keywordLen),
message: genStr('c', errMsgLen),
param_message: genStr('d', keywordLen),
stacktrace: [
{pre_context: [genStr('e', lineLen), genStr('f', lineLen)], context_line: genStr('g', lineLen), post_context: [genStr('h', lineLen), genStr('i', lineLen)]},
{pre_context: [genStr('j', lineLen), genStr('k', lineLen)], context_line: genStr('l', lineLen), post_context: [genStr('m', lineLen), genStr('n', lineLen)]}
]
},
exception: {
message: genStr('o', errMsgLen),
type: genStr('p', keywordLen),
code: genStr('q', keywordLen),
module: genStr('r', keywordLen),
stacktrace: [
{pre_context: [genStr('s', lineLen), genStr('t', lineLen)], context_line: genStr('u', lineLen), post_context: [genStr('v', lineLen), genStr('w', lineLen)]},
{pre_context: [genStr('x', lineLen), genStr('y', lineLen)], context_line: genStr('z', lineLen), post_context: [genStr('A', lineLen), genStr('B', lineLen)]}
]
},
context: {
request: {
method: genStr('C', keywordLen),
url: {
protocol: genStr('D', keywordLen),
hostname: genStr('E', keywordLen),
port: genStr('F', keywordLen),
pathname: genStr('G', keywordLen),
search: genStr('H', keywordLen),
hash: genStr('I', keywordLen),
raw: genStr('J', keywordLen),
full: genStr('K', keywordLen)
}
},
user: {
id: genStr('L', keywordLen),
email: genStr('M', keywordLen),
username: genStr('N', keywordLen)
}
}
}
})
]
const server = APMServer(function (req, res) {
assertReq(t, req)
req = processReq(req)
req.on('data', function (obj) {
datas.shift()(t, obj)
})
req.on('end', function () {
res.end()
server.close()
t.end()
})
}).client(opts, function (client) {
client.sendError({
log: {
level: genStr('a', veryLong),
logger_name: genStr('b', veryLong),
message: genStr('c', veryLong),
param_message: genStr('d', veryLong),
stacktrace: [
{pre_context: [genStr('e', veryLong), genStr('f', veryLong)], context_line: genStr('g', veryLong), post_context: [genStr('h', veryLong), genStr('i', veryLong)]},
{pre_context: [genStr('j', veryLong), genStr('k', veryLong)], context_line: genStr('l', veryLong), post_context: [genStr('m', veryLong), genStr('n', veryLong)]}
]
},
exception: {
message: genStr('o', veryLong),
type: genStr('p', veryLong),
code: genStr('q', veryLong),
module: genStr('r', veryLong),
stacktrace: [
{pre_context: [genStr('s', veryLong), genStr('t', veryLong)], context_line: genStr('u', veryLong), post_context: [genStr('v', veryLong), genStr('w', veryLong)]},
{pre_context: [genStr('x', veryLong), genStr('y', veryLong)], context_line: genStr('z', veryLong), post_context: [genStr('A', veryLong), genStr('B', veryLong)]}
]
},
context: {
request: {
method: genStr('C', veryLong),
url: {
protocol: genStr('D', veryLong),
hostname: genStr('E', veryLong),
port: genStr('F', veryLong),
pathname: genStr('G', veryLong),
search: genStr('H', veryLong),
hash: genStr('I', veryLong),
raw: genStr('J', veryLong),
full: genStr('K', veryLong)
}
},
user: {
id: genStr('L', veryLong),
email: genStr('M', veryLong),
username: genStr('N', veryLong)
}
}
})
client.flush()
})
})
})

function genStr (ch, length) {
return new Array(length + 1).join(ch)
}

0 comments on commit c3244b7

Please sign in to comment.