@@ -26,136 +26,144 @@ var common = require('./common'/*.js*/);
2626
2727var assert = require ( 'assert' ) ;
2828
29- describe ( 'test-trace- mongoose' , function ( ) {
29+ describe ( 'mongoose integration tests ' , function ( ) {
3030 var agent ;
31- var mongoose ;
3231 var Simple ;
3332 before ( function ( ) {
3433 agent = require ( '../../..' ) . start ( {
3534 projectId : '0' ,
3635 samplingRate : 0
3736 } ) ;
38-
39- mongoose = require ( './fixtures/mongoose4' ) ;
40- mongoose . Promise = global . Promise ;
41-
42- var Schema = mongoose . Schema ;
43- var simpleSchema = new Schema ( {
44- f1 : String ,
45- f2 : Boolean ,
46- f3 : Number
47- } ) ;
48-
49- Simple = mongoose . model ( 'Simple' , simpleSchema ) ;
5037 } ) ;
38+
39+ const versions = [ 4 , 5 ] ;
40+ for ( const version of versions ) {
41+ describe ( `mongoose@${ version } ` , ( ) => {
42+ let mongoose ;
43+
44+ before ( ( ) => {
45+ mongoose = require ( `./fixtures/mongoose${ version } ` ) ;
46+ mongoose . Promise = global . Promise ;
47+
48+ var Schema = mongoose . Schema ;
49+ var simpleSchema = new Schema ( {
50+ f1 : String ,
51+ f2 : Boolean ,
52+ f3 : Number
53+ } ) ;
5154
52- beforeEach ( function ( done ) {
53- var sim = new Simple ( {
54- f1 : 'sim' ,
55- f2 : true ,
56- f3 : 42
57- } ) ;
58- mongoose . connect ( 'mongodb://localhost:27017/testdb' , function ( err ) {
59- assert ( ! err , 'Skipping: error connecting to mongo at localhost:27017.' ) ;
60- sim . save ( function ( err ) {
61- assert ( ! err ) ;
62- common . cleanTraces ( ) ;
63- done ( ) ;
55+ Simple = mongoose . model ( 'Simple' , simpleSchema ) ;
6456 } ) ;
65- } ) ;
66- } ) ;
6757
68- afterEach ( function ( done ) {
69- mongoose . connection . db . dropDatabase ( function ( err ) {
70- assert ( ! err ) ;
71- mongoose . connection . close ( function ( err ) {
72- assert ( ! err ) ;
73- common . cleanTraces ( ) ;
74- done ( ) ;
58+ beforeEach ( function ( done ) {
59+ var sim = new Simple ( {
60+ f1 : 'sim' ,
61+ f2 : true ,
62+ f3 : 42
63+ } ) ;
64+ mongoose . connect ( 'mongodb://localhost:27017/testdb' , function ( err ) {
65+ assert ( ! err , 'Skipping: error connecting to mongo at localhost:27017.' ) ;
66+ sim . save ( function ( err ) {
67+ assert ( ! err ) ;
68+ common . cleanTraces ( ) ;
69+ done ( ) ;
70+ } ) ;
71+ } ) ;
7572 } ) ;
76- } ) ;
77- } ) ;
7873
79- it ( 'should accurately measure create time' , function ( done ) {
80- var data = new Simple ( {
81- f1 : 'val' ,
82- f2 : false ,
83- f3 : 1729
84- } ) ;
85- common . runInTransaction ( function ( endTransaction ) {
86- data . save ( function ( err ) {
87- endTransaction ( ) ;
88- assert ( ! err ) ;
89- var trace = common . getMatchingSpan ( mongoPredicate . bind ( null , 'mongo-insert' ) ) ;
90- assert ( trace ) ;
91- done ( ) ;
74+ afterEach ( function ( done ) {
75+ mongoose . connection . db . dropDatabase ( function ( err ) {
76+ assert ( ! err ) ;
77+ mongoose . connection . close ( function ( err ) {
78+ assert ( ! err ) ;
79+ common . cleanTraces ( ) ;
80+ done ( ) ;
81+ } ) ;
82+ } ) ;
9283 } ) ;
93- } ) ;
94- } ) ;
9584
96- it ( 'should accurately measure update time' , function ( done ) {
97- common . runInTransaction ( function ( endTransaction ) {
98- Simple . findOne ( { f1 : 'sim' } , function ( err , res ) {
99- assert ( ! err ) ;
100- res . f2 = false ;
101- res . save ( function ( err ) {
102- endTransaction ( ) ;
103- assert ( ! err ) ;
104- var trace = common . getMatchingSpan ( mongoPredicate . bind ( null , 'mongo-update' ) ) ;
105- assert ( trace ) ;
106- done ( ) ;
85+ it ( 'should accurately measure create time' , function ( done ) {
86+ var data = new Simple ( {
87+ f1 : 'val' ,
88+ f2 : false ,
89+ f3 : 1729
90+ } ) ;
91+ common . runInTransaction ( function ( endTransaction ) {
92+ data . save ( function ( err ) {
93+ endTransaction ( ) ;
94+ assert ( ! err ) ;
95+ var trace = common . getMatchingSpan ( mongoPredicate . bind ( null , 'mongo-insert' ) ) ;
96+ assert ( trace ) ;
97+ done ( ) ;
98+ } ) ;
10799 } ) ;
108100 } ) ;
109- } ) ;
110- } ) ;
111101
112- it ( 'should accurately measure retrieval time' , function ( done ) {
113- common . runInTransaction ( function ( endTransaction ) {
114- Simple . findOne ( { f1 : 'sim' } , function ( err , res ) {
115- endTransaction ( ) ;
116- assert ( ! err ) ;
117- var trace = common . getMatchingSpan ( mongoPredicate . bind ( null , 'mongo-cursor' ) ) ;
118- assert ( trace ) ;
119- done ( ) ;
102+ it ( 'should accurately measure update time' , function ( done ) {
103+ common . runInTransaction ( function ( endTransaction ) {
104+ Simple . findOne ( { f1 : 'sim' } , function ( err , res ) {
105+ assert ( ! err ) ;
106+ res . f2 = false ;
107+ res . save ( function ( err ) {
108+ endTransaction ( ) ;
109+ assert ( ! err ) ;
110+ var trace = common . getMatchingSpan ( mongoPredicate . bind ( null , 'mongo-update' ) ) ;
111+ assert ( trace ) ;
112+ done ( ) ;
113+ } ) ;
114+ } ) ;
115+ } ) ;
120116 } ) ;
121- } ) ;
122- } ) ;
123117
124- it ( 'should accurately measure delete time' , function ( done ) {
125- common . runInTransaction ( function ( endTransaction ) {
126- Simple . remove ( { f1 : 'sim' } , function ( err , res ) {
127- endTransaction ( ) ;
128- assert ( ! err ) ;
129- var trace = common . getMatchingSpan ( mongoPredicate . bind ( null , 'mongo-remove' ) ) ;
130- assert ( trace ) ;
131- done ( ) ;
118+ it ( 'should accurately measure retrieval time' , function ( done ) {
119+ common . runInTransaction ( function ( endTransaction ) {
120+ Simple . findOne ( { f1 : 'sim' } , function ( err , res ) {
121+ endTransaction ( ) ;
122+ assert ( ! err ) ;
123+ var trace = common . getMatchingSpan ( mongoPredicate . bind ( null , 'mongo-cursor' ) ) ;
124+ assert ( trace ) ;
125+ done ( ) ;
126+ } ) ;
127+ } ) ;
132128 } ) ;
133- } ) ;
134- } ) ;
135129
136- it ( 'should not break if no parent transaction' , function ( done ) {
137- Simple . findOne ( { f1 : 'sim' } , function ( err , res ) {
138- assert ( ! err ) ;
139- assert ( res ) ;
140- done ( ) ;
141- } ) ;
142- } ) ;
130+ it ( 'should accurately measure delete time' , function ( done ) {
131+ common . runInTransaction ( function ( endTransaction ) {
132+ Simple . remove ( { f1 : 'sim' } , function ( err , res ) {
133+ endTransaction ( ) ;
134+ assert ( ! err ) ;
135+ var trace = common . getMatchingSpan ( mongoPredicate . bind ( null , 'mongo-remove' ) ) ;
136+ assert ( trace ) ;
137+ done ( ) ;
138+ } ) ;
139+ } ) ;
140+ } ) ;
143141
144- it ( 'should remove trace frames from stack' , function ( done ) {
145- common . runInTransaction ( function ( endTransaction ) {
146- Simple . findOne ( { f1 : 'sim' } , function ( err , res ) {
147- endTransaction ( ) ;
148- assert ( ! err ) ;
149- var trace = common . getMatchingSpan ( mongoPredicate . bind ( null , 'mongo-cursor' ) ) ;
150- var labels = trace . labels ;
151- var stackTrace = JSON . parse ( labels [ TraceLabels . STACK_TRACE_DETAILS_KEY ] ) ;
152- // Ensure that our patch is on top of the stack
153- assert (
154- stackTrace . stack_frame [ 0 ] . method_name . indexOf ( 'next_trace' ) !== - 1 ) ;
155- done ( ) ;
142+ it ( 'should not break if no parent transaction' , function ( done ) {
143+ Simple . findOne ( { f1 : 'sim' } , function ( err , res ) {
144+ assert ( ! err ) ;
145+ assert ( res ) ;
146+ done ( ) ;
147+ } ) ;
148+ } ) ;
149+
150+ it ( 'should remove trace frames from stack' , function ( done ) {
151+ common . runInTransaction ( function ( endTransaction ) {
152+ Simple . findOne ( { f1 : 'sim' } , function ( err , res ) {
153+ endTransaction ( ) ;
154+ assert ( ! err ) ;
155+ var trace = common . getMatchingSpan ( mongoPredicate . bind ( null , 'mongo-cursor' ) ) ;
156+ var labels = trace . labels ;
157+ var stackTrace = JSON . parse ( labels [ TraceLabels . STACK_TRACE_DETAILS_KEY ] ) ;
158+ // Ensure that our patch is on top of the stack
159+ assert (
160+ stackTrace . stack_frame [ 0 ] . method_name . indexOf ( 'next_trace' ) !== - 1 ) ;
161+ done ( ) ;
162+ } ) ;
163+ } ) ;
156164 } ) ;
157165 } ) ;
158- } ) ;
166+ }
159167} ) ;
160168
161169function mongoPredicate ( id , span ) {
0 commit comments