PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jgroups-2.10.0/tests/junit/org/jgroups/tests/SharedTransportTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 595 lines | 441 code | 113 blank | 41 comment | 45 complexity | ad2e968d552c640dd844c07a4db81d9c MD5 | raw file
  1. package org.jgroups.tests;
  2. import org.jgroups.*;
  3. import org.jgroups.conf.ConfiguratorFactory;
  4. import org.jgroups.conf.ProtocolData;
  5. import org.jgroups.conf.ProtocolParameter;
  6. import org.jgroups.conf.ProtocolStackConfigurator;
  7. import org.jgroups.protocols.BasicTCP;
  8. import org.jgroups.protocols.TCPPING;
  9. import org.jgroups.protocols.TP;
  10. import org.jgroups.protocols.UDP;
  11. import org.jgroups.stack.IpAddress;
  12. import org.jgroups.stack.Protocol;
  13. import org.jgroups.stack.ProtocolStack;
  14. import org.jgroups.util.ResourceManager;
  15. import org.jgroups.util.TimeScheduler;
  16. import org.jgroups.util.Util;
  17. import org.testng.AssertJUnit;
  18. import org.testng.annotations.AfterMethod;
  19. import org.testng.annotations.Test;
  20. import java.net.InetAddress;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import java.util.concurrent.CountDownLatch;
  24. import java.util.concurrent.TimeUnit;
  25. /**
  26. * Tests which test the shared transport
  27. * @author Bela Ban
  28. * @version $Id: SharedTransportTest.java,v 1.28 2009/10/20 15:11:42 belaban Exp $
  29. */
  30. @Test(groups=Global.STACK_DEPENDENT,sequential=true)
  31. public class SharedTransportTest extends ChannelTestBase {
  32. private JChannel a, b, c;
  33. private MyReceiver r1, r2, r3;
  34. static final String SINGLETON_1="singleton-1", SINGLETON_2="singleton-2";
  35. @AfterMethod
  36. protected void tearDown() throws Exception {
  37. Util.close(c,b,a);
  38. r1=r2=r3=null;
  39. }
  40. public void testCreationNonSharedTransport() throws Exception {
  41. a=createChannel(true);
  42. a.connect("SharedTransportTest.testCreationNonSharedTransport");
  43. View view=a.getView();
  44. System.out.println("view = " + view);
  45. assert view.size() == 1;
  46. }
  47. public void testCreationOfDuplicateCluster() throws Exception {
  48. a=createSharedChannel(SINGLETON_1);
  49. // makeUnique(a, 2);
  50. b=createSharedChannel(SINGLETON_1);
  51. a.connect("x");
  52. try {
  53. b.connect("x");
  54. assert false : "b should not be able to join cluster 'x' as a has already joined it";
  55. }
  56. catch(Exception ex) {
  57. System.out.println("b was not able to join the same cluster (\"x\") as expected");
  58. }
  59. }
  60. public void testView() throws Exception {
  61. a=createSharedChannel(SINGLETON_1);
  62. b=createSharedChannel(SINGLETON_2);
  63. a.setReceiver(new MyReceiver(SINGLETON_1));
  64. b.setReceiver(new MyReceiver(SINGLETON_2));
  65. a.connect("x");
  66. b.connect("x");
  67. View view=a.getView();
  68. assert view.size() == 2;
  69. view=b.getView();
  70. assert view.size() == 2;
  71. }
  72. public void testView2() throws Exception {
  73. a=createSharedChannel(SINGLETON_1);
  74. b=createSharedChannel(SINGLETON_1);
  75. a.setReceiver(new MyReceiver("first-channel"));
  76. b.setReceiver(new MyReceiver("second-channel"));
  77. a.connect("x");
  78. b.connect("y");
  79. View view=a.getView();
  80. assert view.size() == 1;
  81. view=b.getView();
  82. assert view.size() == 1;
  83. }
  84. /**
  85. * Tests http://jira.jboss.com/jira/browse/JGRP-689: with TCP or UDP.ip_mcast=false, the transport iterates through
  86. * the 'members' instance variable to send a group message. However, the 'members' var is the value of the last
  87. * view change received. If we receive multiple view changes, this leads to incorrect membership.
  88. * @throws Exception
  89. */
  90. public void testView3() throws Exception {
  91. a=createSharedChannel(SINGLETON_1);
  92. b=createSharedChannel(SINGLETON_1);
  93. c=createSharedChannel(SINGLETON_2);
  94. r1=new MyReceiver("A::" + SINGLETON_1);
  95. r2=new MyReceiver("B::" + SINGLETON_1);
  96. r3=new MyReceiver("C::" + SINGLETON_2);
  97. a.setReceiver(r1);
  98. b.setReceiver(r2);
  99. c.setReceiver(r3);
  100. a.connect("cluster-1");
  101. c.connect("cluster-1");
  102. View view=a.getView();
  103. assert view.size() == 2;
  104. view=c.getView();
  105. assert view.size() == 2;
  106. a.send(new Message(null, null, "msg-1"));
  107. c.send(new Message(null, null, "msg-2"));
  108. Util.sleep(1000); // async sending - wait a little
  109. List<Message> list=r1.getList();
  110. assert list.size() == 2;
  111. list=r3.getList();
  112. assert list.size() == 2;
  113. r1.clear();
  114. r2.clear();
  115. r3.clear();
  116. b.connect("cluster-2");
  117. a.send(new Message(null, null, "msg-3"));
  118. b.send(new Message(null, null, "msg-4"));
  119. c.send(new Message(null, null, "msg-5"));
  120. Util.sleep(1000); // async sending - wait a little
  121. // printLists(r1, r2, r3);
  122. list=r1.getList();
  123. assert list.size() == 2;
  124. list=r2.getList();
  125. assert list.size() == 1;
  126. list=r3.getList();
  127. assert list.size() == 2;
  128. }
  129. public void testView4() throws Exception {
  130. a=createSharedChannel(SINGLETON_1);
  131. r1=new MyReceiver("A::" + SINGLETON_1);
  132. a.setReceiver(r1);
  133. a.connect("cluster-X");
  134. a.send(new Message(null, null, "msg-1"));
  135. Util.sleep(1000); // async sending - wait a little
  136. List<Message> list=r1.getList();
  137. assert list.size() == 1;
  138. a.send(new Message(null, null, "msg-2"));
  139. a.send(new Message(null, null, "msg-3"));
  140. a.send(new Message(null, null, "msg-4"));
  141. Util.sleep(1000); // async sending - wait a little
  142. list=r1.getList();
  143. assert list.size() == 4;
  144. }
  145. public void testSharedTransportAndNonsharedTransport() throws Exception {
  146. a=createSharedChannel(SINGLETON_1);
  147. b=createChannel();
  148. a.setReceiver(new MyReceiver("first-channel"));
  149. b.setReceiver(new MyReceiver("second-channel"));
  150. a.connect("x");
  151. b.connect("x");
  152. View view=a.getView();
  153. assert view.size() == 2;
  154. view=b.getView();
  155. assert view.size() == 2;
  156. }
  157. public void testCreationOfDifferentCluster() throws Exception {
  158. a=createSharedChannel(SINGLETON_1);
  159. b=createSharedChannel(SINGLETON_2);
  160. a.connect("x");
  161. b.connect("x");
  162. View view=b.getView();
  163. System.out.println("b's view is " + view);
  164. assert view.size() == 2;
  165. }
  166. public void testReferenceCounting() throws ChannelException {
  167. a=createSharedChannel(SINGLETON_1);
  168. r1=new MyReceiver("a");
  169. a.setReceiver(r1);
  170. b=createSharedChannel(SINGLETON_1);
  171. r2=new MyReceiver("b");
  172. b.setReceiver(r2);
  173. c=createSharedChannel(SINGLETON_1);
  174. r3=new MyReceiver("c");
  175. c.setReceiver(r3);
  176. a.connect("A");
  177. b.connect("B");
  178. c.connect("C");
  179. a.send(null, null, "message from a");
  180. b.send(null, null, "message from b");
  181. c.send(null, null, "message from c");
  182. Util.sleep(500);
  183. assert r1.size() == 1;
  184. assert r2.size() == 1;
  185. assert r3.size() == 1;
  186. r1.clear();
  187. r2.clear();
  188. r3.clear();
  189. b.disconnect();
  190. System.out.println("\n");
  191. a.send(null, null, "message from a");
  192. c.send(null, null, "message from c");
  193. Util.sleep(500);
  194. assert r1.size() == 1 : "size should be 1 but is " + r1.size();
  195. assert r3.size() == 1 : "size should be 1 but is " + r3.size();
  196. r1.clear();
  197. r3.clear();
  198. c.disconnect();
  199. System.out.println("\n");
  200. a.send(null, null, "message from a");
  201. Util.sleep(500);
  202. assert r1.size() == 1;
  203. }
  204. /**
  205. * Tests that a second channel with the same group name can be
  206. * created and connected once the first channel is disconnected.
  207. * @throws Exception
  208. */
  209. public void testSimpleReCreation() throws Exception {
  210. a=createSharedChannel(SINGLETON_1);
  211. a.setReceiver(new MyReceiver("A"));
  212. a.connect("A");
  213. a.disconnect();
  214. b=createSharedChannel(SINGLETON_1);
  215. b.setReceiver(new MyReceiver("A'"));
  216. b.connect("A");
  217. }
  218. /**
  219. * Tests that a second channel with the same group name can be
  220. * created and connected once the first channel is disconnected even
  221. * if 3rd channel with a different group name is still using the shared
  222. * transport.
  223. * @throws Exception
  224. */
  225. public void testCreationFollowedByDeletion() throws Exception {
  226. a=createSharedChannel(SINGLETON_1);
  227. a.setReceiver(new MyReceiver("A"));
  228. a.connect("A");
  229. b=createSharedChannel(SINGLETON_1);
  230. b.setReceiver(new MyReceiver("B"));
  231. b.connect("B");
  232. b.close();
  233. a.close();
  234. }
  235. public void test2ChannelsCreationFollowedByDeletion() throws Exception {
  236. a=createSharedChannel(SINGLETON_1);
  237. a.setReceiver(new MyReceiver("A"));
  238. a.connect("A");
  239. b=createSharedChannel(SINGLETON_2);
  240. b.setReceiver(new MyReceiver("B"));
  241. b.connect("A");
  242. c=createSharedChannel(SINGLETON_2);
  243. c.setReceiver(new MyReceiver("C"));
  244. c.connect("B");
  245. c.send(null, null, "hello world from C");
  246. }
  247. public void testReCreationWithSurvivingChannel() throws Exception {
  248. // Create 2 channels sharing a transport
  249. System.out.println("-- creating A");
  250. a=createSharedChannel(SINGLETON_1);
  251. a.setReceiver(new MyReceiver("A"));
  252. a.connect("A");
  253. System.out.println("-- creating B");
  254. b=createSharedChannel(SINGLETON_1);
  255. b.setReceiver(new MyReceiver("B"));
  256. b.connect("B");
  257. System.out.println("-- disconnecting A");
  258. a.disconnect();
  259. // a is disconnected so we should be able to create a new channel with group "A"
  260. System.out.println("-- creating A'");
  261. c=createSharedChannel(SINGLETON_1);
  262. c.setReceiver(new MyReceiver("A'"));
  263. c.connect("A");
  264. }
  265. /**
  266. * Tests http://jira.jboss.com/jira/browse/JGRP-737
  267. * @throws Exception
  268. */
  269. public void testShutdownOfTimer() throws Exception {
  270. a=createSharedChannel(SINGLETON_1);
  271. b=createSharedChannel(SINGLETON_1);
  272. a.connect("x");
  273. b.connect("y");
  274. TimeScheduler timer1=a.getProtocolStack().getTransport().getTimer();
  275. TimeScheduler timer2=b.getProtocolStack().getTransport().getTimer();
  276. assert timer1 == timer2;
  277. assert !timer1.isShutdown();
  278. assert !timer2.isShutdown();
  279. Util.sleep(500);
  280. b.close();
  281. assert !timer2.isShutdown();
  282. assert !timer1.isShutdown();
  283. a.close(); // now, reference counting reaches 0, so the timer thread pool is stopped
  284. assert timer2.isShutdown();
  285. assert timer1.isShutdown();
  286. }
  287. /** Create channels A, B and C. Close A. This will close the timer and transports threads (!), so B will
  288. * not be able to send messages anymore, so C will not receive any messages
  289. * Tests http://jira.jboss.com/jira/browse/JGRP-737 */
  290. public void testSendingOfMessagesAfterChannelClose() throws ChannelException {
  291. MyReceiver rec_a=new MyReceiver("A"), rec_b=new MyReceiver("B"), rec_c=new MyReceiver("C");
  292. System.out.println("-- creating A");
  293. a=createSharedChannel(SINGLETON_1);
  294. a.setReceiver(rec_a);
  295. a.connect("A");
  296. System.out.println("-- creating B");
  297. b=createSharedChannel(SINGLETON_1);
  298. b.setReceiver(rec_b);
  299. b.connect("B");
  300. System.out.println("-- creating C");
  301. c=createSharedChannel(SINGLETON_2);
  302. c.setReceiver(rec_c);
  303. c.connect("B");
  304. b.send(null, null, "first");
  305. Util.sleep(500); // msg delivery is asynchronous, so give members some time to receive the msg (incl retransmission)
  306. assertSize(1, rec_b, rec_c);
  307. assertSize(0, rec_a);
  308. a.close();
  309. b.send(null, null, "second");
  310. Util.sleep(500);
  311. assertSize(0, rec_a);
  312. assertSize(2, rec_b, rec_c);
  313. }
  314. /**
  315. * Use a CountDownLatch to concurrently connect 3 channels; confirms
  316. * the channels connect
  317. *
  318. * @throws ChannelException
  319. * @throws InterruptedException
  320. */
  321. public void testConcurrentCreation() throws ChannelException, InterruptedException
  322. {
  323. a=createSharedChannel(SINGLETON_1);
  324. r1=new MyReceiver("a");
  325. a.setReceiver(r1);
  326. b=createSharedChannel(SINGLETON_1);
  327. r2=new MyReceiver("b");
  328. b.setReceiver(r2);
  329. c=createSharedChannel(SINGLETON_1);
  330. r3=new MyReceiver("c");
  331. c.setReceiver(r3);
  332. CountDownLatch startLatch = new CountDownLatch(1);
  333. CountDownLatch finishLatch = new CountDownLatch(3);
  334. ConnectTask connectA = new ConnectTask(a, "a", startLatch, finishLatch);
  335. Thread threadA = new Thread(connectA);
  336. threadA.setDaemon(true);
  337. threadA.start();
  338. ConnectTask connectB = new ConnectTask(b, "b", startLatch, finishLatch);
  339. Thread threadB = new Thread(connectB);
  340. threadB.setDaemon(true);
  341. threadB.start();
  342. ConnectTask connectC = new ConnectTask(c, "c", startLatch, finishLatch);
  343. Thread threadC = new Thread(connectC);
  344. threadC.setDaemon(true);
  345. threadC.start();
  346. startLatch.countDown();
  347. try
  348. {
  349. boolean finished = finishLatch.await(20, TimeUnit.SECONDS);
  350. if (connectA.exception != null)
  351. {
  352. AssertJUnit.fail("connectA threw exception " + connectA.exception);
  353. }
  354. if (connectB.exception != null)
  355. {
  356. AssertJUnit.fail("connectB threw exception " + connectB.exception);
  357. }
  358. if (connectC.exception != null)
  359. {
  360. AssertJUnit.fail("connectC threw exception " + connectC.exception);
  361. }
  362. if (!finished) {
  363. if (threadA.isAlive())
  364. AssertJUnit.fail("threadA did not finish");
  365. if (threadB.isAlive())
  366. AssertJUnit.fail("threadB did not finish");
  367. if (threadC.isAlive())
  368. AssertJUnit.fail("threadC did not finish");
  369. }
  370. }
  371. finally
  372. {
  373. if (threadA.isAlive())
  374. threadA.interrupt();
  375. if (threadB.isAlive())
  376. threadB.interrupt();
  377. if (threadC.isAlive())
  378. threadC.interrupt();
  379. }
  380. }
  381. private static void assertSize(int expected, MyReceiver... receivers) {
  382. for(MyReceiver recv: receivers) {
  383. assertEquals(expected, recv.size());
  384. }
  385. }
  386. private JChannel createSharedChannel(String singleton_name) throws ChannelException {
  387. ProtocolStackConfigurator config=ConfiguratorFactory.getStackConfigurator(channel_conf);
  388. ProtocolData[] protocols=config.getProtocolStack();
  389. ProtocolData transport=protocols[0];
  390. transport.getParameters().put(Global.SINGLETON_NAME, new ProtocolParameter(Global.SINGLETON_NAME, singleton_name));
  391. return new JChannel(config);
  392. }
  393. protected static void makeUnique(Channel channel, int num) throws Exception {
  394. ProtocolStack stack=channel.getProtocolStack();
  395. TP transport=stack.getTransport();
  396. InetAddress bind_addr=transport.getBindAddressAsInetAddress();
  397. if(transport instanceof UDP) {
  398. String mcast_addr=ResourceManager.getNextMulticastAddress();
  399. short mcast_port=ResourceManager.getNextMulticastPort(bind_addr);
  400. ((UDP)transport).setMulticastAddress(InetAddress.getByName(mcast_addr));
  401. ((UDP)transport).setMulticastPort(mcast_port);
  402. }
  403. else if(transport instanceof BasicTCP) {
  404. List<Short> ports=ResourceManager.getNextTcpPorts(bind_addr, num);
  405. transport.setBindPort(ports.get(0));
  406. transport.setPortRange(num);
  407. Protocol ping=stack.findProtocol(TCPPING.class);
  408. if(ping == null)
  409. throw new IllegalStateException("TCP stack must consist of TCP:TCPPING - other config are not supported");
  410. List<String> initial_hosts=new LinkedList<String>();
  411. for(short port: ports) {
  412. initial_hosts.add(bind_addr + "[" + port + "]");
  413. }
  414. String tmp=Util.printListWithDelimiter(initial_hosts, ",");
  415. List<IpAddress> init_hosts = Util.parseCommaDelimitedHosts(tmp, 1) ;
  416. ((TCPPING)ping).setInitialHosts(init_hosts) ;
  417. }
  418. else {
  419. throw new IllegalStateException("Only UDP and TCP are supported as transport protocols");
  420. }
  421. }
  422. private static class MyReceiver extends ReceiverAdapter {
  423. final List<Message> list=new LinkedList<Message>();
  424. final String name;
  425. private MyReceiver(String name) {
  426. this.name=name;
  427. }
  428. public List<Message> getList() {
  429. return list;
  430. }
  431. public int size() {
  432. return list.size();
  433. }
  434. public void clear() {
  435. list.clear();
  436. }
  437. public void receive(Message msg) {
  438. System.out.println("[" + name + "]: received message from " + msg.getSrc() + ": " + msg.getObject());
  439. list.add(msg);
  440. }
  441. public void viewAccepted(View new_view) {
  442. StringBuilder sb=new StringBuilder();
  443. sb.append("[" + name + "]: view = " + new_view);
  444. System.out.println(sb);
  445. }
  446. public String toString() {
  447. return super.toString() + " (size=" + list.size() + ")";
  448. }
  449. }
  450. private static class ConnectTask implements Runnable
  451. {
  452. private final Channel channel;
  453. private final String clusterName;
  454. private final CountDownLatch startLatch;
  455. private final CountDownLatch finishLatch;
  456. private Exception exception;
  457. ConnectTask(Channel channel, String clusterName, CountDownLatch startLatch, CountDownLatch finishLatch)
  458. {
  459. this.channel = channel;
  460. this.clusterName = clusterName;
  461. this.startLatch = startLatch;
  462. this.finishLatch = finishLatch;
  463. }
  464. public void run()
  465. {
  466. try
  467. {
  468. startLatch.await();
  469. channel.connect(clusterName);
  470. }
  471. catch (Exception e)
  472. {
  473. e.printStackTrace(System.out);
  474. this.exception = e;
  475. }
  476. finally
  477. {
  478. finishLatch.countDown();
  479. }
  480. }
  481. }
  482. }