|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var agent = require('../..').start({ |
| 4 | + appName: 'test', |
| 5 | + captureExceptions: false |
| 6 | +}) |
| 7 | + |
| 8 | +var http = require('http') |
| 9 | +var send = require('send') |
| 10 | +var test = require('tape') |
| 11 | + |
| 12 | +// run it 5 times in case of false positives due to race conditions |
| 13 | +times(5, function (n, done) { |
| 14 | + test('https://github.com/elastic/apm-agent-nodejs/issues/75 ' + n, function (t) { |
| 15 | + resetAgent(function (endpoint, headers, data, cb) { |
| 16 | + t.equal(data.transactions.length, 2, 'should create transactions') |
| 17 | + data.transactions.forEach(function (trans) { |
| 18 | + t.equal(trans.traces.length, 1, 'transaction should have one trace') |
| 19 | + t.equal(trans.traces[0].name, trans.id, 'trace should belong to transaction') |
| 20 | + }) |
| 21 | + server.close() |
| 22 | + t.end() |
| 23 | + done() |
| 24 | + }) |
| 25 | + |
| 26 | + var server = http.createServer(function (req, res) { |
| 27 | + var trace = agent.buildTrace() |
| 28 | + trace.start(agent._instrumentation.currentTransaction.id) |
| 29 | + setTimeout(function () { |
| 30 | + trace.end() |
| 31 | + send(req, __filename).pipe(res) |
| 32 | + }, 50) |
| 33 | + }) |
| 34 | + |
| 35 | + var requestNo = 0 |
| 36 | + |
| 37 | + server.listen(function () { |
| 38 | + request() |
| 39 | + request() |
| 40 | + }) |
| 41 | + |
| 42 | + function request () { |
| 43 | + var port = server.address().port |
| 44 | + http.get('http://localhost:' + port, function (res) { |
| 45 | + res.on('end', function () { |
| 46 | + if (++requestNo === 2) { |
| 47 | + agent._instrumentation._queue._flush() |
| 48 | + } |
| 49 | + }) |
| 50 | + res.resume() |
| 51 | + }) |
| 52 | + } |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +function times (max, fn) { |
| 57 | + var n = 0 |
| 58 | + run() |
| 59 | + function run () { |
| 60 | + if (++n > max) return |
| 61 | + fn(n, run) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function resetAgent (cb) { |
| 66 | + agent._instrumentation.currentTransaction = null |
| 67 | + agent._instrumentation._queue._clear() |
| 68 | + agent._httpClient = { request: cb || function () {} } |
| 69 | + agent.captureError = function (err) { throw err } |
| 70 | +} |
0 commit comments