/** * Create one broadcaster and 50 receivers. Make sure broadcasting works. Then stop all of the * receivers, and create 50 new ones. Make sure the 50 new ones are receiving data from the * broadcasting. * * @throws Exception */ public void testJGropusChannelReferenceCounting1() throws Exception { JGroupsBroadcastGroupConfiguration jgroupsConfig = new JGroupsBroadcastGroupConfiguration("test-jgroups-file_ping.xml", "tst"); BroadcastEndpointFactory factory = jgroupsConfig.createBroadcastEndpointFactory(); BroadcastEndpoint broadcaster = factory.createBroadcastEndpoint(); broadcaster.openBroadcaster(); int num = 50; BroadcastEndpoint[] receivers = new BroadcastEndpoint[num]; for (int i = 0; i < num; i++) { receivers[i] = factory.createBroadcastEndpoint(); receivers[i].openClient(); } final byte[] data = new byte[] {1, 2, 3, 4, 5}; broadcaster.broadcast(data); for (int i = 0; i < num; i++) { byte[] received = receivers[i].receiveBroadcast(5000, TimeUnit.MILLISECONDS); assertNotNull(received); assertEquals(5, received.length); assertEquals(1, received[0]); assertEquals(2, received[1]); assertEquals(3, received[2]); assertEquals(4, received[3]); assertEquals(5, received[4]); } for (int i = 0; i < num; i++) { receivers[i].close(false); } // new ones for (int i = 0; i < num; i++) { receivers[i] = factory.createBroadcastEndpoint(); receivers[i].openClient(); } broadcaster.broadcast(data); for (int i = 0; i < num; i++) { byte[] received = receivers[i].receiveBroadcast(5000, TimeUnit.MILLISECONDS); assertNotNull(received); assertEquals(5, received.length); assertEquals(1, received[0]); assertEquals(2, received[1]); assertEquals(3, received[2]); assertEquals(4, received[3]); assertEquals(5, received[4]); } for (int i = 0; i < num; i++) { receivers[i].close(false); } broadcaster.close(true); }
public void testStraightSendReceiveJGroups() throws Exception { BroadcastEndpoint broadcaster = null; BroadcastEndpoint client = null; try { JGroupsBroadcastGroupConfiguration jgroupsConfig = new JGroupsBroadcastGroupConfiguration("test-jgroups-file_ping.xml", "tst"); broadcaster = jgroupsConfig.createBroadcastEndpointFactory().createBroadcastEndpoint(); broadcaster.openBroadcaster(); client = jgroupsConfig.createBroadcastEndpointFactory().createBroadcastEndpoint(); client.openClient(); Thread.sleep(1000); byte[] randomBytes = "PQP".getBytes(); broadcaster.broadcast(randomBytes); byte[] btreceived = client.receiveBroadcast(5, TimeUnit.SECONDS); System.out.println("BTReceived = " + btreceived); assertNotNull(btreceived); assertEquals(randomBytes.length, btreceived.length); for (int i = 0; i < randomBytes.length; i++) { assertEquals(randomBytes[i], btreceived[i]); } } finally { try { if (broadcaster != null) broadcaster.close(true); if (client != null) client.close(false); } catch (Exception ignored) { ignored.printStackTrace(); } } }