Skip to content

Commit 3cfab3d

Browse files
author
Eva Sarafianou
committed
Update shard-client tests
1 parent 07e38e7 commit 3cfab3d

File tree

2 files changed

+40
-82
lines changed

2 files changed

+40
-82
lines changed

shard_client.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ const defaults = {
1414
port: 9231
1515
};
1616

17-
function ShardClient(options) {
17+
function ShardClient(options, deps) {
18+
deps = deps || {};
19+
1820
if (typeof options !== 'object' || typeof options.shard !== 'object') {
1921
throw new Error('shard is required');
2022
}
2123

2224
EventEmitter.call(this);
2325

26+
this._LimitdClient = deps.LimitdClient || LimitdClient;
27+
this._dns = deps.dns || dns;
28+
2429
this._options = _.extend({}, defaults, options);
2530
this._clientParams = _.omit(this._options, ['shard', 'port']);
2631

@@ -48,7 +53,7 @@ util.inherits(ShardClient, EventEmitter);
4853

4954

5055
ShardClient.prototype.createClient = function(host) {
51-
const client = new LimitdClient(_.extend(this._clientParams, { host }));
56+
const client = new this._LimitdClient(_.extend(this._clientParams, { host }));
5257
if (client instanceof EventEmitter) {
5358
//map the events from LimitdClient.
5459
//Last parameter is always the underlying client.
@@ -65,7 +70,7 @@ ShardClient.prototype.createClient = function(host) {
6570
};
6671

6772
ShardClient.prototype.discover = function() {
68-
dns.resolve(this.autodiscover.address, this.autodiscover.type || 'A', (err, ips) => {
73+
this._dns.resolve(this.autodiscover.address, this.autodiscover.type || 'A', (err, ips) => {
6974
setTimeout(() => this.discover(), this.autodiscover.refreshInterval);
7075
if (err) {
7176
return this.emit('error', err);

test/shard_client.tests.js

Lines changed: 32 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const ShardClient = require('../shard_client');
22
const assert = require('chai').assert;
33
const _ = require('lodash');
4-
const proxyquire = require('proxyquire').noPreserveCache();
5-
64

75
describe('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: {} }), /unsupported shard configuration/);
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, /no shard available/);
@@ -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

Comments
 (0)