diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/ThreadSafetyTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/ThreadSafetyTests.java new file mode 100644 index 0000000000..59b968a39c --- /dev/null +++ b/jOOQ-test/src/org/jooq/test/_/testcases/ThreadSafetyTests.java @@ -0,0 +1,115 @@ +/** + * Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.test._.testcases; + +import static junit.framework.Assert.assertEquals; + +import org.jooq.TableRecord; +import org.jooq.UpdatableRecord; +import org.jooq.test.BaseTest; +import org.jooq.test.jOOQAbstractTest; + +import org.junit.Test; + +public class ThreadSafetyTests< + A extends UpdatableRecord, + AP, + B extends UpdatableRecord, + S extends UpdatableRecord, + B2S extends UpdatableRecord, + BS extends UpdatableRecord, + L extends TableRecord, + X extends TableRecord, + DATE extends UpdatableRecord, + BOOL extends UpdatableRecord, + D extends UpdatableRecord, + T extends UpdatableRecord, + U extends TableRecord, + I extends TableRecord, + IPK extends UpdatableRecord, + T658 extends TableRecord, + T725 extends UpdatableRecord, + T639 extends UpdatableRecord, + T785 extends TableRecord> +extends BaseTest { + + public ThreadSafetyTests(jOOQAbstractTest delegate) { + super(delegate); + } + + @Test + public void testConcurrentExecution() throws Exception { + // This test is an adapted version of a test case contributed by + // Sergey Epik for [#1543] + + Object lock = new Object(); + for (int i = 0; i < 64; i++) { + new Thread(new RunnableTest(lock)).start(); + } + try { + Thread.sleep(1000); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + synchronized (lock) { + lock.notifyAll(); + } + } + + private class RunnableTest implements Runnable { + private Object lock; + + private RunnableTest(Object lock) { + this.lock = lock; + } + + @Override + public void run() { + try { + synchronized (lock) { + lock.wait(); + } + + // [#371] In this case, all factories share the same connection + // hence, they're probably + assertEquals(4, (int) create().selectCount().from(TBook()).fetchOne(0, Integer.class)); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } +} diff --git a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java index 88f04e73fc..009d0b0a82 100644 --- a/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/org/jooq/test/jOOQAbstractTest.java @@ -112,6 +112,7 @@ import org.jooq.test._.testcases.RenderAndBindTests; import org.jooq.test._.testcases.RoutineAndUDTTests; import org.jooq.test._.testcases.SchemaAndMappingTests; import org.jooq.test._.testcases.SelectTests; +import org.jooq.test._.testcases.ThreadSafetyTests; import org.jooq.tools.JooqLogger; import org.jooq.tools.StopWatch; import org.jooq.tools.StringUtils; @@ -1043,6 +1044,11 @@ public abstract class jOOQAbstractTest< new FetchTests(this).testFetchLater(); } + @Test + public void testConcurrentExecution() throws Exception { + new ThreadSafetyTests(this).testConcurrentExecution(); + } + @Test public void testGrouping() throws Exception { new GroupByTests(this).testGrouping();