public void testStoreCollection()
      throws TimeoutException, InterruptedException, MemcachedException {
    // store list
    List<String> list = new ArrayList<String>();
    for (int i = 0; i < 100; i++) {
      list.add(String.valueOf(i));
    }
    assertTrue(memcachedClient.add("list", 0, list));
    List<String> listFromCache = memcachedClient.get("list");
    assertEquals(100, listFromCache.size());

    for (int i = 0; i < listFromCache.size(); i++) {
      assertEquals(list.get(i), listFromCache.get(i));
    }
    // store map
    Map<String, Integer> map = new HashMap<String, Integer>();

    for (int i = 0; i < 100; i++) {
      map.put(String.valueOf(i), i);
    }
    assertTrue(memcachedClient.add("map", 0, map));
    Map<String, Integer> mapFromCache = memcachedClient.get("map");
    assertEquals(100, listFromCache.size());

    for (int i = 0; i < listFromCache.size(); i++) {
      assertEquals(mapFromCache.get(i), map.get(i));
    }
  }
  public void testAdd() throws Exception {
    assertTrue(memcachedClient.add("name", 0, "dennis"));
    assertFalse(memcachedClient.add("name", 0, "dennis"));
    assertEquals("dennis", memcachedClient.get("name", 2000));

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

    // invalid key
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.add("test\r\n", 0, 1);
      }
    }.check();
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.add("test test2", 0, 1);
      }
    }.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.add(sb.toString(), 0, 1);
      }
    }.check();

    // Transcoder
    new TranscoderChecker(mockTranscoder, 2) {
      @Override
      public void call() throws Exception {
        memcachedClient.add("a", 0, 100, mockTranscoder);
        assertEquals(100, memcachedClient.get("a", mockTranscoder));
      }
    }.check();
  }
  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()));
  }
Exemple #4
0
 public boolean add(String key, Object value, int exp) {
   try {
     return memcachedClient.add(key, exp, value);
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
 public void testFlushAll() throws Exception {
   for (int i = 0; i < 50; i++) {
     assertTrue(memcachedClient.add(String.valueOf(i), 0, i));
   }
   List<String> keys = new ArrayList<String>();
   for (int i = 0; i < 100; i++) {
     keys.add(String.valueOf(i));
   }
   Map<String, Integer> result = memcachedClient.get(keys);
   assertEquals(50, result.size());
   for (int i = 0; i < 50; i++) {
     assertEquals((Integer) i, result.get(String.valueOf(i)));
   }
   memcachedClient.flushAll();
   result = memcachedClient.get(keys);
   assertTrue(result.isEmpty());
 }
  public void testDelete() throws Exception {
    assertTrue(memcachedClient.set("name", 0, "dennis"));
    assertEquals("dennis", memcachedClient.get("name"));
    assertTrue(memcachedClient.delete("name"));
    assertNull(memcachedClient.get("name"));
    assertFalse(memcachedClient.delete("not_exists"));

    memcachedClient.set("name", 0, "dennis");
    assertEquals("dennis", memcachedClient.get("name"));
    assertTrue(memcachedClient.delete("name"));
    assertNull(memcachedClient.get("name"));
    memcachedClient.set("name", 0, "dennis");
    assertEquals("dennis", memcachedClient.get("name"));
    assertTrue(memcachedClient.delete("name", 2000L));
    assertNull(memcachedClient.get("name"));

    // add,replace success
    assertTrue(memcachedClient.add("name", 0, "zhuang"));
    assertTrue(memcachedClient.replace("name", 0, "zhuang"));
  }
  public void testMultiGet() throws Exception {
    for (int i = 0; i < 50; i++) {
      assertTrue(memcachedClient.add(String.valueOf(i), 0, i));
    }

    List<String> keys = new ArrayList<String>();
    for (int i = 0; i < 100; i++) {
      keys.add(String.valueOf(i));
    }

    Map<String, Integer> result = memcachedClient.get(keys, 10000);
    assertEquals(50, result.size());

    for (int i = 0; i < 50; i++) {
      assertEquals((Integer) i, result.get(String.valueOf(i)));
    }

    // blank collection
    assertNull(memcachedClient.get((Collection) null));
    assertNull(memcachedClient.get(new HashSet<String>()));
  }
  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();
  }
  public void testGets() throws Exception {
    memcachedClient.add("name", 0, "dennis");
    GetsResponse<String> getsResponse = memcachedClient.gets("name");
    GetsResponse<String> oldGetsResponse = getsResponse;
    assertEquals("dennis", getsResponse.getValue());
    long oldCas = getsResponse.getCas();
    getsResponse = memcachedClient.gets("name", 2000, new StringTranscoder());
    assertEquals("dennis", getsResponse.getValue());
    // check the same
    assertEquals(oldCas, getsResponse.getCas());
    assertEquals(oldGetsResponse, getsResponse);

    memcachedClient.set("name", 0, "zhuang");
    getsResponse = memcachedClient.gets("name", 2000);
    assertEquals("zhuang", getsResponse.getValue());
    assertFalse(oldCas == getsResponse.getCas());

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

    // invalid key
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.gets("test\r\n");
      }
    }.check();
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.gets("test test2");
      }
    }.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.gets(sb.toString());
      }
    }.check();

    // client is shutdown
    try {
      memcachedClient.shutdown();
      memcachedClient.gets("name");
      fail();
    } catch (MemcachedException e) {
      assertEquals("Xmemcached is stopped", e.getMessage());
    }
  }
  public void testReplace() throws Exception {
    assertTrue(memcachedClient.add("name", 0, "dennis"));
    assertFalse(memcachedClient.replace("unknownKey", 0, "test"));
    assertTrue(memcachedClient.replace("name", 1, "zhuang"));
    assertEquals("zhuang", memcachedClient.get("name", 2000));
    Thread.sleep(2000);
    // expire
    assertNull(memcachedClient.get("name"));

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

    // invalid key
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.replace("test\r\n", 0, 1);
      }
    }.check();
    new InValidKeyChecker() {
      @Override
      public void call() throws Exception {
        memcachedClient.replace("test test2", 0, 1);
      }
    }.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.replace(sb.toString(), 0, 1);
      }
    }.check();

    // timeout
    // new TimeoutChecker(0) {
    // @Override
    // public void call() throws Exception {
    // XMemcachedClientTest.this.memcachedClient.replace("0", 0, 1, 0);
    // }
    // }.check();

    // Transcoder
    new TranscoderChecker(mockTranscoder, 2) {
      @Override
      public void call() throws Exception {
        memcachedClient.set("name", 0, 1);
        memcachedClient.replace("name", 0, "xmemcached", mockTranscoder);
        assertEquals("xmemcached", memcachedClient.get("name", mockTranscoder));
      }
    }.check();
  }