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 static void main(String[] args) throws IOException { MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:11211")); MemcachedClient memcachedClient = builder.build(); try { memcachedClient.set("hello", 0, "Hello,xmemcached"); String value = memcachedClient.get("hello"); System.out.println("hello=" + value); memcachedClient.delete("hello"); value = memcachedClient.get("hello"); System.out.println("hello=" + value); } catch (MemcachedException e) { System.err.println("MemcachedClient operation fail"); e.printStackTrace(); } catch (TimeoutException e) { System.err.println("MemcachedClient operation timeout"); e.printStackTrace(); } catch (InterruptedException e) { // ignore } try { // close memcached client memcachedClient.shutdown(); } catch (IOException e) { System.err.println("Shutdown MemcachedClient fail"); e.printStackTrace(); } }
public void _testOperationEncodeTimeout() throws Exception { memcachedClient.set("name", 0, "dennis"); assertEquals("dennis", memcachedClient.get("name")); long writeMessageCount = memcachedClient.getConnector().getStatistics().getWriteMessageCount(); CountDownLatch latch = new CountDownLatch(1); Command errorCommand = null; if (memcachedClient.getProtocol() == Protocol.Text) { errorCommand = new MockEncodeTimeoutTextGetOneCommand( "name", "name".getBytes(), CommandType.GET_ONE, latch, 1000); } else { errorCommand = new MockEncodeTimeoutBinaryGetCommand( "name", "name".getBytes(), CommandType.GET_ONE, latch, OpCode.GET, false, 1000); } memcachedClient.getConnector().send(errorCommand); // Force write thread to encode command errorCommand.setIoBuffer(null); // wait 100 milliseconds,the operation will be timeout if (!latch.await(100, TimeUnit.MILLISECONDS)) { errorCommand.cancel(); } Thread.sleep(1000); // It is not written to channel,because it is canceled. assertEquals( writeMessageCount, memcachedClient.getConnector().getStatistics().getWriteMessageCount()); // It works assertEquals("dennis", memcachedClient.get("name")); }
public void testOperation() throws InterruptedException, TimeoutException, MemcachedException, IOException { assertEquals(0, getSize()); client.set(keyName, 0, "12345"); assertEquals(1, getSize()); assertEquals("12345", client.get(keyName)); assertEquals(0, getSize()); client.set(keyName + "_" + System.currentTimeMillis(), 0, "12345"); assertEquals(1, getSize()); assertEquals("12345", client.get(keyName + "_" + System.currentTimeMillis())); log.info("push 10000 items"); long start = System.currentTimeMillis(); // 测试顺序写入10000个数据,再按顺序取出来,是否正确 for (int i = 0; i < 10000; i++) { client.set(keyName, 0, "value_" + i); } log.info("push 10000 items :" + (System.currentTimeMillis() - start) + " ms"); assertEquals(10000, getSize()); log.info("poll 10000 items"); start = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { assertEquals("value_" + i, client.get(keyName)); } log.info("poll 10000 items :" + (System.currentTimeMillis() - start) + " ms"); assertEquals(0, getSize()); log.info("开始测试权限状态"); authorization(); log.info("开始测试多线程操作"); mutiThreadWrite(); mutiThreadGet(); assertEquals(0, getSize()); }
public void testRemoveAndAddServer() throws Exception { String servers = properties.getProperty("test.memcached.servers"); memcachedClient.set("name", 0, "dennis"); assertEquals("dennis", memcachedClient.get("name")); memcachedClient.removeServer(servers); synchronized (this) { while (memcachedClient.getAvaliableServers().size() > 0) { wait(1000); } } assertEquals(0, memcachedClient.getAvaliableServers().size()); try { memcachedClient.get("name"); fail(); } catch (MemcachedException e) { assertEquals("There is no available connection at this moment", e.getMessage()); } memcachedClient.addServer(properties.getProperty("test.memcached.servers")); synchronized (this) { while (memcachedClient.getAvaliableServers().size() < AddrUtil.getAddresses(servers).size()) { wait(1000); } } Thread.sleep(5000); assertEquals("dennis", memcachedClient.get("name")); }
@Test public void simpleTest() throws TimeoutException, InterruptedException, MemcachedException { long start = System.currentTimeMillis(); String value = "simpleValue"; Assert.assertTrue(memcachedClient.set(key, 120, value)); Assert.assertEquals(value, memcachedClient.get(key)); // Assert.assertTrue(memcachedClient.touch(key, 120)); Assert.assertTrue(memcachedClient.delete(key)); Assert.assertNull(memcachedClient.get(key)); System.out.println("use time: " + (System.currentTimeMillis() - start)); }
public void authorization() throws TimeoutException, InterruptedException, IOException { try { client.get("xxxxxxxxxx"); } catch (MemcachedException e) { assertEquals("xxxxxxxxxx command Unsupported now", e.getMessage()); } try { client.get("xxxxxxxxxx_xxx"); } catch (MemcachedException e) { assertEquals("Authorization error", e.getMessage()); } }
/** * 测试类 * * @param client */ public void test(MemcachedClient client) { try { System.out.println(client.get("1")); if (client.get("1") == null) { client.set("1", 0, "11111111111"); } } catch (TimeoutException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (MemcachedException e) { e.printStackTrace(); } }
public void _testErrorCommand() throws Exception { Command nonexisCmd = new Command() { @Override public boolean decode(MemcachedTCPSession session, ByteBuffer buffer) { return decodeError(ByteUtils.nextLine(buffer)); } @Override public void encode() { ioBuffer = IoBuffer.wrap(ByteBuffer.wrap("test\r\n".getBytes())); } }; nonexisCmd.setKey("test"); nonexisCmd.setLatch(new CountDownLatch(1)); memcachedClient.getConnector().send(nonexisCmd); // this.memcachedClient.flushAll(); nonexisCmd.getLatch().await(); assertNotNull(nonexisCmd.getException()); assertEquals( "Nonexist command,check your memcached version please.", nonexisCmd.getException().getMessage()); assertTrue(nonexisCmd.getException() instanceof UnknownCommandException); memcachedClient.set("name", 0, "dennis"); assertEquals("dennis", memcachedClient.get("name")); }
private int getSize() throws TimeoutException, InterruptedException, MemcachedException { String value = client.get("size|key"); if (value != null) { return Integer.parseInt(value); } return -1; }
public static Object getXCache(net.rubyeye.xmemcached.MemcachedClient mcc, String key) { try { return mcc.get(key); } catch (Exception e) { throw new RuntimeException(e); } }
public Map<String, Object> get(String[] keys) { try { return memcachedClient.get(Arrays.asList(keys)); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
public Object get(String key) { try { return memcachedClient.get(key); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
public void testWeightedServers() throws Exception { // shutdown current client memcachedClient.shutdown(); MemcachedClientBuilder builder = createWeightedBuilder(); builder.getConfiguration().setStatisticsServer(true); memcachedClient = builder.build(); memcachedClient.flushAll(5000); Map<InetSocketAddress, Map<String, String>> oldStats = memcachedClient.getStats(); for (int i = 0; i < 100; i++) { assertTrue(memcachedClient.set(String.valueOf(i), 0, i)); } for (int i = 0; i < 100; i++) { assertEquals(i, memcachedClient.get(String.valueOf(i))); } List<InetSocketAddress> addressList = AddrUtil.getAddresses(properties.getProperty("test.memcached.servers")); Map<InetSocketAddress, Map<String, String>> newStats = memcachedClient.getStats(); for (InetSocketAddress address : addressList) { int oldSets = Integer.parseInt(oldStats.get(address).get("cmd_set")); int newSets = Integer.parseInt(newStats.get(address).get("cmd_set")); System.out.println("sets:" + (newSets - oldSets)); int oldGets = Integer.parseInt(oldStats.get(address).get("cmd_get")); int newGets = Integer.parseInt(newStats.get(address).get("cmd_get")); System.out.println("gets:" + (newGets - oldGets)); } }
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 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(); }
@Override public boolean isOnline(long uid) { try { Boolean online = memcachedClient.get(MemcachedKeyGenerator.genUserOnlineKey(uid)); return online == null ? false : online; } catch (Exception e) { log.error(e.getMessage(), e); return false; } }
@Override public ValueWrapper get(Object key) { Object value = null; try { value = memcachedClient.get(keyToString(key)); } catch (Exception e) { log.warn("Unable to get cached object", throwableOrMessage(e)); } return null != value ? new SimpleValueWrapper(value) : null; }
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 testOperationDecodeTimeOut() throws Exception { memcachedClient.set("name", 0, "dennis"); assertEquals("dennis", memcachedClient.get("name")); CountDownLatch latch = new CountDownLatch(1); Command errorCommand = null; if (memcachedClient.getProtocol() == Protocol.Text) { errorCommand = new MockDecodeTimeoutTextGetOneCommand( "name", "name".getBytes(), CommandType.GET_ONE, latch, 1000); } else { errorCommand = new MockDecodeTimeoutBinaryGetOneCommand( "name", "name".getBytes(), CommandType.GET_ONE, latch, OpCode.GET, false, 1000); } memcachedClient.getConnector().send(errorCommand); // wait 100 milliseconds,the operation will be timeout latch.await(100, TimeUnit.MILLISECONDS); assertNull(errorCommand.getResult()); Thread.sleep(1000); // It works. assertNotNull(errorCommand.getResult()); assertEquals("dennis", memcachedClient.get("name")); }
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())); }
public void testAutoReconnect() throws Exception { final String key = "name"; memcachedClient.set(key, 0, "dennis"); assertEquals("dennis", memcachedClient.get(key)); CountDownLatch latch = new CountDownLatch(1); int currentServerCount = memcachedClient.getAvaliableServers().size(); MockErrorCommand errorCommand = null; if (memcachedClient.getProtocol() == Protocol.Text) { errorCommand = new MockErrorTextGetOneCommand(key, key.getBytes(), CommandType.GET_ONE, latch); } else { errorCommand = new MockErrorBinaryGetOneCommand( key, key.getBytes(), CommandType.GET_ONE, latch, OpCode.GET, false); } memcachedClient.getConnector().send((Command) errorCommand); latch.await(MemcachedClient.DEFAULT_OP_TIMEOUT, TimeUnit.MILLISECONDS); assertTrue(errorCommand.isDecoded()); // wait for reconnecting Thread.sleep(2000 * 3); assertEquals(currentServerCount, memcachedClient.getAvaliableServers().size()); // It works assertEquals("dennis", memcachedClient.get(key)); }
/* * (non-Javadoc) * @see junit.framework.TestCase#setUp() */ protected void setUp() throws Exception { Config.setSetting("port", "12001"); Config.setSetting("path", "dbtest"); Config.setSetting("logsize", "40"); Config.setSetting("authorization", "key|abc@@bbs|pass"); StartNewQueue.newQueueInstance(Integer.parseInt(Config.getSetting("port"))); log.info("running at port " + Config.getSetting("port")); builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("127.0.0.1:12001")); builder.setConnectionPoolSize(50); // set connection pool size to five try { client = builder.build(); client.setOptimizeGet(false); builder.setSocketOption(StandardSocketOption.SO_KEEPALIVE, true); builder.setSocketOption(StandardSocketOption.SO_RCVBUF, 64 * 1024); builder.setSocketOption(StandardSocketOption.SO_SNDBUF, 64 * 1024); builder.setSocketOption(StandardSocketOption.SO_REUSEADDR, true); builder.setSocketOption(StandardSocketOption.TCP_NODELAY, false); } catch (IOException e) { throw new RuntimeException(e); } client.get("clear|key|abc"); }
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 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(); }
public void testAppendPrepend() throws Exception { // append,prepend assertTrue(memcachedClient.set("name", 0, "dennis", new StringTranscoder(), 1000)); assertTrue(memcachedClient.prepend("name", "hello ")); assertEquals("hello dennis", memcachedClient.get("name")); assertTrue(memcachedClient.append("name", " zhuang")); assertEquals("hello dennis zhuang", memcachedClient.get("name")); memcachedClient.delete("name"); assertFalse(memcachedClient.prepend("name", "hello ", 2000)); assertFalse(memcachedClient.append("name", " zhuang", 2000)); // append test // blank key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.append("", 0, 1); } }.check(); // null key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.append((String) null, 0, 1); } }.check(); // invalid key new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.append("test\r\n", 0, 1); } }.check(); new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.append("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.append(sb.toString(), 0, 1); } }.check(); // prepend test // blank key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.prepend("", 0, 1); } }.check(); // null key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.prepend((String) null, 0, 1); } }.check(); // invalid key new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.prepend("test\r\n", 0, 1); } }.check(); new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.prepend("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.prepend(sb.toString(), 0, 1); } }.check(); }
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 testGet() throws Exception { assertNull(memcachedClient.get("name")); memcachedClient.set("name", 1, "dennis", new StringTranscoder(), 1000); assertEquals("dennis", memcachedClient.get("name", new StringTranscoder())); new TranscoderChecker(mockTranscoder, 1) { @Override public void call() throws Exception { assertEquals("dennis", memcachedClient.get("name", mockTranscoder)); } }.check(); Thread.sleep(2000); // expire assertNull(memcachedClient.get("name")); // blank key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.get(""); } }.check(); // null key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.get((String) null); } }.check(); // invalid key new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.get("test\r\n"); } }.check(); new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.get("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.get(sb.toString()); } }.check(); // client is shutdown try { memcachedClient.shutdown(); memcachedClient.get("name"); fail(); } catch (MemcachedException e) { assertEquals("Xmemcached is stopped", e.getMessage()); } }
public Object getWithException(String key) throws TimeoutException, InterruptedException, MemcachedException { return client.get(key); }
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(); }