1 21 package com.db4o.db4ounit.common.assorted; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 import com.db4o.query.*; 27 28 import db4ounit.*; 29 30 31 public class BackupStressTestCase implements TestLifeCycle { 32 33 private static boolean verbose = false; 34 35 private static boolean runOnOldJDK = false; 36 37 private static final String FILE = "backupstress.yap"; 38 39 private static final int ITERATIONS = 5; 40 41 private static final int OBJECTS = 50; 42 43 private static final int COMMITS = 10; 44 45 private ObjectContainer _objectContainer; 46 47 private volatile boolean _inBackup; 48 49 private volatile boolean _noMoreBackups; 50 51 private int _backups; 52 53 private int _commitCounter; 54 55 56 public static void main(String [] args) throws Exception { 57 58 verbose = true; 59 runOnOldJDK = true; 60 61 BackupStressTestCase stressTest = new BackupStressTestCase(); 62 stressTest.setUp(); 63 stressTest.test(); 64 } 65 66 public void setUp(){ 67 Db4o.configure().objectClass(BackupStressItem.class).objectField("_iteration").indexed(true); 68 } 69 70 public void tearDown() { 71 } 72 73 public void test() throws Exception { 74 openDatabase(); 75 try { 76 runTestIterations(); 77 } finally { 78 closeDatabase(); 79 } 80 checkBackups(); 81 } 82 83 private void runTestIterations() { 84 if(! runOnOldJDK && isOldJDK()) { 85 System.out.println("BackupStressTest is too slow for regression testing on Java JDKs < 1.4"); 86 return; 87 } 88 89 BackupStressIteration iteration = new BackupStressIteration(); 90 _objectContainer.set(iteration); 91 _objectContainer.commit(); 92 startBackupThread(); 93 for (int i = 1; i <= ITERATIONS; i++) { 94 for (int obj = 0; obj < OBJECTS; obj++) { 95 _objectContainer.set(new BackupStressItem("i" + obj, i)); 96 _commitCounter ++; 97 if(_commitCounter >= COMMITS){ 98 _objectContainer.commit(); 99 _commitCounter = 0; 100 } 101 } 102 iteration.setCount(i); 103 _objectContainer.set(iteration); 104 _objectContainer.commit(); 105 } 106 } 107 108 private void startBackupThread() { 109 new Thread (new Runnable () { 110 public void run() { 111 while(!_noMoreBackups){ 112 _backups ++; 113 String fileName = backupFile(_backups); 114 deleteFile(fileName); 115 try { 116 _inBackup = true; 117 _objectContainer.ext().backup(fileName); 118 _inBackup = false; 119 } catch (IOException e) { 120 e.printStackTrace(); 121 } 122 } 123 } 124 }).start(); 125 } 126 127 private void openDatabase(){ 128 deleteFile(FILE); 129 _objectContainer = Db4o.openFile(FILE); 130 } 131 132 private void closeDatabase() throws InterruptedException { 133 _noMoreBackups = true; 134 while(_inBackup){ 135 Thread.sleep(1000); 136 } 137 _objectContainer.close(); 138 } 139 140 private void checkBackups(){ 141 stdout("BackupStressTest"); 142 stdout("Backups created: " + _backups); 143 144 for (int i = 1; i < _backups; i++) { 145 stdout("Backup " + i); 146 ObjectContainer container = Db4o.openFile(backupFile(i)); 147 try { 148 stdout("Open successful"); 149 Query q = container.query(); 150 q.constrain(BackupStressIteration.class); 151 BackupStressIteration iteration = (BackupStressIteration) q.execute().next(); 152 153 int iterations = iteration.getCount(); 154 155 stdout("Iterations in backup: " + iterations); 156 157 if(iterations > 0){ 158 q = container.query(); 159 q.constrain(BackupStressItem.class); 160 q.descend("_iteration").constrain(new Integer (iteration.getCount())); 161 ObjectSet items = q.execute(); 162 Assert.areEqual(OBJECTS, items.size()); 163 while(items.hasNext()){ 164 BackupStressItem item = (BackupStressItem) items.next(); 165 Assert.areEqual(iterations, item._iteration); 166 } 167 } 168 } finally { 169 container.close(); 170 } 171 stdout("Backup OK"); 172 } 173 System.out.println("BackupStressTest " + _backups + " files OK."); 174 for (int i = 1; i <= _backups; i++) { 175 deleteFile(backupFile(i)); 176 } 177 deleteFile(FILE); 178 } 179 180 private boolean deleteFile(String fname) { 181 return new File(fname).delete(); 182 } 183 184 private boolean isOldJDK(){ 185 YapStream stream = (YapStream)_objectContainer; 186 return stream.needsLockFileThread(); 187 } 188 189 private String backupFile(int count){ 190 return "" + count + FILE; 191 } 192 193 private void stdout(String string) { 194 if(verbose){ 195 System.out.println(string); 196 } 197 } 198 199 200 } 201 | Popular Tags |