diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/locking/LockModeTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/locking/LockModeTest.java index 2bf0d04ff5..1a6ed522ed 100644 --- a/hibernate-core/src/matrix/java/org/hibernate/test/locking/LockModeTest.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/locking/LockModeTest.java @@ -38,6 +38,8 @@ import org.junit.Test; import org.hibernate.testing.FailureExpected; import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.async.Executable; +import org.hibernate.testing.async.TimedExecutor; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertNotNull; @@ -184,7 +186,7 @@ public class LockModeTest extends BaseCoreFunctionalTestCase { // we are not allowed to acquire exclusive locks to that row and/or write to that row. That may take // one of two forms: // 1) either the get-with-lock or the update fails immediately with a sql error - // 2) either the get-with-lock or the update blocks indef (in real world, it would block + // 2) either the get-with-lock or the update blocks indefinitely (in real world, it would block // until the txn in the calling method completed. // To be able to cater to the second type, we run this block in a separate thread to be able to "time it out" @@ -237,7 +239,7 @@ public class LockModeTest extends BaseCoreFunctionalTestCase { } @Override - public void forceStop() { + public void timedOut() { s.cancelQuery(); shutDown(); } @@ -248,71 +250,4 @@ public class LockModeTest extends BaseCoreFunctionalTestCase { // timeout is ok, see rule #2 above } } - - interface Executable { - public void execute(); - public void forceStop(); - } - - class TimedExecutor { - private final long timeOut; - private final int checkMilliSeconds; - - TimedExecutor(long timeOut) { - this( timeOut, 1000 ); - } - - TimedExecutor(long timeOut, int checkMilliSeconds) { - this.timeOut = timeOut; - this.checkMilliSeconds = checkMilliSeconds; - } - - public void execute(Executable executable) throws TimeoutException { - final ExecutableAdapter adapter = new ExecutableAdapter( executable ); - final Thread separateThread = new Thread( adapter ); - separateThread.start(); - - int runningTime = 0; - do { - if ( runningTime > timeOut ) { - try { - executable.forceStop(); - } - catch (Exception ignore) { - } - throw new TimeoutException(); - } - try { - Thread.sleep( checkMilliSeconds ); - runningTime += checkMilliSeconds; - } - catch (InterruptedException ignore) { - } - } while ( !adapter.isDone() ); - } - } - - class ExecutableAdapter implements Runnable { - private final Executable executable; - private boolean isDone; - - ExecutableAdapter(Executable executable) { - this.executable = executable; - } - - public boolean isDone() { - return isDone; - } - - @Override - public void run() { - isDone = false; - try { - executable.execute(); - } - finally { - isDone = true; - } - } - } } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/async/Executable.java b/hibernate-testing/src/main/java/org/hibernate/testing/async/Executable.java new file mode 100644 index 0000000000..75758d2e59 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/async/Executable.java @@ -0,0 +1,41 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.async; + +/** + * Something we want to perform with protection to time it out. + * + * @author Steve Ebersole + */ +public interface Executable { + /** + * Perform the action + */ + public void execute(); + + /** + * Called when the timeout period is exceeded. + */ + public void timedOut(); +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/async/ExecutableAdapter.java b/hibernate-testing/src/main/java/org/hibernate/testing/async/ExecutableAdapter.java new file mode 100644 index 0000000000..cf7ad70005 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/async/ExecutableAdapter.java @@ -0,0 +1,51 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.async; + +/** + * @author Steve Ebersole + */ +public class ExecutableAdapter implements Runnable { + private final Executable executable; + private boolean isDone; + + public ExecutableAdapter(Executable executable) { + this.executable = executable; + } + + public boolean isDone() { + return isDone; + } + + @Override + public void run() { + isDone = false; + try { + executable.execute(); + } + finally { + isDone = true; + } + } +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/async/TimedExecutor.java b/hibernate-testing/src/main/java/org/hibernate/testing/async/TimedExecutor.java new file mode 100644 index 0000000000..55852351d3 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/async/TimedExecutor.java @@ -0,0 +1,67 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.async; + +import java.util.concurrent.TimeoutException; + +/** + * @author Steve Ebersole + */ +public class TimedExecutor { + private final long timeOut; + private final int checkMilliSeconds; + + public TimedExecutor(long timeOut) { + this( timeOut, 1000 ); + } + + public TimedExecutor(long timeOut, int checkMilliSeconds) { + this.timeOut = timeOut; + this.checkMilliSeconds = checkMilliSeconds; + } + + public void execute(Executable executable) throws TimeoutException { + final ExecutableAdapter adapter = new ExecutableAdapter( executable ); + final Thread separateThread = new Thread( adapter ); + separateThread.start(); + + int runningTime = 0; + do { + if ( runningTime > timeOut ) { + try { + executable.timedOut(); + } + catch (Exception ignore) { + } + throw new TimeoutException(); + } + try { + Thread.sleep( checkMilliSeconds ); + runningTime += checkMilliSeconds; + } + catch (InterruptedException ignore) { + } + } while ( !adapter.isDone() ); + } +}