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.File;
009: import java.sql.Connection;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012: import java.sql.Statement;
013: import java.util.Random;
014:
015: import org.h2.test.TestBase;
016: import org.h2.tools.Backup;
017: import org.h2.tools.DeleteDbFiles;
018:
019: /**
020: * A recovery test that checks the consistency of a database (if it exists),
021: * then deletes everything and runs in an endless loop executing random
022: * operations. This loop is usually stopped by switching off the computer.
023: */
024: public class TestTimer extends TestBase {
025:
026: public void test() throws Exception {
027: validateOld();
028: DeleteDbFiles.execute(baseDir, "timer", true);
029: loop();
030: }
031:
032: private void loop() throws Exception {
033: println("loop");
034: Connection conn = getConnection("timer");
035: Statement stat = conn.createStatement();
036: stat.execute("CREATE TABLE TEST(ID IDENTITY, NAME VARCHAR)");
037: Random random = new Random();
038: int max = 0;
039: int count = 0;
040: long start = System.currentTimeMillis();
041: while (true) {
042: int action = random.nextInt(10);
043: int x = max == 0 ? 0 : random.nextInt(max);
044: switch (action) {
045: case 0:
046: case 1:
047: case 2:
048: stat.execute("INSERT INTO TEST VALUES(NULL, 'Hello')");
049: ResultSet rs = stat.getGeneratedKeys();
050: rs.next();
051: int i = rs.getInt(1);
052: max = i;
053: count++;
054: break;
055: case 3:
056: case 4:
057: if (count == 0) {
058: break;
059: }
060: stat.execute("UPDATE TEST SET NAME=NAME||'+' WHERE ID="
061: + x);
062: break;
063: case 5:
064: case 6:
065: if (count == 0) {
066: break;
067: }
068: count -= stat
069: .executeUpdate("DELETE FROM TEST WHERE ID=" + x);
070: break;
071: case 7:
072: rs = stat.executeQuery("SELECT COUNT(*) FROM TEST");
073: rs.next();
074: int c = rs.getInt(1);
075: check(c, count);
076: long time = System.currentTimeMillis();
077: if (time > start + 5000) {
078: println("rows: " + count);
079: start = time;
080: }
081: break;
082: }
083: }
084: }
085:
086: private void validateOld() {
087: println("validate");
088: try {
089: Connection conn = getConnection("timer");
090: // TODO validate transactions
091: Statement stat = conn.createStatement();
092: stat
093: .execute("CREATE TABLE IF NOT EXISTS TEST(ID IDENTITY, NAME VARCHAR)");
094: ResultSet rs = stat
095: .executeQuery("SELECT COUNT(*) FROM TEST");
096: rs.next();
097: int count = rs.getInt(1);
098: println("row count: " + count);
099: int real = 0;
100: rs = stat.executeQuery("SELECT * FROM TEST");
101: while (rs.next()) {
102: real++;
103: }
104: if (real != count) {
105: println("real count: " + real);
106: throw new Error("COUNT(*)=" + count + " SELECT=" + real);
107: }
108: rs = stat.executeQuery("SCRIPT");
109: while (rs.next()) {
110: rs.getString(1);
111: }
112: conn.close();
113: } catch (Throwable e) {
114: logError("validate", e);
115: backup();
116: }
117: }
118:
119: private void backup() {
120: println("backup");
121: for (int i = 0;; i++) {
122: String s = "timer." + i + ".zip";
123: File f = new File(s);
124: if (f.exists()) {
125: continue;
126: }
127: try {
128: Backup.execute(s, baseDir, "timer", true);
129: } catch (SQLException e) {
130: logError("backup", e);
131: }
132: break;
133: }
134: }
135:
136: }
|