11'use strict'
22
3+ require ( '../test-setup.spec.js' )
4+
35var expect = require ( 'chai' ) . expect
46var wrapper = require ( '@risingstack/trace/lib/instrumentations/amqplib' )
57var microtime = require ( '@risingstack/trace/lib/optionalDependencies/microtime' )
8+ var Storage = require ( '@risingstack/trace/lib/agent/storage' )
9+
10+ describe . only ( 'amqplib' , function ( ) {
11+ var fakeAgent
612
7- describe ( 'amqplib module wrapper' , function ( ) {
813 beforeEach ( function ( ) {
9- delete require . cache [ require . resolve ( 'amqplib/lib/connection' ) ]
10- delete require . cache [ require . resolve ( 'amqplib/lib/callback_model' ) ]
11- delete require . cache [ require . resolve ( 'amqplib/lib/channel_model' ) ]
12- delete require . cache [ require . resolve ( 'amqplib/callback_api' ) ]
13- delete require . cache [ require . resolve ( 'amqplib/channel_api' ) ]
14- delete require . cache [ require . resolve ( 'amqplib' ) ]
14+ fakeAgent = {
15+ incomingEdgeMetrics : {
16+ report : this . sandbox . spy ( )
17+ } ,
18+ getRequestId : this . sandbox . spy ( ) ,
19+ generateRequestId : this . sandbox . stub ( ) . returns ( '42' ) ,
20+ generateCommId : this . sandbox . stub ( ) . returns ( '52' ) ,
21+ getServiceKey : this . sandbox . stub ( ) . returns ( '62' ) ,
22+ storage : new Storage ( )
23+ }
24+ this . sandbox . stub ( microtime , 'now' ) . returns ( 42 )
1525 } )
1626
17- describe ( 'channel api' , function ( ) {
27+ describe ( 'channel api module wrapper ' , function ( ) {
1828 beforeEach ( function ( ) {
29+ wrapper . instrumentations [ 5 ] . post ( require ( 'amqplib/lib/connection' ) , fakeAgent )
30+ wrapper . instrumentations [ 3 ] . post ( require ( 'amqplib/lib/channel_model' ) , fakeAgent )
31+ } )
32+
33+ afterEach ( function ( ) {
1934 delete require . cache [ require . resolve ( 'amqplib/lib/connection' ) ]
2035 delete require . cache [ require . resolve ( 'amqplib/lib/channel_model' ) ]
2136 delete require . cache [ require . resolve ( 'amqplib/channel_api' ) ]
2237 delete require . cache [ require . resolve ( 'amqplib' ) ]
2338 } )
2439
25- it ( 'instruments publish - consume' , function ( done ) {
26- this . sandbox . stub ( microtime , 'now' ) . returns ( 42 )
27- var fakeAgent = {
28- incomingEdgeMetrics : {
29- report : this . sandbox . spy ( )
30- } ,
31- getRequestId : this . sandbox . spy ( ) ,
32- generateRequestId : this . sandbox . stub ( ) . returns ( '42' ) ,
33- generateCommId : this . sandbox . stub ( ) . returns ( '52' ) ,
34- getServiceKey : this . sandbox . stub ( ) . returns ( '62' )
35- }
36- wrapper . instrumentations [ 5 ] . post ( require ( 'amqplib/lib/connection' ) , fakeAgent )
37- wrapper . instrumentations [ 3 ] . post ( require ( 'amqplib/lib/channel_model' ) , fakeAgent )
40+ it ( 'looks transparent' , function ( done ) {
41+ var open = require ( 'amqplib' ) . connect ( process . env . AMQP_URL )
42+ open . then ( function ( conn ) {
43+ return conn . createChannel ( )
44+ } ) . then ( function ( ch ) {
45+ ch . assertQueue ( 'test' ) . then ( function ( ok ) {
46+ return ch . sendToQueue ( 'test' , new Buffer ( 'something' ) )
47+ } ) . catch ( function ( err ) {
48+ done ( err )
49+ } ) . then ( function ( ) {
50+ ch . consume ( 'test' , function ( msg ) {
51+ try {
52+ ch . ack ( msg )
53+ expect ( fakeAgent . incomingEdgeMetrics . report ) . to . have . been . calledWith ( {
54+ protocol : 'amqp' ,
55+ serviceKey : 62 ,
56+ transportDelay : 0
57+ } )
58+ ch . close ( )
59+ done ( )
60+ } catch ( err ) {
61+ ch . close ( )
62+ done ( err )
63+ }
64+ } )
65+ } )
66+ } )
67+ } )
3868
39- var open = require ( 'amqplib' ) . connect ( 'amqp://localhost' )
69+ it ( 'is instrumented for incoming edges' , function ( done ) {
70+ var open = require ( 'amqplib' ) . connect ( process . env . AMQP_URL )
4071
4172 // Publisher
4273 open . then ( function ( conn ) {
@@ -55,9 +86,10 @@ describe('amqplib module wrapper', function () {
5586 serviceKey : 62 ,
5687 transportDelay : 0
5788 } )
58- expect ( msg . content . toString ( ) ) . to . eql ( 'something' )
89+ ch . close ( )
5990 done ( )
6091 } catch ( err ) {
92+ ch . close ( )
6193 done ( err )
6294 }
6395 } )
@@ -66,35 +98,49 @@ describe('amqplib module wrapper', function () {
6698 } )
6799 } )
68100
69- describe ( 'callback api' , function ( ) {
101+ describe ( 'callback api module wrapper ' , function ( ) {
70102 beforeEach ( function ( ) {
103+ wrapper . instrumentations [ 5 ] . post ( require ( 'amqplib/lib/connection' ) , fakeAgent )
104+ wrapper . instrumentations [ 4 ] . post ( require ( 'amqplib/lib/callback_model' ) , fakeAgent )
105+ } )
106+
107+ afterEach ( function ( ) {
71108 delete require . cache [ require . resolve ( 'amqplib/lib/connection' ) ]
72109 delete require . cache [ require . resolve ( 'amqplib/lib/callback_model' ) ]
73110 delete require . cache [ require . resolve ( 'amqplib/callback_api' ) ]
74111 delete require . cache [ require . resolve ( 'amqplib' ) ]
75112 } )
76113
77- it ( 'instruments publish - consume' , function ( done ) {
78- this . sandbox . stub ( microtime , 'now' ) . returns ( 42 )
79- var fakeAgent = {
80- incomingEdgeMetrics : {
81- report : this . sandbox . spy ( )
82- } ,
83- getRequestId : this . sandbox . spy ( ) ,
84- generateRequestId : this . sandbox . stub ( ) . returns ( '42' ) ,
85- generateCommId : this . sandbox . stub ( ) . returns ( '52' ) ,
86- getServiceKey : this . sandbox . stub ( ) . returns ( '62' )
87- }
88- wrapper . instrumentations [ 5 ] . post ( require ( 'amqplib/lib/connection' ) , fakeAgent )
89- wrapper . instrumentations [ 4 ] . post ( require ( 'amqplib/lib/callback_model' ) , fakeAgent )
114+ it ( 'looks transparent' , function ( done ) {
115+ require ( 'amqplib/callback_api' )
116+ . connect ( process . env . AMQP_URL , function ( err , conn ) {
117+ if ( err != null ) done ( err )
118+ conn . createChannel ( onOpen )
119+ function onOpen ( err , ch ) {
120+ if ( err != null ) done ( err )
121+ ch . consume ( 'test-cb' , function ( msg ) {
122+ try {
123+ ch . ack ( msg )
124+ expect ( msg . content . toString ( ) ) . to . eql ( 'callback' )
125+ ch . close ( )
126+ done ( )
127+ } catch ( err ) {
128+ ch . close ( )
129+ done ( err )
130+ }
131+ } )
132+ ch . sendToQueue ( 'test-cb' , new Buffer ( 'callback' ) )
133+ }
134+ } )
135+ } )
90136
137+ it ( 'is instrumented for incoming edges' , function ( done ) {
91138 require ( 'amqplib/callback_api' )
92- . connect ( 'amqp://localhost' , function ( err , conn ) {
139+ . connect ( process . env . AMQP_URL , function ( err , conn ) {
93140 if ( err != null ) done ( err )
94141 conn . createChannel ( onOpen )
95142 function onOpen ( err , ch ) {
96143 if ( err != null ) done ( err )
97- ch . assertQueue ( 'test-cb' )
98144 ch . consume ( 'test-cb' , function ( msg ) {
99145 try {
100146 ch . ack ( msg )
@@ -103,13 +149,14 @@ describe('amqplib module wrapper', function () {
103149 serviceKey : 62 ,
104150 transportDelay : 0
105151 } )
106- expect ( msg . content . toString ( ) ) . to . eql ( 'callback' )
152+ ch . close ( )
107153 done ( )
108154 } catch ( err ) {
155+ ch . close ( )
109156 done ( err )
110157 }
111158 } )
112- ch . sendToQueue ( 'test-cb' , new Buffer ( 'callback ' ) )
159+ ch . sendToQueue ( 'test-cb' , new Buffer ( '' ) )
113160 }
114161 } )
115162 } )
0 commit comments