1 21 22 package org.apache.derbyTesting.unitTests.services; 23 24 import org.apache.derbyTesting.unitTests.harness.T_Fail; 25 26 import org.apache.derby.iapi.error.StandardException; 27 import org.apache.derby.iapi.services.sanity.SanityManager; 28 import org.apache.derby.iapi.services.locks.*; 29 30 class T_User implements Runnable { 31 32 private LockFactory lf; 33 private Lockable[] refs; 34 private long iterations; 35 private long offset; 36 private int test; 37 38 Throwable error = null; 39 40 41 42 T_User(int test, LockFactory lf, Lockable[] refs, long iterations, long offset) { 43 44 this.lf = lf; 45 this.refs = refs; 46 this.iterations = iterations; 47 this.test = test; 48 this.offset = offset; 49 } 50 51 public void run() { 52 53 try { 54 switch (test) { 55 case 1: 56 T001(); 57 break; 58 case 2: 59 T002(); 60 break; 61 case 3: 62 T003(); 63 break; 64 } 65 } catch (Throwable t) { 66 error = t; 67 } 68 } 69 70 private void T001() throws StandardException, T_Fail { 71 72 Object cs = new Object (); Integer g0 = new Integer (1); 75 checkLockCount(cs, 0); 77 78 T_L1 ref; 79 80 while (--iterations > 0) { 81 long value = offset + iterations; 82 83 lf.lockObject(cs, g0, refs[0], null, C_LockFactory.WAIT_FOREVER); 84 ref = (T_L1) refs[0]; 85 ref.value = value; 86 checkLockCount(cs, 1); 87 Thread.yield(); 88 checkValue(ref, value); 89 90 lf.lockObject(cs, g0, refs[1], null, C_LockFactory.WAIT_FOREVER); 91 ref = (T_L1) refs[1]; 92 ref.value = value; 93 Thread.yield(); 94 95 checkValue((T_L1) refs[0], value); 96 checkValue((T_L1) refs[1], value); 97 98 lf.unlock(cs, g0, refs[0], null); 99 checkValue((T_L1) refs[1], value); 100 101 Thread.yield(); 102 103 lf.unlock(cs, g0, refs[1], null); 104 105 checkLockCount(cs, 0); 107 108 Thread.yield(); 109 110 } 111 } 112 113 private void T002() throws StandardException, T_Fail { 114 115 Object cs = new Object (); Integer g0 = new Integer (1); 118 checkLockCount(cs, 0); 120 121 while (--iterations > 0) { 122 long value = offset + iterations; 123 T_L1 ref = (T_L1) refs[0]; 124 125 lf.lockObject(cs, g0, refs[0], null, C_LockFactory.WAIT_FOREVER); 126 ref.value = value; 127 checkLockCount(cs, 1); 128 Thread.yield(); 129 checkValue(ref, value); 130 131 lf.unlock(cs, g0, refs[0], null); 132 133 checkLockCount(cs, 0); 135 } 136 } 137 138 private void T003() throws StandardException, T_Fail { 139 140 Object cs = new Object (); Integer g0 = new Integer (1); 143 checkLockCount(cs, 0); 145 146 while (--iterations > 0) { 147 148 lf.lockObject(cs, g0, refs[0], null, C_LockFactory.WAIT_FOREVER); 149 checkLockCount(cs, 1); 150 Thread.yield(); 151 lf.unlock(cs, g0, refs[0], null); 152 153 checkLockCount(cs, 0); 155 } 156 } 157 private void T004() throws StandardException, T_Fail { 158 159 Object cs = new Object (); Integer g0 = new Integer (1); 162 checkLockCount(cs, 0); 164 165 while (--iterations > 0) { 166 167 lf.lockObject(cs, g0, refs[0], null, C_LockFactory.WAIT_FOREVER); 168 checkLockCount(cs, 1); 169 Thread.yield(); 170 171 172 lf.lockObject(cs, g0, refs[0], null, C_LockFactory.WAIT_FOREVER); 173 checkLockCount(cs, 2); 174 Thread.yield(); 175 176 lf.unlockGroup(cs, g0); 177 178 checkLockCount(cs, 0); 180 } 181 } 182 183 private void checkValue(T_L1 item, long value) throws T_Fail { 184 if (item.value != value) 185 throw T_Fail.testFailMsg("value corrupted in multi-user test, exapected " + value + ", got " + item.value); 186 } 187 188 void checkLockCount(Object cs, int expected) throws T_Fail { 189 boolean expect = expected != 0; 190 boolean got = lf.areLocksHeld(cs); 191 if (got != expect) 192 throw T_Fail.testFailMsg("Expected lock count (" + expect + "), got (" + got + ")"); 193 } 194 195 196 } 197 | Popular Tags |