|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var agent = require('../..').start({ |
| 4 | + appName: 'test', |
| 5 | + captureExceptions: false, |
| 6 | + ff_asyncHooks: true |
| 7 | +}) |
| 8 | +var ins = agent._instrumentation |
| 9 | + |
| 10 | +var test = require('tape') |
| 11 | +var semver = require('semver') |
| 12 | + |
| 13 | +test('setTimeout', function (t) { |
| 14 | + t.plan(2) |
| 15 | + twice(function () { |
| 16 | + var trans = agent.startTransaction() |
| 17 | + setTimeout(function () { |
| 18 | + t.equal(ins.currentTransaction && ins.currentTransaction.id, trans.id) |
| 19 | + }, 50) |
| 20 | + }) |
| 21 | +}) |
| 22 | + |
| 23 | +test('setInterval', function (t) { |
| 24 | + t.plan(2) |
| 25 | + twice(function () { |
| 26 | + var trans = agent.startTransaction() |
| 27 | + var timer = setInterval(function () { |
| 28 | + clearInterval(timer) |
| 29 | + t.equal(ins.currentTransaction && ins.currentTransaction.id, trans.id) |
| 30 | + }, 50) |
| 31 | + }) |
| 32 | +}) |
| 33 | + |
| 34 | +test('setImmediate', function (t) { |
| 35 | + t.plan(2) |
| 36 | + twice(function () { |
| 37 | + var trans = agent.startTransaction() |
| 38 | + setImmediate(function () { |
| 39 | + t.equal(ins.currentTransaction && ins.currentTransaction.id, trans.id) |
| 40 | + }) |
| 41 | + }) |
| 42 | +}) |
| 43 | + |
| 44 | +test('process.nextTick', function (t) { |
| 45 | + t.plan(2) |
| 46 | + twice(function () { |
| 47 | + var trans = agent.startTransaction() |
| 48 | + process.nextTick(function () { |
| 49 | + t.equal(ins.currentTransaction && ins.currentTransaction.id, trans.id) |
| 50 | + }) |
| 51 | + }) |
| 52 | +}) |
| 53 | + |
| 54 | +// We can't instrument ore-defined promises properly without async-hooks, so |
| 55 | +// lets not run these tests on versions of Node.js without async-hooks |
| 56 | +if (semver.gte(process.version, '8.1.0')) { |
| 57 | + test('pre-defined, pre-resolved shared promise', function (t) { |
| 58 | + t.plan(4) |
| 59 | + |
| 60 | + var p = Promise.resolve('success') |
| 61 | + |
| 62 | + twice(function () { |
| 63 | + var trans = agent.startTransaction() |
| 64 | + p.then(function (result) { |
| 65 | + t.equal(result, 'success') |
| 66 | + t.equal(ins.currentTransaction && ins.currentTransaction.id, trans.id) |
| 67 | + }) |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + test('pre-defined, pre-resolved non-shared promise', function (t) { |
| 72 | + t.plan(4) |
| 73 | + |
| 74 | + twice(function () { |
| 75 | + var p = Promise.resolve('success') |
| 76 | + var trans = agent.startTransaction() |
| 77 | + p.then(function (result) { |
| 78 | + t.equal(result, 'success') |
| 79 | + t.equal(ins.currentTransaction && ins.currentTransaction.id, trans.id) |
| 80 | + }) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + test('pre-defined, post-resolved promise', function (t) { |
| 85 | + t.plan(4) |
| 86 | + twice(function () { |
| 87 | + var p = new Promise(function (resolve) { |
| 88 | + setTimeout(function () { |
| 89 | + resolve('success') |
| 90 | + }, 20) |
| 91 | + }) |
| 92 | + var trans = agent.startTransaction() |
| 93 | + p.then(function (result) { |
| 94 | + t.equal(result, 'success') |
| 95 | + t.equal(ins.currentTransaction && ins.currentTransaction.id, trans.id) |
| 96 | + }) |
| 97 | + }) |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +test('post-defined, post-resolved promise', function (t) { |
| 102 | + t.plan(4) |
| 103 | + twice(function () { |
| 104 | + var trans = agent.startTransaction() |
| 105 | + var p = new Promise(function (resolve) { |
| 106 | + setTimeout(function () { |
| 107 | + resolve('success') |
| 108 | + }, 20) |
| 109 | + }) |
| 110 | + p.then(function (result) { |
| 111 | + t.equal(result, 'success') |
| 112 | + t.equal(ins.currentTransaction && ins.currentTransaction.id, trans.id) |
| 113 | + }) |
| 114 | + }) |
| 115 | +}) |
| 116 | + |
| 117 | +function twice (fn) { |
| 118 | + setImmediate(fn) |
| 119 | + setImmediate(fn) |
| 120 | +} |
0 commit comments