transaction.js
3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict'
var test = require('tape')
var mockInstrumentation = require('./_instrumentation')
var Transaction = require('../../lib/instrumentation/transaction')
var Trace = require('../../lib/instrumentation/trace')
test('init', function (t) {
var ins = mockInstrumentation(function (added) {
t.ok(false)
})
var trans = new Transaction(ins._agent, 'name', 'type', 'result')
t.equal(trans.name, 'name')
t.equal(trans.type, 'type')
t.equal(trans.result, 'result')
t.equal(trans.ended, false)
t.deepEqual(trans.traces, [])
t.end()
})
test('#end() - no traces', function (t) {
var ins = mockInstrumentation(function (added) {
t.equal(added.ended, true)
t.equal(added, trans)
t.equal(trans.traces.length, 1)
t.deepEqual(trans.traces, [trans._rootTrace])
t.end()
})
var trans = new Transaction(ins._agent)
trans.end()
})
test('#end() - with traces', function (t) {
var ins = mockInstrumentation(function (added) {
t.equal(added.ended, true)
t.equal(added, trans)
t.equal(trans.traces.length, 2)
t.deepEqual(trans.traces, [trace, trans._rootTrace])
t.end()
})
var trans = new Transaction(ins._agent)
var trace = new Trace(trans)
trace.start()
trace.end()
trans.end()
})
test('#duration()', function (t) {
var ins = mockInstrumentation(function (added) {
t.ok(added.duration() > 24)
t.ok(added.duration() < 35)
t.end()
})
var trans = new Transaction(ins._agent)
setTimeout(function () {
trans.end()
}, 25)
})
test('#duration() - un-ended transaction', function (t) {
var ins = mockInstrumentation(function (added) {
t.ok(false)
})
var trans = new Transaction(ins._agent)
t.equal(trans.duration(), null)
t.end()
})
test('#setDefaultName() - with initial value', function (t) {
var ins = mockInstrumentation(function (added) {
t.ok(false)
})
var trans = new Transaction(ins._agent, 'default-1')
t.equal(trans.name, 'default-1')
trans.setDefaultName('default-2')
t.equal(trans.name, 'default-2')
t.end()
})
test('#setDefaultName() - no initial value', function (t) {
var ins = mockInstrumentation(function (added) {
t.ok(false)
})
var trans = new Transaction(ins._agent)
t.equal(trans.name, '')
trans.setDefaultName('default')
t.equal(trans.name, 'default')
t.end()
})
test('name - custom first, then default', function (t) {
var ins = mockInstrumentation(function (added) {
t.ok(false)
})
var trans = new Transaction(ins._agent)
trans.name = 'custom'
trans.setDefaultName('default')
t.equal(trans.name, 'custom')
t.end()
})
test('name - default first, then custom', function (t) {
var ins = mockInstrumentation(function (added) {
t.ok(false)
})
var trans = new Transaction(ins._agent)
trans.setDefaultName('default')
trans.name = 'custom'
t.equal(trans.name, 'custom')
t.end()
})
test('parallel transactions', function (t) {
var calls = 0
var ins = mockInstrumentation(function (added) {
t.equal(added._rootTrace.signature, 'transaction')
t.equal(added.traces.length, 1, added.name + ' should have 1 trace')
t.equal(added.traces[0], added._rootTrace)
calls++
if (calls === 1) {
t.equal(added.name, 'second')
} else if (calls === 2) {
t.equal(added.name, 'first')
t.end()
}
})
ins.currentTransaction = null
setImmediate(function () {
var t1 = new Transaction(ins._agent, 'first')
setTimeout(function () {
t1.end()
}, 100)
})
setTimeout(function () {
var t2 = new Transaction(ins._agent, 'second')
setTimeout(function () {
t2.end()
}, 25)
}, 25)
})