1 8 9 package com.sleepycat.je.dbi; 10 11 import java.io.File ; 12 import java.util.HashSet ; 13 import java.util.Iterator ; 14 import java.util.Set ; 15 16 import junit.framework.TestCase; 17 18 import com.sleepycat.je.Database; 19 import com.sleepycat.je.DatabaseConfig; 20 import com.sleepycat.je.DbInternal; 21 import com.sleepycat.je.Environment; 22 import com.sleepycat.je.EnvironmentConfig; 23 import com.sleepycat.je.config.EnvironmentParams; 24 import com.sleepycat.je.junit.JUnitThread; 25 import com.sleepycat.je.log.FileManager; 26 import com.sleepycat.je.tree.IN; 27 import com.sleepycat.je.util.TestUtils; 28 29 public class INListTest extends TestCase { 30 private static String DB_NAME = "INListTestDb"; 31 private File envHome; 32 private volatile int sequencer = 0; 33 private Environment env; 34 private EnvironmentImpl envImpl; 35 private Database db; 36 private DatabaseImpl dbImpl; 37 private DatabaseConfig dbConfig; 38 private INList inList1 = null; 39 40 public INListTest() { 41 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 42 dbConfig = new DatabaseConfig(); 43 dbConfig.setAllowCreate(true); 44 45 } 46 47 public void setUp() 48 throws Exception { 49 50 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 51 sequencer = 0; 52 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 53 envConfig.setAllowCreate(true); 54 envConfig.setConfigParam(EnvironmentParams.ENV_RUN_EVICTOR.getName(), 55 "false"); 56 env = new Environment(envHome, envConfig); 57 envImpl = DbInternal.envGetEnvironmentImpl(env); 58 59 inList1 = new INList(envImpl); 60 db = env.openDatabase(null, DB_NAME, dbConfig); 61 dbImpl = DbInternal.dbGetDatabaseImpl(db); 62 } 63 64 public void tearDown() 65 throws Exception { 66 67 inList1 = null; 68 db.close(); 69 env.close(); 70 TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX); 71 } 72 73 public void testMajorMinorLatching() 74 throws Throwable { 75 76 JUnitThread tester1 = 77 new JUnitThread("testMajorMinorLatching-Thread1") { 78 public void testBody() { 79 80 try { 81 82 for (int i = 0; i < 2; i++) { 83 IN in = new IN(dbImpl, null, 1, 1); 84 inList1.add(in); 85 } 86 87 91 inList1.latchMajor(); 92 93 95 sequencer = 1; 96 while (sequencer <= 1) { 97 Thread.yield(); 98 } 99 100 105 int count = 0; 106 Iterator iter = inList1.iterator(); 107 while (iter.hasNext()) { 108 iter.next(); 109 count++; 110 } 111 112 assertEquals(2, count); 113 114 119 sequencer++; while (sequencer <= 3) { 121 Thread.yield(); 122 } 123 124 129 inList1.releaseMajorLatch(); 130 131 135 inList1.latchMajor(); 136 count = 0; 137 iter = inList1.iterator(); 138 while (iter.hasNext()) { 139 iter.next(); 140 count++; 141 } 142 143 assertEquals(4, count); 144 inList1.releaseMajorLatch(); 145 } catch (Throwable T) { 146 T.printStackTrace(System.out); 147 fail("Thread 1 caught some Throwable: " + T); 148 } 149 } 150 }; 151 152 JUnitThread tester2 = 153 new JUnitThread("testMajorMinorLatching-Thread2") { 154 public void testBody() { 155 156 try { 157 158 while (sequencer < 1) { 159 Thread.yield(); 160 } 161 162 assertEquals(1, sequencer); 163 164 168 inList1.add(new IN(dbImpl, null, 1, 1)); 169 sequencer++; 170 171 172 173 while (sequencer < 3) { 174 Thread.yield(); 175 } 176 177 assertEquals(3, sequencer); 178 179 inList1.add(new IN(dbImpl, null, 1, 1)); 180 sequencer++; 181 } catch (Throwable T) { 182 T.printStackTrace(System.out); 183 fail("Thread 2 caught some Throwable: " + T); 184 } 185 } 186 }; 187 188 tester1.start(); 189 tester2.start(); 190 tester1.finishTest(); 191 tester2.finishTest(); 192 } 193 194 200 public void testFetchingWhenHoldingLatch() 201 throws Exception { 202 203 Set expectedNodes = new HashSet (); 204 205 206 IN startIN = null; 207 for (int i = 0; i < 3; i++) { 208 startIN = new IN(dbImpl, null, 1, 1); 209 inList1.add(startIN); 210 expectedNodes.add(startIN); 211 } 212 213 inList1.latchMajor(); 214 try { 215 216 IN inA = new IN(dbImpl, null, 1, 1); 217 inList1.add(inA); 218 IN inB = new IN(dbImpl, null, 1, 1); 219 inList1.add(inB); 220 221 222 checkContents(expectedNodes); 223 224 228 inList1.removeLatchAlreadyHeld(startIN); 229 230 231 expectedNodes.remove(startIN); 232 checkContents(expectedNodes); 233 234 238 inList1.removeLatchAlreadyHeld(inA); 239 expectedNodes.add(inB); 240 checkContents(expectedNodes); 241 242 243 inList1.add(inA); 244 245 246 inList1.releaseMajorLatch(); 247 248 inList1.latchMajor(); 249 expectedNodes.add(inA); 250 checkContents(expectedNodes); 251 252 } finally { 253 inList1.releaseMajorLatchIfHeld(); 254 } 255 } 256 257 private void checkContents(Set expectedNodes) 258 throws Exception { 259 260 Set seen = new HashSet (); 261 Iterator iter = inList1.iterator(); 262 while (iter.hasNext()) { 263 IN foo = (IN)iter.next(); 264 assertTrue(expectedNodes.contains(foo)); 265 assertTrue(!seen.contains(foo)); 266 seen.add(foo); 267 } 268 assertEquals(expectedNodes.size(), seen.size()); 269 } 270 } 271 | Popular Tags |