001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: TxnFSyncTest.java,v 1.10.2.3 2008/01/07 15:14:34 cwl Exp $
007: */
008:
009: package com.sleepycat.je.txn;
010:
011: import junit.framework.Test;
012:
013: import com.sleepycat.je.Database;
014: import com.sleepycat.je.DatabaseConfig;
015: import com.sleepycat.je.DatabaseEntry;
016: import com.sleepycat.je.DbInternal;
017: import com.sleepycat.je.Environment;
018: import com.sleepycat.je.EnvironmentConfig;
019: import com.sleepycat.je.LockMode;
020: import com.sleepycat.je.OperationStatus;
021: import com.sleepycat.je.Transaction;
022: import com.sleepycat.je.config.EnvironmentParams;
023: import com.sleepycat.je.dbi.DbEnvPool;
024: import com.sleepycat.je.dbi.EnvironmentImpl;
025: import com.sleepycat.je.test.TxnTestCase;
026: import com.sleepycat.je.util.TestUtils;
027:
028: /*
029: * Make sure that transactions sync to disk. Mimic a crash by failing to
030: * close the environment and explicitly flush the log manager. If we haven't
031: * properly written and synced data to disk, we'll have unflushed data and
032: * we won't find the expected data in the log.
033: *
034: * Note that this test is run with the TxnTestCase framework and will
035: * be exercised with app-created and autocommit txns.
036: */
037:
038: public class TxnFSyncTest extends TxnTestCase {
039:
040: private static final int NUM_RECS = 5;
041:
042: private static EnvironmentConfig envConfig = TestUtils
043: .initEnvConfig();
044: static {
045: envConfig.setAllowCreate(true);
046: setupEnvConfig(envConfig);
047: }
048:
049: private static void setupEnvConfig(EnvironmentConfig envConfig) {
050: envConfig.setTransactional(true);
051: envConfig.setConfigParam(EnvironmentParams.ENV_RUN_CHECKPOINTER
052: .getName(), "false");
053: }
054:
055: public static Test suite() {
056: /* Run these tests with user and autocommit txns. */
057: return txnTestSuite(TxnFSyncTest.class, envConfig,
058: new String[] { TxnTestCase.TXN_USER,
059: TxnTestCase.TXN_AUTO });
060: }
061:
062: public void testFSyncButNoClose() throws Exception {
063:
064: try {
065: /* Create a database. */
066: DatabaseConfig dbConfig = new DatabaseConfig();
067: dbConfig.setTransactional(isTransactional);
068: dbConfig.setAllowCreate(true);
069: Transaction txn = txnBegin();
070: Database db = env.openDatabase(txn, "foo", dbConfig);
071:
072: /* Insert data. */
073: DatabaseEntry key = new DatabaseEntry();
074: DatabaseEntry data = new DatabaseEntry();
075: for (int i = 0; i < NUM_RECS; i++) {
076: Integer val = new Integer(i);
077: key.setData(val.toString().getBytes());
078: data.setData(val.toString().getBytes());
079:
080: assertEquals(OperationStatus.SUCCESS, db
081: .putNoOverwrite(txn, key, data));
082: }
083: txnCommit(txn);
084:
085: /*
086: * Now throw away this environment WITHOUT flushing the log
087: * manager. We do need to release the environment file lock
088: * and all file handles so we can recover in this test and
089: * run repeated test cases within this one test program.
090: */
091: EnvironmentImpl envImpl = DbInternal
092: .envGetEnvironmentImpl(env);
093: envImpl.getFileManager().clear(); // release file handles
094: envImpl.getFileManager().close(); // release file lock
095: env = null;
096: DbEnvPool.getInstance().clear();
097:
098: /*
099: * Open the environment and database again. The database should
100: * exist.
101: */
102: EnvironmentConfig envConfig2 = TestUtils.initEnvConfig();
103: setupEnvConfig(envConfig2);
104: env = new Environment(envHome, envConfig2);
105: dbConfig.setAllowCreate(false);
106: db = env.openDatabase(null, "foo", dbConfig);
107:
108: /* Read all the data. */
109: for (int i = 0; i < NUM_RECS; i++) {
110: Integer val = new Integer(i);
111: key.setData(val.toString().getBytes());
112:
113: assertEquals(OperationStatus.SUCCESS, db.get(null, key,
114: data, LockMode.DEFAULT));
115: /* add test of data. */
116: }
117: db.close();
118: } catch (Exception e) {
119: e.printStackTrace();
120: throw e;
121: }
122: }
123: }
|