1 package org.jgroups.tests; 2 3 import junit.framework.TestCase; 4 import junit.framework.TestSuite; 5 import org.jgroups.Event; 6 import org.jgroups.Message; 7 import org.jgroups.View; 8 import org.jgroups.debug.Simulator; 9 import org.jgroups.protocols.BARRIER; 10 import org.jgroups.protocols.PING; 11 import org.jgroups.protocols.VIEW_SYNC; 12 import org.jgroups.stack.IpAddress; 13 import org.jgroups.stack.Protocol; 14 import org.jgroups.util.Util; 15 16 import java.util.Vector ; 17 18 23 public class BARRIERTest extends TestCase { 24 IpAddress a1; 25 Vector members; 26 View v; 27 Simulator s; 28 BARRIER barrier_prot=new BARRIER(); 29 PING bottom_prot; 30 31 32 public BARRIERTest(String name) { 33 super(name); 34 } 35 36 37 public void setUp() throws Exception { 38 super.setUp(); 39 a1=new IpAddress(1111); 40 members=new Vector (); 41 members.add(a1); 42 v=new View(a1, 1, members); 43 s=new Simulator(); 44 s.setLocalAddress(a1); 45 s.setView(v); 46 s.addMember(a1); 47 bottom_prot=new PING(); 48 Protocol[] stack=new Protocol[]{new VIEW_SYNC(), barrier_prot, bottom_prot}; 49 s.setProtocolStack(stack); 50 s.start(); 51 } 52 53 public void tearDown() throws Exception { 54 super.tearDown(); 55 s.stop(); 56 } 57 58 59 public void testBlocking() { 60 assertFalse(barrier_prot.isClosed()); 61 s.send(new Event(Event.CLOSE_BARRIER)); 62 assertTrue(barrier_prot.isClosed()); 63 s.send(new Event(Event.OPEN_BARRIER)); 64 assertFalse(barrier_prot.isClosed()); 65 } 66 67 68 public void testThreadsBlockedOnBarrier() { 69 MyReceiver receiver=new MyReceiver(); 70 s.setReceiver(receiver); 71 s.send(new Event(Event.CLOSE_BARRIER)); 72 for(int i=0; i < 5; i++) { 73 new Thread () { 74 public void run() { 75 bottom_prot.up(new Event(Event.MSG, new Message(null, null, null))); 76 } 77 }.start(); 78 } 79 80 Util.sleep(500); 81 int num_in_flight_threads=barrier_prot.getNumberOfInFlightThreads(); 82 assertEquals(5, num_in_flight_threads); 83 84 s.send(new Event(Event.OPEN_BARRIER)); 85 Util.sleep(500); 86 num_in_flight_threads=barrier_prot.getNumberOfInFlightThreads(); 87 assertEquals(0, num_in_flight_threads); 88 assertEquals(5, receiver.getNumberOfReceivedMessages()); 89 } 90 91 92 93 94 95 static class MyReceiver implements Simulator.Receiver { 96 int num_mgs_received=0; 97 98 public void receive(Event evt) { 99 if(evt.getType() == Event.MSG) { 100 num_mgs_received++; 101 if(num_mgs_received % 1000 == 0) 102 System.out.println("<== " + num_mgs_received); 103 } 104 } 105 106 public int getNumberOfReceivedMessages() { 107 return num_mgs_received; 108 } 109 } 110 111 112 113 public static junit.framework.Test suite() { 114 return new TestSuite(BARRIERTest.class); 115 } 116 117 public static void main(String [] args) { 118 junit.textui.TestRunner.run(suite()); 119 } 120 } 121 | Popular Tags |