|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var expect = require('chai').expect |
| 4 | +var wrapper = require('./redis') |
| 5 | +var Shimmer = require('../utils/shimmer') |
| 6 | +var consts = require('../consts') |
| 7 | + |
| 8 | +describe('The redis wrapper module', function () { |
| 9 | + it('should wrap redis.RedisClient.prototype.send_command', function () { |
| 10 | + var shimmerWrapStub = this.sandbox.stub(Shimmer, 'wrap') |
| 11 | + |
| 12 | + var fakeRedis = { |
| 13 | + RedisClient: function () { } |
| 14 | + } |
| 15 | + |
| 16 | + // wrapped as a side effect |
| 17 | + wrapper(fakeRedis, null) |
| 18 | + |
| 19 | + expect(shimmerWrapStub).to.have.been.calledWith( |
| 20 | + fakeRedis.RedisClient.prototype, |
| 21 | + 'redis.RedisClient.prototype', |
| 22 | + 'send_command' |
| 23 | + ) |
| 24 | + }) |
| 25 | + |
| 26 | + it('should call clientSend on the Agent when command is sent', function () { |
| 27 | + var shimmerWrapStub = this.sandbox.stub(Shimmer, 'wrap') |
| 28 | + |
| 29 | + var fakeRedis = { |
| 30 | + RedisClient: function () { |
| 31 | + this.address = 'fakeRedisAddress' |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + var fakeAgent = { |
| 36 | + generateSpanId: function () { return 'fakeSpanId' }, |
| 37 | + getMicrotime: function () { return 42 }, |
| 38 | + getTransactionId: function () { return 'fakeTransactionId' }, |
| 39 | + clientSend: this.sandbox.spy(), |
| 40 | + clientReceive: this.sandbox.spy(), |
| 41 | + CLIENT_SEND: 'fakeSend' |
| 42 | + } |
| 43 | + |
| 44 | + // wrapped as a side effect |
| 45 | + wrapper(fakeRedis, fakeAgent) |
| 46 | + var wrapOp = shimmerWrapStub.args[0][3] |
| 47 | + |
| 48 | + var fakeRedisClientSend = this.sandbox.spy(function (command, args, callback) { |
| 49 | + callback() // simulate the response |
| 50 | + }) |
| 51 | + |
| 52 | + wrapOp(fakeRedisClientSend).apply(new fakeRedis.RedisClient(), ['hset', ['abc', 'def']]) |
| 53 | + |
| 54 | + expect(fakeAgent.clientSend).to.have.been.calledOnce |
| 55 | + expect(fakeAgent.clientSend).to.have.been.calledWith({ |
| 56 | + id: 'fakeTransactionId', |
| 57 | + spanId: 'fakeSpanId', |
| 58 | + host: 'fakeRedisAddress', |
| 59 | + time: 42, |
| 60 | + type: fakeAgent.CLIENT_SEND, |
| 61 | + url: 'unknown', |
| 62 | + method: 'hset' |
| 63 | + }) |
| 64 | + |
| 65 | + expect(fakeAgent.clientReceive).to.have.been.calledOnce |
| 66 | + expect(fakeAgent.clientReceive).to.have.been.calledWith({ |
| 67 | + mustCollect: undefined, |
| 68 | + responseTime: 0, |
| 69 | + status: consts.EDGE_STATUS.OK, |
| 70 | + statusCode: 200, |
| 71 | + id: 'fakeTransactionId', |
| 72 | + spanId: 'fakeSpanId', |
| 73 | + host: 'fakeRedisAddress', |
| 74 | + time: 42, |
| 75 | + url: 'unknown', |
| 76 | + method: 'hset', |
| 77 | + protocol: consts.PROTOCOLS.REDIS |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should instrument errors too', function () { |
| 82 | + var shimmerWrapStub = this.sandbox.stub(Shimmer, 'wrap') |
| 83 | + |
| 84 | + var fakeRedis = { |
| 85 | + RedisClient: function () { |
| 86 | + this.address = 'fakeRedisAddress' |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + var fakeAgent = { |
| 91 | + generateSpanId: function () { return 'fakeSpanId' }, |
| 92 | + getMicrotime: function () { return 42 }, |
| 93 | + getTransactionId: function () { return 'fakeTransactionId' }, |
| 94 | + clientSend: this.sandbox.spy(), |
| 95 | + clientReceive: this.sandbox.spy(), |
| 96 | + CLIENT_SEND: 'fakeSend' |
| 97 | + } |
| 98 | + |
| 99 | + // wrapped as a side effect |
| 100 | + wrapper(fakeRedis, fakeAgent) |
| 101 | + var wrapOp = shimmerWrapStub.args[0][3] |
| 102 | + |
| 103 | + var fakeRedisClientSend = this.sandbox.spy(function (command, args, callback) { |
| 104 | + callback(new Error('error')) // simulate the response |
| 105 | + }) |
| 106 | + |
| 107 | + wrapOp(fakeRedisClientSend).apply(new fakeRedis.RedisClient(), ['hset', ['abc', 'def']]) |
| 108 | + |
| 109 | + expect(fakeAgent.clientSend).to.have.been.calledOnce |
| 110 | + expect(fakeAgent.clientSend).to.have.been.calledWith({ |
| 111 | + id: 'fakeTransactionId', |
| 112 | + spanId: 'fakeSpanId', |
| 113 | + host: 'fakeRedisAddress', |
| 114 | + time: 42, |
| 115 | + type: fakeAgent.CLIENT_SEND, |
| 116 | + url: 'unknown', |
| 117 | + method: 'hset' |
| 118 | + }) |
| 119 | + |
| 120 | + expect(fakeAgent.clientReceive).to.have.been.calledOnce |
| 121 | + expect(fakeAgent.clientReceive).to.have.been.calledWith({ |
| 122 | + mustCollect: consts.MUST_COLLECT.ERROR, |
| 123 | + responseTime: 0, |
| 124 | + status: consts.EDGE_STATUS.NOT_OK, |
| 125 | + statusCode: undefined, |
| 126 | + id: 'fakeTransactionId', |
| 127 | + spanId: 'fakeSpanId', |
| 128 | + host: 'fakeRedisAddress', |
| 129 | + time: 42, |
| 130 | + url: 'unknown', |
| 131 | + method: 'hset', |
| 132 | + protocol: consts.PROTOCOLS.REDIS |
| 133 | + }) |
| 134 | + }) |
| 135 | +}) |
0 commit comments