|
@@ -21,6 +21,7 @@ package org.apache.zookeeper.test;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.net.InetSocketAddress;
|
|
import java.net.InetSocketAddress;
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.ByteBuffer;
|
|
|
|
+import java.nio.channels.SocketChannel;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Random;
|
|
import java.util.Random;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
@@ -196,4 +197,63 @@ public class CnxManagerTest extends ZKTestCase {
|
|
if((end - begin) > 6000) Assert.fail("Waited more than necessary");
|
|
if((end - begin) > 6000) Assert.fail("Waited more than necessary");
|
|
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Tests a bug in QuorumCnxManager that causes a spin lock
|
|
|
|
+ * when a negative value is sent. This test checks if the
|
|
|
|
+ * connection is being closed upon a message with negative
|
|
|
|
+ * length.
|
|
|
|
+ *
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void testCnxManagerSpinLock() throws Exception {
|
|
|
|
+ QuorumPeer peer = new QuorumPeer(peers, tmpdir[1], tmpdir[1], port[1], 3, 1, 2, 2, 2);
|
|
|
|
+ QuorumCnxManager cnxManager = new QuorumCnxManager(peer);
|
|
|
|
+ QuorumCnxManager.Listener listener = cnxManager.listener;
|
|
|
|
+ if(listener != null){
|
|
|
|
+ listener.start();
|
|
|
|
+ } else {
|
|
|
|
+ LOG.error("Null listener when initializing cnx manager");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int port = peers.get(peer.getId()).electionAddr.getPort();
|
|
|
|
+ LOG.info("Election port: " + port);
|
|
|
|
+ InetSocketAddress addr = new InetSocketAddress(port);
|
|
|
|
+
|
|
|
|
+ Thread.sleep(1000);
|
|
|
|
+
|
|
|
|
+ SocketChannel sc = SocketChannel.open();
|
|
|
|
+ sc.socket().connect(peers.get(new Long(1)).electionAddr, 5000);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Write id first then negative length.
|
|
|
|
+ */
|
|
|
|
+ byte[] msgBytes = new byte[8];
|
|
|
|
+ ByteBuffer msgBuffer = ByteBuffer.wrap(msgBytes);
|
|
|
|
+ msgBuffer.putLong(new Long(2));
|
|
|
|
+ msgBuffer.position(0);
|
|
|
|
+ sc.write(msgBuffer);
|
|
|
|
+
|
|
|
|
+ msgBuffer = ByteBuffer.wrap(new byte[4]);
|
|
|
|
+ msgBuffer.putInt(-20);
|
|
|
|
+ msgBuffer.position(0);
|
|
|
|
+ sc.write(msgBuffer);
|
|
|
|
+
|
|
|
|
+ Thread.sleep(1000);
|
|
|
|
+
|
|
|
|
+ try{
|
|
|
|
+ /*
|
|
|
|
+ * Write a number of times until it
|
|
|
|
+ * detects that the socket is broken.
|
|
|
|
+ */
|
|
|
|
+ for(int i = 0; i < 100; i++){
|
|
|
|
+ msgBuffer.position(0);
|
|
|
|
+ sc.write(msgBuffer);
|
|
|
|
+ }
|
|
|
|
+ Assert.fail("Socket has not been closed");
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ LOG.info("Socket has been closed as expected");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|