PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/zookeeper-3.3.1/src/java/test/org/apache/zookeeper/test/CnxManagerTest.java

https://github.com/jmhsieh/mesos
Java | 183 lines | 117 code | 35 blank | 31 comment | 21 complexity | 350069ea965cbc4ef2fa067597aaba22 MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.zookeeper.test;
  19. import java.io.File;
  20. import java.net.InetSocketAddress;
  21. import java.nio.ByteBuffer;
  22. import java.util.HashMap;
  23. import java.util.concurrent.TimeUnit;
  24. import junit.framework.TestCase;
  25. import org.apache.log4j.Logger;
  26. import org.apache.zookeeper.PortAssignment;
  27. import org.apache.zookeeper.server.quorum.QuorumCnxManager;
  28. import org.apache.zookeeper.server.quorum.QuorumPeer;
  29. import org.apache.zookeeper.server.quorum.QuorumCnxManager.Message;
  30. import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
  31. import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState;
  32. import org.junit.Test;
  33. /**
  34. * This test uses two mock servers, each running an instance of QuorumCnxManager.
  35. * It simulates the situation in which a peer P sends a message to another peer Q
  36. * while Q is trying to open a connection to P. In this test, Q iniates a connection
  37. * to P as soon as it receives a message from P, and verifies that it receives a
  38. * copy of the message.
  39. *
  40. * This simple tests verifies that the new mechanism that duplicates the last message
  41. * sent upon a re-connection works.
  42. *
  43. */
  44. public class CnxManagerTest extends TestCase {
  45. protected static final Logger LOG = Logger.getLogger(FLENewEpochTest.class);
  46. protected static final int THRESHOLD = 4;
  47. int count;
  48. HashMap<Long,QuorumServer> peers;
  49. File tmpdir[];
  50. int port[];
  51. public void setUp() throws Exception {
  52. this.count = 3;
  53. this.peers = new HashMap<Long,QuorumServer>(count);
  54. tmpdir = new File[count];
  55. port = new int[count];
  56. for(int i = 0; i < count; i++) {
  57. int clientport = PortAssignment.unique();
  58. peers.put(Long.valueOf(i),
  59. new QuorumServer(i,
  60. new InetSocketAddress(clientport),
  61. new InetSocketAddress(PortAssignment.unique())));
  62. tmpdir[i] = ClientBase.createTmpDir();
  63. port[i] = clientport;
  64. }
  65. }
  66. public void tearDown() {
  67. }
  68. ByteBuffer createMsg(int state, long leader, long zxid, long epoch){
  69. byte requestBytes[] = new byte[28];
  70. ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
  71. /*
  72. * Building notification packet to send
  73. */
  74. requestBuffer.clear();
  75. requestBuffer.putInt(state);
  76. requestBuffer.putLong(leader);
  77. requestBuffer.putLong(zxid);
  78. requestBuffer.putLong(epoch);
  79. return requestBuffer;
  80. }
  81. class CnxManagerThread extends Thread {
  82. boolean failed;
  83. CnxManagerThread(){
  84. failed = false;
  85. }
  86. public void run(){
  87. try {
  88. QuorumPeer peer = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 2, 2, 2);
  89. QuorumCnxManager cnxManager = new QuorumCnxManager(peer);
  90. QuorumCnxManager.Listener listener = cnxManager.listener;
  91. if(listener != null){
  92. listener.start();
  93. } else {
  94. LOG.error("Null listener when initializing cnx manager");
  95. }
  96. long sid = 1;
  97. cnxManager.toSend(sid, createMsg(ServerState.LOOKING.ordinal(), 0, -1, 1));
  98. Message m = null;
  99. int numRetries = 1;
  100. while((m == null) && (numRetries++ <= THRESHOLD)){
  101. m = cnxManager.recvQueue.poll(3000, TimeUnit.MILLISECONDS);
  102. if(m == null) cnxManager.connectAll();
  103. }
  104. if(numRetries > THRESHOLD){
  105. failed = true;
  106. return;
  107. }
  108. cnxManager.testInitiateConnection(sid);
  109. m = cnxManager.recvQueue.poll(3000, TimeUnit.MILLISECONDS);
  110. if(m == null){
  111. failed = true;
  112. return;
  113. }
  114. } catch (Exception e) {
  115. LOG.error("Exception while running mock thread", e);
  116. fail("Unexpected exception");
  117. }
  118. }
  119. }
  120. @Test
  121. public void testCnxManager() throws Exception {
  122. CnxManagerThread thread = new CnxManagerThread();
  123. thread.start();
  124. QuorumPeer peer = new QuorumPeer(peers, tmpdir[1], tmpdir[1], port[1], 3, 1, 2, 2, 2);
  125. QuorumCnxManager cnxManager = new QuorumCnxManager(peer);
  126. QuorumCnxManager.Listener listener = cnxManager.listener;
  127. if(listener != null){
  128. listener.start();
  129. } else {
  130. LOG.error("Null listener when initializing cnx manager");
  131. }
  132. cnxManager.toSend(new Long(0), createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1));
  133. Message m = null;
  134. int numRetries = 1;
  135. while((m == null) && (numRetries++ <= THRESHOLD)){
  136. m = cnxManager.recvQueue.poll(3000, TimeUnit.MILLISECONDS);
  137. if(m == null) cnxManager.connectAll();
  138. }
  139. assertTrue("Exceeded number of retries", numRetries <= THRESHOLD);
  140. thread.join(5000);
  141. if (thread.isAlive()) {
  142. fail("Thread didn't join");
  143. } else {
  144. if(thread.failed)
  145. fail("Did not receive expected message");
  146. }
  147. }
  148. }