Skip to content

Commit

Permalink
Added tests for startSegment
Browse files Browse the repository at this point in the history
  • Loading branch information
NatalieWolfe authored and Peter Svetlichny committed Mar 26, 2018
1 parent 5c8dc44 commit 77129ce
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ API.prototype.startSegment = function startSegment(name, record, handler, callba
name: name,
recorder: record ? customRecorder : null,
callback: callback ? shim.FIRST : null,
promise: !!callback
promise: !callback
}
})

Expand Down
85 changes: 85 additions & 0 deletions test/unit/api/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,91 @@ describe('the New Relic agent API', function() {
})
})

describe('when creating a segment with `startSegment`', function() {
it('should name the segment as provided', function() {
helper.runInTransaction(agent, function() {
api.startSegment('foobar', false, function() {
var segment = api.shim.getSegment()
expect(segment).to.exist().and.have.property('name', 'foobar')
})
})
})

it('should return the return value of the handler', function() {
helper.runInTransaction(agent, function() {
var obj = {}
var ret = api.startSegment('foobar', false, function() {
return obj
})
expect(ret).to.equal(obj)
})
})

it('should not record a metric when `record` is `false`', function(done) {
helper.runInTransaction(agent, function(tx) {
tx.name = 'test'
api.startSegment('foobar', false, function() {
var segment = api.shim.getSegment()
expect(segment).to.exist().and.have.property('name', 'foobar')
})
tx.end(function() {
expect(tx.metrics.scoped).to.not.have.property(tx.name)
expect(tx.metrics.unscoped).to.not.have.property('Custom/foobar')
done()
})
})
})

it('should record a metric when `record` is `true`', function(done) {
helper.runInTransaction(agent, function(tx) {
tx.name = 'test'
api.startSegment('foobar', true, function() {
var segment = api.shim.getSegment()
expect(segment).to.exist().and.have.property('name', 'foobar')
})
tx.end(function() {
expect(tx.metrics.scoped).property(tx.name)
.to.have.property('Custom/foobar')
expect(tx.metrics.unscoped).to.have.property('Custom/foobar')
done()
})
})
})

it('should time the segment from the callback if provided', function(done) {
helper.runInTransaction(agent, function() {
api.startSegment('foobar', false, function(cb) {
var segment = api.shim.getSegment()
setTimeout(cb, 150, null, segment)
}, function(err, segment) {
expect(err).to.be.null()
expect(segment).to.exist()
expect(segment.getDurationInMillis()).to.be.within(100, 200)
done()
})
})
})

it('should time the segment from a returned promise', function() {
// TODO: Once Node <0.12 is deprecated, remove this check for Promise.
if (!global.Promise) {
return
}

return helper.runInTransaction(agent, function() {
return api.startSegment('foobar', false, function() {
var segment = api.shim.getSegment()
return new Promise(function(resolve) {
setTimeout(resolve, 150, segment)
})
}).then(function(segment) {
expect(segment).to.exist()
expect(segment.getDurationInMillis()).to.be.within(100, 200)
})
})
})
})

describe("when starting a web transaction using startWebTransaction", function() {
var thenCalled = false
var FakePromise = {
Expand Down
19 changes: 17 additions & 2 deletions test/unit/api/stub.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ describe("the stubbed New Relic agent API", function() {
expect(api.setDispatcher).a('function')
})


it("shouldn't throw when transaction is named", function() {
expect(function() { api.setTransactionName('TEST/*') }).not.throws()
})
Expand Down Expand Up @@ -115,7 +114,23 @@ describe("the stubbed New Relic agent API", function() {
it("should return a function when calling createTracer", function() {
function myNop() {}
var retVal = api.createTracer('name', myNop)
expect(retVal).to.be.equal(myNop)
expect(retVal).to.equal(myNop)
})

it('should call the function passed into `startSegment`', function(done) {
api.startSegment('foo', false, done)
})

it('should not throw when a non-function is passed to `startSegment`', function() {
expect(function() {
api.startSegment('foo', false, null)
}).to.not.throw()
})

it('should return the return value of the handler', function() {
var obj = {}
var ret = api.startSegment('foo', false, function() { return obj })
expect(obj).to.equal(ret)
})

it("shouldn't throw when a custom web transaction is started", function() {
Expand Down

0 comments on commit 77129ce

Please sign in to comment.