11const ShardClient = require ( '../shard_client' ) ;
22const assert = require ( 'chai' ) . assert ;
33const _ = require ( 'lodash' ) ;
4- const proxyquire = require ( 'proxyquire' ) . noPreserveCache ( ) ;
5-
64
75describe ( 'ShardClient' , function ( ) {
86 it ( 'should fail if shard is not specified' , function ( ) {
@@ -11,22 +9,14 @@ describe('ShardClient', function() {
119 assert . throws ( ( ) => new ShardClient ( { shard : { } } ) , / u n s u p p o r t e d s h a r d c o n f i g u r a t i o n / ) ;
1210 } ) ;
1311
14- function ShardClientCtor ( client ) {
15- return proxyquire ( '../shard_client' , {
16- './client' : client
17- } ) ;
18- }
19-
2012 it ( 'should fail when no shards are available' , function ( done ) {
21- const client = function ( params ) {
13+ const LimitdClient = function ( params ) {
2214 this . host = params . host ;
2315 } ;
2416
25- const ShardClient = ShardClientCtor ( client ) ;
26-
2717 const shardClient = new ShardClient ( {
2818 shard : { hosts : [ ] }
29- } ) ;
19+ } , { LimitdClient } ) ;
3020
3121 shardClient . put ( 'test' , 'foo' , ( err ) => {
3222 assert . match ( err . message , / n o s h a r d a v a i l a b l e / ) ;
@@ -35,15 +25,13 @@ describe('ShardClient', function() {
3525 } ) ;
3626
3727 it ( 'should work when full url are provided' , function ( ) {
38- const client = function ( params ) {
28+ const LimitdClient = function ( params ) {
3929 this . host = params . host ;
4030 } ;
4131
42- const ShardClient = ShardClientCtor ( client ) ;
43-
4432 const shardClient = new ShardClient ( {
4533 shard : { hosts : [ 'limitd://host-2:9231' , 'limitd://host-1:9231' ] }
46- } ) ;
34+ } , { LimitdClient } ) ;
4735
4836 assert . equal ( shardClient . clients [ 'host-1:9231' ] . host , 'limitd://host-1:9231' ) ;
4937 assert . equal ( shardClient . clients [ 'host-2:9231' ] . host , 'limitd://host-2:9231' ) ;
@@ -52,15 +40,13 @@ describe('ShardClient', function() {
5240 } ) ;
5341
5442 it ( 'should create a new client for each host' , function ( ) {
55- const client = function ( params ) {
43+ const LimitdClient = function ( params ) {
5644 this . host = params . host ;
5745 } ;
5846
59- const ShardClient = ShardClientCtor ( client ) ;
60-
6147 const shardClient = new ShardClient ( {
6248 shard : { hosts : [ 'host-2' , 'host-1' ] }
63- } ) ;
49+ } , { LimitdClient } ) ;
6450
6551 assert . equal ( shardClient . clients [ 'host-1:9231' ] . host , 'limitd://host-1:9231' ) ;
6652 assert . equal ( shardClient . clients [ 'host-2:9231' ] . host , 'limitd://host-2:9231' ) ;
@@ -78,7 +64,7 @@ describe('ShardClient', function() {
7864
7965
8066 it ( 'should invoke PUT on the client based on the hash' , function ( done ) {
81- const client = function ( params ) {
67+ const LimitdClient = function ( params ) {
8268 this . host = params . host ;
8369 this . put = function ( type , key , count , callback ) {
8470 assert . equal ( this . host , 'limitd://host-1:9231' ) ;
@@ -90,18 +76,15 @@ describe('ShardClient', function() {
9076 } ;
9177 } ;
9278
93- const ShardClient = ShardClientCtor ( client ) ;
94-
9579 const shardClient = new ShardClient ( {
96- client,
9780 shard : { hosts : [ 'host-1' , 'host-2' ] }
98- } ) ;
81+ } , { LimitdClient } ) ;
9982
10083 shardClient . put ( 'ip' , '10.0.0.1' , 1 , _ . noop ) ;
10184 } ) ;
10285
10386 it ( 'should invoke TAKE on the client based on the hash' , function ( done ) {
104- const client = function ( params ) {
87+ const LimitdClient = function ( params ) {
10588 this . host = params . host ;
10689 this . take = function ( type , key , count , callback ) {
10790 assert . equal ( this . host , 'limitd://host-1:9231' ) ;
@@ -113,17 +96,15 @@ describe('ShardClient', function() {
11396 } ;
11497 } ;
11598
116- const ShardClient = ShardClientCtor ( client ) ;
117-
11899 const shardClient = new ShardClient ( {
119100 shard : { hosts : [ 'host-1' , 'host-2' ] }
120- } ) ;
101+ } , { LimitdClient } ) ;
121102
122103 shardClient . take ( 'ip' , '10.0.0.2' , 1 , _ . noop ) ;
123104 } ) ;
124105
125106 it ( 'should invoke PUT on the client based on the hash (2)' , function ( done ) {
126- const client = function ( params ) {
107+ const LimitdClient = function ( params ) {
127108 this . host = params . host ;
128109 this . put = function ( type , key , count , callback ) {
129110 assert . equal ( this . host , 'limitd://host-1:9231' ) ;
@@ -135,17 +116,15 @@ describe('ShardClient', function() {
135116 } ;
136117 } ;
137118
138- const ShardClient = ShardClientCtor ( client ) ;
139-
140119 const shardClient = new ShardClient ( {
141120 shard : { hosts : [ 'host-1' , 'host-2' ] }
142- } ) ;
121+ } , { LimitdClient } ) ;
143122
144123 shardClient . put ( 'ip' , '10.0.0.2' , 1 , _ . noop ) ;
145124 } ) ;
146125
147126 it ( 'should call every host on status' , function ( done ) {
148- const client = function ( params ) {
127+ const LimitdClient = function ( params ) {
149128 this . host = params . host ;
150129 this . status = function ( type , prefix , callback ) {
151130 if ( this . host === 'limitd://host-1:9231' ) {
@@ -164,19 +143,17 @@ describe('ShardClient', function() {
164143 } ;
165144 } ;
166145
167- const ShardClient = ShardClientCtor ( client ) ;
168-
169146 const shardClient = new ShardClient ( {
170147 shard : { hosts : [ 'host-1' , 'host-2' ] }
171- } ) ;
148+ } , { LimitdClient } ) ;
172149 // Mock routing
173150 shardClient . getDestinationClient = function ( type , key ) {
174151 if ( key === 'item1-from-host-1' ) {
175152 return this . clients [ 'host-1:9231' ] ;
176153 } else {
177154 return this . clients [ 'host-2:9231' ] ;
178155 }
179- }
156+ } ;
180157
181158 shardClient . status ( 'ip' , '10.0.0.2' , ( err , response ) => {
182159 if ( err ) { return done ( err ) ; }
@@ -189,7 +166,7 @@ describe('ShardClient', function() {
189166
190167 describe ( 'when adding a new host to the shard' , function ( ) {
191168 it ( 'should not return instances from hosts that does not hold the instance anymore' , function ( done ) {
192- const client = function ( params ) {
169+ const LimitdClient = function ( params ) {
193170 this . host = params . host ;
194171 this . status = function ( type , prefix , callback ) {
195172 // All hosts returns the same instances, the shard client must select
@@ -219,18 +196,13 @@ describe('ShardClient', function() {
219196 }
220197 } ;
221198
222- const SharedClient = proxyquire ( '../shard_client' , {
223- './client' : client ,
224- 'dns' : dns
225- } ) ;
226-
227- const shardClient = new SharedClient ( {
199+ const shardClient = new ShardClient ( {
228200 shard : {
229201 autodiscover : {
230202 address : 'foo.bar.company.example.com'
231203 }
232204 }
233- } ) ;
205+ } , { LimitdClient , dns } ) ;
234206
235207 // Mock routing
236208 shardClient . getDestinationClient = function ( type , key ) {
@@ -239,7 +211,7 @@ describe('ShardClient', function() {
239211 } else if ( key === 'ip|2|limitd://host-b:9231' ) {
240212 return this . clients [ 'host-b:9231' ] ;
241213 }
242- }
214+ } ;
243215
244216 shardClient . status ( 'ip' , '10.0.0.2' , ( err , response ) => {
245217 if ( err ) { return done ( err ) ; }
@@ -258,7 +230,7 @@ describe('ShardClient', function() {
258230 } ) ;
259231
260232 it ( 'should swallow error from client on status' , function ( done ) {
261- const client = function ( params ) {
233+ const LimitdClient = function ( params ) {
262234 this . host = params . host ;
263235 this . status = function ( type , prefix , callback ) {
264236 if ( this . host === 'limitd://host-2:9231' ) {
@@ -273,11 +245,9 @@ describe('ShardClient', function() {
273245 } ;
274246 } ;
275247
276- const ShardClient = ShardClientCtor ( client ) ;
277-
278248 const shardClient = new ShardClient ( {
279249 shard : { hosts : [ 'host-1' , 'host-2' ] }
280- } ) ;
250+ } , { LimitdClient } ) ;
281251
282252 shardClient . status ( 'ip' , '10.0.0.2' , ( err , response ) => {
283253 if ( err ) { return done ( err ) ; }
@@ -292,19 +262,17 @@ describe('ShardClient', function() {
292262
293263 it ( 'should ping all hosts on ping' , function ( done ) {
294264 const pinged = [ ] ;
295- const client = function ( params ) {
265+ const LimitdClient = function ( params ) {
296266 this . host = params . host ;
297267 this . ping = function ( callback ) {
298268 pinged . push ( this . host ) ;
299269 callback ( null , { } ) ;
300270 } ;
301271 } ;
302272
303- const ShardClient = ShardClientCtor ( client ) ;
304-
305273 const shardClient = new ShardClient ( {
306274 shard : { hosts : [ 'host-1' , 'host-2' ] }
307- } ) ;
275+ } , { LimitdClient } ) ;
308276
309277 shardClient . ping ( ( err ) => {
310278 if ( err ) { return done ( err ) ; }
@@ -316,7 +284,7 @@ describe('ShardClient', function() {
316284
317285
318286 it ( 'should autodiscover limitd shards' , function ( ) {
319- const client = function ( params ) {
287+ const LimitdClient = function ( params ) {
320288 this . host = params . host ;
321289 } ;
322290
@@ -328,18 +296,13 @@ describe('ShardClient', function() {
328296 }
329297 } ;
330298
331- const SharedClient = proxyquire ( '../shard_client' , {
332- './client' : client ,
333- 'dns' : dns
334- } ) ;
335-
336- const shardClient = new SharedClient ( {
299+ const shardClient = new ShardClient ( {
337300 shard : {
338301 autodiscover : {
339302 address : 'foo.bar.company.example.com'
340303 }
341304 }
342- } ) ;
305+ } , { LimitdClient , dns } ) ;
343306
344307 assert . equal ( shardClient . clients [ 'host-a:9231' ] . host , 'limitd://host-a:9231' ) ;
345308 assert . equal ( shardClient . clients [ 'host-b:9231' ] . host , 'limitd://host-b:9231' ) ;
@@ -349,7 +312,7 @@ describe('ShardClient', function() {
349312
350313 it ( 'should add new shards' , function ( done ) {
351314 var clientsCreated = 0 ;
352- const client = function ( params ) {
315+ const LimitdClient = function ( params ) {
353316 clientsCreated ++ ;
354317 this . host = params . host ;
355318 } ;
@@ -369,19 +332,14 @@ describe('ShardClient', function() {
369332 }
370333 } ;
371334
372- const SharedClient = proxyquire ( '../shard_client' , {
373- './client' : client ,
374- 'dns' : dns
375- } ) ;
376-
377- const shardClient = new SharedClient ( {
335+ const shardClient = new ShardClient ( {
378336 shard : {
379337 autodiscover : {
380338 refreshInterval : 10 ,
381339 address : 'foo.bar.company.example.com'
382340 }
383341 }
384- } ) ;
342+ } , { LimitdClient , dns } ) ;
385343
386344
387345 shardClient . on ( 'new client' , ( ) => {
@@ -405,7 +363,7 @@ describe('ShardClient', function() {
405363 it ( 'should remove shards' , function ( done ) {
406364 var clientsCreated = 0 ;
407365 var clients = [ ] ;
408- const client = function ( params ) {
366+ const LimitdClient = function ( params ) {
409367 clientsCreated ++ ;
410368 this . host = params . host ;
411369 this . disconnect = ( ) => this . disconnected = true ;
@@ -427,19 +385,14 @@ describe('ShardClient', function() {
427385 }
428386 } ;
429387
430- const SharedClient = proxyquire ( '../shard_client' , {
431- './client' : client ,
432- 'dns' : dns
433- } ) ;
434-
435- const shardClient = new SharedClient ( {
388+ const shardClient = new ShardClient ( {
436389 shard : {
437390 autodiscover : {
438391 refreshInterval : 10 ,
439392 address : 'foo.bar.company.example.com'
440393 }
441394 }
442- } ) ;
395+ } , { LimitdClient , dns } ) ;
443396
444397
445398 shardClient . once ( 'new client' , ( ) => {
0 commit comments