KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > tree > SR13126Test


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2005,2006 Oracle. All rights reserved.
5  *
6  * $Id: SR13126Test.java,v 1.9 2006/10/30 21:14:52 bostic Exp $
7  */

8
9 package com.sleepycat.je.tree;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import com.sleepycat.collections.CurrentTransaction;
17 import com.sleepycat.collections.TransactionRunner;
18 import com.sleepycat.collections.TransactionWorker;
19 import com.sleepycat.compat.DbCompat;
20 import com.sleepycat.je.Cursor;
21 import com.sleepycat.je.Database;
22 import com.sleepycat.je.DatabaseConfig;
23 import com.sleepycat.je.DatabaseEntry;
24 import com.sleepycat.je.DatabaseException;
25 import com.sleepycat.je.Environment;
26 import com.sleepycat.je.EnvironmentConfig;
27 import com.sleepycat.je.OperationStatus;
28 import com.sleepycat.je.RunRecoveryException;
29 import com.sleepycat.je.Transaction;
30 import com.sleepycat.je.config.EnvironmentParams;
31 import com.sleepycat.je.dbi.MemoryBudget;
32 import com.sleepycat.je.util.TestUtils;
33
34 /**
35  */

36 public class SR13126Test extends TestCase {
37
38     private File JavaDoc envHome;
39     private Environment env;
40     private Database db;
41     private long maxMem;
42
43     public SR13126Test() {
44         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
45     }
46
47     public void setUp()
48         throws IOException JavaDoc {
49
50         TestUtils.removeLogFiles("Setup", envHome, false);
51     }
52     
53     public void tearDown()
54         throws Exception JavaDoc {
55
56         try {
57             if (env != null) {
58         env.close();
59             }
60         } catch (Exception JavaDoc e) {
61             System.out.println("During tearDown: " + e);
62         }
63
64         env = null;
65         db = null;
66
67         TestUtils.removeLogFiles("TearDown", envHome, false);
68     }
69
70     private boolean open()
71     throws DatabaseException {
72
73         maxMem = MemoryBudget.getRuntimeMaxMemory();
74         if (maxMem == -1) {
75             System.out.println
76                 ("*** Warning: not able to run this test because the JVM " +
77                  "heap size is not available");
78             return false;
79         }
80
81         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
82         envConfig.setAllowCreate(true);
83         envConfig.setTransactional(true);
84         /* Do not run the daemons to avoid timing considerations. */
85         envConfig.setConfigParam
86             (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");
87         envConfig.setConfigParam
88             (EnvironmentParams.ENV_RUN_EVICTOR.getName(), "false");
89         envConfig.setConfigParam
90         (EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), "false");
91         envConfig.setConfigParam
92             (EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(), "false");
93         env = new Environment(envHome, envConfig);
94
95         DatabaseConfig dbConfig = new DatabaseConfig();
96         dbConfig.setAllowCreate(true);
97         dbConfig.setTransactional(true);
98         db = env.openDatabase(null, "foo", dbConfig);
99
100         return true;
101     }
102
103     private void close()
104     throws DatabaseException {
105
106         db.close();
107         db = null;
108
109         env.close();
110         env = null;
111     }
112
113     public void testSR13126()
114     throws DatabaseException {
115
116         if (!open()) {
117             return;
118         }
119
120         Transaction txn = env.beginTransaction(null, null);
121
122         try {
123             insertUntilOutOfMemory(txn);
124             fail("Expected OutOfMemoryError");
125         } catch (RunRecoveryException expected) {}
126
127         verifyDataAndClose();
128     }
129
130     public void testTransactionRunner()
131     throws Exception JavaDoc {
132
133         if (!open()) {
134             return;
135         }
136
137         final CurrentTransaction currentTxn =
138             CurrentTransaction.getInstance(env);
139
140         TransactionRunner runner = new TransactionRunner(env);
141     /* Don't print exception stack traces during test runs. */
142     DbCompat.TRANSACTION_RUNNER_PRINT_STACK_TRACES = false;
143         try {
144             runner.run(new TransactionWorker() {
145                 public void doWork()
146                     throws Exception JavaDoc {
147
148                     insertUntilOutOfMemory(currentTxn.getTransaction());
149                 }
150             });
151             fail("Expected OutOfMemoryError");
152         } catch (RunRecoveryException expected) { }
153
154         /*
155          * If TransactionRunner does not abort the transaction, this thread
156          * will be left with a transaction attached.
157          */

158         assertNull(currentTxn.getTransaction());
159
160         verifyDataAndClose();
161     }
162
163     private void insertUntilOutOfMemory(Transaction txn)
164     throws DatabaseException, OutOfMemoryError JavaDoc {
165
166         DatabaseEntry key = new DatabaseEntry(new byte[1]);
167         DatabaseEntry data = new DatabaseEntry();
168
169         int startMem = (int) (maxMem / 3);
170         int bumpMem = (int) ((maxMem - maxMem / 3) / 5);
171
172         /* Insert larger and larger LNs until an OutOfMemoryError occurs. */
173         for (int memSize = startMem;; memSize += bumpMem) {
174
175             /*
176              * If the memory error occurs when we do "new byte[]" below, this
177              * is not a test of the bug in question, so the test fails.
178              */

179             data.setData(new byte[memSize]);
180             try {
181                 db.put(null, key, data);
182             } catch (OutOfMemoryError JavaDoc e) {
183                 //System.err.println("Error during write " + memSize);
184
throw e;
185             }
186         }
187     }
188
189     private void verifyDataAndClose()
190     throws DatabaseException {
191
192         DatabaseEntry key = new DatabaseEntry();
193         DatabaseEntry data = new DatabaseEntry();
194
195         /*
196          * If a NULL_LSN is present in a BIN entry because of an incomplete
197          * insert, an assertion will fire during the checkpoint when writing
198          * the BIN.
199          */

200         env.close();
201         env = null;
202
203         /*
204          * If the NULL_LSN was written above because assertions are disabled,
205          * check that we don't get an exception when fetching it.
206          */

207         open();
208         Cursor c = db.openCursor(null, null);
209         while (c.getNext(key, data, null) == OperationStatus.SUCCESS) {}
210         c.close();
211         close();
212     }
213 }
214
Popular Tags