1 3 package org.jgroups.blocks; 4 5 import junit.framework.Test; 6 import junit.framework.TestCase; 7 import junit.framework.TestSuite; 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import java.util.Vector ; 12 13 public class DistributedQueueTest extends TestCase 14 { 15 16 final int NUM_ITEMS = 10; 17 static Log logger = LogFactory.getLog(DistributedQueueTest.class); 18 String props; 19 20 public DistributedQueueTest(String testName) 21 { 22 super(testName); 23 } 24 25 public static Test suite() 26 { 27 return new TestSuite(DistributedQueueTest.class); 28 } 29 30 protected DistributedQueue queue1; 31 protected DistributedQueue queue2; 32 protected DistributedQueue queue3; 33 34 public void setUp() throws Exception 35 { 36 37 super.setUp(); 38 props="UDP(mcast_recv_buf_size=80000;mcast_send_buf_size=150000;mcast_port=45566;" + 39 "mcast_addr=228.8.8.8;ip_ttl=32):" + 40 "PING(timeout=2000;num_initial_members=3):" + 41 "FD_SOCK:" + 42 "VERIFY_SUSPECT(timeout=1500):" + 43 "UNICAST(timeout=600,1200,2000,2500):" + 44 "FRAG(frag_size=8096;down_thread=false;up_thread=false):" + 45 "TOTAL_TOKEN(unblock_sending=10;block_sending=50):" + 46 "pbcast.GMS(print_local_addr=true;join_timeout=3000;join_retry_timeout=2000;shun=true):" + 47 "STATE_TRANSFER:" + 48 "QUEUE"; 49 50 51 52 queue1 = new DistributedQueue("testing", null, props, 5000); 53 log("created queue1"); 54 55 try 57 { 58 Thread.sleep(1000); 59 } 60 catch (Exception ex) 61 { 62 } 63 64 queue2 = new DistributedQueue("testing", null, props, 5000); 65 log("created queue2"); 66 67 try 68 { 69 Thread.sleep(1000); 70 } 71 catch (InterruptedException ex) 72 { 73 } 74 75 queue3 = new DistributedQueue("testing", null, props, 5000); 76 log("created queue3"); 77 78 try 79 { 80 Thread.sleep(1000); 81 } 82 catch (InterruptedException ex) 83 { 84 } 85 } 86 87 public void tearDown() throws Exception 88 { 89 super.tearDown(); 90 log("stopping queue1"); 91 queue1.stop(); 92 log("stopped queue1"); 93 94 log("stopping queue2"); 95 queue2.stop(); 96 log("stopped queue2"); 97 98 log("stopping queue3"); 99 queue3.stop(); 100 log("stopped queue3"); 101 } 102 103 void log(String msg) { 104 System.out.println("-- [" + Thread.currentThread().getName() + "]: " + msg); 105 } 106 107 class PutTask implements Runnable 108 { 109 protected DistributedQueue queue; 110 protected String name; 111 protected boolean finished; 112 113 public PutTask(String name, DistributedQueue q) 114 { 115 queue = q; 116 this.name = name; 117 finished = false; 118 } 119 120 public void run() 121 { 122 for (int i = 0; i < NUM_ITEMS; i++) 123 { 124 queue.add(name + '_' + i); 125 } 126 finished = true; 127 log("added " + NUM_ITEMS + " elements - done"); 128 } 129 130 public boolean finished() 131 { 132 return finished; 133 } 134 } 135 136 137 public void testMultipleWriter() throws Exception 138 { 139 PutTask t1 = new PutTask("Queue1", queue1); 140 PutTask t2 = new PutTask("Queue2", queue2); 141 PutTask t3 = new PutTask("Queue3", queue3); 142 Thread rTask1 = new Thread (t1); 143 Thread rTask2 = new Thread (t2); 144 Thread rTask3 = new Thread (t3); 145 146 rTask1.start(); 147 rTask2.start(); 148 rTask3.start(); 149 150 while (!t1.finished() || !t2.finished() || !t3.finished()) 151 { 152 try 153 { 154 Thread.sleep(1000); 155 } 156 catch (InterruptedException ex) 157 { 158 } 159 } 160 161 assertEquals(queue1.size(), queue2.size()); 162 assertEquals(queue1.size(), queue3.size()); 163 164 checkContents(queue1.getContents(), queue2.getContents()); 165 checkContents(queue1.getContents(), queue3.getContents()); 166 } 167 168 protected void checkContents(Vector q1, Vector q2) 169 { 170 for (int i = 0; i < q1.size(); i++) 171 { 172 Object e1 = q1.elementAt(i); 173 Object e2 = q2.elementAt(i); 174 boolean t = e1.equals(e2); 175 if (!t) 176 { 177 logger.error("Data order differs :" + e1 + "!=" + e2); 178 } else 179 logger.debug("Data order ok :" + e1 + "==" + e2); 180 assertTrue(e1.equals(e2)); 181 } 182 } 183 184 public static void main(String [] args) 185 { 186 junit.textui.TestRunner.run(suite()); 187 } 188 } 189 | Popular Tags |