KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > InterruptTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: InterruptTest.java,v 1.11 2006/11/28 19:29:42 mark Exp $
7  */

8
9 package com.sleepycat.je;
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.config.EnvironmentParams;
17 import com.sleepycat.je.util.TestUtils;
18
19 /**
20  * @author Paul.Kendall@orionhealth.com
21  *
22  * This test throws thread interrupts while JE is doing I/O intensive
23  * work. When an interrupt is received during various NIO activities, NIO
24  * closes the underlying file descriptor. In this multi-threaded test, abruptly
25  * closing the file descriptor causes exceptions such as
26  * java.nio.ChannelClosedException, because the uninterrupted thread may be in
27  * the middle of using that file.
28  *
29  * JE must convert all such exceptions to
30  * com.sleepycat.je.RunRecoveryException.
31  */

32 public class InterruptTest extends TestCase {
33
34     private File JavaDoc envHome;
35     private int NUM_OPS = 1000;
36     private int NUM_ITERATIONS = 1;
37
38     public InterruptTest() {
39         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
40     }
41
42     public void setUp()
43         throws IOException JavaDoc {
44
45         TestUtils.removeLogFiles("Setup", envHome, false);
46     }
47     
48     public void testInterruptHandling()
49         throws Exception JavaDoc {
50
51         for (int i = 0; i < NUM_ITERATIONS; i++) {
52             interruptThreads(i);
53         }
54     }
55
56     public void interruptThreads(int i)
57         throws Exception JavaDoc {
58
59         // TestUtils.removeLogFiles("Loop", envHome, false);
60
Environment env = null;
61         Database db = null;
62
63         try {
64             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
65             envConfig.setTransactional(true);
66             envConfig.setAllowCreate(true);
67         envConfig.setConfigParam
68         (EnvironmentParams.ENV_CHECK_LEAKS.getName(), "false");
69             env = new Environment(envHome, envConfig);
70
71             DatabaseConfig dbConfig = new DatabaseConfig();
72             dbConfig.setAllowCreate(true);
73             dbConfig.setTransactional(true);
74             db = env.openDatabase(null, "testDB" + i, dbConfig);
75             
76             ActionThread putter = new ActionThread(env, db, 1) {
77                     protected void doStuff(Database db,
78                                            Transaction txn,
79                                            DatabaseEntry key,
80                                            DatabaseEntry value)
81                         throws DatabaseException {
82                         db.put(txn, key, value);
83                     }
84                 };
85                         
86             ActionThread deleter = new ActionThread(env, db, 1) {
87                     protected void doStuff(Database db,
88                                            Transaction txn,
89                                            DatabaseEntry key,
90                                            DatabaseEntry value)
91                         throws DatabaseException {
92                         db.delete(txn, key);
93                     }
94                 };
95
96             putter.start();
97             Thread.sleep(1000);
98
99             deleter.start();
100             Thread.sleep(2000);
101             
102             /*
103              * Interrupting these threads will catch them in the middle of an
104              * NIO operation, expect a RunRecovery exception.
105              */

106             putter.interrupt();
107             deleter.interrupt();
108             
109             putter.join();
110             deleter.join();
111         } finally {
112             try {
113                 if (db != null) {
114                     db.close();
115                 }
116             } catch (RunRecoveryException ok) {
117
118                 /*
119          * Expect a run recovery exception. Since it will be detected
120                  * when we try to close the database, close the environment
121                  * now so we can re-start in the same JVM.
122                  */

123             } catch (Throwable JavaDoc t) {
124                 t.printStackTrace();
125                 fail("Should not see any other kind of exception. Iteration=" +
126              i);
127             } finally {
128                 if (env != null) {
129                     try {
130                         env.close();
131                         env = null;
132                     } catch (RunRecoveryException ignore) {
133                         /* Sometimes the checkpointer can't close down. */
134                     }
135                 }
136             }
137         }
138     }
139     
140     abstract class ActionThread extends Thread JavaDoc {
141         private Environment env;
142         private Database db;
143         private int threadNumber;
144         
145         public ActionThread(Environment env, Database db, int threadNumber) {
146             this.env = env;
147             this.db = db;
148             this.threadNumber = threadNumber;
149         }
150         
151         public void run() {
152             int i=0;
153             Transaction txn = null;
154             try {
155                 for ( ; i < NUM_OPS ; i++) {
156                     txn = env.beginTransaction(null, null);
157                     DatabaseEntry key = new DatabaseEntry();
158                     key.setData(("" + threadNumber * 10000 + i).getBytes());
159                     DatabaseEntry value = new DatabaseEntry();
160                     value.setData(new byte[8192]);
161                     doStuff(db, txn, key, value);
162                     Thread.sleep(10);
163                     txn.commit();
164                     txn = null;
165                 }
166             } catch (InterruptedException JavaDoc e) {
167                 /* possible outcome. */
168             } catch (RunRecoveryException e) {
169                 /* possible outcome. */
170             } catch (DatabaseException e) {
171                 /* possible outcome. */
172                 //System.out.println("Put to " + i);
173
//e.printStackTrace();
174
} finally {
175                 try {
176                     if (txn != null) {
177                         txn.abort();
178                     }
179                 } catch (DatabaseException ignored) {
180                 }
181             }
182         }
183
184         abstract protected void doStuff(Database db,
185                                         Transaction txn,
186                                         DatabaseEntry key,
187                                         DatabaseEntry value)
188             throws DatabaseException;
189     }
190 }
191
Popular Tags