1 8 9 package com.sleepycat.collections.test; 10 11 import com.sleepycat.je.Database; 12 import com.sleepycat.je.DeadlockException; 13 import com.sleepycat.je.Environment; 14 import com.sleepycat.je.TransactionConfig; 15 import com.sleepycat.collections.StoredSortedMap; 16 import com.sleepycat.collections.TransactionRunner; 17 import com.sleepycat.collections.TransactionWorker; 18 import com.sleepycat.util.ExceptionUnwrapper; 19 import junit.framework.Test; 20 import junit.framework.TestCase; 21 import junit.framework.TestSuite; 22 23 32 public class SecondaryDeadlockTest extends TestCase { 33 34 private static final Long N_ONE = new Long (1); 35 private static final Long N_101 = new Long (101); 36 private static final int N_ITERS = 20; 37 private static final int MAX_RETRIES = 1000; 38 39 public static void main(String [] args) 40 throws Exception { 41 42 junit.framework.TestResult tr = 43 junit.textui.TestRunner.run(suite()); 44 if (tr.errorCount() > 0 || 45 tr.failureCount() > 0) { 46 System.exit(1); 47 } else { 48 System.exit(0); 49 } 50 } 51 52 public static Test suite() 53 throws Exception { 54 55 TestSuite suite = new TestSuite(SecondaryDeadlockTest.class); 56 return suite; 57 } 58 59 private Environment env; 60 private Database store; 61 private Database index; 62 private StoredSortedMap storeMap; 63 private StoredSortedMap indexMap; 64 private Exception exception; 65 66 public SecondaryDeadlockTest(String name) { 67 68 super(name); 69 } 70 71 public void setUp() 72 throws Exception { 73 74 env = TestEnv.TXN.open("SecondaryDeadlockTest"); 75 store = TestStore.BTREE_UNIQ.open(env, "store.db"); 76 index = TestStore.BTREE_UNIQ.openIndex(store, "index.db"); 77 storeMap = new StoredSortedMap(store, 78 TestStore.BTREE_UNIQ.getKeyBinding(), 79 TestStore.BTREE_UNIQ.getValueBinding(), 80 true); 81 indexMap = new StoredSortedMap(index, 82 TestStore.BTREE_UNIQ.getKeyBinding(), 83 TestStore.BTREE_UNIQ.getValueBinding(), 84 true); 85 } 86 87 public void tearDown() { 88 89 if (index != null) { 90 try { 91 index.close(); 92 } catch (Exception e) { 93 System.out.println("Ignored exception during tearDown: " + e); 94 } 95 } 96 if (store != null) { 97 try { 98 store.close(); 99 } catch (Exception e) { 100 System.out.println("Ignored exception during tearDown: " + e); 101 } 102 } 103 if (env != null) { 104 try { 105 env.close(); 106 } catch (Exception e) { 107 System.out.println("Ignored exception during tearDown: " + e); 108 } 109 } 110 111 env = null; 112 store = null; 113 index = null; 114 storeMap = null; 115 indexMap = null; 116 } 117 118 public void testSecondaryDeadlock() 119 throws Exception { 120 121 final TransactionRunner runner = new TransactionRunner(env); 122 runner.setMaxRetries(MAX_RETRIES); 123 124 129 TransactionConfig txnConfig = new TransactionConfig(); 130 runner.setTransactionConfig(txnConfig); 131 132 136 final Thread thread1 = new Thread (new Runnable () { 137 public void run() { 138 try { 139 140 for (int i = 0; i < N_ITERS; i +=1 ) { 141 runner.run(new TransactionWorker() { 142 public void doWork() throws Exception { 143 assertEquals(null, storeMap.put(N_ONE, N_101)); 144 } 145 }); 146 runner.run(new TransactionWorker() { 147 public void doWork() throws Exception { 148 assertEquals(N_101, storeMap.remove(N_ONE)); 149 } 150 }); 151 } 152 } catch (Exception e) { 153 e.printStackTrace(); 154 exception = e; 155 } 156 } 157 }, "ThreadOne"); 158 159 163 final Thread thread2 = new Thread (new Runnable () { 164 public void run() { 165 try { 166 for (int i = 0; i < N_ITERS; i +=1 ) { 167 for (int j = 0; j < MAX_RETRIES; j += 1) { 168 try { 169 Object value = indexMap.get(N_ONE); 170 assertTrue(value == null || 171 N_101.equals(value)); 172 break; 173 } catch (Exception e) { 174 e = ExceptionUnwrapper.unwrap(e); 175 if (e instanceof DeadlockException) { 176 continue; 177 } else { 178 throw e; 179 } 180 } 181 } 182 } 183 } catch (Exception e) { 184 e.printStackTrace(); 185 exception = e; 186 } 187 } 188 }, "ThreadTwo"); 189 190 thread1.start(); 191 thread2.start(); 192 thread1.join(); 193 thread2.join(); 194 195 index.close(); 196 index = null; 197 store.close(); 198 store = null; 199 env.close(); 200 env = null; 201 202 if (exception != null) { 203 fail(exception.toString()); 204 } 205 } 206 } 207 | Popular Tags |