Beispiel #1
0
 public long decr(String key, long delta, long initValue) {
   try {
     return memcachedClient.decr(key, delta, initValue);
   } catch (Exception e) {
     throw new IllegalStateException(e.getMessage(), e);
   }
 }
  public void testDecr() throws Exception {
    assertEquals(0, memcachedClient.decr("a", 5));

    assertTrue(memcachedClient.set("a", 0, "100"));
    assertEquals(50, memcachedClient.decr("a", 50));
    assertEquals(46, memcachedClient.decr("a", 4));

    // test decr with initValue
    memcachedClient.delete("a");
    assertEquals(100, memcachedClient.decr("a", 5, 100));
    assertEquals(50, memcachedClient.decr("a", 50));
    assertEquals(46, memcachedClient.decr("a", 4));

    // test decr with initValue and expire time
    memcachedClient.delete("a");
    assertEquals(1, memcachedClient.decr("a", 5, 1, 1000, 1));
    Thread.sleep(2000);
    assertNull(memcachedClient.get("a"));

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

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