public void testAddEncodeAndDecode() { Command command = this.commandFactory.createCASCommand( this.key, this.keyBytes, 0, this.value, 9L, this.noreply, this.transcoder); command.encode(); ByteBuffer encodeBuffer = command.getIoBuffer().buf(); assertNotNull(encodeBuffer); assertEquals(42, encodeBuffer.capacity()); byte opCode = encodeBuffer.get(1); // cas use set command assertEquals(OpCode.SET.fieldValue(), opCode); ByteBuffer buffer = constructResponse( OpCode.SET.fieldValue(), (short) 0, (byte) 0, (byte) 0, (short) 0, 0, 0, 10L, null, null, null); assertTrue(command.decode(null, buffer)); assertTrue((Boolean) command.getResult()); assertEquals(0, buffer.remaining()); buffer = constructResponse( OpCode.SET.fieldValue(), (short) 0, (byte) 0, (byte) 0, (short) 0x0005, 0, 0, 0L, null, null, null); command = this.commandFactory.createCASCommand( this.key, this.keyBytes, 0, this.value, 9L, this.noreply, this.transcoder); assertTrue(command.decode(null, buffer)); assertFalse((Boolean) command.getResult()); assertEquals(0, buffer.remaining()); }
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")); }
private void doAuth() { SaslClient saslClient = null; try { saslClient = Sasl.createSaslClient( authInfo.getMechanisms(), null, "memcached", memcachedTCPSession.getRemoteSocketAddress().toString(), null, this.authInfo.getCallbackHandler()); final AtomicBoolean done = new AtomicBoolean(false); byte[] response = saslClient.hasInitialResponse() ? saslClient.evaluateChallenge(EMPTY_BYTES) : EMPTY_BYTES; CountDownLatch latch = new CountDownLatch(1); Command command = this.commandFactory.createAuthStartCommand( saslClient.getMechanismName(), latch, response); if (!this.memcachedTCPSession.isClosed()) this.memcachedTCPSession.write(command); else { log.error("Authentication fail,because the connection has been closed"); throw new RuntimeException("Authentication fai,connection has been close"); } while (!done.get()) { try { latch.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); done.set(true); } ResponseStatus responseStatus = ((BaseBinaryCommand) command).getResponseStatus(); switch (responseStatus) { case NO_ERROR: done.set(true); log.info( "Authentication to " + this.memcachedTCPSession.getRemoteSocketAddress() + " successfully"); break; case AUTH_REQUIRED: log.error( "Authentication failed to " + this.memcachedTCPSession.getRemoteSocketAddress()); log.warn( "Reopen connection to " + this.memcachedTCPSession.getRemoteSocketAddress() + ",beacause auth fail"); this.memcachedTCPSession.setAuthFailed(true); // It it is not first time,try to sleep 1 second if (!this.authInfo.isFirstTime()) { Thread.sleep(1000); } this.memcachedTCPSession.close(); done.set(true); break; case FUTHER_AUTH_REQUIRED: String result = (String) command.getResult(); response = saslClient.evaluateChallenge(ByteUtils.getBytes(result)); latch = new CountDownLatch(1); command = commandFactory.createAuthStepCommand( saslClient.getMechanismName(), latch, response); if (!this.memcachedTCPSession.isClosed()) this.memcachedTCPSession.write(command); else { log.error("Authentication fail,because the connection has been closed"); throw new RuntimeException("Authentication fai,connection has been close"); } break; default: done.set(true); log.error( "Authentication failed to " + this.memcachedTCPSession.getRemoteSocketAddress() + ",response status=" + responseStatus); break; } } } catch (Exception e) { log.error("Create saslClient error", e); } finally { if (saslClient != null) { try { saslClient.dispose(); } catch (SaslException e) { log.error("Dispose saslClient error", e); } } } }