1 8 9 package com.sleepycat.je.tree; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 14 import junit.framework.TestCase; 15 16 import com.sleepycat.collections.CurrentTransaction; 17 import com.sleepycat.collections.TransactionRunner; 18 import com.sleepycat.collections.TransactionWorker; 19 import com.sleepycat.compat.DbCompat; 20 import com.sleepycat.je.Cursor; 21 import com.sleepycat.je.Database; 22 import com.sleepycat.je.DatabaseConfig; 23 import com.sleepycat.je.DatabaseEntry; 24 import com.sleepycat.je.DatabaseException; 25 import com.sleepycat.je.Environment; 26 import com.sleepycat.je.EnvironmentConfig; 27 import com.sleepycat.je.OperationStatus; 28 import com.sleepycat.je.RunRecoveryException; 29 import com.sleepycat.je.Transaction; 30 import com.sleepycat.je.config.EnvironmentParams; 31 import com.sleepycat.je.dbi.MemoryBudget; 32 import com.sleepycat.je.util.TestUtils; 33 34 36 public class SR13126Test extends TestCase { 37 38 private File envHome; 39 private Environment env; 40 private Database db; 41 private long maxMem; 42 43 public SR13126Test() { 44 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 45 } 46 47 public void setUp() 48 throws IOException { 49 50 TestUtils.removeLogFiles("Setup", envHome, false); 51 } 52 53 public void tearDown() 54 throws Exception { 55 56 try { 57 if (env != null) { 58 env.close(); 59 } 60 } catch (Exception e) { 61 System.out.println("During tearDown: " + e); 62 } 63 64 env = null; 65 db = null; 66 67 TestUtils.removeLogFiles("TearDown", envHome, false); 68 } 69 70 private boolean open() 71 throws DatabaseException { 72 73 maxMem = MemoryBudget.getRuntimeMaxMemory(); 74 if (maxMem == -1) { 75 System.out.println 76 ("*** Warning: not able to run this test because the JVM " + 77 "heap size is not available"); 78 return false; 79 } 80 81 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 82 envConfig.setAllowCreate(true); 83 envConfig.setTransactional(true); 84 85 envConfig.setConfigParam 86 (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false"); 87 envConfig.setConfigParam 88 (EnvironmentParams.ENV_RUN_EVICTOR.getName(), "false"); 89 envConfig.setConfigParam 90 (EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), "false"); 91 envConfig.setConfigParam 92 (EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(), "false"); 93 env = new Environment(envHome, envConfig); 94 95 DatabaseConfig dbConfig = new DatabaseConfig(); 96 dbConfig.setAllowCreate(true); 97 dbConfig.setTransactional(true); 98 db = env.openDatabase(null, "foo", dbConfig); 99 100 return true; 101 } 102 103 private void close() 104 throws DatabaseException { 105 106 db.close(); 107 db = null; 108 109 env.close(); 110 env = null; 111 } 112 113 public void testSR13126() 114 throws DatabaseException { 115 116 if (!open()) { 117 return; 118 } 119 120 Transaction txn = env.beginTransaction(null, null); 121 122 try { 123 insertUntilOutOfMemory(txn); 124 fail("Expected OutOfMemoryError"); 125 } catch (RunRecoveryException expected) {} 126 127 verifyDataAndClose(); 128 } 129 130 public void testTransactionRunner() 131 throws Exception { 132 133 if (!open()) { 134 return; 135 } 136 137 final CurrentTransaction currentTxn = 138 CurrentTransaction.getInstance(env); 139 140 TransactionRunner runner = new TransactionRunner(env); 141 142 DbCompat.TRANSACTION_RUNNER_PRINT_STACK_TRACES = false; 143 try { 144 runner.run(new TransactionWorker() { 145 public void doWork() 146 throws Exception { 147 148 insertUntilOutOfMemory(currentTxn.getTransaction()); 149 } 150 }); 151 fail("Expected OutOfMemoryError"); 152 } catch (RunRecoveryException expected) { } 153 154 158 assertNull(currentTxn.getTransaction()); 159 160 verifyDataAndClose(); 161 } 162 163 private void insertUntilOutOfMemory(Transaction txn) 164 throws DatabaseException, OutOfMemoryError { 165 166 DatabaseEntry key = new DatabaseEntry(new byte[1]); 167 DatabaseEntry data = new DatabaseEntry(); 168 169 int startMem = (int) (maxMem / 3); 170 int bumpMem = (int) ((maxMem - maxMem / 3) / 5); 171 172 173 for (int memSize = startMem;; memSize += bumpMem) { 174 175 179 data.setData(new byte[memSize]); 180 try { 181 db.put(null, key, data); 182 } catch (OutOfMemoryError e) { 183 throw e; 185 } 186 } 187 } 188 189 private void verifyDataAndClose() 190 throws DatabaseException { 191 192 DatabaseEntry key = new DatabaseEntry(); 193 DatabaseEntry data = new DatabaseEntry(); 194 195 200 env.close(); 201 env = null; 202 203 207 open(); 208 Cursor c = db.openCursor(null, null); 209 while (c.getNext(key, data, null) == OperationStatus.SUCCESS) {} 210 c.close(); 211 close(); 212 } 213 } 214 | Popular Tags |