1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: FSyncManagerTest.java,v 1.13.2.2 2010/01/04 15:30:44 cwl Exp $
*/
package com.sleepycat.je.log;
import java.io.File;
import junit.framework.TestCase;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.DbInternal;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.dbi.EnvironmentImpl;
import com.sleepycat.je.junit.JUnitThread;
import com.sleepycat.je.util.TestUtils;
/**
* Exercise the synchronization aspects of the sync manager.
*/
public class FSyncManagerTest extends TestCase {
private File envHome;
public FSyncManagerTest() {
super();
envHome = new File(System.getProperty(TestUtils.DEST_DIR));
}
protected void setUp()
throws Exception {
/* Remove files to start with a clean slate. */
TestUtils.removeLogFiles("Setup", envHome, false);
}
protected void tearDown()
throws Exception {
TestUtils.removeLogFiles("TearDown", envHome, false);
}
public void testBasic()
throws Throwable{
Environment env = null;
try {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setConfigParam(EnvironmentParams.LOG_FSYNC_TIMEOUT.getName(),
"50000000");
envConfig.setAllowCreate(true);
env = new Environment(envHome, envConfig);
WaitVal waitVal = new WaitVal(0);
FSyncManager syncManager =
new TestSyncManager(DbInternal.envGetEnvironmentImpl(env),
waitVal);
JUnitThread t1 = new TestSyncThread(syncManager);
JUnitThread t2 = new TestSyncThread(syncManager);
JUnitThread t3 = new TestSyncThread(syncManager);
t1.start();
t2.start();
t3.start();
/* Wait for all threads to request a sync, so they form a group.*/
Thread.sleep(500);
/* Free thread 1. */
synchronized (waitVal) {
waitVal.value = 1;
waitVal.notify();
}
t1.join();
t2.join();
t3.join();
/*
* All three threads ask for fsyncs.
* 2 do fsyncs -- the initial leader, and the leader of the
* waiting group of 2.
* The last thread gets a free ride.
*/
assertEquals(3, syncManager.getNFSyncRequests());
assertEquals(2, syncManager.getNFSyncs());
assertEquals(0, syncManager.getNTimeouts());
} finally {
if (env != null) {
env.close();
}
}
}
/* This test class waits for an object instead of executing a sync.
* This way, we can manipulate grouping behavior.
*/
class TestSyncManager extends FSyncManager {
private WaitVal waitVal;
TestSyncManager(EnvironmentImpl env, WaitVal waitVal)
throws DatabaseException {
super(env);
this.waitVal = waitVal;
}
protected void executeFSync()
throws DatabaseException {
try {
synchronized (waitVal) {
if (waitVal.value < 1) {
waitVal.wait();
}
}
} catch (InterruptedException e) {
// woken up.
}
}
}
class TestSyncThread extends JUnitThread {
private FSyncManager syncManager;
TestSyncThread(FSyncManager syncManager) {
super("syncThread");
this.syncManager = syncManager;
}
public void testBody()
throws Throwable {
syncManager.fsync();
}
}
class WaitVal {
public int value;
WaitVal(int value) {
this.value = value;
}
}
}
|