001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.assorted;
022:
023: import java.io.IOException;
024:
025: import com.db4o.*;
026: import com.db4o.foundation.io.*;
027: import com.db4o.internal.*;
028: import com.db4o.query.*;
029:
030: import db4ounit.*;
031: import db4ounit.extensions.*;
032:
033: public class BackupStressTestCase implements Db4oTestCase {
034:
035: private static boolean verbose = false;
036:
037: private static boolean runOnOldJDK = false;
038:
039: private static final String FILE = "backupstress.yap";
040:
041: private static final int ITERATIONS = 5;
042:
043: private static final int OBJECTS = 50;
044:
045: private static final int COMMITS = 10;
046:
047: private ObjectContainer _objectContainer;
048:
049: private volatile boolean _inBackup;
050:
051: private volatile boolean _noMoreBackups;
052:
053: private int _backups;
054:
055: private int _commitCounter;
056:
057: public static void main(String[] args) throws Exception {
058:
059: verbose = true;
060: runOnOldJDK = true;
061:
062: BackupStressTestCase stressTest = new BackupStressTestCase();
063: try {
064: stressTest.setUp();
065: stressTest.test();
066: } finally {
067: stressTest.tearDown();
068: }
069: }
070:
071: public void setUp() throws Exception {
072: deleteFile(FILE);
073: Db4o.configure().objectClass(BackupStressItem.class)
074: .objectField("_iteration").indexed(true);
075: Db4o.configure().reflectWith(
076: Platform4.reflectorForType(BackupStressItem.class));
077: }
078:
079: public void tearDown() throws IOException {
080: deleteFile(FILE);
081: }
082:
083: public void test() throws Exception {
084: openDatabase();
085: try {
086: runTestIterations();
087: } finally {
088: closeDatabase();
089: }
090: checkBackups();
091: }
092:
093: private void runTestIterations() throws Exception {
094: if (!runOnOldJDK && isOldJDK()) {
095: System.out
096: .println("BackupStressTest is too slow for regression testing on Java JDKs < 1.4");
097: return;
098: }
099:
100: BackupStressIteration iteration = new BackupStressIteration();
101: _objectContainer.set(iteration);
102: _objectContainer.commit();
103: Thread backupThread = startBackupThread();
104: for (int i = 1; i <= ITERATIONS; i++) {
105: for (int obj = 0; obj < OBJECTS; obj++) {
106: _objectContainer
107: .set(new BackupStressItem("i" + obj, i));
108: _commitCounter++;
109: if (_commitCounter >= COMMITS) {
110: _objectContainer.commit();
111: _commitCounter = 0;
112: }
113: }
114: iteration.setCount(i);
115: _objectContainer.set(iteration);
116: _objectContainer.commit();
117: }
118: _noMoreBackups = true;
119: backupThread.join();
120: }
121:
122: private Thread startBackupThread() {
123: Thread thread = new Thread(new Runnable() {
124: public void run() {
125: while (!_noMoreBackups) {
126: _backups++;
127: String fileName = backupFile(_backups);
128: deleteFile(fileName);
129: _inBackup = true;
130: _objectContainer.ext().backup(fileName);
131: _inBackup = false;
132: }
133: }
134: });
135: thread.start();
136: return thread;
137: }
138:
139: private void openDatabase() {
140: deleteFile(FILE);
141: _objectContainer = Db4o.openFile(FILE);
142: }
143:
144: private void closeDatabase() throws InterruptedException {
145: while (_inBackup) {
146: Thread.sleep(1000);
147: }
148: _objectContainer.close();
149: }
150:
151: private void checkBackups() throws IOException {
152: stdout("BackupStressTest");
153: stdout("Backups created: " + _backups);
154:
155: for (int i = 1; i < _backups; i++) {
156: stdout("Backup " + i);
157: ObjectContainer container = Db4o.openFile(backupFile(i));
158: try {
159: stdout("Open successful");
160: Query q = container.query();
161: q.constrain(BackupStressIteration.class);
162: BackupStressIteration iteration = (BackupStressIteration) q
163: .execute().next();
164:
165: int iterations = iteration.getCount();
166:
167: stdout("Iterations in backup: " + iterations);
168:
169: if (iterations > 0) {
170: q = container.query();
171: q.constrain(BackupStressItem.class);
172: q.descend("_iteration").constrain(
173: new Integer(iteration.getCount()));
174: ObjectSet items = q.execute();
175: Assert.areEqual(OBJECTS, items.size());
176: while (items.hasNext()) {
177: BackupStressItem item = (BackupStressItem) items
178: .next();
179: Assert.areEqual(iterations, item._iteration);
180: }
181: }
182: } finally {
183: container.close();
184: }
185: stdout("Backup OK");
186: }
187: stdout("BackupStressTest " + _backups + " files OK.");
188: for (int i = 1; i <= _backups; i++) {
189: deleteFile(backupFile(i));
190: }
191: }
192:
193: private void deleteFile(String fname) {
194: File4.delete(fname);
195: }
196:
197: private boolean isOldJDK() {
198: ObjectContainerBase stream = (ObjectContainerBase) _objectContainer;
199: return stream.needsLockFileThread();
200: }
201:
202: private String backupFile(int count) {
203: return "" + count + FILE;
204: }
205:
206: private void stdout(String string) {
207: if (verbose) {
208: System.out.println(string);
209: }
210: }
211:
212: }
|