KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > txn > TxnTimeoutTest


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

8
9 package com.sleepycat.je.txn;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import com.sleepycat.je.Database;
17 import com.sleepycat.je.DatabaseConfig;
18 import com.sleepycat.je.DatabaseException;
19 import com.sleepycat.je.DeadlockException;
20 import com.sleepycat.je.Environment;
21 import com.sleepycat.je.EnvironmentConfig;
22 import com.sleepycat.je.LockStats;
23 import com.sleepycat.je.Transaction;
24 import com.sleepycat.je.log.FileManager;
25 import com.sleepycat.je.util.TestUtils;
26
27 /*
28  * Test transaction and lock timeouts.
29  */

30 public class TxnTimeoutTest extends TestCase {
31     
32     private Environment env;
33     private File JavaDoc envHome;
34    
35     public TxnTimeoutTest()
36     throws DatabaseException {
37
38         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
39     }
40
41     public void setUp()
42         throws IOException JavaDoc, DatabaseException {
43         TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
44     }
45
46     public void tearDown()
47         throws IOException JavaDoc, DatabaseException {
48
49         if (env != null) {
50             env.close();
51         }
52         TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
53     }
54
55     private void createEnv(boolean setTimeout,
56                            long txnTimeoutVal,
57                            long lockTimeoutVal)
58         throws DatabaseException {
59         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
60         envConfig.setTransactional(true);
61         envConfig.setAllowCreate(true);
62         if (setTimeout) {
63             envConfig.setTxnTimeout(txnTimeoutVal);
64             envConfig.setLockTimeout(lockTimeoutVal);
65         }
66
67         env = new Environment(envHome, envConfig);
68     }
69
70     /**
71      * Test timeout set at txn level.
72      */

73     public void testTxnTimeout()
74         throws Throwable JavaDoc {
75
76         try {
77             createEnv(false, 0, 0);
78
79             Transaction txnA = env.beginTransaction(null, null);
80
81             /* Grab a lock */
82             DatabaseConfig dbConfig = new DatabaseConfig();
83             dbConfig.setTransactional(true);
84             dbConfig.setAllowCreate(true);
85             env.openDatabase(txnA, "foo", dbConfig);
86
87             /* Now make a second txn so we can induce some blocking. */
88             Transaction txnB = env.beginTransaction(null, null);
89             txnB.setTxnTimeout(300000); // microseconds
90
txnB.setLockTimeout(9000000);
91             Thread.sleep(400);
92
93             try {
94                 env.openDatabase(txnB, "foo", dbConfig);
95                 fail("Should time out");
96             } catch (DeadlockException e) {
97                 /* Skip the version string. */
98                 assertTrue(TestUtils.skipVersion(e).startsWith("Transaction "));
99                 /* Good, expect this exception */
100                 txnB.abort();
101             } catch (Exception JavaDoc e) {
102                 e.printStackTrace();
103                 fail("Should not get another kind of exception");
104             }
105
106             /* Now try a lock timeout. */
107             txnB = env.beginTransaction(null, null);
108             txnB.setLockTimeout(100000);
109
110             try {
111                 env.openDatabase(txnB, "foo", dbConfig);
112                 fail("Should time out");
113             } catch (DeadlockException e) {
114                 assertTrue(TestUtils.skipVersion(e).startsWith("Lock "));
115                 /* Good, expect this exception */
116                 txnB.abort();
117             } catch (Exception JavaDoc e) {
118                 e.printStackTrace();
119                 fail("Should not get another kind of exception");
120             }
121
122             txnA.abort();
123             LockStats stats = env.getLockStats(TestUtils.FAST_STATS);
124             assertEquals(2, stats.getNWaits());
125
126         } catch (Throwable JavaDoc t) {
127
128             /*
129              * Print stack trace before trying to clean up je files in
130              * teardown.
131              */

132             t.printStackTrace();
133             throw t;
134         }
135     }
136
137     /**
138      * Use Txn.setTimeOut(), expect a txn timeout.
139      */

140     public void testPerTxnTimeout()
141         throws Throwable JavaDoc {
142         doEnvTimeout(false, true, true, 300000, 9000000, false);
143     }
144
145     /**
146      * Use EnvironmentConfig.setTxnTimeOut(), expect a txn timeout.
147      */

148     public void testEnvTxnTimeout()
149         throws Throwable JavaDoc {
150         doEnvTimeout(true, true, true, 300000, 9000000, false);
151     }
152
153     /**
154      * Use EnvironmentConfig.setTxnTimeOut(), use
155      * EnvironmentConfig.setLockTimeout(0), expect a txn timeout.
156      */

157     public void testEnvNoLockTimeout()
158         throws Throwable JavaDoc {
159         doEnvTimeout(true, true, true, 300000, 0, false);
160     }
161
162     /**
163      * Use Txn.setLockTimeout(), expect a lock timeout.
164      */

165     public void testPerLockTimeout()
166         throws Throwable JavaDoc {
167         doEnvTimeout(false, false, true, 0, 100000, true);
168     }
169
170     /**
171      * Use EnvironmentConfig.setTxnTimeOut(0), Use
172      * EnvironmentConfig.setLockTimeout(xxx), expect a lcok timeout.
173      */

174     public void testEnvLockTimeout()
175         throws Throwable JavaDoc {
176         doEnvTimeout(true, false, true, 0, 100000, true);
177     }
178
179     /**
180      * @param setEnvConfigTimeout
181      * if true, use EnvironmentConfig.set{Lock,Txn}TimeOut
182      * @param setPerTxnTimeout if true, use Txn.setTxnTimeout()
183      * @param setPerLockTimeout if true, use Txn.setLockTimeout()
184      * @param long txnTimeout value for txn timeout
185      * @param long lockTimeout value for lock timeout
186      * @param expectLockException if true, expect a LockTimoutException, if
187      * false, expect a TxnTimeoutException
188      */

189     private void doEnvTimeout(boolean setEnvConfigTimeout,
190                               boolean setPerTxnTimeout,
191                               boolean setPerLockTimeout,
192                               long txnTimeout,
193                               long lockTimeout,
194                               boolean expectLockException)
195         throws Throwable JavaDoc {
196
197         try {
198             createEnv(setEnvConfigTimeout, txnTimeout, lockTimeout);
199
200             Transaction txnA = env.beginTransaction(null, null);
201             DatabaseConfig dbConfig = new DatabaseConfig();
202             dbConfig.setTransactional(true);
203             dbConfig.setAllowCreate(true);
204             Database dbA = env.openDatabase(txnA, "foo", dbConfig);
205
206             /*
207          * Now make a second txn so we can induce some blocking. Make the
208          * txn timeout environment wide.
209          */

210             Transaction txnB = env.beginTransaction(null, null);
211             if (!setEnvConfigTimeout) {
212                 if (setPerTxnTimeout) {
213                     txnB.setTxnTimeout(300000);
214                 }
215                 if (setPerLockTimeout) {
216                     txnB.setLockTimeout(9000000);
217                 }
218             }
219
220             Thread.sleep(400);
221
222             try {
223                 env.openDatabase(txnB, "foo", dbConfig);
224                 fail("Should time out");
225             } catch (DeadlockException e) {
226                 if (expectLockException) {
227                     assertTrue(TestUtils.skipVersion(e).startsWith("Lock "));
228                 } else {
229                     assertTrue(TestUtils.skipVersion(e).startsWith("Transaction "));
230                 }
231
232                 /* Good, expect this exception */
233                 txnB.abort();
234             } catch (Exception JavaDoc e) {
235                 e.printStackTrace();
236                 fail("Should not get another kind of exception");
237             }
238
239             dbA.close();
240             txnA.abort();
241         } catch (Throwable JavaDoc t) {
242
243             /*
244              * Print stack trace before trying to clean up je files in
245              * teardown.
246              */

247             t.printStackTrace();
248             throw t;
249         }
250     }
251 }
252
Popular Tags