KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > test > MultiEnvOpenCloseTest


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

8
9 package com.sleepycat.je.test;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import com.sleepycat.bind.tuple.IntegerBinding;
17 import com.sleepycat.je.Database;
18 import com.sleepycat.je.DatabaseConfig;
19 import com.sleepycat.je.DatabaseEntry;
20 import com.sleepycat.je.Environment;
21 import com.sleepycat.je.EnvironmentConfig;
22 import com.sleepycat.je.util.TestUtils;
23
24 /**
25  * Test out-of-memory fix to DaemonThread [#10504].
26  */

27 public class MultiEnvOpenCloseTest extends TestCase {
28
29     private File JavaDoc envHome;
30
31     public void setUp()
32         throws IOException JavaDoc {
33
34         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
35         TestUtils.removeLogFiles("Setup", envHome, false);
36     }
37     
38     public void tearDown()
39         throws Exception JavaDoc {
40
41         TestUtils.removeLogFiles("TearDown", envHome, false);
42     }
43     
44     public void testMultiOpenClose()
45         throws Exception JavaDoc {
46
47         /*
48          * Before fixing the bug in DaemonThread [#10504] this test would run
49          * out of memory after 7 iterations. The bug was, if we open an
50          * environment read-only we won't start certain daemon threads, they
51          * will not be GC'ed because they are part of a thread group, and they
52          * will retain a reference to the Environment. The fix was to not
53          * create the threads until we need to start them.
54          */

55         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
56         envConfig.setAllowCreate(true);
57
58         DatabaseConfig dbConfig = new DatabaseConfig();
59         dbConfig.setAllowCreate(true);
60
61         final int DATA_SIZE = 1024 * 10;
62         final int N_RECORDS = 1000;
63         final int N_ITERS = 30;
64
65         DatabaseEntry key = new DatabaseEntry();
66         DatabaseEntry data = new DatabaseEntry(new byte[DATA_SIZE]);
67
68         Environment env = new Environment(envHome, envConfig);
69         Database db = env.openDatabase(null, "MultiEnvOpenCloseTest",
70                                        dbConfig);
71         for (int i = 0; i < N_RECORDS; i += 1) {
72             IntegerBinding.intToEntry(i, key);
73             db.put(null, key, data);
74         }
75
76         db.close();
77         env.close();
78
79         envConfig.setAllowCreate(false);
80         envConfig.setReadOnly(true);
81         dbConfig.setAllowCreate(false);
82         dbConfig.setReadOnly(true);
83
84         for (int i = 1; i <= N_ITERS; i += 1) {
85             //System.out.println("MultiEnvOpenCloseTest iteration # " + i);
86
env = new Environment(envHome, envConfig);
87             db = env.openDatabase(null, "MultiEnvOpenCloseTest", dbConfig);
88             for (int j = 0; j < N_RECORDS; j += 1) {
89                 IntegerBinding.intToEntry(j, key);
90                 db.get(null, key, data, null);
91             }
92             db.close();
93             env.close();
94         }
95     }
96 }
97
Popular Tags