1 8 9 package com.sleepycat.je.txn; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 14 import junit.framework.TestCase; 15 16 import com.sleepycat.je.Database; 17 import com.sleepycat.je.DatabaseConfig; 18 import com.sleepycat.je.DatabaseException; 19 import com.sleepycat.je.DeadlockException; 20 import com.sleepycat.je.Environment; 21 import com.sleepycat.je.EnvironmentConfig; 22 import com.sleepycat.je.LockStats; 23 import com.sleepycat.je.Transaction; 24 import com.sleepycat.je.log.FileManager; 25 import com.sleepycat.je.util.TestUtils; 26 27 30 public class TxnTimeoutTest extends TestCase { 31 32 private Environment env; 33 private File envHome; 34 35 public TxnTimeoutTest() 36 throws DatabaseException { 37 38 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 39 } 40 41 public void setUp() 42 throws IOException , DatabaseException { 43 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 44 } 45 46 public void tearDown() 47 throws IOException , DatabaseException { 48 49 if (env != null) { 50 env.close(); 51 } 52 TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX); 53 } 54 55 private void createEnv(boolean setTimeout, 56 long txnTimeoutVal, 57 long lockTimeoutVal) 58 throws DatabaseException { 59 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 60 envConfig.setTransactional(true); 61 envConfig.setAllowCreate(true); 62 if (setTimeout) { 63 envConfig.setTxnTimeout(txnTimeoutVal); 64 envConfig.setLockTimeout(lockTimeoutVal); 65 } 66 67 env = new Environment(envHome, envConfig); 68 } 69 70 73 public void testTxnTimeout() 74 throws Throwable { 75 76 try { 77 createEnv(false, 0, 0); 78 79 Transaction txnA = env.beginTransaction(null, null); 80 81 82 DatabaseConfig dbConfig = new DatabaseConfig(); 83 dbConfig.setTransactional(true); 84 dbConfig.setAllowCreate(true); 85 env.openDatabase(txnA, "foo", dbConfig); 86 87 88 Transaction txnB = env.beginTransaction(null, null); 89 txnB.setTxnTimeout(300000); txnB.setLockTimeout(9000000); 91 Thread.sleep(400); 92 93 try { 94 env.openDatabase(txnB, "foo", dbConfig); 95 fail("Should time out"); 96 } catch (DeadlockException e) { 97 98 assertTrue(TestUtils.skipVersion(e).startsWith("Transaction ")); 99 100 txnB.abort(); 101 } catch (Exception e) { 102 e.printStackTrace(); 103 fail("Should not get another kind of exception"); 104 } 105 106 107 txnB = env.beginTransaction(null, null); 108 txnB.setLockTimeout(100000); 109 110 try { 111 env.openDatabase(txnB, "foo", dbConfig); 112 fail("Should time out"); 113 } catch (DeadlockException e) { 114 assertTrue(TestUtils.skipVersion(e).startsWith("Lock ")); 115 116 txnB.abort(); 117 } catch (Exception e) { 118 e.printStackTrace(); 119 fail("Should not get another kind of exception"); 120 } 121 122 txnA.abort(); 123 LockStats stats = env.getLockStats(TestUtils.FAST_STATS); 124 assertEquals(2, stats.getNWaits()); 125 126 } catch (Throwable t) { 127 128 132 t.printStackTrace(); 133 throw t; 134 } 135 } 136 137 140 public void testPerTxnTimeout() 141 throws Throwable { 142 doEnvTimeout(false, true, true, 300000, 9000000, false); 143 } 144 145 148 public void testEnvTxnTimeout() 149 throws Throwable { 150 doEnvTimeout(true, true, true, 300000, 9000000, false); 151 } 152 153 157 public void testEnvNoLockTimeout() 158 throws Throwable { 159 doEnvTimeout(true, true, true, 300000, 0, false); 160 } 161 162 165 public void testPerLockTimeout() 166 throws Throwable { 167 doEnvTimeout(false, false, true, 0, 100000, true); 168 } 169 170 174 public void testEnvLockTimeout() 175 throws Throwable { 176 doEnvTimeout(true, false, true, 0, 100000, true); 177 } 178 179 189 private void doEnvTimeout(boolean setEnvConfigTimeout, 190 boolean setPerTxnTimeout, 191 boolean setPerLockTimeout, 192 long txnTimeout, 193 long lockTimeout, 194 boolean expectLockException) 195 throws Throwable { 196 197 try { 198 createEnv(setEnvConfigTimeout, txnTimeout, lockTimeout); 199 200 Transaction txnA = env.beginTransaction(null, null); 201 DatabaseConfig dbConfig = new DatabaseConfig(); 202 dbConfig.setTransactional(true); 203 dbConfig.setAllowCreate(true); 204 Database dbA = env.openDatabase(txnA, "foo", dbConfig); 205 206 210 Transaction txnB = env.beginTransaction(null, null); 211 if (!setEnvConfigTimeout) { 212 if (setPerTxnTimeout) { 213 txnB.setTxnTimeout(300000); 214 } 215 if (setPerLockTimeout) { 216 txnB.setLockTimeout(9000000); 217 } 218 } 219 220 Thread.sleep(400); 221 222 try { 223 env.openDatabase(txnB, "foo", dbConfig); 224 fail("Should time out"); 225 } catch (DeadlockException e) { 226 if (expectLockException) { 227 assertTrue(TestUtils.skipVersion(e).startsWith("Lock ")); 228 } else { 229 assertTrue(TestUtils.skipVersion(e).startsWith("Transaction ")); 230 } 231 232 233 txnB.abort(); 234 } catch (Exception e) { 235 e.printStackTrace(); 236 fail("Should not get another kind of exception"); 237 } 238 239 dbA.close(); 240 txnA.abort(); 241 } catch (Throwable t) { 242 243 247 t.printStackTrace(); 248 throw t; 249 } 250 } 251 } 252 | Popular Tags |