@@ -12,6 +12,7 @@ const helper = require('../../lib/agent_helper')
12
12
const { TransactionSpec } = require ( '../../../lib/shim/specs' )
13
13
const TransactionShim = require ( '../../../lib/shim/transaction-shim' )
14
14
const notRunningStates = [ 'stopped' , 'stopping' , 'errored' ]
15
+ const sinon = require ( 'sinon' )
15
16
16
17
/**
17
18
* Creates CAT headers to be used in handleMqTracingHeaders
@@ -220,15 +221,16 @@ test('TransactionShim', async function (t) {
220
221
221
222
await t . test ( 'should not create a nested transaction when `spec.nest` is false' , function ( t ) {
222
223
const { shim } = t . nr
224
+ sinon . stub ( shim . logger , 'trace' )
223
225
let webTx = null
224
226
let bgTx = null
225
227
let webCalled = false
226
228
let bgCalled = false
227
- const bg = shim . bindCreateTransaction ( function ( ) {
229
+ const bg = shim . bindCreateTransaction ( function bgTxFn ( ) {
228
230
bgCalled = true
229
231
bgTx = shim . tracer . getTransaction ( )
230
232
} , new TransactionSpec ( { type : shim . BG } ) )
231
- const web = shim . bindCreateTransaction ( function ( ) {
233
+ const web = shim . bindCreateTransaction ( function webTxFn ( ) {
232
234
webCalled = true
233
235
webTx = shim . tracer . getTransaction ( )
234
236
bg ( )
@@ -237,7 +239,51 @@ test('TransactionShim', async function (t) {
237
239
web ( )
238
240
assert . equal ( webCalled , true )
239
241
assert . equal ( bgCalled , true )
240
- assert . equal ( webTx , bgTx )
242
+ assert . equal ( webTx . id , bgTx . id )
243
+ assert . deepEqual ( shim . logger . trace . args , [
244
+ [ 'Wrapping nodule itself (%s).' , 'bgTxFn' ] ,
245
+ [ 'Wrapping nodule itself (%s).' , 'webTxFn' ] ,
246
+ [ 'Creating new %s transaction for %s' , 'web' , 'webTxFn' ] ,
247
+ [ 'Applying segment %s' , 'ROOT' ] ,
248
+ [
249
+ 'Transaction %s exists, not creating new transaction %s for %s' ,
250
+ bgTx . id ,
251
+ 'bg' ,
252
+ 'bgTxFn'
253
+ ]
254
+ ] )
255
+ } )
256
+
257
+ await t . test ( 'should create a new transaction when `spec.nest` is false and current transaction is not active' , function ( t ) {
258
+ const { shim } = t . nr
259
+ sinon . stub ( shim . logger , 'trace' )
260
+ let webTx = null
261
+ let bgTx = null
262
+ let webCalled = false
263
+ let bgCalled = false
264
+ const bg = shim . bindCreateTransaction ( function bgTxFn ( ) {
265
+ bgCalled = true
266
+ bgTx = shim . tracer . getTransaction ( )
267
+ } , new TransactionSpec ( { type : shim . BG } ) )
268
+ const web = shim . bindCreateTransaction ( function webTxFn ( ) {
269
+ webCalled = true
270
+ webTx = shim . tracer . getTransaction ( )
271
+ webTx . end ( )
272
+ bg ( )
273
+ } , new TransactionSpec ( { type : shim . WEB } ) )
274
+
275
+ web ( )
276
+ assert . equal ( webCalled , true )
277
+ assert . equal ( bgCalled , true )
278
+ assert . notEqual ( webTx . id , bgTx . id )
279
+ assert . deepEqual ( shim . logger . trace . args , [
280
+ [ 'Wrapping nodule itself (%s).' , 'bgTxFn' ] ,
281
+ [ 'Wrapping nodule itself (%s).' , 'webTxFn' ] ,
282
+ [ 'Creating new %s transaction for %s' , 'web' , 'webTxFn' ] ,
283
+ [ 'Applying segment %s' , 'ROOT' ] ,
284
+ [ 'Creating new %s transaction for %s' , 'bg' , 'bgTxFn' ] ,
285
+ [ 'Applying segment %s' , 'ROOT' ]
286
+ ] )
241
287
} )
242
288
243
289
for ( const agentState of notRunningStates ) {
@@ -319,6 +365,25 @@ test('TransactionShim', async function (t) {
319
365
}
320
366
} )
321
367
368
+ await t . test ( 'should not nest transaction if first transaction is inactive and same type' , function ( t ) {
369
+ const { shim } = t . nr
370
+ const transactions = [ ]
371
+ const web = shim . bindCreateTransaction ( function ( cb ) {
372
+ const tx = shim . tracer . getTransaction ( )
373
+ transactions . push ( tx )
374
+ tx . end ( )
375
+ if ( cb ) {
376
+ cb ( )
377
+ }
378
+ } , new TransactionSpec ( { type : shim . WEB , nest : true } ) )
379
+
380
+ const web2 = shim . bindCreateTransaction ( function ( ) {
381
+ transactions . push ( shim . tracer . getTransaction ( ) )
382
+ } , new TransactionSpec ( { type : shim . WEB , nest : true } ) )
383
+ web ( web2 )
384
+ assert . notEqual ( transactions [ 0 ] . id , transactions [ 1 ] . id )
385
+ } )
386
+
322
387
for ( const agentState of notRunningStates ) {
323
388
await t . test ( `should not create transaction when agent state is ${ agentState } ` , ( t ) => {
324
389
const { agent, shim } = t . nr
0 commit comments