コード例 #1
0
  public void testDumpAndRestore()
      throws ExecutionException, InterruptedException, TimeoutException {
    String key = "DUMP_KEY";
    client.delete(key).get();
    client.set(key, "OK").get();
    byte[] dump = client.dump(key).get(1, TimeUnit.SECONDS);

    client.delete(key).get();
    String reply = client.restore(key, 0, dump).get();
    assertEquals("OK", reply);
    reply = (String) client.get(key).get();
    assertEquals("OK", reply);
  }
コード例 #2
0
  public void testMsetnx() throws ExecutionException, InterruptedException {
    Map<String, String> map = new HashMap<String, String>();
    map.put("MAPNX_KEY1", "TES");
    map.put("MAPNX_KEY2", "EST");

    client.delete("MAPNX_KEY1", "MAPNX_KEY2");
    long reply = client.msetObjectNx(map).get();
    assertEquals(1, reply);

    client.delete("MAPNX_KEY1");

    reply = client.msetObjectNx(map).get();
    assertEquals(0, reply);
  }
コード例 #3
0
  public void testMultClient() throws ExecutionException, InterruptedException {
    NettyRedisClient client2 = new NettyRedisClient("127.0.0.1:6379", 0, null);
    String key = "MIGRATE_KEY";
    client.delete(key).get();
    client2.delete(key).get();
    client.set(key, "OK").get();
    client2.set(key, "OK").get();
    //        String reply = client.migrate("127.0.0.1", 6379, key, 0, 1000).get();
    //        assertEquals("OK", reply);
    long existed = client.exists(key).get();
    assertEquals(1, existed);

    String reply = (String) client2.get(key).get();
    assertEquals("OK", reply);
  }
コード例 #4
0
  public void testHvals() throws ExecutionException, InterruptedException {
    String key = "HASH_HVALS_KEY2";
    client.delete(key);
    String field1 = "TEST_FIELD1";
    String field2 = "TEST_FIELD2";
    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put(field1, 1);
    map.put(field2, 2);
    String reply = client.hmsetInt(key, map).get();
    assertEquals("OK", reply);
    List<Integer> cached = client.hvalsInt(key).get();
    assertEquals(2, cached.size());
    //        assertEquals(1, cached.get(0).intValue());
    //        assertEquals(2, cached.get(1).intValue());

    Map<String, String> map2 = new HashMap<String, String>();
    map2.put(field1, "OK");
    map2.put(field2, "KO");
    reply = client.hmset(key, map2).get();
    assertEquals("OK", reply);
    List<String> cachedStr = (List<String>) client.hvals(key).get();
    assertEquals(2, cached.size());
    //        assertEquals("OK", cachedStr.get(0));
    //        assertEquals("KO", cachedStr.get(1));

  }
コード例 #5
0
 public void testZadd() throws ExecutionException, InterruptedException {
   String key = "ZSET_ADD_KEY1";
   client.delete(key).get();
   long reply = client.zadd(key, 1, "a").get();
   assertEquals(1, reply);
   reply = client.zadd(key, 2, "b", new ZEntity<String>("c", 3)).get();
   assertEquals(2, reply);
   reply = client.zcard(key).get();
   assertEquals(3, reply);
   reply = client.zcount(key, ZNumbers.MINIMUM, ZNumbers.MAXIMUM).get();
   assertEquals(3, reply);
   reply = client.zcount(key, ZNumbers.includeNumber(1), ZNumbers.MAXIMUM).get();
   assertEquals(3, reply);
   reply = client.zcount(key, ZNumbers.excludeNumber(1), ZNumbers.MAXIMUM).get();
   assertEquals(2, reply);
   List<String> cached = (List<String>) client.zrange(key, 0, 1).get();
   assertEquals(2, cached.size());
   assertEquals("a", cached.get(0));
   assertEquals("b", cached.get(1));
   List<ZEntity<?>> entitys = client.zrangeWithScores(key, 0, 1).get();
   assertEquals(2, entitys.size());
   assertEquals("a", entitys.get(0).member);
   assertEquals("b", entitys.get(1).member);
   assertEquals(1.0, entitys.get(0).score);
   assertEquals(2.0, entitys.get(1).score);
 }
コード例 #6
0
 public void testZinterAndUnion() throws ExecutionException, InterruptedException {
   String key1 = "ZSET1";
   String key2 = "ZSET2";
   client.delete(key1, key2).get();
   client.zadd(key1, 1, "one");
   client.zadd(key1, 2, "two");
   client.zadd(key2, 1, "one");
   client.zadd(key2, 2, "two");
   client.zadd(key2, 3, "three");
   long reply = client.zinterStore("out", new String[] {key1, key2}, new int[] {2, 3}, null).get();
   assertEquals(2, reply);
   List<ZEntity<?>> entitys = client.zrangeWithScores("out", 0, -1).get();
   assertEquals(2, entitys.size());
   assertEquals("one", entitys.get(0).member);
   assertEquals("two", entitys.get(1).member);
   assertEquals(5.0, entitys.get(0).score);
   assertEquals(10.0, entitys.get(1).score);
   reply = client.zunionStore("out", new String[] {key1, key2}, new int[] {2, 3}, null).get();
   assertEquals(3, reply);
   entitys = client.zrangeWithScores("out", 0, -1).get();
   assertEquals(3, entitys.size());
   assertEquals("one", entitys.get(0).member);
   assertEquals("three", entitys.get(1).member);
   assertEquals("two", entitys.get(2).member);
   assertEquals(5.0, entitys.get(0).score);
   assertEquals(9.0, entitys.get(1).score);
   assertEquals(10.0, entitys.get(2).score);
 }
コード例 #7
0
 public void testLlen() throws ExecutionException, InterruptedException {
   String key = "LIST_LEN_KEY2";
   client.delete(key);
   long reply = client.lpush(key, "KO", "OK").get();
   assertEquals(2, reply);
   reply = client.llen(key).get();
   assertEquals(2, reply);
 }
コード例 #8
0
 public void testExistsAndDel() throws ExecutionException, InterruptedException {
   String key1 = "DEL_KEY1";
   String key2 = "DEL_KEY2";
   client.delete(key1, key2);
   client.set(key1, 1);
   client.set(key2, 2);
   long reply = client.exists(key1).get();
   assertEquals(1, reply);
   reply = client.exists(key2).get();
   assertEquals(1, reply);
   long number = client.delete(key1, key2).get();
   assertEquals(2, number);
   reply = client.exists(key1).get();
   assertEquals(0, reply);
   reply = client.exists(key2).get();
   assertEquals(0, reply);
 }
コード例 #9
0
 public void testSisMember() throws ExecutionException, InterruptedException {
   String key = "SET_ADD_KEY2";
   client.delete(key).get();
   long reply = client.sadd(key, "OK", "KO").get();
   assertEquals(2, reply);
   reply = client.sisMember(key, "OK").get();
   assertEquals(1, reply);
   reply = client.sisMember(key, "OKO").get();
   assertEquals(0, reply);
 }
コード例 #10
0
 public void testZIncrBy() throws ExecutionException, InterruptedException {
   String key = "ZSET_ADD_KEY2";
   client.delete(key).get();
   long reply = client.zadd(key, 1, "a").get();
   assertEquals(1, reply);
   double score = client.zincrBy(key, 1, "a").get();
   assertEquals(2.0, score);
   score = client.zscore(key, "a").get();
   assertEquals(2.0, score);
 }
コード例 #11
0
 public void testZrank() throws ExecutionException, InterruptedException {
   String key = "ZSET";
   client.delete(key).get();
   client.zadd(key, 1, "one").get();
   client.zadd(key, 2, "two").get();
   client.zadd(key, 3, "three").get();
   long rank = client.zrank(key, "three").get();
   assertEquals(2, rank);
   rank = client.zrevRank(key, "three").get();
   assertEquals(0, rank);
 }
コード例 #12
0
 public void testSaddAndCardAndMembers() throws ExecutionException, InterruptedException {
   String key = "SET_ADD_KEY";
   client.delete(key).get();
   long reply = client.sadd(key, "OK", "KO").get();
   assertEquals(2, reply);
   reply = client.scard(key).get();
   assertEquals(2, reply);
   List<String> cached = (List<String>) client.smembers(key).get();
   assertEquals(2, cached.size());
   assertEquals("OK", cached.get(0));
   assertEquals("KO", cached.get(1));
 }
コード例 #13
0
  public void testSetNx() throws ExecutionException, InterruptedException {
    String key1 = "NX_KEY1";
    client.delete(key1);
    long reply = client.setNx(key1, "sss").get();
    assertEquals(1, reply);

    reply = client.setNx(key1, 2).get();
    assertEquals(0, reply);

    String cached = (String) client.get(key1).get();
    assertEquals("sss", cached);
  }
コード例 #14
0
 public void testRename() throws ExecutionException, InterruptedException {
   String key1 = "SOURCE_KEY";
   String key2 = "DEST_KEY";
   client.delete(key1, key2).get();
   client.set(key1, "OK").get();
   String reply = client.rename(key1, key2).get();
   assertEquals("OK", reply);
   long existed = client.exists(key1).get();
   assertEquals(0, existed);
   String cached = (String) client.get(key2).get();
   assertEquals("OK", cached);
   client.set(key1, "OK").get();
   long result = client.renameNx(key1, key2).get();
   assertEquals(0, result);
   client.delete(key2).get();
   result = client.renameNx(key1, key2).get();
   assertEquals(1, result);
   existed = client.exists(key1).get();
   assertEquals(0, existed);
   cached = (String) client.get(key2).get();
   assertEquals("OK", cached);
 }
コード例 #15
0
 public void testZrem() throws ExecutionException, InterruptedException {
   String key = "ZSET";
   client.delete(key).get();
   client.zadd(key, 1, "one").get();
   client.zadd(key, 2, "two").get();
   client.zadd(key, 3, "three").get();
   long reply = client.zrem(key, "two").get();
   assertEquals(1, reply);
   List<ZEntity<?>> entitys =
       client.zrangeByScoreWithScores(key, ZNumbers.MINIMUM, ZNumbers.MAXIMUM).get();
   assertEquals(2, entitys.size());
   assertEquals("one", entitys.get(0).member);
   assertEquals("three", entitys.get(1).member);
   assertEquals(1.0, entitys.get(0).score);
   assertEquals(3.0, entitys.get(1).score);
   client.delete(key).get();
   client.zadd(key, 1, "one").get();
   client.zadd(key, 2, "two").get();
   client.zadd(key, 3, "three").get();
   reply = client.zremRangeByRank(key, 0, 1).get();
   assertEquals(2, reply);
   entitys = client.zrangeByScoreWithScores(key, ZNumbers.MINIMUM, ZNumbers.MAXIMUM).get();
   assertEquals(1, entitys.size());
   assertEquals("three", entitys.get(0).member);
   assertEquals(3.0, entitys.get(0).score);
   client.delete(key).get();
   client.zadd(key, 1, "one").get();
   client.zadd(key, 2, "two").get();
   client.zadd(key, 3, "three").get();
   reply = client.zremRangeByScore(key, ZNumbers.MINIMUM, ZNumbers.excludeNumber(2)).get();
   assertEquals(1, reply);
   entitys = client.zrangeByScoreWithScores(key, ZNumbers.MINIMUM, ZNumbers.MAXIMUM).get();
   assertEquals(2, entitys.size());
   assertEquals("two", entitys.get(0).member);
   assertEquals("three", entitys.get(1).member);
   assertEquals(2.0, entitys.get(0).score);
   assertEquals(3.0, entitys.get(1).score);
 }
コード例 #16
0
  public void testLtrim() throws ExecutionException, InterruptedException {
    String key = "LIST_TRIM_KEY2";
    client.delete(key);
    long reply = client.lpush(key, "1", "1", "OK", "2", "2").get();
    assertEquals(5, reply);
    String result = client.ltrim(key, 1, 3).get();

    assertEquals("OK", result);
    List<String> range = (List<String>) client.lrange(key, 0, -1).get();
    assertEquals(3, range.size());
    assertEquals("2", range.get(0));
    assertEquals("OK", range.get(1));
    assertEquals("1", range.get(2));
  }
コード例 #17
0
 public void testExpire() throws ExecutionException, InterruptedException {
   String key = "EXPIRE_KEY";
   client.delete(key).get();
   client.set(key, "OK").get();
   long reply = client.expire(key, 10).get();
   assertEquals(1, reply);
   reply = client.ttl(key).get();
   assertEquals(10, reply);
   int timestamp = (int) (System.currentTimeMillis() / 1000 + 10);
   reply = client.expireAt(key, timestamp).get();
   assertEquals(1, reply);
   reply = client.ttl(key).get();
   assertTrue(reply <= 10 && reply >= 9);
 }
コード例 #18
0
 public void testHincrBy() throws ExecutionException, InterruptedException {
   String key = "HASH_INCR_KEY2";
   client.delete(key);
   String field1 = "TEST_FIELD1";
   String field2 = "TEST_FIELD2";
   long reply = client.hset(key, field1, 1).get();
   assertEquals(1, reply);
   reply = client.hincrBy(key, field2, 1).get();
   assertEquals(1, reply);
   reply = client.hincrBy(key, field1, 1).get();
   assertEquals(2, reply);
   reply = client.hincrBy(key, field1, -3).get();
   assertEquals(-1, reply);
 }
コード例 #19
0
 public void testHlen() throws ExecutionException, InterruptedException {
   String key = "HASH_KEYS_KEY2";
   client.delete(key);
   String field1 = "TEST_FIELD1";
   String field2 = "TEST_FIELD2";
   long reply = client.hset(key, field1, 1).get();
   assertEquals(1, reply);
   reply = client.hset(key, field2, 2).get();
   assertEquals(1, reply);
   long size = client.hlen(key).get();
   assertEquals(2, size);
   size = client.hlen("NONNsd2").get();
   assertEquals(0, size);
 }
コード例 #20
0
 public void testLpushAndIndex() throws ExecutionException, InterruptedException {
   String key = "LIST_PUSH_KEY2";
   client.delete(key);
   long reply = client.lpushx(key, "OK").get();
   assertEquals(0, reply);
   reply = client.lpush(key, "OK").get();
   assertEquals(1, reply);
   reply = client.lpush(key, "KO").get();
   assertEquals(2, reply);
   String cached = (String) client.lindex(key, 1).get();
   assertEquals("OK", cached);
   cached = (String) client.lindex(key, 0).get();
   assertEquals("KO", cached);
 }
コード例 #21
0
 public void testGetAndSet() throws ExecutionException, InterruptedException {
   String key = "GETANDSET_KEY";
   client.delete(key).get();
   long reply = client.incr(key).get();
   assertEquals(1, reply);
   reply = client.getAndSet(key, 0).get();
   assertEquals(1, reply);
   reply = client.getInt(key).get();
   assertEquals(0, reply);
   client.set(key, "HEL");
   String replyStr = (String) client.getAndSet(key, "LLO").get();
   assertEquals("HEL", replyStr);
   replyStr = (String) client.get(key).get();
   assertEquals("LLO", replyStr);
 }
コード例 #22
0
 public void testHgetAndHset() throws ExecutionException, InterruptedException {
   String key = "HASH_KEY";
   client.delete(key);
   String field = "TEST_FIELD";
   long reply = client.hset(key, field, "OK").get();
   assertEquals(1, reply);
   reply = client.hset(key, field, "KO").get();
   assertEquals(0, reply);
   String cached = (String) client.hget(key, field).get();
   assertEquals("KO", cached);
   reply = client.hset(key, field, 1.01).get();
   assertEquals(0, reply);
   double cachedDouble = client.hgetDouble(key, field).get();
   assertEquals(1.01, cachedDouble);
 }
コード例 #23
0
  public void testLset() throws ExecutionException, InterruptedException {
    String key = "LIST_SET_KEY2";
    client.delete(key);
    long reply = client.lpush(key, "1", "1", "OK", "2", "2").get();
    assertEquals(5, reply);
    String result = client.lset(key, 1, "3").get();

    assertEquals("OK", result);
    String cached = (String) client.lindex(key, 1).get();
    assertEquals("3", cached);

    result = client.lset(key, 6, "3").get();

    assertEquals("ERR index out of range", result);
  }
コード例 #24
0
  public void testLrem() throws ExecutionException, InterruptedException {
    String key = "LIST_REM_KEY2";
    client.delete(key);
    long reply = client.lpush(key, "1", "1", "OK", "2", "2").get();
    assertEquals(5, reply);
    reply = client.lrem(key, 1, "2").get();
    assertEquals(1, reply);
    String cached = (String) client.lindex(key, 0).get();
    assertEquals("2", cached);

    reply = client.lrem(key, -2, "1").get();
    assertEquals(2, reply);
    cached = (String) client.lindex(key, 1).get();
    assertEquals("OK", cached);
  }
コード例 #25
0
 public void testHmget() throws ExecutionException, InterruptedException {
   String key = "HASH_HMGET_KEY2";
   client.delete(key);
   String field1 = "TEST_FIELD1";
   String field2 = "TEST_FIELD2";
   long reply = client.hset(key, field1, 1).get();
   assertEquals(1, reply);
   reply = client.hset(key, field2, 2).get();
   assertEquals(1, reply);
   List<Integer> cached = client.hmgetInt(key, new String[] {field1, field2, "dfd"}).get();
   assertEquals(3, cached.size());
   assertEquals(1, cached.get(0).intValue());
   assertEquals(2, cached.get(1).intValue());
   assertNull(cached.get(2));
 }
コード例 #26
0
 public void testRpopAndPush() throws ExecutionException, InterruptedException {
   String key = "LIST_RPUSH_KEY2";
   client.delete(key);
   long reply = client.rpushx(key, "OK").get();
   assertEquals(0, reply);
   reply = client.rpush(key, "OK", "KO").get();
   assertEquals(2, reply);
   String cached = (String) client.rpop(key).get();
   assertEquals("KO", cached);
   cached = (String) client.rpop(key).get();
   assertEquals("OK", cached);
   cached = (String) client.rpop(key).get();
   assertEquals(null, cached);
   cached = (String) client.rpop("gfgr4").get();
   assertEquals(null, cached);
 }
コード例 #27
0
  public void testSdiffAndInterAndUnion() throws ExecutionException, InterruptedException {
    String key1 = "SET_DIFF_KEY1";
    String key2 = "SET_DIFF_KEY2";
    String key3 = "SET_DIFF_KEY3";
    client.delete(key1, key2, key3).get();
    long reply = client.sadd(key1, "a", "b", "c", "d").get();
    assertEquals(4, reply);
    reply = client.sadd(key2, "c").get();
    assertEquals(1, reply);
    reply = client.sadd(key3, "a", "c", "e").get();
    assertEquals(3, reply);
    List<String> diff = (List<String>) client.sdiff(key1, key2, key3).get();
    assertEquals(2, diff.size());
    assertEquals("b", diff.get(0));
    assertEquals("d", diff.get(1));
    reply = client.sdiffStore("SET_DIFF_RESULT", key1, key2, key3).get();
    assertEquals(2, reply);
    List<String> cached = (List<String>) client.smembers("SET_DIFF_RESULT").get();
    assertEquals(2, cached.size());
    assertEquals("b", cached.get(0));
    assertEquals("d", cached.get(1));
    diff = (List<String>) client.sinter(key1, key2, key3).get();
    assertEquals(1, diff.size());
    assertEquals("c", diff.get(0));
    reply = client.sinterStore("SET_DIFF_RESULT", key1, key2, key3).get();
    assertEquals(1, reply);
    cached = (List<String>) client.smembers("SET_DIFF_RESULT").get();
    assertEquals(1, cached.size());
    assertEquals("c", cached.get(0));
    diff = (List<String>) client.sunion(key1, key2, key3).get();
    assertEquals(5, diff.size());
    assertTrue(diff.contains("a"));
    assertTrue(diff.contains("b"));
    assertTrue(diff.contains("c"));
    assertTrue(diff.contains("d"));
    assertTrue(diff.contains("e"));

    reply = client.sunionStore("SET_DIFF_RESULT", key1, key2, key3).get();
    assertEquals(5, reply);
    cached = (List<String>) client.smembers("SET_DIFF_RESULT").get();
    assertEquals(5, cached.size());
    assertTrue(cached.contains("a"));
    assertTrue(cached.contains("b"));
    assertTrue(cached.contains("c"));
    assertTrue(cached.contains("d"));
    assertTrue(cached.contains("e"));
  }
コード例 #28
0
 public void testSmove() throws ExecutionException, InterruptedException {
   String key1 = "SET_MOVE_KEY1";
   String key2 = "SET_MOVE_KEY2";
   client.delete(key1, key2).get();
   long reply = client.sadd(key1, "a", "b", "c", "d").get();
   assertEquals(4, reply);
   reply = client.sadd(key2, "e").get();
   assertEquals(1, reply);
   reply = client.smove(key1, key2, "d").get();
   assertEquals(1, reply);
   reply = client.scard(key1).get();
   assertEquals(3, reply);
   reply = client.scard(key2).get();
   assertEquals(2, reply);
   reply = client.smove(key1, key2, "e").get();
   assertEquals(0, reply);
 }
コード例 #29
0
  public void testHkeys() throws ExecutionException, InterruptedException {
    String key = "HASH_KEYS_KEY2";
    client.delete(key);
    String field1 = "TEST_FIELD1";
    String field2 = "TEST_FIELD2";
    long reply = client.hset(key, field1, 1).get();
    assertEquals(1, reply);
    reply = client.hset(key, field2, 2).get();
    assertEquals(1, reply);
    List<String> keys = client.hkeys(key).get();
    assertEquals(2, keys.size());
    assertEquals(field1, keys.get(0));
    assertEquals(field2, keys.get(1));

    keys = client.hkeys("NONNsd2").get();
    assertEquals(0, keys.size());
  }
コード例 #30
0
 public void testLinsert() throws ExecutionException, InterruptedException {
   String key = "LIST_INSERT_KEY2";
   client.delete(key);
   long reply = client.lpush(key, "KO", "OK").get();
   assertEquals(2, reply);
   reply = client.linsertAfter(key, "OKO", "KOK").get();
   assertEquals(-1, reply);
   reply = client.linsertAfter(key, "OK", "KK").get();
   assertEquals(3, reply);
   String cached = (String) client.lindex(key, 1).get();
   assertEquals("KK", cached);
   reply = client.linsertBefore(key, "KK", "OO").get();
   assertEquals(4, reply);
   cached = (String) client.lindex(key, 1).get();
   assertEquals("OO", cached);
   cached = (String) client.lindex(key, 3).get();
   assertEquals("KO", cached);
 }