|
1 | 1 | 'use strict' |
2 | 2 |
|
| 3 | +var path = require('path') |
| 4 | + , chai = require('chai') |
| 5 | + , expect = chai.expect |
| 6 | + , helper = require('../../lib/agent_helper') |
| 7 | + |
3 | 8 | describe("agent instrumentation of PostgreSQL", function () { |
| 9 | + var agent |
| 10 | + , initialize |
| 11 | + |
| 12 | + before(function () { |
| 13 | + agent = helper.loadMockedAgent() |
| 14 | + initialize = require('../../../lib/instrumentation/pg') |
| 15 | + }) |
| 16 | + |
| 17 | + after(function () { |
| 18 | + helper.unloadAgent(agent) |
| 19 | + }) |
| 20 | + |
| 21 | + describe("shouldn't cause bootstrapping to fail", function () { |
| 22 | + it("when passed no module", function () { |
| 23 | + expect(function () { initialize(agent); }).not.throws() |
| 24 | + }) |
| 25 | + |
| 26 | + it("when passed an empty module", function () { |
| 27 | + expect(function () { initialize(agent, {}); }).not.throws() |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + describe("lazy loading of native PG client", function() { |
| 32 | + |
| 33 | + function getMockModule() { |
| 34 | + function PG(clientConstructor) { |
| 35 | + this.Client = clientConstructor |
| 36 | + } |
| 37 | + |
| 38 | + function DefaultClient() {} |
| 39 | + DefaultClient.prototype.query = function() {} |
| 40 | + function NativeClient() {} |
| 41 | + NativeClient.prototype.query = function() {} |
| 42 | + |
| 43 | + var mockPg = new PG(DefaultClient) |
| 44 | + mockPg.__defineGetter__("native", function() { |
| 45 | + delete mockPg.native; |
| 46 | + mockPg.native = new PG(NativeClient); |
| 47 | + return mockPg.native; |
| 48 | + }) |
| 49 | + return mockPg |
| 50 | + } |
| 51 | + |
| 52 | + it("instruments when native getter is called", function() { |
| 53 | + var mockPg = getMockModule() |
| 54 | + |
| 55 | + initialize(agent, mockPg) |
| 56 | + |
| 57 | + var pg = mockPg.native |
| 58 | + expect(pg.Client['__NR_original'].name).equal('NativeClient') |
| 59 | + |
| 60 | + var pg = mockPg |
| 61 | + expect(pg.Client.name).equal('DefaultClient') |
| 62 | + }) |
| 63 | + |
| 64 | + it("does not fail when getter is called multiple times", function() { |
| 65 | + var mockPg = getMockModule() |
| 66 | + |
| 67 | + initialize(agent, mockPg) |
| 68 | + var pg1 = mockPg.native |
| 69 | + |
| 70 | + initialize(agent, mockPg) |
| 71 | + var pg2 = mockPg.native |
| 72 | + |
| 73 | + expect(pg1).equal(pg2) |
| 74 | + }) |
| 75 | + |
| 76 | + it("does not interfere with non-native instrumentation", function() { |
| 77 | + var mockPg = getMockModule() |
| 78 | + |
| 79 | + initialize(agent, mockPg) |
| 80 | + var nativeClient = mockPg.native |
| 81 | + expect(nativeClient.Client['__NR_original'].name).equal('NativeClient') |
| 82 | + var defaultClient = mockPg |
| 83 | + expect(defaultClient.Client.name).equal('DefaultClient') |
| 84 | + |
| 85 | + initialize(agent, mockPg) |
| 86 | + var nativeClient = mockPg.native |
| 87 | + expect(nativeClient.Client['__NR_original'].name).equal('NativeClient') |
| 88 | + var defaultClient = mockPg |
| 89 | + expect(defaultClient.Client.name).equal('DefaultClient') |
| 90 | + }) |
| 91 | + |
| 92 | + it("when pg modules is refreshed in cache", function() { |
| 93 | + var mockPg = getMockModule() |
| 94 | + |
| 95 | + // instrument once |
| 96 | + initialize(agent, mockPg) |
| 97 | + var pg1 = mockPg.native |
| 98 | + expect(pg1.Client['__NR_original'].name).equal('NativeClient') |
| 99 | + |
| 100 | + // simulate deleting from module cache |
| 101 | + mockPg = getMockModule() |
| 102 | + initialize(agent, mockPg) |
| 103 | + var pg2 = mockPg.native |
| 104 | + expect(pg2.Client['__NR_original'].name).equal('NativeClient') |
| 105 | + |
| 106 | + expect(pg1).not.equal(pg2) |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
4 | 110 | describe("for each operation", function () { |
5 | 111 | it("should update the global database aggregate statistics") |
6 | 112 | it("should also update the global web aggregate statistics") |
|
0 commit comments