Generate keys for different shards
This commit is contained in:
@@ -22,14 +22,31 @@ public class ShardedJedisTest extends Assert {
|
|||||||
private static HostAndPort redis2 = HostAndPortUtil.getRedisServers()
|
private static HostAndPort redis2 = HostAndPortUtil.getRedisServers()
|
||||||
.get(1);
|
.get(1);
|
||||||
|
|
||||||
|
private List<String> getKeysDifferentShard(ShardedJedis jedis) {
|
||||||
|
List<String> ret = new ArrayList<String> ();
|
||||||
|
JedisShardInfo first = jedis.getShardInfo("a0");
|
||||||
|
ret.add("a0");
|
||||||
|
for (int i =1; i < 100; ++i) {
|
||||||
|
JedisShardInfo actual = jedis.getShardInfo("a" + i);
|
||||||
|
if (actual != first) {
|
||||||
|
ret.add("a" + i);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkSharding() {
|
public void checkSharding() {
|
||||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||||
shards.add(new JedisShardInfo(redis1.host, redis1.port));
|
shards.add(new JedisShardInfo(redis1.host, redis1.port));
|
||||||
shards.add(new JedisShardInfo(redis2.host, redis2.port));
|
shards.add(new JedisShardInfo(redis2.host, redis2.port));
|
||||||
ShardedJedis jedis = new ShardedJedis(shards);
|
ShardedJedis jedis = new ShardedJedis(shards);
|
||||||
JedisShardInfo s1 = jedis.getShardInfo("a1");
|
List<String> keys = getKeysDifferentShard(jedis);
|
||||||
JedisShardInfo s2 = jedis.getShardInfo("b2");
|
JedisShardInfo s1 = jedis.getShardInfo(keys.get(0));
|
||||||
|
JedisShardInfo s2 = jedis.getShardInfo(keys.get(1));
|
||||||
assertNotSame(s1, s2);
|
assertNotSame(s1, s2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,8 +125,9 @@ public class ShardedJedisTest extends Assert {
|
|||||||
JedisShardInfo s2 = jedis.getShardInfo("foo{bar}");
|
JedisShardInfo s2 = jedis.getShardInfo("foo{bar}");
|
||||||
assertSame(s1, s2);
|
assertSame(s1, s2);
|
||||||
|
|
||||||
JedisShardInfo s3 = jedis.getShardInfo("a112");
|
List<String> keys = getKeysDifferentShard(jedis);
|
||||||
JedisShardInfo s4 = jedis.getShardInfo("b112");
|
JedisShardInfo s3 = jedis.getShardInfo(keys.get(0));
|
||||||
|
JedisShardInfo s4 = jedis.getShardInfo(keys.get(1));
|
||||||
assertNotSame(s3, s4);
|
assertNotSame(s3, s4);
|
||||||
|
|
||||||
ShardedJedis jedis2 = new ShardedJedis(shards);
|
ShardedJedis jedis2 = new ShardedJedis(shards);
|
||||||
@@ -131,15 +149,16 @@ public class ShardedJedisTest extends Assert {
|
|||||||
shards.get(1).setPassword("foobared");
|
shards.get(1).setPassword("foobared");
|
||||||
ShardedJedis jedis = new ShardedJedis(shards);
|
ShardedJedis jedis = new ShardedJedis(shards);
|
||||||
|
|
||||||
jedis.set("a112", "a");
|
final List<String> keys = getKeysDifferentShard(jedis);
|
||||||
jedis.set("b112", "b");
|
jedis.set(keys.get(0), "a");
|
||||||
|
jedis.set(keys.get(1), "b");
|
||||||
|
|
||||||
assertNotSame(jedis.getShard("a112"), jedis.getShard("b112"));
|
assertNotSame(jedis.getShard(keys.get(0)), jedis.getShard(keys.get(1)));
|
||||||
|
|
||||||
List<Object> results = jedis.pipelined(new ShardedJedisPipeline() {
|
List<Object> results = jedis.pipelined(new ShardedJedisPipeline() {
|
||||||
public void execute() {
|
public void execute() {
|
||||||
get("a112");
|
get(keys.get(0));
|
||||||
get("b112");
|
get(keys.get(1));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -221,4 +240,5 @@ public class ShardedJedisTest extends Assert {
|
|||||||
assertTrue(shard_6380 > 300 && shard_6380 < 400);
|
assertTrue(shard_6380 > 300 && shard_6380 < 400);
|
||||||
assertTrue(shard_6381 > 300 && shard_6381 < 400);
|
assertTrue(shard_6381 > 300 && shard_6381 < 400);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user