Skip to content

Commit 0e010e2

Browse files
committed
fix(tests): fix tests
1 parent 325d102 commit 0e010e2

2 files changed

Lines changed: 123 additions & 101 deletions

File tree

lib/instrumentations/utils/wrapQuery.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var microtime = require('../../optionalDependencies/microtime')
2-
var consts = require('../../consts')
32
var EventEmitter = require('events')
43
var debug = require('debug')('risingstack/trace:agent:instrumentations')
54
var format = require('util').format
@@ -24,14 +23,14 @@ function wrapQuery (original, args, agent, params) {
2423
resource: _params.url,
2524
host: _params.host,
2625
data: _params.protocolSpecific,
27-
severity: severity
26+
severity: _params.severity
2827
}, {
2928
communication: communication
3029
})
3130

32-
debug('#wrapQuery', format('CS(%s) [%s %s %s %s]',
33-
cs.briefcase.csCtx.communicationId, _params.protocol,
34-
action, resource, _params.host))
31+
debug('#wrapQuery', format('CS(%s) [%s %s %s %s]',
32+
cs.briefcase.csCtx.communicationId, _params.protocol,
33+
action, resource, _params.host))
3534

3635
return cs
3736
},

lib/instrumentations/utils/wrapQuery.spec.js

Lines changed: 119 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -46,106 +46,122 @@ describe('wrapQuery', function () {
4646
this.sandbox.stub(microtime, 'now').returns(5)
4747
})
4848

49-
it('should call tracer.collector.clientSend on send', function () {
50-
var query = this.sandbox.spy()
51-
wrapQuery(query, [], agent, {
52-
protocol: 'protocol',
53-
url: 'fakeUrl',
54-
host: 'fakeHost',
55-
method: 'fakeMethod',
56-
continuationMethod: 'callback'
49+
describe('callback api', function () {
50+
it('should call tracer.collector.clientSend on send', function (done) {
51+
var query = this.sandbox.spy(function (cb) {
52+
cb()
53+
})
54+
55+
function cb () {
56+
expect(agent.tracer.collector.clientSend).to.have.been.calledWith({
57+
action: 'fakeMethod',
58+
data: undefined,
59+
host: 'fakeHost',
60+
protocol: 'protocol',
61+
resource: 'fakeUrl',
62+
severity: agent.tracer.collector.defaultSeverity
63+
}, {
64+
communication: {}
65+
})
66+
done()
67+
}
68+
69+
wrapQuery(query, [ cb ], agent, {
70+
protocol: 'protocol',
71+
url: 'fakeUrl',
72+
host: 'fakeHost',
73+
method: 'fakeMethod',
74+
continuationMethod: 'callback'
75+
})
5776
})
58-
expect(agent.tracer.collector.clientSend).to.have.been.calledWith({
59-
action: 'fakeMethod',
60-
data: undefined,
61-
host: 'fakeHost',
62-
protocol: 'protocol',
63-
resource: 'fakeUrl',
64-
severity: agent.tracer.collector.defaultSeverity
65-
}, {
66-
communication: {}
77+
it('should call the original', function () {
78+
var query = this.sandbox.spy()
79+
wrapQuery(query, [], agent)
80+
expect(query).to.have.been.calledWith()
6781
})
68-
})
69-
it('should call the original', function () {
70-
var query = this.sandbox.spy()
71-
wrapQuery(query, [], agent)
72-
expect(query).to.have.been.calledWith()
73-
})
7482

75-
it('should pass the callback', function () {
76-
var query = function (cb) {
77-
expect(cb).to.be.a('function')
78-
}
79-
var cb = this.sandbox.spy()
83+
it('should pass the callback', function () {
84+
var query = function (cb) {
85+
expect(cb).to.be.a('function')
86+
}
87+
var cb = this.sandbox.spy()
8088

81-
wrapQuery(query, [cb], agent, {
82-
continuationMethod: 'callback'
89+
wrapQuery(query, [ cb ], agent, {
90+
continuationMethod: 'callback'
91+
})
8392
})
84-
})
8593

86-
it('should pass the callback if it\'s in an array', function () {
87-
var query = function (_, cb) {
88-
expect(cb[0]).to.be.a('function')
89-
}
90-
var cb = this.sandbox.spy()
91-
wrapQuery(query, ['otherArgument', [cb]], agent)
92-
})
94+
it('should pass the callback if it\'s in an array', function () {
95+
var query = function (_, cb) {
96+
expect(cb[ 0 ]).to.be.a('function')
97+
}
98+
var cb = this.sandbox.spy()
99+
wrapQuery(query, [ 'otherArgument', [ cb ] ], agent)
100+
})
93101

94-
it('should shove a callback', function () {
95-
var query = function (cb) {
96-
expect(cb).to.be.a('function')
97-
}
102+
it('should shove a callback', function () {
103+
var query = function (cb) {
104+
expect(cb).to.be.a('function')
105+
}
98106

99-
wrapQuery(query, [], agent, {
100-
continuationMethod: 'callback'
107+
wrapQuery(query, [], agent, {
108+
continuationMethod: 'callback'
109+
})
101110
})
102-
})
103111

104-
it('should call tracer.collector.clientRecv on receive', function () {
105-
var query = function (cb) { cb() }
112+
it('should call tracer.collector.clientRecv on receive', function () {
113+
var query = function (cb) {
114+
cb()
115+
}
106116

107-
wrapQuery(query, [], agent, {
108-
url: 'fakeUrl',
109-
host: 'fakeHost',
110-
parameter: 'fakeParam',
111-
method: 'fakeMethod',
112-
protocol: 'fakeProtocol',
113-
continuationMethod: 'callback'
117+
wrapQuery(query, [], agent, {
118+
url: 'fakeUrl',
119+
host: 'fakeHost',
120+
parameter: 'fakeParam',
121+
method: 'fakeMethod',
122+
protocol: 'fakeProtocol',
123+
continuationMethod: 'callback'
124+
})
125+
expect(agent.tracer.collector.clientRecv).to.have.been.calledWith({
126+
protocol: 'fakeProtocol',
127+
status: 'ok'
128+
}, {
129+
severity: agent.tracer.collector.defaultSeverity
130+
}, clientSendResult.briefcase)
114131
})
115-
expect(agent.tracer.collector.clientRecv).to.have.been.calledWith({
116-
protocol: 'fakeProtocol',
117-
status: 'ok'
118-
}, {
119-
severity: agent.tracer.collector.defaultSeverity
120-
}, clientSendResult.briefcase)
121-
})
122132

123-
it('should set mustCollect severity on error', function () {
124-
var query = function (cb) { cb(new Error('damn')) }
133+
it('should set mustCollect severity on error', function () {
134+
var query = function (cb) {
135+
cb(new Error('damn'))
136+
}
125137

126-
wrapQuery(query, [], agent, {
127-
continuationMethod: 'callback'
138+
wrapQuery(query, [], agent, {
139+
continuationMethod: 'callback'
140+
})
141+
expect(agent.tracer.collector.clientRecv.args[ 0 ][ 1 ].severity)
142+
.to.eql(agent.tracer.collector.mustCollectSeverity)
128143
})
129-
expect(agent.tracer.collector.clientRecv.args[0][1].severity)
130-
.to.eql(agent.tracer.collector.mustCollectSeverity)
131-
})
132144

133-
it('should report external edge', function () {
134-
var query = function (cb) { cb(new Error(null)) }
135-
wrapQuery(query, [], agent, {
136-
protocol: 'mongodb',
137-
host: 'target',
138-
continuationMethod: 'callback'
139-
})
140-
expect(agent.externalEdgeMetrics.report).to.be.calledWith({
141-
protocol: 'mongodb',
142-
responseTime: 0,
143-
status: 1,
144-
targetHost: 'target'
145+
it('should report external edge', function () {
146+
147+
var query = function (cb) {
148+
cb(new Error(null))
149+
}
150+
wrapQuery(query, [], agent, {
151+
protocol: 'mongodb',
152+
host: 'target',
153+
continuationMethod: 'callback'
154+
})
155+
expect(agent.externalEdgeMetrics.report).to.be.calledWith({
156+
protocol: 'mongodb',
157+
responseTime: 0,
158+
status: 1,
159+
targetHost: 'target'
160+
})
145161
})
146162
})
147163

148-
describe('promise api', function () {
164+
describe('promise api', function (done) {
149165
it('should notify agent on send', function () {
150166
var query = this.sandbox.spy(function () {
151167
return Promise.resolve()
@@ -154,20 +170,25 @@ describe('wrapQuery', function () {
154170
protocol: 'protocol',
155171
url: 'fakeUrl',
156172
host: 'fakeHost',
157-
parameter: 'fakeParam',
158173
method: 'fakeMethod',
159174
continuationMethod: 'promise'
160175
})
161-
expect(agent.tracer.collector.clientSend).to.have.been.calledWith({
162-
action: 'fakeMethod',
163-
data: undefined,
164-
host: 'fakeHost',
165-
protocol: 'protocol',
166-
resource: 'fakeUrl'
167-
}, {
168-
communication: {},
169-
severity: undefined
170-
})
176+
.then(function () {
177+
expect(agent.tracer.collector.clientSend).to.have.been.calledWith({
178+
action: 'fakeMethod',
179+
data: undefined,
180+
host: 'fakeHost',
181+
protocol: 'protocol',
182+
resource: 'fakeUrl'
183+
}, {
184+
communication: {},
185+
severity: undefined
186+
})
187+
done()
188+
})
189+
.catch(function (err) {
190+
done(err)
191+
})
171192
})
172193

173194
it('should call the original', function () {
@@ -256,7 +277,7 @@ describe('wrapQuery', function () {
256277
})
257278
})
258279

259-
it('should signal an error', function (done) {
280+
it('should set mustCollect severity on error', function (done) {
260281
var query = function () {
261282
return Promise.reject(new Error())
262283
}
@@ -267,7 +288,8 @@ describe('wrapQuery', function () {
267288
done('It should have failed')
268289
})
269290
.catch(function () {
270-
expect(agent.tracer.collector.clientRecv.args[0][1].severity).to.eql(agent.tracer.collector.mustCollectSeverity)
291+
expect(agent.tracer.collector.clientRecv.args[0][1].severity)
292+
.to.eql(agent.tracer.collector.mustCollectSeverity)
271293
done()
272294
})
273295
.catch(function (err) {
@@ -421,7 +443,7 @@ describe('wrapQuery', function () {
421443
readStream.push(null)
422444
})
423445

424-
it('should signal an error', function () {
446+
it('should set mustCollect severity on error', function () {
425447
var query = function () {
426448
return new Readable()
427449
}
@@ -432,7 +454,8 @@ describe('wrapQuery', function () {
432454
readStream.resume()
433455

434456
readStream.on('error', function () {
435-
expect(agent.tracer.collector.clientRecv.args[0][1].severity).to.eql(agent.tracer.collector.mustCollectSeverity)
457+
expect(agent.tracer.collector.clientRecv.args[0][1].severity)
458+
.to.eql(agent.tracer.collector.mustCollectSeverity)
436459
})
437460

438461
readStream.emit('error', new Error('damn'))

0 commit comments

Comments
 (0)