use of org.h2.test.synth.TestMultiThreaded in project h2database by h2database.
the class TestCompress method testMultiThreaded.
private void testMultiThreaded() throws Exception {
Task[] tasks = new Task[3];
for (int i = 0; i < tasks.length; i++) {
Task t = new Task() {
@Override
public void call() {
CompressTool tool = CompressTool.getInstance();
byte[] b = new byte[1024];
Random r = new Random();
while (!stop) {
r.nextBytes(b);
byte[] test = tool.expand(tool.compress(b, "LZF"));
assertEquals(b, test);
}
}
};
tasks[i] = t;
t.execute();
}
Thread.sleep(1000);
for (Task t : tasks) {
t.get();
}
}
use of org.h2.test.synth.TestMultiThreaded in project h2database by h2database.
the class TestGetGeneratedKeys method testMultithreaded.
/**
* Test method for shared connection between several statements in different
* threads.
*
* @param conn
* connection
* @throws Exception
* on exception
*/
private void testMultithreaded(final Connection conn) throws Exception {
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST (ID BIGINT PRIMARY KEY AUTO_INCREMENT," + "VALUE INT NOT NULL)");
final int count = 4, iterations = 10_000;
Thread[] threads = new Thread[count];
final long[] keys = new long[count * iterations];
for (int i = 0; i < count; i++) {
final int num = i;
threads[num] = new Thread("getGeneratedKeys-" + num) {
@Override
public void run() {
try {
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (?)", Statement.RETURN_GENERATED_KEYS);
for (int i = 0; i < iterations; i++) {
int value = iterations * num + i;
prep.setInt(1, value);
prep.execute();
ResultSet rs = prep.getGeneratedKeys();
rs.next();
keys[value] = rs.getLong(1);
rs.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
};
}
for (int i = 0; i < count; i++) {
threads[i].start();
}
for (int i = 0; i < count; i++) {
threads[i].join();
}
ResultSet rs = stat.executeQuery("SELECT VALUE, ID FROM TEST ORDER BY VALUE");
for (int i = 0; i < keys.length; i++) {
assertTrue(rs.next());
assertEquals(i, rs.getInt(1));
assertEquals(keys[i], rs.getLong(2));
}
assertFalse(rs.next());
rs.close();
stat.execute("DROP TABLE TEST");
}
use of org.h2.test.synth.TestMultiThreaded in project h2database by h2database.
the class TestAll method test.
/**
* Run all tests with the current settings.
*/
private void test() throws SQLException {
System.out.println();
System.out.println("Test " + toString() + " (" + Utils.getMemoryUsed() + " KB used)");
beforeTest();
// db
addTest(new TestScriptSimple());
addTest(new TestScript());
addTest(new TestAlter());
addTest(new TestAlterSchemaRename());
addTest(new TestAutoRecompile());
addTest(new TestBackup());
addTest(new TestBigDb());
addTest(new TestBigResult());
addTest(new TestCases());
addTest(new TestCheckpoint());
addTest(new TestCompatibility());
addTest(new TestCompatibilityOracle());
addTest(new TestCsv());
addTest(new TestDeadlock());
if (vmlens) {
return;
}
addTest(new TestDrop());
addTest(new TestDuplicateKeyUpdate());
addTest(new TestEncryptedDb());
addTest(new TestExclusive());
addTest(new TestFullText());
addTest(new TestFunctionOverload());
addTest(new TestFunctions());
addTest(new TestInit());
addTest(new TestIndex());
addTest(new TestIndexHints());
addTest(new TestLargeBlob());
addTest(new TestLinkedTable());
addTest(new TestListener());
addTest(new TestLob());
addTest(new TestMergeUsing());
addTest(new TestMultiConn());
addTest(new TestMultiDimension());
addTest(new TestMultiThreadedKernel());
addTest(new TestOpenClose());
addTest(new TestOptimizations());
addTest(new TestOptimizerHints());
addTest(new TestOutOfMemory());
addTest(new TestReadOnly());
addTest(new TestRecursiveQueries());
addTest(new TestGeneralCommonTableQueries());
if (!memory) {
// requires persistent store for reconnection tests
addTest(new TestPersistentCommonTableExpressions());
}
addTest(new TestRights());
addTest(new TestRunscript());
addTest(new TestSQLInjection());
addTest(new TestSessionsLocks());
addTest(new TestSelectCountNonNullColumn());
addTest(new TestSequence());
addTest(new TestShow());
addTest(new TestSpaceReuse());
addTest(new TestSpatial());
addTest(new TestSpeed());
addTest(new TestTableEngines());
addTest(new TestRowFactory());
addTest(new TestTempTables());
addTest(new TestTransaction());
addTest(new TestTriggersConstraints());
addTest(new TestTwoPhaseCommit());
addTest(new TestView());
addTest(new TestViewAlterTable());
addTest(new TestViewDropView());
addTest(new TestReplace());
addTest(new TestSynonymForTable());
addTest(new TestColumnNamer());
// jaqu
addTest(new AliasMapTest());
addTest(new AnnotationsTest());
addTest(new ClobTest());
addTest(new ModelsTest());
addTest(new SamplesTest());
addTest(new UpdateTest());
// jdbc
addTest(new TestBatchUpdates());
addTest(new TestCallableStatement());
addTest(new TestCancel());
addTest(new TestConcurrentConnectionUsage());
addTest(new TestConnection());
addTest(new TestDatabaseEventListener());
addTest(new TestJavaObject());
addTest(new TestLimitUpdates());
addTest(new TestLobApi());
addTest(new TestManyJdbcObjects());
addTest(new TestMetaData());
addTest(new TestNativeSQL());
addTest(new TestPreparedStatement());
addTest(new TestResultSet());
addTest(new TestStatement());
addTest(new TestGetGeneratedKeys());
addTest(new TestTransactionIsolation());
addTest(new TestUpdatableResultSet());
addTest(new TestZloty());
addTest(new TestCustomDataTypesHandler());
addTest(new TestSetCollation());
// jdbcx
addTest(new TestConnectionPool());
addTest(new TestDataSource());
addTest(new TestXA());
addTest(new TestXASimple());
// server
addTest(new TestAutoServer());
addTest(new TestNestedLoop());
// mvcc & row level locking
addTest(new TestMvcc1());
addTest(new TestMvcc2());
addTest(new TestMvcc3());
addTest(new TestMvcc4());
addTest(new TestMvccMultiThreaded());
addTest(new TestMvccMultiThreaded2());
addTest(new TestRowLocks());
// synth
addTest(new TestBtreeIndex());
addTest(new TestConcurrentUpdate());
addTest(new TestDiskFull());
addTest(new TestCrashAPI());
addTest(new TestFuzzOptimizations());
addTest(new TestLimit());
addTest(new TestRandomCompare());
addTest(new TestKillRestart());
addTest(new TestKillRestartMulti());
addTest(new TestMultiThreaded());
addTest(new TestOuterJoins());
addTest(new TestNestedJoins());
addTest(new TestStringAggCompatibility());
runAddedTests();
// serial
addTest(new TestDateStorage());
addTest(new TestDriver());
addTest(new TestJavaObjectSerializer());
addTest(new TestLocale());
addTest(new TestMemoryUsage());
addTest(new TestMultiThread());
addTest(new TestPowerOff());
addTest(new TestReorderWrites());
addTest(new TestRandomSQL());
addTest(new TestQueryCache());
addTest(new TestUrlJavaObjectSerializer());
addTest(new TestWeb());
runAddedTests(1);
afterTest();
}
use of org.h2.test.synth.TestMultiThreaded in project h2database by h2database.
the class TestFullText method testMultiThreaded.
private void testMultiThreaded(boolean lucene) throws Exception {
final String prefix = lucene ? "FTL" : "FT";
trace("Testing multithreaded " + prefix);
deleteDb("fullText");
ArrayList<Connection> connList = new ArrayList<>();
try {
int len = 2;
Task[] task = new Task[len];
for (int i = 0; i < len; i++) {
final Connection conn = getConnection("fullText;LOCK_TIMEOUT=60000", connList);
Statement stat = conn.createStatement();
initFullText(stat, lucene);
initFullText(stat, lucene);
final String tableName = "TEST" + i;
stat.execute("CREATE TABLE " + tableName + "(ID INT PRIMARY KEY, DATA VARCHAR)");
stat.execute("CALL " + prefix + "_CREATE_INDEX('PUBLIC', '" + tableName + "', NULL)");
task[i] = new Task() {
@Override
public void call() throws SQLException {
trace("starting thread " + Thread.currentThread());
PreparedStatement prep = conn.prepareStatement("INSERT INTO " + tableName + " VALUES(?, ?)");
Statement stat = conn.createStatement();
Random random = new Random();
int x = 0;
while (!stop) {
trace("stop = " + stop + " for " + Thread.currentThread());
StringBuilder buff = new StringBuilder();
for (int j = 0; j < 1000; j++) {
buff.append(" ").append(random.nextInt(10000));
buff.append(" x").append(j);
buff.append(" ").append(KNOWN_WORDS[j % KNOWN_WORDS.length]);
}
prep.setInt(1, x);
prep.setString(2, buff.toString());
prep.execute();
x++;
for (String knownWord : KNOWN_WORDS) {
trace("searching for " + knownWord + " with " + Thread.currentThread());
ResultSet rs = stat.executeQuery("SELECT * FROM " + prefix + "_SEARCH('" + knownWord + "', 0, 0)");
assertTrue(rs.next());
}
}
trace("closing connection");
if (!config.memory) {
conn.close();
}
trace("completed thread " + Thread.currentThread());
}
};
}
for (Task t : task) {
t.execute();
}
trace("sleeping");
Thread.sleep(1000);
trace("setting stop to true");
for (Task t : task) {
trace("joining " + t);
t.get();
trace("done joining " + t);
}
} finally {
close(connList);
}
}
Aggregations