-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathredis.test.js
242 lines (216 loc) · 6.49 KB
/
redis.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
'use strict';
if (process.env.GITHUB_ACTIONS === 'true' && process.platform === 'win32') {
console.log('# SKIP: GH Actions do not support docker services on Windows');
process.exit(0);
} else if (process.env.TEST_WITHOUT_SERVICES === 'true') {
console.log('# SKIP: env.TEST_WITHOUT_SERVICES=true');
process.exit(0);
}
const redisVersion = require('redis/package.json').version;
const semver = require('semver');
if (semver.lt(redisVersion, '4.0.0')) {
console.log('# SKIP: skipping redis.test.js tests <4.0.0');
process.exit(0);
}
if (semver.lt(process.version, '14.0.0')) {
console.log('# SKIP: skipping redis.test.js tests node node <14 ');
process.exit(0);
}
const agent = require('../../..').start({
serviceName: 'test-redis',
captureExceptions: false,
metricsInterval: '0s',
centralConfig: false,
spanCompressionEnabled: false,
logLevel: 'off',
});
const redis = require('redis');
const test = require('tape');
const mockClient = require('../../_mock_http_client');
test('redis', function (t) {
resetAgent(function (data) {
t.strictEqual(data.transactions.length, 1, 'have 1 transaction');
const trans = data.transactions[0];
t.ok(trans.name, 'aTrans', 'trans.name');
t.strictEqual(trans.result, 'success', 'trans.result');
// Sort the remaining spans by timestamp, because asynchronous-span.end()
// means they can be set to APM server out of order.
const spans = data.spans.sort((a, b) => {
return a.timestamp < b.timestamp ? -1 : 1;
});
const expectedSpanNames = [
'FLUSHALL',
'SET',
'SET',
'HSET',
'HSET',
'HKEYS',
];
t.equal(
spans.length,
expectedSpanNames.length,
'have the expected number of spans',
);
for (let i = 0; i < expectedSpanNames.length; i++) {
const expectedName = expectedSpanNames[i];
const span = spans[i];
t.strictEqual(span.transaction_id, trans.id, 'span.transaction_id');
t.strictEqual(span.name, expectedName, 'span.name');
t.strictEqual(span.type, 'db', 'span.type');
t.strictEqual(span.subtype, 'redis', 'span.subtype');
t.deepEqual(
span.context.destination,
{
address: process.env.REDIS_HOST || 'localhost',
port: 6379,
service: { name: '', type: '', resource: 'redis' },
},
'span.context.destination',
);
t.deepEqual(span.context.db, { type: 'redis' }, 'span.context.db');
t.strictEqual(
span.parent_id,
trans.id,
'span is a child of the transaction',
);
const offset = span.timestamp - trans.timestamp;
t.ok(
offset + span.duration * 1000 < trans.duration * 1000,
'span ended before transaction ended',
);
}
t.end();
});
const client = redis.createClient({
socket: {
port: '6379',
host: process.env.REDIS_HOST,
},
});
client.connect();
const trans = agent.startTransaction('aTrans');
client
.flushAll()
.then(function (reply) {
t.strictEqual(reply, 'OK', 'reply is OK');
let done = 0;
client
.set('string key', 'string val')
.then(function (reply) {
t.strictEqual(reply, 'OK', 'reply is OK');
done++;
})
.catch(function (err) {
t.error(err);
});
// callback is optional
client.set('string key', 'string val');
client
.hSet('hash key', 'hashtest 1', 'some value')
.then(function (reply) {
t.strictEqual(reply, 1, 'hset reply is 1');
done++;
})
.catch(function (err) {
t.error(err, 'no hset error');
});
client
.hSet('hash key', ['hashtest 2', 'some other value'])
.then(function (reply) {
t.strictEqual(reply, 1, 'hset reply is 1');
done++;
})
.catch(function (err) {
t.error(err, 'no hset error');
});
client
.hKeys('hash key')
.then(function (replies) {
t.strictEqual(replies.length, 2, 'got two replies');
replies.forEach(function (reply, i) {
t.strictEqual(reply, 'hashtest ' + (i + 1), `reply ${i} value`);
});
done++;
t.strictEqual(done, 4, 'done 4 callbacks');
trans.end();
client.quit();
agent.flush();
})
.catch(function (err) {
t.error(err, 'no hkeys error');
});
})
.catch(function (err) {
t.error(err, 'no flushall error');
});
});
// The `redis.set('foo')` we are using to trigger a client error case only
// causes an error in redis >=4.1.0.
test(
'redis client error',
{ skip: semver.lt(redisVersion, '4.1.0') },
function (t) {
resetAgent(function (data) {
t.equal(data.transactions.length, 1, 'got 1 transaction');
t.equal(data.spans.length, 1, 'got 1 span');
t.equal(data.errors.length, 1, 'got 1 error');
t.equal(data.spans[0].name, 'SET', 'span.name');
t.equal(
data.spans[0].parent_id,
data.transactions[0].id,
'span.parent_id',
);
t.equal(data.spans[0].outcome, 'failure', 'span.outcome');
t.equal(
data.errors[0].transaction_id,
data.transactions[0].id,
'error.transaction_id',
);
t.equal(
data.errors[0].parent_id,
data.spans[0].id,
'error.parent_id, error is a child of the failing span',
);
t.equal(
data.errors[0].exception.type,
'TypeError',
'error.exception.type',
);
t.end();
});
// no .finally in Node 8, endPromise performs
// actions we'd normally perform there
function endProimse(t0, client, agent) {
t0.end();
client.quit();
agent.flush();
}
const client = redis.createClient({
socket: {
port: '6379',
host: process.env.REDIS_HOST,
},
});
client.connect();
const t0 = agent.startTransaction('t0');
client
.set('foo')
.then(function (response) {
t.fail('no response expected');
endProimse(t0, client, agent);
})
.catch(function (error) {
t.ok(error, 'expected error');
endProimse(t0, client, agent);
});
},
);
function resetAgent(cb) {
agent._instrumentation.testReset();
agent._apmClient = mockClient(cb);
}