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 _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"));
  }
 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 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));
 }