1 package org.jgroups.service.lease; 2 3 import junit.framework.TestCase; 4 import org.jgroups.JChannel; 5 6 public class LeaseFactoryClientTest extends TestCase { 7 8 public static final String SERVER_PROTOCOL_STACK="" 9 + "UDP(mcast_addr=224.0.0.35;mcast_port=12345;ip_ttl=1;" 10 + "mcast_send_buf_size=150000;mcast_recv_buf_size=80000;down_thread=false)" 11 + ":PING(timeout=500;num_initial_members=1;down_thread=false;up_thread=false)" 12 + ":FD(down_thread=false;up_thread=false)" 13 + ":VERIFY_SUSPECT(timeout=1500;down_thread=false;up_thread=false)" 14 15 + ":pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;down_thread=false)" 16 + ":FRAG(frag_size=4096;down_thread=false)" 17 + ":UNICAST(timeout=5000)" 18 + ":pbcast.STABLE(desired_avg_gossip=200;down_thread=false;up_thread=false)" 19 + ":pbcast.GMS(join_timeout=5000;join_retry_timeout=1000;" 20 + "shun=false;print_local_addr=false;down_thread=true;up_thread=true)" 21 + ":pbcast.STATE_TRANSFER(down_thread=false)" 23 ; 24 25 public static final String CLIENT_PROTOCOL_STACK="" 26 + "UDP(mcast_addr=224.0.0.36;mcast_port=56789;ip_ttl=1;" 27 + "mcast_send_buf_size=150000;mcast_recv_buf_size=80000;down_thread=false)" 28 + ":FRAG(frag_size=4096;down_thread=false;up_thread=false)" 29 + ":UNICAST(timeout=5000;use_gms=false)" 32 ; 33 34 35 public LeaseFactoryClientTest(String testName) { 36 super(testName); 37 } 38 39 protected JChannel svcServerChannel; 40 41 protected JChannel svcClientChannel; 42 43 protected JChannel clientChannel; 44 45 protected LeaseFactoryService leaseFactory; 46 47 protected LeaseFactoryClient leaseClient; 48 49 protected void setUp() throws Exception { 50 51 svcServerChannel=new JChannel(SERVER_PROTOCOL_STACK); 52 svcServerChannel.setOpt(JChannel.GET_STATE_EVENTS, Boolean.TRUE); 53 svcServerChannel.connect("server"); 54 55 try { 56 Thread.sleep(1000); 57 } 58 catch(InterruptedException ex) { 59 } 60 61 62 svcClientChannel=new JChannel(CLIENT_PROTOCOL_STACK); 63 svcClientChannel.connect("client"); 64 65 clientChannel=new JChannel(CLIENT_PROTOCOL_STACK); 66 clientChannel.connect("client"); 67 68 leaseFactory=new LeaseFactoryService(svcServerChannel, svcClientChannel); 69 leaseClient=new LeaseFactoryClient(clientChannel); 70 } 71 72 protected void tearDown() throws Exception { 73 clientChannel.close(); 74 svcClientChannel.close(); 75 svcServerChannel.close(); 76 } 77 78 public void test() throws Exception { 79 System.out.println("testing obtaining lease..."); 80 Lease lease=leaseClient.newLease("1", "foo", 10000, false); 81 82 assertTrue("lease should be granted", lease != null); 83 System.out.println("lease was granted."); 84 85 try { 86 Thread.sleep(100); 87 } 88 catch(InterruptedException ex) { 89 } 90 91 try { 92 leaseClient.newLease("1", "bar", 1000, false); 93 assertTrue("Lease should not be granted.", false); 94 } 95 catch(LeaseDeniedException ex) { 96 } 98 99 System.out.println("trying to cancel lease..."); 100 leaseClient.cancelLease(lease); 101 102 System.out.println("lease was canceled."); 103 104 lease=leaseClient.newLease("1", "bar", 1000, false); 105 assertTrue("new lease should have been granted.", lease != null); 106 107 try { 108 Thread.sleep(500); 109 } 110 catch(InterruptedException ex) { 111 } 112 113 System.out.println("renewing lease..."); 114 lease=leaseClient.renewLease(lease, 1000, false); 115 assertTrue("lease should have been renewed", lease != null); 116 System.out.println("lease renewed."); 117 118 try { 119 Thread.sleep(2000); 120 } 121 catch(InterruptedException ex) { 122 } 123 124 try { 125 lease=leaseClient.renewLease(lease, 1000, false); 126 assertTrue("lease should be expired", false); 127 } 128 catch(LeaseDeniedException ex) { 129 } 131 132 lease=leaseClient.newLease("1", "bar", 1000, false); 133 leaseClient.cancelLease(lease); 134 } 135 136 137 public static void main(String [] args) { 138 String [] testCaseName={LeaseFactoryClientTest.class.getName()}; 139 junit.textui.TestRunner.main(testCaseName); 140 System.out.println("press key to return"); 141 try { 142 System.in.read(); 143 } 144 catch(Exception ex) { 145 System.err.println(ex); 146 } 147 } 148 149 } 150 | Popular Tags |