1 8 9 package com.sleepycat.je; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 14 import junit.framework.TestCase; 15 16 import com.sleepycat.je.config.EnvironmentParams; 17 import com.sleepycat.je.util.TestUtils; 18 19 32 public class InterruptTest extends TestCase { 33 34 private File envHome; 35 private int NUM_OPS = 1000; 36 private int NUM_ITERATIONS = 1; 37 38 public InterruptTest() { 39 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 40 } 41 42 public void setUp() 43 throws IOException { 44 45 TestUtils.removeLogFiles("Setup", envHome, false); 46 } 47 48 public void testInterruptHandling() 49 throws Exception { 50 51 for (int i = 0; i < NUM_ITERATIONS; i++) { 52 interruptThreads(i); 53 } 54 } 55 56 public void interruptThreads(int i) 57 throws Exception { 58 59 Environment env = null; 61 Database db = null; 62 63 try { 64 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 65 envConfig.setTransactional(true); 66 envConfig.setAllowCreate(true); 67 envConfig.setConfigParam 68 (EnvironmentParams.ENV_CHECK_LEAKS.getName(), "false"); 69 env = new Environment(envHome, envConfig); 70 71 DatabaseConfig dbConfig = new DatabaseConfig(); 72 dbConfig.setAllowCreate(true); 73 dbConfig.setTransactional(true); 74 db = env.openDatabase(null, "testDB" + i, dbConfig); 75 76 ActionThread putter = new ActionThread(env, db, 1) { 77 protected void doStuff(Database db, 78 Transaction txn, 79 DatabaseEntry key, 80 DatabaseEntry value) 81 throws DatabaseException { 82 db.put(txn, key, value); 83 } 84 }; 85 86 ActionThread deleter = new ActionThread(env, db, 1) { 87 protected void doStuff(Database db, 88 Transaction txn, 89 DatabaseEntry key, 90 DatabaseEntry value) 91 throws DatabaseException { 92 db.delete(txn, key); 93 } 94 }; 95 96 putter.start(); 97 Thread.sleep(1000); 98 99 deleter.start(); 100 Thread.sleep(2000); 101 102 106 putter.interrupt(); 107 deleter.interrupt(); 108 109 putter.join(); 110 deleter.join(); 111 } finally { 112 try { 113 if (db != null) { 114 db.close(); 115 } 116 } catch (RunRecoveryException ok) { 117 118 123 } catch (Throwable t) { 124 t.printStackTrace(); 125 fail("Should not see any other kind of exception. Iteration=" + 126 i); 127 } finally { 128 if (env != null) { 129 try { 130 env.close(); 131 env = null; 132 } catch (RunRecoveryException ignore) { 133 134 } 135 } 136 } 137 } 138 } 139 140 abstract class ActionThread extends Thread { 141 private Environment env; 142 private Database db; 143 private int threadNumber; 144 145 public ActionThread(Environment env, Database db, int threadNumber) { 146 this.env = env; 147 this.db = db; 148 this.threadNumber = threadNumber; 149 } 150 151 public void run() { 152 int i=0; 153 Transaction txn = null; 154 try { 155 for ( ; i < NUM_OPS ; i++) { 156 txn = env.beginTransaction(null, null); 157 DatabaseEntry key = new DatabaseEntry(); 158 key.setData(("" + threadNumber * 10000 + i).getBytes()); 159 DatabaseEntry value = new DatabaseEntry(); 160 value.setData(new byte[8192]); 161 doStuff(db, txn, key, value); 162 Thread.sleep(10); 163 txn.commit(); 164 txn = null; 165 } 166 } catch (InterruptedException e) { 167 168 } catch (RunRecoveryException e) { 169 170 } catch (DatabaseException e) { 171 172 } finally { 175 try { 176 if (txn != null) { 177 txn.abort(); 178 } 179 } catch (DatabaseException ignored) { 180 } 181 } 182 } 183 184 abstract protected void doStuff(Database db, 185 Transaction txn, 186 DatabaseEntry key, 187 DatabaseEntry value) 188 throws DatabaseException; 189 } 190 } 191 | Popular Tags |