1 package org.jgroups.blocks; 2 3 import java.lang.reflect.Constructor ; 4 import java.lang.reflect.Field ; 5 import java.util.HashMap ; 6 7 import junit.framework.Test; 8 import junit.framework.TestSuite; 9 10 import org.jgroups.Channel; 11 import org.jgroups.tests.ChannelTestBase; 12 13 19 public class DistributedLockManagerTest extends ChannelTestBase { 20 21 public DistributedLockManagerTest(String testName) { 22 super(testName); 23 } 24 25 public static Test suite() { 26 return new TestSuite(DistributedLockManagerTest.class); 27 } 28 29 30 private Channel channel1; 31 private Channel channel2; 32 33 protected VotingAdapter adapter1; 34 protected VotingAdapter adapter2; 35 36 protected LockManager lockManager1; 37 protected LockManager lockManager2; 38 39 protected static boolean logConfigured; 40 41 public void setUp() throws Exception { 42 super.setUp(); 43 channel1=createChannel("A"); 44 adapter1=new VotingAdapter(channel1); 45 channel1.connect("voting"); 46 47 48 lockManager1=new DistributedLockManager(adapter1, "1"); 49 50 try { 52 Thread.sleep(1000); 53 } 54 catch(Exception ex) { 55 } 56 57 channel2=createChannel("A"); 58 adapter2=new VotingAdapter(channel2); 59 lockManager2=new DistributedLockManager(adapter2, "2"); 60 61 channel2.connect("voting"); 62 63 try { 64 Thread.sleep(1000); 65 } 66 catch(InterruptedException ex) { 67 } 68 } 69 70 public void tearDown() throws Exception { 71 super.tearDown(); 72 channel2.close(); 73 74 try { 75 Thread.sleep(1000); 76 } 77 catch (InterruptedException ex) { 78 } 79 80 81 channel1.close(); 82 } 83 84 public void test() throws Exception { 85 lockManager1.lock("obj1", "owner1", 10000); 86 87 try { 88 lockManager1.lock("obj1", "owner2", 10000); 89 assertTrue("obj1 should not be locked.", false); 90 } catch (LockNotGrantedException ex) { 91 } 93 94 lockManager2.lock("obj2", "owner2", 1000); 95 96 lockManager1.unlock("obj1", "owner1"); 97 98 try { 99 lockManager1.unlock("obj2", "owner1"); 100 assertTrue("obj2 should not be released.", false); 101 } 102 catch (LockNotReleasedException ex) { 103 } 105 106 lockManager1.unlock("obj2", "owner2"); 107 108 } 109 110 public void testMultiLock() throws Exception { 111 lockManager1.lock("obj1", "owner1", 10000); 112 113 Class acquireLockDecreeClass = Class.forName("org.jgroups.blocks.DistributedLockManager$AcquireLockDecree"); 116 Constructor acquireLockDecreeConstructor = acquireLockDecreeClass.getDeclaredConstructor(new Class [] {Object .class, Object .class, Object .class}); 117 acquireLockDecreeConstructor.setAccessible(true); 118 Object acquireLockDecree = acquireLockDecreeConstructor.newInstance(new Object [] {"obj1", "owner2", "2"}); 119 120 Field heldLocksField = lockManager2.getClass().getDeclaredField("heldLocks"); 121 heldLocksField.setAccessible(true); 122 HashMap heldLocks = (HashMap )heldLocksField.get(lockManager2); 123 heldLocks.put("obj1", acquireLockDecree); 124 125 127 try { 128 lockManager1.unlock("obj1", "owner1", true); 129 assertTrue("obj1 should throw a lockMultiLockedException upon release.", false); 130 } catch (LockMultiLockedException e) { 131 } 133 134 try { 135 lockManager1.lock("obj1", "owner1", 10000); 136 assertTrue("obj1 should throw a LockNotGrantedException because it is still locked by lockManager2.", false); 137 } catch (LockNotGrantedException e) { 138 } 140 141 try { 142 lockManager2.unlock("obj1", "owner2", true); 143 assertTrue("obj1 should throw a lockMultiLockedException upon release.", false); 144 } catch (LockMultiLockedException e) { 145 } 147 148 try { 150 lockManager1.lock("obj1", "owner1", 10000); 151 } catch (LockNotGrantedException e) { 152 assertTrue("obj1 should be unlocked", false); 153 } 154 155 lockManager1.unlock("obj1", "owner1", true); 156 } 157 158 public static void main(String [] args) { 159 junit.textui.TestRunner.run(suite()); 160 } 161 } 162 | Popular Tags |