Update the way the Redis host(s) can be configured for the tests.

There is now a single property : redis-hosts.
This property must contain at least 2 host definitions of the form "host:port" (comma separated).
Sharding tests need 2 hosts ...

If this is not the case, the default value used is "localhost:6379,localhost:6380".
Tests that required one host are using the first definition.
This commit is contained in:
Yaourt
2010-09-15 10:49:36 +02:00
27 changed files with 2051 additions and 420 deletions

View File

@@ -0,0 +1,58 @@
package redis.clients.jedis.tests;
import java.util.ArrayList;
import java.util.List;
import redis.clients.jedis.Protocol;
public class HostAndPortUtil {
private static List<HostAndPort> hostAndPortList = new ArrayList<HostAndPortUtil.HostAndPort>(2);
static {
final HostAndPort defaulthnp1 = new HostAndPort();
defaulthnp1.host = "localhost";
defaulthnp1.port = Protocol.DEFAULT_PORT;
hostAndPortList.add(defaulthnp1);
final HostAndPort defaulthnp2 = new HostAndPort();
defaulthnp2.host = "localhost";
defaulthnp2.port = Protocol.DEFAULT_PORT + 1;
hostAndPortList.add(defaulthnp2);
final String envHosts = System.getProperty("redis-hosts");
if (null != envHosts && 0 < envHosts.length()) {
final String[] hostDefs = envHosts.split(",");
if (null != hostDefs && 2 <= hostDefs.length) {
hostAndPortList = new ArrayList<HostAndPortUtil.HostAndPort>(hostDefs.length);
for(String hostDef : hostDefs) {
final String[] hostAndPort = hostDef.split(":");
if (null != hostAndPort && 2 == hostAndPort.length) {
final HostAndPort hnp = new HostAndPort();
hnp.host = hostAndPort[0];
try {
hnp.port = Integer.parseInt(hostAndPort[1]);
} catch(final NumberFormatException nfe){
hnp.port = Protocol.DEFAULT_PORT;
}
hostAndPortList.add(hnp);
}
}
}
}
final StringBuilder strb = new StringBuilder("Redis hosts to be used : ");
for(HostAndPort hnp : hostAndPortList){
strb.append('[').append(hnp.host).append(':').append(hnp.port).append(']').append(' ');
}
System.out.println(strb);
}
public static List<HostAndPort> getRedisServers() {
return hostAndPortList;
}
public static class HostAndPort {
public String host;
public int port;
}
}

View File

@@ -0,0 +1,72 @@
package redis.clients.jedis.tests;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.Transaction;
public class JedisNewCommandsCheckTest extends Assert {
@Test
@Ignore(value = "Ignored because still missing information for DEBUG and LINSERT commands")
public void checkJedisIsUpdated() throws IOException {
String[] commands = getAvailableCommands();
Set<String> implementedCommands = getImplementedCommands();
Set<String> missingCommands = new HashSet<String>();
for (String command : commands) {
if (!implementedCommands.contains(command.trim())) {
missingCommands.add(command);
}
}
if (!missingCommands.isEmpty()) {
fail("There are missing commands: " + missingCommands.toString());
}
}
private Set<String> getImplementedCommands() {
Method[] methods = Jedis.class.getDeclaredMethods();
Set<String> implementedCommands = new HashSet<String>();
for (Method method : methods) {
implementedCommands.add(method.getName().trim().toLowerCase());
}
methods = JedisPubSub.class.getDeclaredMethods();
for (Method method : methods) {
implementedCommands.add(method.getName().trim().toLowerCase());
}
methods = Transaction.class.getDeclaredMethods();
for (Method method : methods) {
implementedCommands.add(method.getName().trim().toLowerCase());
}
implementedCommands.add("config");
return implementedCommands;
}
private String[] getAvailableCommands() throws MalformedURLException,
IOException {
URL url = new URL("http://dimaion.com/redis/master");
InputStream openStream = url.openStream();
DataInputStream dis = new DataInputStream(new BufferedInputStream(
openStream));
byte[] all = new byte[dis.available()];
dis.readFully(all);
String commandList = new String(all);
String[] commands = commandList.split("\n");
return commands;
}
}

View File

@@ -4,35 +4,18 @@ import java.io.IOException;
import java.util.concurrent.TimeoutException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class JedisPoolTest extends Assert {
private static String host = "localhost";
private static int port = Protocol.DEFAULT_PORT;
static {
final String envHost = System.getProperty("redis-host");
final String envPort = System.getProperty("redis-port");
if (null != envHost && 0 < envHost.length()) {
host = envHost;
}
if (null != envPort && 0 < envPort.length()) {
try {
port = Integer.parseInt(envPort);
} catch (final NumberFormatException e) {}
}
System.out.println("Redis host to be used : " + host + ":" + port);
}
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
@Test
public void checkConnections() throws TimeoutException {
JedisPool pool = new JedisPool(host, port, 2000);
JedisPool pool = new JedisPool(hnp.host, hnp.port, 2000);
pool.setResourcesNumber(10);
pool.init();
@@ -46,7 +29,7 @@ public class JedisPoolTest extends Assert {
@Test
public void checkConnectionWithDefaultPort() throws TimeoutException {
JedisPool pool = new JedisPool(host, port);
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(10);
pool.init();
@@ -60,7 +43,7 @@ public class JedisPoolTest extends Assert {
@Test
public void checkJedisIsReusedWhenReturned() throws TimeoutException {
JedisPool pool = new JedisPool(host, port);
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
@@ -79,7 +62,7 @@ public class JedisPoolTest extends Assert {
@Test
public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException,
IOException {
JedisPool pool = new JedisPool(host, port);
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
@@ -97,7 +80,7 @@ public class JedisPoolTest extends Assert {
@Test(expected = TimeoutException.class)
public void checkPoolOverflow() throws TimeoutException {
JedisPool pool = new JedisPool(host, port);
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();

View File

@@ -6,13 +6,16 @@ import java.util.Map;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.tests.commands.JedisCommandTestBase;
import redis.clients.util.RedisOutputStream;
import redis.clients.util.ShardInfo;
public class JedisTest extends JedisCommandTestBase {
@Test
public void useWithoutConnecting() {
Jedis jedis = new Jedis("localhost");
jedis.auth("foobared");
jedis.dbSize();
}
@@ -30,4 +33,11 @@ public class JedisTest extends JedisCommandTestBase {
assertEquals(hash, jedis.hgetAll("foo"));
}
@Test
public void connectWithShardInfo() {
ShardInfo shardInfo = new ShardInfo("localhost", Protocol.DEFAULT_PORT);
shardInfo.setPassword("foobared");
Jedis jedis = new Jedis(shardInfo);
jedis.get("foo");
}
}

View File

@@ -12,32 +12,16 @@ import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPipeline;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class PipeliningTest extends Assert {
private static String host = "localhost";
private static int port = Protocol.DEFAULT_PORT;
static {
final String envHost = System.getProperty("redis-host");
final String envPort = System.getProperty("redis-port");
if (null != envHost && 0 < envHost.length()) {
host = envHost;
}
if (null != envPort && 0 < envPort.length()) {
try {
port = Integer.parseInt(envPort);
} catch (final NumberFormatException e) {
}
}
System.out.println("Redis host to be used : " + host + ":" + port);
}
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private Jedis jedis;
@Before
public void setUp() throws Exception {
jedis = new Jedis(host, port, 500);
jedis = new Jedis(hnp.host, hnp.port, 500);
jedis.connect();
jedis.auth("foobared");
jedis.flushAll();

View File

@@ -0,0 +1,85 @@
package redis.clients.jedis.tests;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
import redis.clients.util.Hashing;
import redis.clients.util.ShardInfo;
public class ShardedJedisTest extends Assert {
private static HostAndPort redis1 = HostAndPortUtil.getRedisServers().get(0);
private static HostAndPort redis2 = HostAndPortUtil.getRedisServers().get(1);
@Test
public void checkSharding() throws IOException {
List<ShardInfo> shards = new ArrayList<ShardInfo>();
shards.add(new ShardInfo(redis1.host, redis1.port));
shards.add(new ShardInfo(redis2.host, redis2.port));
ShardedJedis jedis = new ShardedJedis(shards);
ShardInfo s1 = jedis.getShardInfo("a");
ShardInfo s2 = jedis.getShardInfo("b");
assertNotSame(s1, s2);
}
@Test
public void trySharding() throws IOException {
List<ShardInfo> shards = new ArrayList<ShardInfo>();
ShardInfo si = new ShardInfo(redis1.host, redis1.port);
si.setPassword("foobared");
shards.add(si);
si = new ShardInfo(redis2.host, redis2.port);
si.setPassword("foobared");
shards.add(si);
ShardedJedis jedis = new ShardedJedis(shards);
jedis.set("a", "bar");
ShardInfo s1 = jedis.getShardInfo("a");
jedis.set("b", "bar1");
ShardInfo s2 = jedis.getShardInfo("b");
jedis.disconnect();
Jedis j = new Jedis(s1.getHost(), s1.getPort());
j.auth("foobared");
assertEquals("bar", j.get("a"));
j.disconnect();
j = new Jedis(s2.getHost(), s2.getPort());
j.auth("foobared");
assertEquals("bar1", j.get("b"));
j.disconnect();
}
@Test
public void tryShardingWithMurmure() throws IOException {
List<ShardInfo> shards = new ArrayList<ShardInfo>();
ShardInfo si = new ShardInfo(redis1.host, redis1.port);
si.setPassword("foobared");
shards.add(si);
si = new ShardInfo(redis2.host, redis2.port);
si.setPassword("foobared");
shards.add(si);
ShardedJedis jedis = new ShardedJedis(shards, Hashing.MURMURE_HASH);
jedis.set("a", "bar");
ShardInfo s1 = jedis.getShardInfo("a");
jedis.set("b", "bar1");
ShardInfo s2 = jedis.getShardInfo("b");
jedis.disconnect();
Jedis j = new Jedis(s1.getHost(), s1.getPort());
j.auth("foobared");
assertEquals("bar", j.get("a"));
j.disconnect();
j = new Jedis(s2.getHost(), s2.getPort());
j.auth("foobared");
assertEquals("bar1", j.get("b"));
j.disconnect();
}
}

View File

@@ -5,13 +5,16 @@ import java.net.UnknownHostException;
import java.util.Calendar;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class GetSetBenchmark {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private static final int TOTAL_OPERATIONS = 100000;
public static void main(String[] args) throws UnknownHostException,
IOException {
Jedis jedis = new Jedis("localhost");
Jedis jedis = new Jedis(hnp.host, hnp.port);
jedis.connect();
jedis.auth("foobared");
jedis.flushAll();

View File

@@ -6,13 +6,16 @@ import java.util.Calendar;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPipeline;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class PipelinedGetSetBenchmark {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private static final int TOTAL_OPERATIONS = 200000;
public static void main(String[] args) throws UnknownHostException,
IOException {
Jedis jedis = new Jedis("localhost");
Jedis jedis = new Jedis(hnp.host, hnp.port);
jedis.connect();
jedis.auth("foobared");
jedis.flushAll();

View File

@@ -5,16 +5,20 @@ import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class PoolBenchmark {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private static final int TOTAL_OPERATIONS = 100000;
public static void main(String[] args) throws UnknownHostException,
IOException, TimeoutException, InterruptedException {
Jedis j = new Jedis("localhost");
Jedis j = new Jedis(hnp.host, hnp.port);
j.connect();
j.auth("foobared");
j.flushAll();
@@ -24,7 +28,7 @@ public class PoolBenchmark {
// withoutPool();
withPool();
long elapsed = System.currentTimeMillis() - t;
System.out.println(((1000 * 3 * TOTAL_OPERATIONS) / elapsed) + " ops");
System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
}
private static void withoutPool() throws InterruptedException {
@@ -35,7 +39,7 @@ public class PoolBenchmark {
Thread hj = new Thread(new Runnable() {
@Override
public void run() {
Jedis j = new Jedis("localhost");
Jedis j = new Jedis(hnp.host, hnp.port);
try {
j.connect();
j.auth("foobared");
@@ -59,31 +63,37 @@ public class PoolBenchmark {
}
private static void withPool() throws InterruptedException {
final JedisPool pool = new JedisPool("localhost");
pool.setResourcesNumber(1000);
pool.setDefaultPoolWait(20);
final JedisPool pool = new JedisPool(hnp.host, hnp.port,
2000, "foobared");
pool.setResourcesNumber(50);
pool.setDefaultPoolWait(1000000);
pool.init();
List<Thread> tds = new ArrayList<Thread>();
for (int i = 0; i < TOTAL_OPERATIONS; i++) {
final String key = "foo" + i;
final AtomicInteger ind = new AtomicInteger();
for (int i = 0; i < 50; i++) {
Thread hj = new Thread(new Runnable() {
@Override
public void run() {
try {
Jedis j = pool.getResource();
j.auth("foobared");
j.set(key, key);
j.get(key);
pool.returnResource(j);
} catch (Exception e) {
e.printStackTrace();
for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS;) {
try {
Jedis j = pool.getResource();
final String key = "foo" + i;
j.set(key, key);
j.get(key);
pool.returnResource(j);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
tds.add(hj);
hj.start();
}
for (Thread t : tds)
t.join();
pool.destroy();
}
}

View File

@@ -219,4 +219,20 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase {
jedis.select(1);
assertEquals(0, jedis.dbSize());
}
@Test
public void persist() {
jedis.setex("foo", 60 * 60, "bar");
assertTrue(jedis.ttl("foo") > 0);
int status = jedis.persist("foo");
assertEquals(1, status);
assertEquals(-1, jedis.ttl("foo"));
}
@Test
public void echo() {
String result = jedis.echo("hello world");
assertEquals("hello world", result);
}
}

View File

@@ -16,8 +16,13 @@ public class ControlCommandsTest extends JedisCommandTestBase {
@Test
public void bgsave() {
String status = jedis.bgsave();
assertEquals("Background saving started", status);
try {
String status = jedis.bgsave();
assertEquals("Background saving started", status);
} catch (JedisException e) {
assertEquals("ERR background save already in progress", e
.getMessage());
}
}
@Test
@@ -72,4 +77,10 @@ public class ControlCommandsTest extends JedisCommandTestBase {
assertEquals("OK", status);
jedis.configSet("maxmemory", memory);
}
@Test
public void sync() {
jedis.sync();
}
}

View File

@@ -9,26 +9,11 @@ import org.junit.After;
import org.junit.Before;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public abstract class JedisCommandTestBase extends Assert {
protected static String host = "localhost";
protected static int port = Protocol.DEFAULT_PORT;
static {
final String envHost = System.getProperty("redis-host");
final String envPort = System.getProperty("redis-port");
if (null != envHost && 0 < envHost.length()) {
host = envHost;
}
if (null != envPort && 0 < envPort.length()) {
try {
port = Integer.parseInt(envPort);
} catch (final NumberFormatException e) {
}
}
System.out.println("Redis host to be used : " + host + ":" + port);
}
protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
protected Jedis jedis;
@@ -38,7 +23,7 @@ public abstract class JedisCommandTestBase extends Assert {
@Before
public void setUp() throws Exception {
jedis = new Jedis(host, port, 500);
jedis = new Jedis(hnp.host, hnp.port, 500);
jedis.connect();
jedis.auth("foobared");
jedis.flushAll();
@@ -50,7 +35,7 @@ public abstract class JedisCommandTestBase extends Assert {
}
protected Jedis createJedis() throws UnknownHostException, IOException {
Jedis j = new Jedis(host, port);
Jedis j = new Jedis(hnp.host, hnp.port);
j.connect();
j.auth("foobared");
j.flushAll();

View File

@@ -249,6 +249,25 @@ public class ListCommandsTest extends JedisCommandTestBase {
assertEquals(2, result.size());
assertEquals("foo", result.get(0));
assertEquals("bar", result.get(1));
}
@Test
public void lpushx() {
int status = jedis.lpushx("foo", "bar");
assertEquals(0, status);
jedis.lpush("foo", "a");
status = jedis.lpushx("foo", "b");
assertEquals(2, status);
}
@Test
public void rpushx() {
int status = jedis.rpushx("foo", "bar");
assertEquals(0, status);
jedis.lpush("foo", "a");
status = jedis.rpushx("foo", "b");
assertEquals(2, status);
}
}

View File

@@ -169,4 +169,10 @@ public class StringValuesCommandsTest extends JedisCommandTestBase {
assertEquals("This is a string", jedis.substr("s", 0, -1));
assertEquals(" string", jedis.substr("s", 9, 100000));
}
@Test
public void strlen() {
jedis.set("s", "This is a string");
assertEquals("This is a string".length(), jedis.strlen("s"));
}
}

View File

@@ -19,7 +19,7 @@ public class TransactionCommandsTest extends JedisCommandTestBase {
public void setUp() throws Exception {
super.setUp();
nj = new Jedis(host, port, 500);
nj = new Jedis(hnp.host, hnp.port, 500);
nj.connect();
nj.auth("foobared");
nj.flushAll();