예제 #1
0
  public void testSanitizeKey() throws Exception {
    memcachedClient.setSanitizeKeys(true);

    String key = "The string 眉@foo-bar";
    assertTrue(memcachedClient.add(key, 0, 0));
    assertEquals(0, memcachedClient.get(key));

    assertTrue(memcachedClient.replace(key, 0, 1));
    assertEquals(1, memcachedClient.get(key, 2000));

    assertTrue(memcachedClient.set(key, 0, 2));
    assertEquals((Integer) 2, memcachedClient.get(key, 2000, new IntegerTranscoder()));

    assertTrue(memcachedClient.set(key, 0, "xmemcached", new StringTranscoder()));
    assertTrue(memcachedClient.append(key, " great"));
    assertTrue(memcachedClient.prepend(key, "hello "));

    assertEquals("hello xmemcached great", memcachedClient.get(key));

    // test bulk get
    List<String> keys = new ArrayList<String>();
    for (int i = 0; i < 100; i++) {
      memcachedClient.add(key + i, 0, i);
      keys.add(key + i);
    }

    Map<String, Integer> result = memcachedClient.get(keys, 5000);
    for (int i = 0; i < 100; i++) {
      assertEquals((Integer) i, result.get(key + i));
    }

    for (int i = 0; i < 100; i++) {
      assertTrue(memcachedClient.delete(key + i));
      assertNull(memcachedClient.get(key + i));
    }

    // test cas
    memcachedClient.set(key, 0, 1);
    memcachedClient.cas(
        key,
        new CASOperation<Integer>() {

          public int getMaxTries() {
            return 1;
          }

          public Integer getNewValue(long currentCAS, Integer currentValue) {
            return currentValue + 1;
          }
        });
    assertEquals((Integer) 2, memcachedClient.get(key, 2000, new IntegerTranscoder()));
  }
예제 #2
0
  public void testCAS() throws Exception {
    memcachedClient.add("name", 0, "dennis");
    GetsResponse<String> getsResponse = memcachedClient.gets("name");
    assertEquals("dennis", getsResponse.getValue());
    final CASOperation<String> operation =
        new CASOperation<String>() {

          public int getMaxTries() {
            return 1;
          }

          public String getNewValue(long currentCAS, String currentValue) {
            return "zhuang";
          }
        };
    assertTrue(memcachedClient.cas("name", getsResponse, operation));
    assertEquals("zhuang", memcachedClient.get("name"));
    getsResponse = memcachedClient.gets("name");
    memcachedClient.set("name", 0, "dennis");
    // cas fail
    assertFalse(memcachedClient.cas("name", 0, "zhuang", getsResponse.getCas()));
    assertEquals("dennis", memcachedClient.get("name"));

    // blank key
    new BlankKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.cas("", operation);
      }
    }.check();
    // null key
    new BlankKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.cas((String) null, operation);
      }
    }.check();

    // invalid key
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.cas("test\r\n", operation);
      }
    }.check();
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.cas("test test2", operation);
      }
    }.check();

    // key is too long
    new TooLongKeyChecker(memcachedClient) {
      @Override
      public void call() throws Exception {
        int keyLength = memcachedClient.getProtocol() == Protocol.Text ? 256 : 65536;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < keyLength; i++) {
          sb.append(i);
        }
        memcachedClient.cas(sb.toString(), operation);
      }
    }.check();
  }