6
6
'use strict'
7
7
8
8
const tap = require ( 'tap' )
9
- const client = require ( '../../../lib/instrumentation/@node-redis/client' )
9
+ const sinon = require ( 'sinon' )
10
+ const helper = require ( '../../lib/agent_helper' )
11
+ const DatastoreShim = require ( '../../../lib/shim/datastore-shim.js' )
12
+ const { redisClientOpts } = require ( '../../../lib/symbols' )
10
13
11
14
tap . test ( 'getRedisParams should behave as expected' , function ( t ) {
12
- t . autoend ( )
13
-
15
+ const { getRedisParams } = require ( '../../../lib/instrumentation/@node-redis/client' )
14
16
t . test ( 'given no opts, should return sensible defaults' , function ( t ) {
15
- t . autoend ( )
16
- const params = client . getRedisParams ( )
17
+ const params = getRedisParams ( )
17
18
const expected = {
18
19
host : 'localhost' ,
19
20
port_path_or_id : '6379' ,
20
21
database_name : 0
21
22
}
22
23
t . match ( params , expected , 'redis client should be definable without params' )
24
+ t . end ( )
23
25
} )
24
26
t . test ( 'if host/port are defined incorrectly, should return expected defaults' , function ( t ) {
25
- t . autoend ( )
26
- const params = client . getRedisParams ( { host : 'myLocalHost' , port : '1234' } )
27
+ const params = getRedisParams ( { host : 'myLocalHost' , port : '1234' } )
27
28
const expected = {
28
29
host : 'localhost' ,
29
30
port_path_or_id : '6379' ,
30
31
database_name : 0
31
32
}
32
33
t . match ( params , expected , 'should return sensible defaults if defined without socket' )
34
+ t . end ( )
33
35
} )
34
36
t . test ( 'if host/port are defined correctly, we should see them in config' , function ( t ) {
35
- t . autoend ( )
36
- const params = client . getRedisParams ( { socket : { host : 'myLocalHost' , port : '1234' } } )
37
+ const params = getRedisParams ( { socket : { host : 'myLocalHost' , port : '1234' } } )
37
38
const expected = {
38
39
host : 'myLocalHost' ,
39
40
port_path_or_id : '1234' ,
40
41
database_name : 0
41
42
}
42
43
t . match ( params , expected , 'host/port should be returned when defined correctly' )
44
+ t . end ( )
43
45
} )
44
46
t . test ( 'path should be used if defined' , function ( t ) {
45
- t . autoend ( )
46
- const params = client . getRedisParams ( { socket : { path : '5678' } } )
47
+ const params = getRedisParams ( { socket : { path : '5678' } } )
47
48
const expected = {
48
49
host : 'localhost' ,
49
50
port_path_or_id : '5678' ,
50
51
database_name : 0
51
52
}
52
53
t . match ( params , expected , 'path should show up in params' )
54
+ t . end ( )
53
55
} )
54
56
t . test ( 'path should be preferred over port' , function ( t ) {
55
- t . autoend ( )
56
- const params = client . getRedisParams ( {
57
+ const params = getRedisParams ( {
57
58
socket : { host : 'myLocalHost' , port : '1234' , path : '5678' }
58
59
} )
59
60
const expected = {
@@ -62,15 +63,145 @@ tap.test('getRedisParams should behave as expected', function (t) {
62
63
database_name : 0
63
64
}
64
65
t . match ( params , expected , 'path should show up in params' )
66
+ t . end ( )
65
67
} )
66
68
t . test ( 'database name should be definable' , function ( t ) {
67
- t . autoend ( )
68
- const params = client . getRedisParams ( { database : 12 } )
69
+ const params = getRedisParams ( { database : 12 } )
69
70
const expected = {
70
71
host : 'localhost' ,
71
72
port_path_or_id : '6379' ,
72
73
database_name : 12
73
74
}
74
75
t . match ( params , expected , 'database should be definable' )
76
+ t . end ( )
77
+ } )
78
+
79
+ t . test ( 'host/port/database should be extracted from url when it exists' , function ( t ) {
80
+ const params = getRedisParams ( { url : 'redis://host:6369/db' } )
81
+ const expected = {
82
+ host : 'host' ,
83
+ port_path_or_id : '6369' ,
84
+ database_name : 'db'
85
+ }
86
+ t . match ( params , expected , 'host/port/database should match' )
87
+ t . end ( )
88
+ } )
89
+
90
+ t . test ( 'should default port to 6379 when no port specified in URL' , function ( t ) {
91
+ const params = getRedisParams ( { url : 'redis://host/db' } )
92
+ const expected = {
93
+ host : 'host' ,
94
+ port_path_or_id : '6379' ,
95
+ database_name : 'db'
96
+ }
97
+ t . match ( params , expected , 'host/port/database should match' )
98
+ t . end ( )
99
+ } )
100
+
101
+ t . test ( 'should default database to 0 when no db pecified in URL' , function ( t ) {
102
+ const params = getRedisParams ( { url : 'redis://host' } )
103
+ const expected = {
104
+ host : 'host' ,
105
+ port_path_or_id : '6379' ,
106
+ database_name : 0
107
+ }
108
+ t . match ( params , expected , 'host/port/database should match' )
109
+ t . end ( )
110
+ } )
111
+ t . end ( )
112
+ } )
113
+
114
+ tap . test ( 'createClient saves connection options' , function ( t ) {
115
+ t . beforeEach ( ( t ) => {
116
+ t . context . sandbox = sinon . createSandbox ( )
117
+ t . context . agent = helper . loadMockedAgent ( )
118
+ t . context . shim = new DatastoreShim ( t . context . agent , 'redis' )
119
+ t . context . instrumentation = require ( '../../../lib/instrumentation/@node-redis/client' )
120
+ t . context . clients = {
121
+ 1 : { socket : { host : '1' , port : 2 } } ,
122
+ 2 : { socket : { host : '2' , port : 3 } }
123
+ }
124
+ let i = 0
125
+ class CommandQueueClass {
126
+ constructor ( ) {
127
+ i ++
128
+ this . id = i
129
+ const expectedValues = t . context . clients [ this . id ]
130
+ t . match ( t . context . shim [ redisClientOpts ] , {
131
+ host : expectedValues . socket . host ,
132
+ port_path_or_id : expectedValues . socket . port
133
+ } )
134
+ }
135
+
136
+ async addCommand ( ) { }
137
+ }
138
+
139
+ const commandQueueStub = { default : CommandQueueClass }
140
+ const redis = Object . create ( {
141
+ createClient : function ( ) {
142
+ const instance = Object . create ( { } )
143
+ // eslint-disable-next-line new-cap
144
+ instance . queue = new commandQueueStub . default ( )
145
+ return instance
146
+ }
147
+ } )
148
+
149
+ t . context . sandbox . stub ( t . context . shim , 'require' ) . returns ( commandQueueStub )
150
+ t . context . redis = redis
151
+ } )
152
+
153
+ t . afterEach ( ( t ) => {
154
+ helper . unloadAgent ( t . context . agent )
155
+ t . context . sandbox . restore ( )
156
+ } )
157
+
158
+ t . test ( 'should remove connect options after creation' , function ( t ) {
159
+ const { agent, redis, shim, instrumentation, clients } = t . context
160
+ instrumentation ( agent , redis , 'redis' , shim )
161
+ redis . createClient ( clients [ 1 ] )
162
+ t . notOk ( shim [ redisClientOpts ] , 'should remove client options after creation' )
163
+ redis . createClient ( clients [ 2 ] )
164
+ t . notOk ( shim [ redisClientOpts ] , 'should remove client options after creation' )
165
+ t . end ( )
166
+ } )
167
+
168
+ t . test ( 'should keep the connection details per client' , function ( t ) {
169
+ const { agent, redis, shim, instrumentation, clients } = t . context
170
+ instrumentation ( agent , redis , 'redis' , shim )
171
+ const client = redis . createClient ( clients [ 1 ] )
172
+ const client2 = redis . createClient ( clients [ 2 ] )
173
+ helper . runInTransaction ( agent , async function ( tx ) {
174
+ await client . queue . addCommand ( [ 'test' , 'key' , 'value' ] )
175
+ await client2 . queue . addCommand ( [ 'test2' , 'key2' , 'value2' ] )
176
+ const [ redisSegment , redisSegment2 ] = tx . trace . root . children
177
+ const attrs = redisSegment . getAttributes ( )
178
+ t . same (
179
+ attrs ,
180
+ {
181
+ host : '1' ,
182
+ port_path_or_id : 2 ,
183
+ key : '"key"' ,
184
+ value : '"value"' ,
185
+ product : 'Redis' ,
186
+ database_name : '0'
187
+ } ,
188
+ 'should have appropriate segment attrs'
189
+ )
190
+ const attrs2 = redisSegment2 . getAttributes ( )
191
+ t . same (
192
+ attrs2 ,
193
+ {
194
+ host : '2' ,
195
+ port_path_or_id : 3 ,
196
+ key : '"key2"' ,
197
+ value : '"value2"' ,
198
+ product : 'Redis' ,
199
+ database_name : '0'
200
+ } ,
201
+ 'should have appropriate segment attrs'
202
+ )
203
+ t . end ( )
204
+ } )
75
205
} )
206
+ t . end ( )
76
207
} )
0 commit comments