1 8 9 package com.sleepycat.je.log; 10 11 import java.io.File ; 12 13 import junit.framework.TestCase; 14 15 import com.sleepycat.je.DatabaseException; 16 import com.sleepycat.je.DbInternal; 17 import com.sleepycat.je.Environment; 18 import com.sleepycat.je.EnvironmentConfig; 19 import com.sleepycat.je.config.EnvironmentParams; 20 import com.sleepycat.je.dbi.EnvironmentImpl; 21 import com.sleepycat.je.junit.JUnitThread; 22 import com.sleepycat.je.util.TestUtils; 23 24 27 public class FSyncManagerTest extends TestCase { 28 private File envHome; 29 30 public FSyncManagerTest() { 31 super(); 32 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 33 } 34 35 protected void setUp() 36 throws Exception { 37 38 TestUtils.removeLogFiles("Setup", envHome, false); 39 } 40 41 protected void tearDown() 42 throws Exception { 43 44 TestUtils.removeLogFiles("TearDown", envHome, false); 45 } 46 47 public void testBasic() 48 throws Throwable { 49 Environment env = null; 50 51 try { 52 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 53 envConfig.setConfigParam(EnvironmentParams.LOG_FSYNC_TIMEOUT.getName(), 54 "50000000"); 55 envConfig.setAllowCreate(true); 56 env = new Environment(envHome, envConfig); 57 58 WaitVal waitVal = new WaitVal(0); 59 60 FSyncManager syncManager = 61 new TestSyncManager(DbInternal.envGetEnvironmentImpl(env), 62 waitVal); 63 JUnitThread t1 = new TestSyncThread(syncManager); 64 JUnitThread t2 = new TestSyncThread(syncManager); 65 JUnitThread t3 = new TestSyncThread(syncManager); 66 t1.start(); 67 t2.start(); 68 t3.start(); 69 70 71 Thread.sleep(500); 72 73 74 synchronized (waitVal) { 75 waitVal.value = 1; 76 waitVal.notify(); 77 } 78 79 t1.join(); 80 t2.join(); 81 t3.join(); 82 83 89 assertEquals(3, syncManager.getNFSyncRequests()); 90 assertEquals(2, syncManager.getNFSyncs()); 91 assertEquals(0, syncManager.getNTimeouts()); 92 } finally { 93 if (env != null) { 94 env.close(); 95 } 96 } 97 } 98 99 102 class TestSyncManager extends FSyncManager { 103 private WaitVal waitVal; 104 TestSyncManager(EnvironmentImpl env, WaitVal waitVal) 105 throws DatabaseException { 106 super(env); 107 this.waitVal = waitVal; 108 } 109 protected void executeFSync() 110 throws DatabaseException { 111 try { 112 synchronized (waitVal) { 113 if (waitVal.value < 1) { 114 waitVal.wait(); 115 } 116 } 117 } catch (InterruptedException e) { 118 } 120 } 121 } 122 123 class TestSyncThread extends JUnitThread { 124 private FSyncManager syncManager; 125 TestSyncThread(FSyncManager syncManager) { 126 super("syncThread"); 127 this.syncManager = syncManager; 128 } 129 130 public void testBody() 131 throws Throwable { 132 syncManager.fsync(); 133 } 134 } 135 136 class WaitVal { 137 public int value; 138 139 WaitVal(int value) { 140 this.value = value; 141 } 142 } 143 } 144 | Popular Tags |