Skip to content

Commit 51d479a

Browse files
author
Ben Buckman
committed
fix client.select() flow, was breaking tests intermittently
1 parent aa50b9a commit 51d479a

File tree

1 file changed

+54
-43
lines changed

1 file changed

+54
-43
lines changed

test/test_sharding.js

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -253,58 +253,69 @@ describe('RedisShardingClient', function(){
253253

254254
// the tests below rely on select(db) to differentiate the clients.
255255
// so make sure the selector is working.
256+
//
257+
// note, some of these tests rely on RedisClient's internal queuing for async flow.
258+
//
256259
describe('test helpers', function(){
257260

258-
// @todo this seems to fail every so often!!! why?
259261
it('should select different dbs', function(done){
260-
var client0 = redis.createClient(PORT, HOST);
261-
client0.select(0);
262+
var client0, client1, clients, shardClient, testKey, testVal0, testVal1;
262263

263-
var client1 = redis.createClient(PORT, HOST);
264-
client1.select(1);
265-
266-
// should be equivalent to [client0, client1]
267-
var clients = clientArray(2, true);
264+
async.series([
265+
function(next){
266+
client0 = redis.createClient(PORT, HOST);
267+
client0.select(0, next);
268+
},
269+
function(next){
270+
client1 = redis.createClient(PORT, HOST);
271+
client1.select(1, next);
272+
},
273+
function(next){
274+
// should be equivalent to [client0, client1]
275+
clients = clientArray(2, true);
268276

269-
var testKey = randomTestKey(),
270-
testVal0 = randomTestKey(),
277+
testKey = randomTestKey();
278+
testVal0 = randomTestKey();
271279
testVal1 = randomTestKey();
272280

273-
client0.set(testKey, testVal0);
274-
client1.set(testKey, testVal1);
275-
276-
client0.get(testKey, function(error, val){
277-
assert.ifError(error);
278-
assert.strictEqual(val, testVal0);
279-
});
280-
client1.get(testKey, function(error, val){
281-
assert.ifError(error);
282-
assert.strictEqual(val, testVal1);
283-
});
284-
285-
clients[0].get(testKey, function(error, val){
286-
assert.ifError(error);
287-
assert.strictEqual(val, testVal0);
288-
});
289-
clients[1].get(testKey, function(error, val){
290-
assert.ifError(error);
291-
assert.strictEqual(val, testVal1);
292-
});
281+
client0.set(testKey, testVal0);
282+
client1.set(testKey, testVal1);
293283

294-
var shardClient = new redis.RedisShardingClient({
295-
clients: clients,
296-
hashFunction: function(){}
297-
});
298-
shardClient.clients[0].get(testKey, function(error, val){
299-
assert.ifError(error);
300-
assert.strictEqual(val, testVal0);
301-
});
302-
shardClient.clients[1].get(testKey, function(error, val){
303-
assert.ifError(error);
304-
assert.strictEqual(val, testVal1);
284+
client0.get(testKey, function(error, val){
285+
assert.ifError(error);
286+
assert.strictEqual(val, testVal0);
287+
});
288+
client1.get(testKey, function(error, val){
289+
assert.ifError(error);
290+
assert.strictEqual(val, testVal1);
291+
});
305292

306-
done();
307-
});
293+
clients[0].get(testKey, function(error, val){
294+
assert.ifError(error);
295+
assert.strictEqual(val, testVal0);
296+
});
297+
clients[1].get(testKey, function(error, val){
298+
assert.ifError(error);
299+
assert.strictEqual(val, testVal1);
300+
next();
301+
});
302+
},
303+
function(next){
304+
shardClient = new redis.RedisShardingClient({
305+
clients: clients,
306+
hashFunction: function(){}
307+
});
308+
shardClient.clients[0].get(testKey, function(error, val){
309+
assert.ifError(error);
310+
assert.strictEqual(val, testVal0);
311+
});
312+
shardClient.clients[1].get(testKey, function(error, val){
313+
assert.ifError(error);
314+
assert.strictEqual(val, testVal1);
315+
next();
316+
});
317+
}
318+
], done);
308319
});
309320
});
310321

0 commit comments

Comments
 (0)