@@ -5,14 +5,14 @@ const test = t.test
5
5
const Fastify = require ( 'fastify' )
6
6
const fastifyRedis = require ( './index' )
7
7
8
- t . beforeEach ( done => {
8
+ t . beforeEach ( ( done ) => {
9
9
const fastify = Fastify ( )
10
10
11
11
fastify . register ( fastifyRedis , {
12
12
host : '127.0.0.1'
13
13
} )
14
14
15
- fastify . ready ( err => {
15
+ fastify . ready ( ( err ) => {
16
16
t . error ( err )
17
17
18
18
fastify . redis . flushall ( ( ) => {
@@ -22,33 +22,33 @@ t.beforeEach(done => {
22
22
} )
23
23
} )
24
24
25
- test ( 'fastify.redis should exist' , t => {
25
+ test ( 'fastify.redis should exist' , ( t ) => {
26
26
t . plan ( 2 )
27
27
const fastify = Fastify ( )
28
28
fastify . register ( fastifyRedis , {
29
29
host : '127.0.0.1'
30
30
} )
31
31
32
- fastify . ready ( err => {
32
+ fastify . ready ( ( err ) => {
33
33
t . error ( err )
34
34
t . ok ( fastify . redis )
35
35
36
36
fastify . close ( )
37
37
} )
38
38
} )
39
39
40
- test ( 'fastify.redis should be the redis client' , t => {
40
+ test ( 'fastify.redis should be the redis client' , ( t ) => {
41
41
t . plan ( 4 )
42
42
const fastify = Fastify ( )
43
43
44
44
fastify . register ( fastifyRedis , {
45
45
host : '127.0.0.1'
46
46
} )
47
47
48
- fastify . ready ( err => {
48
+ fastify . ready ( ( err ) => {
49
49
t . error ( err )
50
50
51
- fastify . redis . set ( 'key' , 'value' , err => {
51
+ fastify . redis . set ( 'key' , 'value' , ( err ) => {
52
52
t . error ( err )
53
53
fastify . redis . get ( 'key' , ( err , val ) => {
54
54
t . error ( err )
@@ -60,41 +60,84 @@ test('fastify.redis should be the redis client', t => {
60
60
} )
61
61
} )
62
62
63
- test ( 'promises support' , t => {
63
+ test ( 'fastify.redis.test namespace should exist' , ( t ) => {
64
+ t . plan ( 3 )
65
+
66
+ const fastify = Fastify ( )
67
+ fastify . register ( fastifyRedis , {
68
+ host : '127.0.0.1' ,
69
+ namespace : 'test'
70
+ } )
71
+
72
+ fastify . ready ( ( err ) => {
73
+ t . error ( err )
74
+ t . ok ( fastify . redis )
75
+ t . ok ( fastify . redis . test )
76
+
77
+ fastify . close ( )
78
+ } )
79
+ } )
80
+
81
+ test ( 'fastify.redis.test should be the redis client' , ( t ) => {
82
+ t . plan ( 4 )
83
+ const fastify = Fastify ( )
84
+
85
+ fastify . register ( fastifyRedis , {
86
+ host : '127.0.0.1' ,
87
+ namespace : 'test'
88
+ } )
89
+
90
+ fastify . ready ( ( err ) => {
91
+ t . error ( err )
92
+
93
+ fastify . redis . test . set ( 'key_namespace' , 'value_namespace' , ( err ) => {
94
+ t . error ( err )
95
+ fastify . redis . test . get ( 'key_namespace' , ( err , val ) => {
96
+ t . error ( err )
97
+ t . equal ( val , 'value_namespace' )
98
+
99
+ fastify . close ( )
100
+ } )
101
+ } )
102
+ } )
103
+ } )
104
+
105
+ test ( 'promises support' , ( t ) => {
64
106
t . plan ( 2 )
65
107
const fastify = Fastify ( )
66
108
67
109
fastify . register ( fastifyRedis , {
68
110
host : '127.0.0.1'
69
111
} )
70
112
71
- fastify . ready ( err => {
113
+ fastify . ready ( ( err ) => {
72
114
t . error ( err )
73
115
74
- fastify . redis . set ( 'key' , 'value' )
116
+ fastify . redis
117
+ . set ( 'key' , 'value' )
75
118
. then ( ( ) => {
76
119
return fastify . redis . get ( 'key' )
77
120
} )
78
- . then ( val => {
121
+ . then ( ( val ) => {
79
122
t . equal ( val , 'value' )
80
123
fastify . close ( )
81
124
} )
82
- . catch ( err => t . fail ( err ) )
125
+ . catch ( ( err ) => t . fail ( err ) )
83
126
} )
84
127
} )
85
128
86
- test ( 'custom client' , t => {
129
+ test ( 'custom client' , ( t ) => {
87
130
t . plan ( 7 )
88
131
const fastify = Fastify ( )
89
132
const redis = require ( 'redis' ) . createClient ( { host : 'localhost' , port : 6379 } )
90
133
91
134
fastify . register ( fastifyRedis , { client : redis } )
92
135
93
- fastify . ready ( err => {
136
+ fastify . ready ( ( err ) => {
94
137
t . error ( err )
95
138
t . is ( fastify . redis , redis )
96
139
97
- fastify . redis . set ( 'key' , 'value' , err => {
140
+ fastify . redis . set ( 'key' , 'value' , ( err ) => {
98
141
t . error ( err )
99
142
fastify . redis . get ( 'key' , ( err , val ) => {
100
143
t . error ( err )
@@ -110,3 +153,107 @@ test('custom client', t => {
110
153
} )
111
154
} )
112
155
} )
156
+
157
+ test ( 'custom client inside a namespace' , ( t ) => {
158
+ t . plan ( 7 )
159
+ const fastify = Fastify ( )
160
+ const redis = require ( 'redis' ) . createClient ( { host : 'localhost' , port : 6379 } )
161
+
162
+ fastify . register ( fastifyRedis , {
163
+ namespace : 'test' ,
164
+ client : redis
165
+ } )
166
+
167
+ fastify . ready ( ( err ) => {
168
+ t . error ( err )
169
+ t . is ( fastify . redis . test , redis )
170
+
171
+ fastify . redis . test . set ( 'key' , 'value' , ( err ) => {
172
+ t . error ( err )
173
+ fastify . redis . test . get ( 'key' , ( err , val ) => {
174
+ t . error ( err )
175
+ t . equal ( val , 'value' )
176
+
177
+ fastify . close ( function ( err ) {
178
+ t . error ( err )
179
+ fastify . redis . test . quit ( function ( err ) {
180
+ t . error ( err )
181
+ } )
182
+ } )
183
+ } )
184
+ } )
185
+ } )
186
+ } )
187
+
188
+ test ( 'fastify.redis.test should throw with duplicate connection namespaces' , ( t ) => {
189
+ t . plan ( 1 )
190
+
191
+ const namespace = 'test'
192
+
193
+ const fastify = Fastify ( )
194
+ t . teardown ( ( ) => fastify . close ( ) )
195
+
196
+ fastify
197
+ . register ( fastifyRedis , {
198
+ host : '127.0.0.1' ,
199
+ namespace
200
+ } )
201
+ . register ( fastifyRedis , {
202
+ host : '127.0.0.1' ,
203
+ namespace
204
+ } )
205
+
206
+ fastify . ready ( ( err ) => {
207
+ t . is ( err . message , `Redis '${ namespace } ' instance namespace has already been registered` )
208
+ } )
209
+ } )
210
+
211
+ test ( 'Should throw when trying to register multiple instances without giving a namespace' , ( t ) => {
212
+ t . plan ( 1 )
213
+
214
+ const fastify = Fastify ( )
215
+ t . teardown ( ( ) => fastify . close ( ) )
216
+
217
+ fastify
218
+ . register ( fastifyRedis , {
219
+ host : '127.0.0.1'
220
+ } )
221
+ . register ( fastifyRedis , {
222
+ host : '127.0.0.1'
223
+ } )
224
+
225
+ fastify . ready ( ( err ) => {
226
+ t . is ( err . message , 'fastify-redis has already been registered' )
227
+ } )
228
+ } )
229
+
230
+ test ( 'Should not throw within different contexts' , ( t ) => {
231
+ t . plan ( 1 )
232
+
233
+ const fastify = Fastify ( )
234
+ t . teardown ( ( ) => fastify . close ( ) )
235
+
236
+ fastify . register ( function ( instance , options , next ) {
237
+ instance . register ( fastifyRedis , {
238
+ host : '127.0.0.1'
239
+ } )
240
+ next ( )
241
+ } )
242
+
243
+ fastify . register ( function ( instance , options , next ) {
244
+ instance
245
+ . register ( fastifyRedis , {
246
+ host : '127.0.0.1' ,
247
+ namespace : 'test1'
248
+ } )
249
+ . register ( fastifyRedis , {
250
+ host : '127.0.0.1' ,
251
+ namespace : 'test2'
252
+ } )
253
+ next ( )
254
+ } )
255
+
256
+ fastify . ready ( ( error ) => {
257
+ t . is ( error , null )
258
+ } )
259
+ } )
0 commit comments