Repository: incubator-geode Updated Branches: refs/heads/develop d0d48588c -> 1a6a0ef53
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/1a6a0ef5/gemfire-core/src/test/java/com/gemstone/gemfire/redis/SortedSetsJUnitTest.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/redis/SortedSetsJUnitTest.java b/gemfire-core/src/test/java/com/gemstone/gemfire/redis/SortedSetsJUnitTest.java new file mode 100755 index 0000000..4adfe07 --- /dev/null +++ b/gemfire-core/src/test/java/com/gemstone/gemfire/redis/SortedSetsJUnitTest.java @@ -0,0 +1,414 @@ +package com.gemstone.gemfire.redis; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Random; +import java.util.Set; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.Tuple; + +import com.gemstone.gemfire.cache.CacheFactory; +import com.gemstone.gemfire.cache.GemFireCache; +import com.gemstone.gemfire.internal.AvailablePortHelper; +import com.gemstone.gemfire.test.junit.categories.IntegrationTest; + +@Category(IntegrationTest.class) +public class SortedSetsJUnitTest { + private static Jedis jedis; + private static GemFireRedisServer server; + private static GemFireCache cache; + private static Random rand; + private static int port = 6379; + + @BeforeClass + public static void setUp() throws IOException { + rand = new Random(); + CacheFactory cf = new CacheFactory(); + //cf.set("log-file", "redis.log"); + cf.set("log-level", "error"); + cf.set("mcast-port", "0"); + cf.set("locators", ""); + cache = cf.create(); + port = AvailablePortHelper.getRandomAvailableTCPPort(); + server = new GemFireRedisServer("localhost", port); + + server.start(); + jedis = new Jedis("localhost", port, 10000000); + } + + @Test + public void testZAddZRange() { + int numMembers = 10; + String key = randString(); + Map<String, Double> scoreMembers = new HashMap<String, Double>(); + + for (int i = 0; i < numMembers; i++) + scoreMembers.put(randString(), rand.nextDouble()); + + jedis.zadd(key, scoreMembers); + int k = 0; + for (String entry: scoreMembers.keySet()) + assertNotNull(jedis.zscore(key, entry)); + + Set<Tuple> results = jedis.zrangeWithScores(key, 0, -1); + Map<String, Double> resultMap = new HashMap<String, Double>(); + for (Tuple t: results) { + resultMap.put(t.getElement(), t.getScore()); + } + + assertEquals(scoreMembers, resultMap); + + for (int i = 0; i < 10; i++) { + int start; + int stop; + do { + start = rand.nextInt(numMembers); + stop = rand.nextInt(numMembers); + } while (start > stop); + results = jedis.zrangeWithScores(key, start, stop); + List<Entry<String, Double>> resultList = new ArrayList<Entry<String, Double>>(); + for (Tuple t: results) + resultList.add(new AbstractMap.SimpleEntry<String, Double>(t.getElement(), t.getScore())); + List<Entry<String, Double>> list = new ArrayList<Entry<String, Double>>(scoreMembers.entrySet()); + Collections.sort(list, new EntryCmp()); + list = list.subList(start, stop + 1); + assertEquals(list, resultList); + } + } + + @Test + public void testZRevRange() { + int numMembers = 10; + String key = randString(); + + Map<String, Double> scoreMembers = new HashMap<String, Double>(); + + for (int i = 0; i < numMembers; i++) + scoreMembers.put(randString(), rand.nextDouble()); + + jedis.zadd(key, scoreMembers); + + Set<Tuple> results; + + for (int i = 0; i < 10; i++) { + int start; + int stop; + do { + start = rand.nextInt(numMembers); + stop = rand.nextInt(numMembers); + } while (start > stop); + results = jedis.zrevrangeWithScores(key, start, stop); + List<Entry<String, Double>> resultList = new ArrayList<Entry<String, Double>>(); + for (Tuple t: results) + resultList.add(new AbstractMap.SimpleEntry<String, Double>(t.getElement(), t.getScore())); + List<Entry<String, Double>> list = new ArrayList<Entry<String, Double>>(scoreMembers.entrySet()); + Collections.sort(list, new EntryRevCmp()); + list = list.subList(start, stop + 1); + assertEquals(list, resultList); + } + } + + @Test + public void testZCount() { + int num = 10; + int runs = 2; + for (int i = 0; i < runs; i++) { + Double min; + Double max; + do { + min = rand.nextDouble(); + max = rand.nextDouble(); + } while (min > max); + + + int count = 0; + + String key = randString(); + Map<String, Double> scoreMembers = new HashMap<String, Double>(); + + for (int j = 0; j < num; j++) { + Double nextDouble = rand.nextDouble(); + if (nextDouble >= min && nextDouble <= max) + count++; + scoreMembers.put(randString(), nextDouble); + } + + jedis.zadd(key, scoreMembers); + Long countResult = jedis.zcount(key, min, max); + assertTrue(count == countResult); + } + + } + + @Test + public void testZIncrBy() { + String key = randString(); + String member = randString(); + Double score = 0.0; + for (int i = 0; i < 20; i++) { + Double incr = rand.nextDouble(); + Double result = jedis.zincrby(key, incr, member); + score += incr; + assertEquals(score, result, 1.0/100000000.0); + } + + + jedis.zincrby(key, Double.MAX_VALUE, member); + Double infResult = jedis.zincrby(key, Double.MAX_VALUE, member); + + + assertEquals(infResult, Double.valueOf(Double.POSITIVE_INFINITY)); + } + + public void testZRangeByScore() { + Double min; + Double max; + for (int j = 0; j < 2; j++) { + do { + min = rand.nextDouble(); + max = rand.nextDouble(); + } while (min > max); + int numMembers = 500; + String key = randString(); + Map<String, Double> scoreMembers = new HashMap<String, Double>(); + List<Entry<String, Double>> expected = new ArrayList<Entry<String, Double>>(); + for (int i = 0; i < numMembers; i++) { + String s = randString(); + Double d = rand.nextDouble(); + scoreMembers.put(s, d); + if (d > min && d < max) + expected.add(new AbstractMap.SimpleEntry<String, Double>(s, d)); + } + jedis.zadd(key, scoreMembers); + Set<Tuple> results = jedis.zrangeByScoreWithScores(key, min, max); + List<Entry<String, Double>> resultList = new ArrayList<Entry<String, Double>>(); + for (Tuple t: results) + resultList.add(new AbstractMap.SimpleEntry<String, Double>(t.getElement(), t.getScore())); + Collections.sort(expected, new EntryCmp()); + + assertEquals(expected, resultList); + jedis.del(key); + } + } + + public void testZRevRangeByScore() { + Double min; + Double max; + for (int j = 0; j < 2; j++) { + do { + min = rand.nextDouble(); + max = rand.nextDouble(); + } while (min > max); + int numMembers = 500; + String key = randString(); + Map<String, Double> scoreMembers = new HashMap<String, Double>(); + List<Entry<String, Double>> expected = new ArrayList<Entry<String, Double>>(); + for (int i = 0; i < numMembers; i++) { + String s = randString(); + Double d = rand.nextDouble(); + scoreMembers.put(s, d); + if (d > min && d < max) + expected.add(new AbstractMap.SimpleEntry<String, Double>(s, d)); + } + jedis.zadd(key, scoreMembers); + Set<Tuple> results = jedis.zrevrangeByScoreWithScores(key, max, min); + List<Entry<String, Double>> resultList = new ArrayList<Entry<String, Double>>(); + for (Tuple t: results) + resultList.add(new AbstractMap.SimpleEntry<String, Double>(t.getElement(), t.getScore())); + Collections.sort(expected, new EntryRevCmp()); + + assertEquals(expected, resultList); + jedis.del(key); + } + } + + @Test + public void testZRemZScore() { + Double min; + Double max; + for (int j = 0; j < 2; j++) { + do { + min = rand.nextDouble(); + max = rand.nextDouble(); + } while (min > max); + int numMembers = 5000; + String key = randString(); + Map<String, Double> scoreMembers = new HashMap<String, Double>(); + List<Entry<String, Double>> expected = new ArrayList<Entry<String, Double>>(); + for (int i = 0; i < numMembers; i++) { + String s = randString(); + Double d = rand.nextDouble(); + scoreMembers.put(s, d); + if (d > min && d < max) + expected.add(new AbstractMap.SimpleEntry<String, Double>(s, d)); + } + jedis.zadd(key, scoreMembers); + Collections.sort(expected, new EntryCmp()); + for (int i = expected.size(); i <= 0; i--) { + Entry<String, Double> remEntry = expected.remove(i); + String rem = remEntry.getKey(); + Double val = remEntry.getValue(); + assertEquals(val, jedis.zscore(key, rem)); + + assertTrue(jedis.zrem(key, rem) == 1); + } + String s = randString(); + if (!expected.contains(s)) + assertTrue(jedis.zrem(key, s) == 0); + jedis.del(key); + } + } + + @Test + public void testZRank() { + for (int j = 0; j < 2; j++) { + int numMembers = 10; + String key = randString(); + Map<String, Double> scoreMembers = new HashMap<String, Double>(); + List<Entry<String, Double>> expected = new ArrayList<Entry<String, Double>>(); + for (int i = 0; i < numMembers; i++) { + String s = randString(); + Double d = rand.nextDouble(); + scoreMembers.put(s, d); + expected.add(new AbstractMap.SimpleEntry<String, Double>(s, d)); + } + Collections.sort(expected, new EntryCmp()); + jedis.zadd(key, scoreMembers); + for (int i = 0; i < expected.size(); i++) { + Entry<String, Double> en = expected.get(i); + String field = en.getKey(); + Long rank = jedis.zrank(key, field); + assertEquals(new Long(i), rank); + } + String field = randString(); + if (!expected.contains(field)) + assertNull(jedis.zrank(key, field)); + jedis.del(key); + } + } + + @Test + public void testZRevRank() { + for (int j = 0; j < 2; j++) { + int numMembers = 10; + String key = randString(); + Map<String, Double> scoreMembers = new HashMap<String, Double>(); + List<Entry<String, Double>> expected = new ArrayList<Entry<String, Double>>(); + for (int i = 0; i < numMembers; i++) { + String s = randString(); + Double d = rand.nextDouble(); + scoreMembers.put(s, d); + expected.add(new AbstractMap.SimpleEntry<String, Double>(s, d)); + } + Collections.sort(expected, new EntryRevCmp()); + jedis.zadd(key, scoreMembers); + for (int i = 0; i < expected.size(); i++) { + Entry<String, Double> en = expected.get(i); + String field = en.getKey(); + Long rank = jedis.zrevrank(key, field); + assertEquals(new Long(i), rank); + } + String field = randString(); + if (!expected.contains(field)) + assertNull(jedis.zrank(key, field)); + jedis.del(key); + } + } + + private class EntryCmp implements Comparator<Entry<String, Double>> { + + @Override + public int compare(Entry<String, Double> o1, Entry<String, Double> o2) { + Double diff = o1.getValue() - o2.getValue(); + if (diff == 0) + return o2.getKey().compareTo(o1.getKey()); + else + return diff > 0 ? 1 : -1; + } + + } + + private class EntryRevCmp implements Comparator<Entry<String, Double>> { + + @Override + public int compare(Entry<String, Double> o1, Entry<String, Double> o2) { + Double diff = o2.getValue() - o1.getValue(); + if (diff == 0) + return o1.getKey().compareTo(o2.getKey()); + else + return diff > 0 ? 1 : -1; + } + + } + + @Test + public void testZRemRangeByScore() { + Double min; + Double max; + for (int j = 0; j < 3; j++) { + do { + min = rand.nextDouble(); + max = rand.nextDouble(); + } while (min > max); + int numMembers = 10; + String key = randString(); + Map<String, Double> scoreMembers = new HashMap<String, Double>(); + List<Entry<String, Double>> fullList = new ArrayList<Entry<String, Double>>(); + List<Entry<String, Double>> toRemoveList = new ArrayList<Entry<String, Double>>(); + for (int i = 0; i < numMembers; i++) { + String s = randString(); + Double d = rand.nextDouble(); + scoreMembers.put(s, d); + fullList.add(new AbstractMap.SimpleEntry<String, Double>(s, d)); + if (d > min && d < max) + toRemoveList.add(new AbstractMap.SimpleEntry<String, Double>(s, d)); + } + jedis.zadd(key, scoreMembers); + Long numRemoved = jedis.zremrangeByScore(key, min, max); + List<Entry<String, Double>> expectedList = new ArrayList<Entry<String, Double>>(fullList); + expectedList.removeAll(toRemoveList); + Collections.sort(expectedList, new EntryCmp()); + Set<Tuple> result = jedis.zrangeWithScores(key, 0, -1); + List<Entry<String, Double>> resultList = new ArrayList<Entry<String, Double>>(); + for (Tuple t: result) + resultList.add(new AbstractMap.SimpleEntry<String, Double>(t.getElement(), t.getScore())); + assertEquals(expectedList, resultList); + jedis.del(key); + } + } + + private String randString() { + return Long.toHexString(Double.doubleToLongBits(Math.random())); + } + + @After + public void flushAll() { + jedis.flushAll(); + } + + @AfterClass + public static void tearDown() { + jedis.close(); + cache.close(); + server.shutdown(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/1a6a0ef5/gemfire-core/src/test/java/com/gemstone/gemfire/redis/StringsJunitTest.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/redis/StringsJunitTest.java b/gemfire-core/src/test/java/com/gemstone/gemfire/redis/StringsJunitTest.java new file mode 100755 index 0000000..55ba061 --- /dev/null +++ b/gemfire-core/src/test/java/com/gemstone/gemfire/redis/StringsJunitTest.java @@ -0,0 +1,296 @@ +package com.gemstone.gemfire.redis; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import redis.clients.jedis.Jedis; + +import com.gemstone.gemfire.cache.CacheFactory; +import com.gemstone.gemfire.cache.GemFireCache; +import com.gemstone.gemfire.internal.AvailablePortHelper; +import com.gemstone.gemfire.test.junit.categories.IntegrationTest; + +@Category(IntegrationTest.class) +public class StringsJunitTest { + + private static Jedis jedis; + private static GemFireRedisServer server; + private static GemFireCache cache; + private static Random rand; + private static int port = 6379; + + @BeforeClass + public static void setUp() throws IOException { + rand = new Random(); + CacheFactory cf = new CacheFactory(); + //cf.set("log-file", "redis.log"); + cf.set("log-level", "error"); + cf.set("mcast-port", "0"); + cf.set("locators", ""); + cache = cf.create(); + port = AvailablePortHelper.getRandomAvailableTCPPort(); + server = new GemFireRedisServer("localhost", port); + + server.start(); + jedis = new Jedis("localhost", port, 10000000); + } + + @Test + public void testAppendAndStrlen() { + String key = randString(); + int len = key.length(); + String full = key; + jedis.set(key, key); + for (int i = 0; i < 15; i++) { + String rand = randString(); + jedis.append(key, rand); + len += rand.length(); + full += rand; + } + String ret = jedis.get(key); + assertTrue(ret.length() == len); + assertTrue(full.equals(ret)); + assertTrue(full.length() == jedis.strlen(key)); + } + + @Test + public void testDecr() { + String key1 = randString(); + String key2 = randString(); + String key3 = randString(); + int num1 = 100; + int num2 = -100; + jedis.set(key1, ""+num1); + //jedis.set(key3, "-100"); + jedis.set(key2, ""+num2); + + jedis.decr(key1); + jedis.decr(key3); + jedis.decr(key2); + assertTrue(jedis.get(key1).equals("" + (num1 - 1))); + assertTrue(jedis.get(key2).equals("" + (num2 - 1))); + assertTrue(jedis.get(key3).equals("" + (-1))); + } + + @Test + public void testIncr() { + String key1 = randString(); + String key2 = randString(); + String key3 = randString(); + int num1 = 100; + int num2 = -100; + jedis.set(key1, ""+num1); + //jedis.set(key3, "-100"); + jedis.set(key2, ""+num2); + + jedis.incr(key1); + jedis.incr(key3); + jedis.incr(key2); + + assertTrue(jedis.get(key1).equals("" + (num1 + 1))); + assertTrue(jedis.get(key2).equals("" + (num2 + 1))); + assertTrue(jedis.get(key3).equals("" + (+1))); + } + + @Test + public void testDecrBy() { + String key1 = randString(); + String key2 = randString(); + String key3 = randString(); + int decr1 = rand.nextInt(100); + int decr2 = rand.nextInt(100); + Long decr3 = Long.MAX_VALUE/2; + int num1 = 100; + int num2 = -100; + jedis.set(key1, ""+num1); + jedis.set(key2, ""+num2); + jedis.set(key3, ""+Long.MIN_VALUE); + + jedis.decrBy(key1, decr1); + jedis.decrBy(key2, decr2); + + assertTrue(jedis.get(key1).equals("" + (num1 - decr1*1))); + assertTrue(jedis.get(key2).equals("" + (num2 - decr2*1))); + + Exception ex= null; + try { + jedis.decrBy(key3, decr3); + } catch(Exception e) { + ex = e; + } + assertNotNull(ex); + + } + + @Test + public void testIncrBy() { + String key1 = randString(); + String key2 = randString(); + String key3 = randString(); + int incr1 = rand.nextInt(100); + int incr2 = rand.nextInt(100); + Long incr3 = Long.MAX_VALUE/2; + int num1 = 100; + int num2 = -100; + jedis.set(key1, ""+num1); + jedis.set(key2, ""+num2); + jedis.set(key3, ""+Long.MAX_VALUE); + + jedis.incrBy(key1, incr1); + jedis.incrBy(key2, incr2); + assertTrue(jedis.get(key1).equals("" + (num1 + incr1*1))); + assertTrue(jedis.get(key2).equals("" + (num2 + incr2*1))); + + Exception ex= null; + try { + jedis.incrBy(key3, incr3); + } catch(Exception e) { + ex = e; + } + assertNotNull(ex); + } + + @Test + public void testGetRange() { + String sent = randString(); + String contents = randString(); + jedis.set(sent, contents); + for (int i = 0; i < sent.length(); i++) { + String range = jedis.getrange(sent, i, -1); + assertTrue(contents.substring(i).equals(range)); + } + assertNull(jedis.getrange(sent, 2,0)); + } + + @Test + public void testGetSet() { + String key = randString(); + String contents = randString(); + jedis.set(key, contents); + String newContents = randString(); + String oldContents = jedis.getSet(key, newContents); + assertTrue(oldContents.equals(contents)); + contents = newContents; + } + + @Test + public void testMSetAndGet() { + int r = 5; + String[] keyvals = new String[(r*2)]; + String[] keys = new String[r]; + String[] vals = new String[r]; + for(int i = 0; i < r; i++) { + String key = randString(); + String val = randString(); + keyvals[2*i] = key; + keyvals[2*i+1] = val; + keys[i] = key; + vals[i] = val; + } + + jedis.mset(keyvals); + + List<String> ret = jedis.mget(keys); + Object[] retArray = ret.toArray(); + + assertTrue(Arrays.equals(vals, retArray)); + } + + @Test + public void testMSetNX() { + Set<String> strings = new HashSet<String>(); + for(int i = 0; i < 2 * 5; i++) + strings.add(randString()); + String[] array = strings.toArray(new String[0]); + long response = jedis.msetnx(array); + + assertTrue(response == 1); + + long response2 = jedis.msetnx(array[0], randString()); + + assertTrue(response2 == 0); + assertEquals(array[1], jedis.get(array[0])); + } + + @Test + public void testSetNX() { + String key1 = randString(); + String key2; + do { + key2 = randString(); + } while (key2.equals(key1)); + + long response1 = jedis.setnx(key1, key1); + long response2 = jedis.setnx(key2, key2); + long response3 = jedis.setnx(key1, key2); + + assertTrue(response1 == 1); + assertTrue(response2 == 1); + assertTrue(response3 == 0); + } + + @Test + public void testPAndSetex() { + Random r = new Random(); + int setex = r.nextInt(5); + if (setex == 0) + setex = 1; + String key = randString(); + jedis.setex(key, setex, randString()); + try { + Thread.sleep((setex + 5) * 1000); + } catch (InterruptedException e) { + return; + } + String result = jedis.get(key); + //System.out.println(result); + assertNull(result); + + int psetex = r.nextInt(5000); + if (psetex == 0) + psetex = 1; + key = randString(); + jedis.psetex(key, psetex, randString()); + long start = System.currentTimeMillis(); + try { + Thread.sleep(psetex + 5000); + } catch (InterruptedException e) { + return; + } + long stop = System.currentTimeMillis(); + result = jedis.get(key); + assertTrue(stop - start >= psetex); + assertNull(result); + } + + private String randString() { + return Long.toHexString(Double.doubleToLongBits(Math.random())); + } + + @After + public void flushAll() { + jedis.flushAll(); + } + + @AfterClass + public static void tearDown() { + jedis.close(); + cache.close(); + server.shutdown(); + } +}