|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const crypto = require('crypto') |
| 4 | +const test = require('tape') |
| 5 | +const utils = require('./lib/utils') |
| 6 | + |
| 7 | +const APMServer = utils.APMServer |
| 8 | +const processReq = utils.processReq |
| 9 | +const assertReq = utils.assertReq |
| 10 | +const assertMetadata = utils.assertMetadata |
| 11 | +const assertEvent = utils.assertEvent |
| 12 | + |
| 13 | +const testCases = [ |
| 14 | + { |
| 15 | + name: 'only one error', |
| 16 | + expectedData: [ |
| 17 | + assertMetadata, |
| 18 | + assertEvent({span: {req: 2}}), |
| 19 | + assertMetadata, |
| 20 | + assertEvent({span: {req: 3}}), |
| 21 | + assertMetadata, |
| 22 | + assertEvent({span: {req: 4}}) |
| 23 | + ], |
| 24 | + requests: [ |
| 25 | + [0, true], // no back-off in effect, request fails |
| 26 | + [0, false], // back-off in effect, request succeeds |
| 27 | + [0, false], // no back-off in effect, request succeeds |
| 28 | + [0, false] // no back-off in effect, request succeeds |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + name: 'only two errors', |
| 33 | + expectedData: [ |
| 34 | + assertMetadata, |
| 35 | + assertEvent({span: {req: 3}}), |
| 36 | + assertMetadata, |
| 37 | + assertEvent({span: {req: 4}}), |
| 38 | + assertMetadata, |
| 39 | + assertEvent({span: {req: 5}}) |
| 40 | + ], |
| 41 | + requests: [ |
| 42 | + [0, true], // no back-off in effect, request fails |
| 43 | + [0, true], // back-off in effect, request fails |
| 44 | + [1, false], // back-off in effect, request succeeds |
| 45 | + [0, false], // no back-off in effect, request succeeds |
| 46 | + [0, false] // no back-off in effect, request succeeds |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + name: 'top out at full back-off', |
| 51 | + expectedData: [ |
| 52 | + assertMetadata, |
| 53 | + assertEvent({span: {req: 9}}), |
| 54 | + assertMetadata, |
| 55 | + assertEvent({span: {req: 10}}), |
| 56 | + assertMetadata, |
| 57 | + assertEvent({span: {req: 11}}) |
| 58 | + ], |
| 59 | + requests: [ |
| 60 | + [0, true], // no back-off in effect, request fails |
| 61 | + [0, true], // back-off in effect, request fails |
| 62 | + [1, true], // back-off in effect, request fails |
| 63 | + [4, true], // back-off in effect, request fails |
| 64 | + [9, true], // back-off in effect, request fails |
| 65 | + [16, true], // back-off in effect, request fails |
| 66 | + [25, true], // back-off in effect, request fails |
| 67 | + [36, true], // back-off in effect, request fails |
| 68 | + [36, false], // back-off in effect, request succeeds |
| 69 | + [0, false], // no back-off in effect, request succeeds |
| 70 | + [0, false] // no back-off in effect, request succeeds |
| 71 | + ] |
| 72 | + } |
| 73 | +] |
| 74 | + |
| 75 | +testCases.forEach(function ({name, expectedData, requests}) { |
| 76 | + test('backoff delays - ' + name, function (t) { |
| 77 | + let reqNo = 0 |
| 78 | + let start, client |
| 79 | + |
| 80 | + const server = APMServer(function (req, res) { |
| 81 | + const diff = Date.now() - start |
| 82 | + const backoffTime = requests[reqNo - 1][0] * 1000 |
| 83 | + t.ok(diff > backoffTime && diff < backoffTime + 200, `should delay request between ${backoffTime} and ${backoffTime + 200}ms (was delayed ${diff}ms)`) |
| 84 | + |
| 85 | + if (requests[reqNo - 1][1] === true) { |
| 86 | + res.writeHead(500) |
| 87 | + res.end() |
| 88 | + } else { |
| 89 | + assertReq(t, req) |
| 90 | + req = processReq(req) |
| 91 | + req.on('data', function (obj) { |
| 92 | + expectedData.shift()(t, obj) |
| 93 | + }) |
| 94 | + req.on('end', function () { |
| 95 | + res.end() |
| 96 | + if (reqNo < 4) { |
| 97 | + setTimeout(makeReq, 10) |
| 98 | + } else { |
| 99 | + t.equal(expectedData.length, 0, 'should have seen all expected data') |
| 100 | + server.close() |
| 101 | + t.end() |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
| 105 | + }).client({time: 1000}, function (_client) { |
| 106 | + client = _client |
| 107 | + let emittedErrors = 0 |
| 108 | + |
| 109 | + client.on('error', function (err) { |
| 110 | + emittedErrors++ |
| 111 | + if (requests[reqNo - 1][1] === true) { |
| 112 | + t.equal(err.message, 'Unexpected response code from APM Server: 500', 'client should emit error') |
| 113 | + t.equal(client._errors, emittedErrors, 'client error count should have been incremented to ' + emittedErrors) |
| 114 | + makeReq() |
| 115 | + } else { |
| 116 | + t.error(err) |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + makeReq() |
| 121 | + }) |
| 122 | + |
| 123 | + function makeReq () { |
| 124 | + client.sendSpan({req: ++reqNo}) |
| 125 | + start = Date.now() |
| 126 | + client.flush() |
| 127 | + } |
| 128 | + }) |
| 129 | +}) |
| 130 | + |
| 131 | +test('backoff - dropping data', function (t) { |
| 132 | + let start, timer |
| 133 | + let reqNo = 0 |
| 134 | + const backoffTimes = [0, 0, 1, 0] |
| 135 | + |
| 136 | + const server = APMServer(function (req, res) { |
| 137 | + const diff = Date.now() - start |
| 138 | + const backoffTime = backoffTimes.shift() * 1000 |
| 139 | + t.ok(diff > backoffTime && diff < backoffTime + 200, `should delay request between ${backoffTime} and ${backoffTime + 200}ms (was delayed ${diff}ms)`) |
| 140 | + |
| 141 | + req = processReq(req) |
| 142 | + req.on('data', function (obj) { |
| 143 | + if ('metadata' in obj) return |
| 144 | + t.equal(obj.span.req, reqNo, 'event belongs to expected request no ' + reqNo) |
| 145 | + t.equal(obj.span.ok, true, 'expected the event to get sent') |
| 146 | + }) |
| 147 | + req.on('end', function () { |
| 148 | + if (reqNo <= 2) { |
| 149 | + res.writeHead(500) |
| 150 | + res.end() |
| 151 | + } else { |
| 152 | + clearTimeout(timer) |
| 153 | + res.end() |
| 154 | + server.close() |
| 155 | + t.end() |
| 156 | + } |
| 157 | + }) |
| 158 | + }).client({size: 256, time: 500}, function (client) { |
| 159 | + client.on('error', function (err) { |
| 160 | + if (reqNo === 1) { |
| 161 | + t.equal(err.message, 'Unexpected response code from APM Server: 500', 'client should emit error') |
| 162 | + t.equal(client._errors, 1, 'client error count should have been incremented to 1') |
| 163 | + |
| 164 | + client.sendSpan({req: ++reqNo, ok: true, filler: crypto.randomBytes(32).toString('hex')}) |
| 165 | + start = Date.now() |
| 166 | + client.flush() |
| 167 | + } else if (reqNo === 2) { |
| 168 | + t.equal(err.message, 'Unexpected response code from APM Server: 500', 'client should emit error') |
| 169 | + t.equal(client._errors, 2, 'client error count should have been incremented to 2') |
| 170 | + |
| 171 | + reqNo++ |
| 172 | + start = Date.now() |
| 173 | + |
| 174 | + // these will be dropped because they are too big to be cached before the backoff |
| 175 | + client.sendSpan({req: reqNo, ok: false, filler: crypto.randomBytes(32).toString('hex')}) // will not overflow |
| 176 | + client.sendSpan({req: reqNo, ok: false, filler: crypto.randomBytes(32).toString('hex')}) // will trigger overflow |
| 177 | + |
| 178 | + // this will be the first to get through after the backoff |
| 179 | + client.sendSpan({req: reqNo, ok: true, filler: crypto.randomBytes(32).toString('hex')}) |
| 180 | + |
| 181 | + timer = setTimeout(function () { |
| 182 | + t.fail('took too long') |
| 183 | + }, 2000) |
| 184 | + } else { |
| 185 | + t.error(err) |
| 186 | + } |
| 187 | + }) |
| 188 | + |
| 189 | + client.sendSpan({req: ++reqNo, ok: true, filler: crypto.randomBytes(32).toString('hex')}) |
| 190 | + start = Date.now() |
| 191 | + client.flush() |
| 192 | + }) |
| 193 | +}) |
0 commit comments