Permalink
Browse files

Add first test for Redis endpoint: set and get

  • Loading branch information...
justinsb committed Dec 13, 2013
1 parent b1aa313 commit 807d988f64e313d85812e32802209926b05dff2d
@@ -23,6 +23,13 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.2.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -121,4 +121,8 @@ public synchronized void stop() {
}
}
public SocketAddress getRedisSocketAddress() {
return redisSocketAddress;
}
}
@@ -14,8 +14,6 @@
import java.net.SocketAddress;
;
public class RedisEndpoint {
DefaultEventExecutorGroup group;
@@ -60,13 +58,13 @@ protected void initChannel(SocketChannel ch) throws Exception {
// Start the server.
ChannelFuture f = b.bind().sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
// return f;
}
public void stop() {
group.shutdownGracefully();
// Wait until the server socket is closed.
// f.channel().closeFuture().sync();
}
}
@@ -0,0 +1,81 @@
package com.cloudata.keyvalue;
import java.io.File;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.robotninjas.barge.Replica;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
public class IntegrationTestBase {
protected static KeyValueServer[] SERVERS;
static File TEMPDIR;
@BeforeClass
public static void startServers() throws Exception {
TEMPDIR = Files.createTempDir();
SERVERS = new KeyValueServer[3];
for (int i = 0; i < SERVERS.length; i++) {
Replica local = Replica.fromString("localhost:" + (10000 + i));
List<Replica> members = Lists.newArrayList();
for (int j = 0; j < SERVERS.length; j++) {
members.add(Replica.fromString("localhost:" + (10000 + j)));
}
members.remove(local);
int httpPort = 9990 + i;
File baseDir = new File(TEMPDIR, "" + i);
SocketAddress redisAddress = new InetSocketAddress(6379 + i);
SERVERS[i] = new KeyValueServer(baseDir, local, members, httpPort, redisAddress);
SERVERS[i].start();
}
// TODO: Remove the need for a sleep here
Thread.sleep(1000);
}
@AfterClass
public static void stopServers() {
for (int i = 0; i < SERVERS.length; i++) {
SERVERS[i].stop();
}
rmdir(TEMPDIR);
}
private static void rmdir(File dir) {
for (File f : dir.listFiles()) {
if (f.isFile()) {
f.delete();
} else {
rmdir(f);
}
}
dir.delete();
}
static long nextLogId = 1;
protected long newLogId() {
return nextLogId++;
}
protected static byte[] buildValue(int length) {
byte[] data = new byte[length];
for (int j = 0; j < length; j++) {
data[j] = (byte) (j % 0xff);
}
return data;
}
}
@@ -1,72 +1,12 @@
package com.cloudata.keyvalue;
import java.io.File;
import java.net.SocketAddress;
import java.util.List;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.robotninjas.barge.Replica;
import com.cloudata.keyvalue.KeyValueClient.KeyValueEntry;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.protobuf.ByteString;
public class KeyValueIntegrationTest {
static KeyValueServer[] SERVERS;
static File TEMPDIR;
@BeforeClass
public static void startServers() throws Exception {
TEMPDIR = Files.createTempDir();
SERVERS = new KeyValueServer[3];
for (int i = 0; i < SERVERS.length; i++) {
Replica local = Replica.fromString("localhost:" + (10000 + i));
List<Replica> members = Lists.newArrayList();
for (int j = 0; j < SERVERS.length; j++) {
members.add(Replica.fromString("localhost:" + (10000 + j)));
}
members.remove(local);
int httpPort = 9990 + i;
File baseDir = new File(TEMPDIR, "" + i);
SocketAddress redisAddress = null;
SERVERS[i] = new KeyValueServer(baseDir, local, members, httpPort, redisAddress);
SERVERS[i].start();
}
// TODO: Remove the need for a sleep here
Thread.sleep(1000);
}
@AfterClass
public static void stopServers() {
for (int i = 0; i < SERVERS.length; i++) {
SERVERS[i].stop();
}
rmdir(TEMPDIR);
}
private static void rmdir(File dir) {
for (File f : dir.listFiles()) {
if (f.isFile()) {
f.delete();
} else {
rmdir(f);
}
}
dir.delete();
}
public class KeyValueIntegrationTest extends IntegrationTestBase {
@Test
public void testSetAndGet() throws Exception {
@@ -281,17 +221,4 @@ public void testDelete() throws Exception {
}
}
static long nextLogId = 1;
private long newLogId() {
return nextLogId++;
}
private byte[] buildValue(int i) {
byte[] data = new byte[i];
for (int j = 0; j < i; j++) {
data[j] = (byte) (j % 0xff);
}
return data;
}
}
@@ -0,0 +1,30 @@
package com.cloudata.keyvalue;
import java.net.InetSocketAddress;
import org.junit.Assert;
import org.junit.Test;
import redis.clients.jedis.Jedis;
public class RedisIntegrationTest extends IntegrationTestBase {
@Test
public void testSetAndGet() throws Exception {
InetSocketAddress redisSocketAddress = (InetSocketAddress) SERVERS[0].getRedisSocketAddress();
Jedis jedis = new Jedis(redisSocketAddress.getHostName(), redisSocketAddress.getPort());
for (int i = 1; i < 100; i++) {
byte[] key = Integer.toString(i).getBytes();
byte[] value = buildValue(i);
jedis.set(key, value);
byte[] actual = jedis.get(key);
Assert.assertArrayEquals(value, actual);
}
}
}

0 comments on commit 807d988

Please sign in to comment.