001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.synth;
007:
008: import java.io.InputStream;
009: import java.sql.Connection;
010: import java.sql.DriverManager;
011: import java.sql.PreparedStatement;
012: import java.sql.ResultSet;
013: import java.sql.Statement;
014: import java.util.Random;
015:
016: import org.h2.test.TestBase;
017: import org.h2.test.unit.SelfDestructor;
018:
019: /**
020: * Standalone recovery test. A new process is started and then killed while it
021: * executes random statements.
022: */
023: public class TestKillRestart extends TestBase {
024:
025: public void test() throws Exception {
026: if (config.networked) {
027: return;
028: }
029: deleteDb("killRestart");
030: String url = getURL("killRestart", true);
031: // String url = getURL(
032: // "killRestart;CACHE_SIZE=2048;WRITE_DELAY=0;STORAGE=TEXT", true);
033: String user = getUser(), password = getPassword();
034: String selfDestruct = SelfDestructor.getPropertyString(60);
035: String[] procDef = new String[] { "java", selfDestruct, "-cp",
036: "bin", getClass().getName(), "-url", url, "-user",
037: user, "-password", password };
038:
039: int len = getSize(2, 15);
040: for (int i = 0; i < len; i++) {
041: Process p = Runtime.getRuntime().exec(procDef);
042: InputStream in = p.getInputStream();
043: OutputCatcher catcher = new OutputCatcher(in);
044: catcher.start();
045: while (true) {
046: String s = catcher.readLine(5 * 60 * 1000);
047: // System.out.println("> " + s);
048: if (s == null) {
049: error("No reply from process");
050: } else if (!s.startsWith("#")) {
051: // System.out.println(s);
052: error("Expected: #..., got: " + s);
053: } else if (s.startsWith("#Running")) {
054: Thread.sleep(100);
055: printTime("killing: " + i);
056: p.destroy();
057: break;
058: } else if (s.startsWith("#Fail")) {
059: error("Failed: " + s);
060: }
061: }
062: }
063: }
064:
065: public static void main(String[] args) throws Exception {
066: SelfDestructor.startCountdown(60);
067: String driver = "org.h2.Driver";
068: String url = "jdbc:h2:test", user = "sa", password = "sa";
069: for (int i = 0; i < args.length; i++) {
070: if ("-url".equals(args[i])) {
071: url = args[++i];
072: } else if ("-driver".equals(args[i])) {
073: driver = args[++i];
074: } else if ("-user".equals(args[i])) {
075: user = args[++i];
076: } else if ("-password".equals(args[i])) {
077: password = args[++i];
078: }
079: }
080: System.out.println("#Started; driver: " + driver + " url: "
081: + url + " user: " + user + " password: " + password);
082: try {
083: Class.forName(driver);
084: System.out.println("#Opening...");
085: Connection conn = DriverManager.getConnection(url, user,
086: password);
087: Statement stat = conn.createStatement();
088: stat
089: .execute("CREATE TABLE IF NOT EXISTS TEST(ID IDENTITY, NAME VARCHAR)");
090: stat
091: .execute("CREATE TABLE IF NOT EXISTS TEST2(ID IDENTITY, NAME VARCHAR)");
092: ResultSet rs = stat.executeQuery("SELECT * FROM TEST");
093: while (rs.next()) {
094: rs.getLong("ID");
095: rs.getString("NAME");
096: }
097: rs = stat.executeQuery("SELECT * FROM TEST2");
098: while (rs.next()) {
099: rs.getLong("ID");
100: rs.getString("NAME");
101: }
102: stat.execute("DROP ALL OBJECTS DELETE FILES");
103: System.out.println("#Closing with delete...");
104: conn.close();
105: System.out.println("#Starting...");
106: conn = DriverManager.getConnection(url, user, password);
107: stat = conn.createStatement();
108: stat.execute("DROP ALL OBJECTS");
109: stat
110: .execute("CREATE TABLE TEST(ID IDENTITY, NAME VARCHAR)");
111: stat
112: .execute("CREATE TABLE TEST2(ID IDENTITY, NAME VARCHAR)");
113: stat.execute("CREATE TABLE TEST_META(ID INT)");
114: PreparedStatement prep = conn
115: .prepareStatement("INSERT INTO TEST(NAME) VALUES(?)");
116: PreparedStatement prep2 = conn
117: .prepareStatement("INSERT INTO TEST2(NAME) VALUES(?)");
118: Random r = new Random(0);
119: // Runnable stopper = new Runnable() {
120: // public void run() {
121: // try {
122: // Thread.sleep(500);
123: // } catch (InterruptedException e) {
124: // }
125: // System.out.println("#Halt...");
126: // Runtime.getRuntime().halt(0);
127: // }
128: // };
129: // new Thread(stopper).start();
130: for (int i = 0; i < 2000; i++) {
131: if (i == 100) {
132: System.out.println("#Running...");
133: }
134: if (r.nextInt(100) < 10) {
135: conn
136: .createStatement()
137: .execute(
138: "ALTER TABLE TEST_META ALTER COLUMN ID INT DEFAULT 10");
139: }
140: if (r.nextBoolean()) {
141: if (r.nextBoolean()) {
142: prep.setString(1, new String(new char[r
143: .nextInt(30) * 10]));
144: prep.execute();
145: } else {
146: prep2.setString(1, new String(new char[r
147: .nextInt(30) * 10]));
148: prep2.execute();
149: }
150: } else {
151: if (r.nextBoolean()) {
152: conn.createStatement().execute(
153: "UPDATE TEST SET NAME = NULL");
154: } else {
155: conn.createStatement().execute(
156: "UPDATE TEST2 SET NAME = NULL");
157: }
158: }
159: }
160: } catch (Throwable e) {
161: e.printStackTrace(System.out);
162: System.out.println("#Fail: " + e.toString());
163: }
164: }
165:
166: }
|