1 5 package org.h2.test.synth; 6 7 import java.io.File ; 8 import java.io.FileWriter ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.PrintWriter ; 12 import java.sql.Connection ; 13 import java.sql.DriverManager ; 14 import java.sql.SQLException ; 15 import java.text.SimpleDateFormat ; 16 import java.util.Date ; 17 import java.util.LinkedList ; 18 import java.util.Random ; 19 20 import org.h2.test.TestBase; 21 import org.h2.tools.DeleteDbFiles; 22 import org.h2.util.IOUtils; 23 24 public abstract class TestHalt extends TestBase { 25 26 private SimpleDateFormat dateFormat = new SimpleDateFormat ("MM-dd HH:mm:ss "); 27 protected static final int OP_INSERT = 1, OP_DELETE = 2, OP_UPDATE = 4, OP_SELECT = 8; 28 protected static final int FLAG_NODELAY = 1, FLAG_LOBS = 2; 29 protected int operations, flags, value; 30 protected Connection conn; 31 protected Random random = new Random (); 32 33 abstract void testInit() throws Exception ; 34 abstract void testCheckAfterCrash() throws Exception ; 35 abstract void testWaitAfterAppStart() throws Exception ; 36 abstract void appStart() throws Exception ; 37 abstract void appRun() throws Exception ; 38 39 public void test() throws Exception { 40 for(int i=0;; i++) { 41 operations = OP_INSERT | i; 42 flags = i >> 4; 43 try { 45 runTest(); 46 } catch(Throwable t) { 47 System.out.println("Error: " + t); 48 t.printStackTrace(); 49 } 50 } 51 } 52 53 Connection getConnection() throws Exception { 54 Class.forName("org.h2.Driver"); 55 return DriverManager.getConnection("jdbc:h2:test", "sa", "sa"); 56 } 57 58 protected void start(String [] args) throws Exception { 59 if(args.length == 0) { 60 runTest(); 61 } else { 62 operations = Integer.parseInt(args[0]); 63 flags = Integer.parseInt(args[1]); 64 value = Integer.parseInt(args[2]); 65 runRandom(); 66 } 67 } 68 69 private void runRandom() throws Exception { 70 log("connecting", null); 71 connect(); 72 try { 73 log("connected, operations:" + operations + " flags:" + flags + " value:" + value, null); 74 appStart(); 75 System.out.println("READY"); 76 System.out.println("READY"); 77 System.out.println("READY"); 78 appRun(); 79 log("done", null); 80 } catch(Exception e) { 81 log("run", e); 82 } 83 disconnect(); 84 } 85 86 private void connect() throws Exception { 87 try { 88 conn = getConnection(); 89 } catch(Exception e) { 90 log("connect", e); 91 e.printStackTrace(); 92 throw e; 93 } 94 } 95 96 protected void log(String s, Exception e) { 97 FileWriter writer = null; 98 try { 99 writer = new FileWriter ("log.txt", true); 100 PrintWriter w = new PrintWriter (writer); 101 s = dateFormat.format(new Date ()) + ": " + s; 102 w.println(s); 103 if(e != null) { 104 e.printStackTrace(w); 105 } 106 } catch(IOException e2) { 107 e2.printStackTrace(); 108 } finally { 109 IOUtils.closeSilently(writer); 110 } 111 } 112 113 private void runTest() throws Exception { 114 DeleteDbFiles.execute(null, "test", true); 115 new File ("log.txt").delete(); 116 connect(); 117 testInit(); 118 disconnect(); 119 for(int i=0; i<10; i++) { 120 int value = random.nextInt(1000); 125 String classPath = ""; 128 String command = "java " + classPath + " " + getClass().getName() + " " + operations + " " + flags + " " + value; 129 log("start: " + command); 130 Process p = Runtime.getRuntime().exec(command); 131 InputStream in = p.getInputStream(); 132 OutputCatcher catcher = new OutputCatcher(in); 133 catcher.start(); 134 String s = catcher.readLine(5000); 135 if(s == null) { 136 throw new IOException ("No reply from process"); 137 } else if(s.startsWith("READY")) { 138 log("got reply: " + s); 139 } 140 testWaitAfterAppStart(); 141 p.destroy(); 142 connect(); 143 testCheckAfterCrash(); 144 disconnect(); 145 } 146 } 147 148 protected void disconnect() { 149 try { 150 conn.close(); 151 } catch(Exception e) { 152 log("disconnect", e); 153 } 154 } 155 156 private void log(String string) { 157 System.out.println(string); 158 } 159 160 private static class OutputCatcher extends Thread { 161 private InputStream in; 162 private LinkedList list = new LinkedList (); 163 164 OutputCatcher(InputStream in) { 165 this.in = in; 166 } 167 168 private String readLine(long wait) { 169 long start = System.currentTimeMillis(); 170 while(true) { 171 synchronized(list) { 172 if(list.size() > 0) { 173 return (String ) list.removeFirst(); 174 } 175 try { 176 list.wait(wait); 177 } catch (InterruptedException e) { 178 } 179 long time = System.currentTimeMillis() - start; 180 if(time >= wait) { 181 return null; 182 } 183 } 184 } 185 } 186 187 public void run() { 188 StringBuffer buff = new StringBuffer (); 189 while(true) { 190 try { 191 int x = in.read(); 192 if(x < 0) { 193 break; 194 } 195 if(x < ' ') { 196 if(buff.length() > 0) { 197 String s = buff.toString(); 198 buff.setLength(0); 199 synchronized(list) { 200 list.add(s); 201 list.notifyAll(); 202 } 203 } 204 } else { 205 buff.append((char) x); 206 } 207 } catch(IOException e) { 208 } 210 } 211 IOUtils.closeSilently(in); 212 } 213 } 214 215 public Connection getConnectionHSQLDB() throws Exception { 216 File lock = new File ("test.lck"); 217 while(lock.exists()) { 218 lock.delete(); 219 System.gc(); 220 } 221 Class.forName("org.hsqldb.jdbcDriver"); 222 return DriverManager.getConnection("jdbc:hsqldb:test", "sa", ""); 223 } 224 225 public Connection getConnectionDerby() throws Exception { 226 File lock = new File ("test3/db.lck"); 227 while(lock.exists()) { 228 lock.delete(); 229 System.gc(); 230 } 231 Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); 232 try { 233 return DriverManager.getConnection("jdbc:derby:test3;create=true", "sa", "sa"); 234 } catch(SQLException e) { 235 Exception e2 = e; 236 do { 237 e.printStackTrace(); 238 e = e.getNextException(); 239 } while(e != null); 240 throw e2; 241 } 242 } 243 244 public void disconnectHSQLDB() { 245 try { 246 conn.createStatement().execute("SHUTDOWN"); 247 } catch(Exception e) { 248 } 250 } 252 253 public void disconnectDerby() { 254 try { 256 Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); 257 DriverManager.getConnection("jdbc:derby:;shutdown=true", "sa", "sa"); 258 } catch(Exception e) { 259 } 261 } 262 263 protected String getRandomString(int len) { 264 StringBuffer buff = new StringBuffer (); 265 for(int i=0; i<len; i++) { 266 buff.append('a' + random.nextInt(20)); 267 } 268 return buff.toString(); 269 } 270 271 } 272 | Popular Tags |