KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > log > FSyncManagerTest


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

8
9 package com.sleepycat.je.log;
10
11 import java.io.File JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import com.sleepycat.je.DatabaseException;
16 import com.sleepycat.je.DbInternal;
17 import com.sleepycat.je.Environment;
18 import com.sleepycat.je.EnvironmentConfig;
19 import com.sleepycat.je.config.EnvironmentParams;
20 import com.sleepycat.je.dbi.EnvironmentImpl;
21 import com.sleepycat.je.junit.JUnitThread;
22 import com.sleepycat.je.util.TestUtils;
23
24 /**
25  * Exercise the synchronization aspects of the sync manager.
26  */

27 public class FSyncManagerTest extends TestCase {
28     private File JavaDoc envHome;
29
30     public FSyncManagerTest() {
31         super();
32         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
33     }
34
35     protected void setUp()
36         throws Exception JavaDoc {
37         /* Remove files to start with a clean slate. */
38         TestUtils.removeLogFiles("Setup", envHome, false);
39     }
40
41     protected void tearDown()
42         throws Exception JavaDoc {
43
44         TestUtils.removeLogFiles("TearDown", envHome, false);
45     }
46
47     public void testBasic()
48         throws Throwable JavaDoc{
49         Environment env = null;
50
51         try {
52             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
53             envConfig.setConfigParam(EnvironmentParams.LOG_FSYNC_TIMEOUT.getName(),
54                                      "50000000");
55             envConfig.setAllowCreate(true);
56             env = new Environment(envHome, envConfig);
57
58             WaitVal waitVal = new WaitVal(0);
59
60             FSyncManager syncManager =
61                 new TestSyncManager(DbInternal.envGetEnvironmentImpl(env),
62                                     waitVal);
63             JUnitThread t1 = new TestSyncThread(syncManager);
64             JUnitThread t2 = new TestSyncThread(syncManager);
65             JUnitThread t3 = new TestSyncThread(syncManager);
66             t1.start();
67             t2.start();
68             t3.start();
69
70             /* Wait for all threads to request a sync, so they form a group.*/
71             Thread.sleep(500);
72
73             /* Free thread 1. */
74             synchronized (waitVal) {
75                 waitVal.value = 1;
76                 waitVal.notify();
77             }
78
79             t1.join();
80             t2.join();
81             t3.join();
82             
83             /*
84              * All three threads ask for fsyncs.
85              * 2 do fsyncs -- the initial leader, and the leader of the
86              * waiting group of 2.
87              * The last thread gets a free ride.
88              */

89             assertEquals(3, syncManager.getNFSyncRequests());
90             assertEquals(2, syncManager.getNFSyncs());
91             assertEquals(0, syncManager.getNTimeouts());
92         } finally {
93             if (env != null) {
94                 env.close();
95             }
96         }
97     }
98
99     /* This test class waits for an object instead of executing a sync.
100      * This way, we can manipulate grouping behavior.
101      */

102     class TestSyncManager extends FSyncManager {
103         private WaitVal waitVal;
104         TestSyncManager(EnvironmentImpl env, WaitVal waitVal)
105             throws DatabaseException {
106             super(env);
107             this.waitVal = waitVal;
108         }
109         protected void executeFSync()
110             throws DatabaseException {
111             try {
112                 synchronized (waitVal) {
113                     if (waitVal.value < 1) {
114                         waitVal.wait();
115                     }
116                 }
117             } catch (InterruptedException JavaDoc e) {
118                 // woken up.
119
}
120         }
121     }
122
123     class TestSyncThread extends JUnitThread {
124         private FSyncManager syncManager;
125         TestSyncThread(FSyncManager syncManager) {
126             super("syncThread");
127             this.syncManager = syncManager;
128         }
129         
130         public void testBody()
131             throws Throwable JavaDoc {
132             syncManager.fsync();
133         }
134     }
135
136     class WaitVal {
137         public int value;
138
139         WaitVal(int value) {
140             this.value = value;
141         }
142     }
143 }
144
Popular Tags