You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jdo-commits@db.apache.org by mb...@apache.org on 2009/08/14 22:12:52 UTC
svn commit: r804355 - in /db/jdo/trunk/tck2/src/java/org/apache/jdo/tck:
JDO_Test.java query/api/QueryCancel.java query/api/QueryTimeout.java
Author: mbo
Date: Fri Aug 14 20:12:52 2009
New Revision: 804355
URL: http://svn.apache.org/viewvc?rev=804355&view=rev
Log:
JDO-623: Query cancel and timeout support
Adding TCK classes testing query cancel and query timeout.
Added:
db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryCancel.java (with props)
db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryTimeout.java (with props)
Modified:
db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/JDO_Test.java
Modified: db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/JDO_Test.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/JDO_Test.java?rev=804355&r1=804354&r2=804355&view=diff
==============================================================================
--- db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/JDO_Test.java (original)
+++ db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/JDO_Test.java Fri Aug 14 20:12:52 2009
@@ -872,6 +872,12 @@
"javax.jdo.option.GetDataStoreConnection");
}
+ /** Reports whether canceling a running query is supported. */
+ public boolean isQueryCancelSupported() {
+ return supportedOptions.contains(
+ "javax.jdo.option.QueryCancel");
+ }
+
/** Reports whether a feature is supported */
public boolean isSupported(String option) {
return supportedOptions.contains(option);
Added: db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryCancel.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryCancel.java?rev=804355&view=auto
==============================================================================
--- db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryCancel.java (added)
+++ db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryCancel.java Fri Aug 14 20:12:52 2009
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jdo.tck.query.api;
+
+import junit.framework.AssertionFailedError;
+
+import javax.jdo.JDOFatalException;
+import javax.jdo.JDOQueryInterruptedException;
+import javax.jdo.JDOUnsupportedOptionException;
+import javax.jdo.PersistenceManager;
+import javax.jdo.Query;
+import javax.jdo.Transaction;
+
+import org.apache.jdo.tck.JDO_Test;
+import org.apache.jdo.tck.pc.mylib.PCPoint;
+import org.apache.jdo.tck.pc.mylib.PCPoint2;
+import org.apache.jdo.tck.query.QueryTest;
+import org.apache.jdo.tck.util.BatchTestRunner;
+import org.apache.jdo.tck.util.ThreadExceptionHandler;
+
+/**
+ *<B>Title:</B> QueryCancel
+ *<BR>
+ *<B>Keywords:</B> query
+ *<BR>
+ *<B>Assertion ID:</B> A14.6.1-8
+ *<BR>
+ *<B>Assertion Description: </B>
+These methods cancel a running query (or queries). The thread that has its query
+canceled will throw a JDOQueryInterruptedException.
+If cancel is not supported (most likely due to the underlying implementation not
+supporting cancel) then JDOUnsupportedOptionException is thrown to the caller.
+ */
+
+public class QueryCancel extends QueryTest {
+
+ /** */
+ private static final String ASSERTION_FAILED =
+ "Assertion A14.6.1-8 (QueryCancel) failed: ";
+
+ /** Single String JDOQL Query to be canceled. */
+ private static String SSJDOQL =
+ "select avg (this.x + point2.y) " +
+ "from PCPoint " +
+ "where this.y >= 0 && point2.x >= 0 " +
+ "variables PCPoint2 point2 " +
+ "import org.apache.jdo.tck.pc.mylib.PCPoint; " +
+ "import org.apache.jdo.tck.pc.mylib.PCPoint2; ";
+
+ /**
+ * The <code>main</code> is called when the class
+ * is directly executed from the command line.
+ * @param args The arguments passed to the program.
+ */
+ public static void main(String[] args) {
+ BatchTestRunner.run(QueryCancel.class);
+ }
+
+ /** */
+ public void testCancel() throws Exception {
+ PersistenceManager pm = getPM();
+ Query query = pm.newQuery(SSJDOQL);
+
+ // Thread executing the query
+ ThreadExceptionHandler group = new ThreadExceptionHandler();
+ QueryExecutor runnable = new QueryExecutor(pm, query);
+ Thread t = new Thread(group, runnable, "Query Executor");
+ t.start();
+
+ try {
+ // cancel query
+ query.cancel(t);
+ if (!isQueryCancelSupported()) {
+ fail(ASSERTION_FAILED,
+ "Query.cancel should throw a JDOQueryInterruptedException, " +
+ "if query canceling is not supported ");
+ }
+ }
+ catch (JDOUnsupportedOptionException ex) {
+ if (isQueryCancelSupported()) {
+ fail(ASSERTION_FAILED,
+ "Query.cancel should not result in a JDOQueryInterruptedException, " +
+ "if query canceling is supported ");
+ }
+ }
+
+ t.join();
+ Throwable problem = group.getUncaughtException(t);
+ if (problem != null) {
+ if (problem instanceof AssertionFailedError)
+ throw (AssertionFailedError)problem;
+ else
+ throw new JDOFatalException( "Thread " + t.getName()+
+ " results in exception ", problem);
+ }
+ }
+
+ /** */
+ public void testCancelAll() throws Exception {
+ PersistenceManager pm = getPM();
+ Query query = pm.newQuery(SSJDOQL);
+
+ // Thread executing the query
+ ThreadExceptionHandler group = new ThreadExceptionHandler();
+ QueryExecutor runnable = new QueryExecutor(pm, query);
+ Thread t = new Thread(group, runnable, "Query Executor");
+ t.start();
+
+ try {
+ // cancel query
+ query.cancelAll();
+ if (!isQueryCancelSupported()) {
+ fail(ASSERTION_FAILED,
+ "Query.cancel should throw a JDOQueryInterruptedException, " +
+ "if query canceling is not supported ");
+ }
+ }
+ catch (JDOUnsupportedOptionException ex) {
+ if (isQueryCancelSupported()) {
+ fail(ASSERTION_FAILED,
+ "Query.cancel should not result in a JDOQueryInterruptedException, " +
+ "if query canceling is supported ");
+ }
+ }
+
+ t.join();
+ Throwable problem = group.getUncaughtException(t);
+ if (problem != null) {
+ if (problem instanceof AssertionFailedError)
+ throw (AssertionFailedError)problem;
+ else
+ throw new JDOFatalException( "Thread " + t.getName()+
+ " results in exception ", problem);
+ }
+ }
+
+ /** Runnable class executing the query. */
+ class QueryExecutor implements Runnable {
+
+ PersistenceManager pm;
+ Query query;
+
+ QueryExecutor(PersistenceManager pm, Query query) {
+ this.pm = pm;
+ this.query = query;
+ }
+
+ public void run() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ Object result = query.execute();
+ tx.commit();
+ tx = null;
+ if (isQueryCancelSupported()) {
+ fail(ASSERTION_FAILED,
+ "Query.execute should result in a JDOQueryInterruptedException, " +
+ "if query canceling is supported.");
+ }
+ }
+ catch (JDOQueryInterruptedException ex) {
+ if (!isQueryCancelSupported()) {
+ fail(ASSERTION_FAILED,
+ "Query.execute should not result in a JDOQueryInterruptedException, " +
+ "if query canceling is not supported.");
+ }
+ }
+ finally {
+ if ((tx != null) && tx.isActive())
+ tx.rollback();
+ }
+ }
+ }
+
+ /**
+ * @see JDO_Test#localSetUp()
+ */
+ protected void localSetUp() {
+ addTearDownClass(PCPoint.class);
+ addTearDownClass(PCPoint2.class);
+
+ // create PCPoint and PCPoint2 instances
+ PersistenceManager pm = getPM();
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ for (int i = 0; i < 1000; i++) {
+ PCPoint obj = new PCPoint(i, i);
+ pm.makePersistent(obj);
+ }
+ for (int i = 0; i < 1000; i++) {
+ PCPoint2 obj = new PCPoint2(i, i);
+ pm.makePersistent(obj);
+ }
+ tx.commit();
+ tx = null;
+ }
+ finally {
+ if ((tx != null) && tx.isActive())
+ tx.rollback();
+ }
+ }
+}
+
Propchange: db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryCancel.java
------------------------------------------------------------------------------
svn:eol-style = LF
Added: db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryTimeout.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryTimeout.java?rev=804355&view=auto
==============================================================================
--- db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryTimeout.java (added)
+++ db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryTimeout.java Fri Aug 14 20:12:52 2009
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jdo.tck.query.api;
+
+import javax.jdo.JDOQueryTimeoutException;
+import javax.jdo.PersistenceManager;
+import javax.jdo.Query;
+import javax.jdo.Transaction;
+
+import org.apache.jdo.tck.JDO_Test;
+import org.apache.jdo.tck.pc.mylib.PCPoint;
+import org.apache.jdo.tck.pc.mylib.PCPoint2;
+import org.apache.jdo.tck.query.QueryTest;
+import org.apache.jdo.tck.util.BatchTestRunner;
+
+/**
+ *<B>Title:</B> QueryTimeout
+ *<BR>
+ *<B>Keywords:</B> query
+ *<BR>
+ *<B>Assertion ID:</B> A14.6.1-7
+ *<BR>
+ *<B>Assertion Description: </B>
+If a non-null timeout value is specified, a running query will be interrupted if
+it exceeds the execution time of the timeout value. The thread executing the query
+will throw JDOQueryTimeoutException. Specifying a timeout value of 0 indicates
+that there is no timeout for this query (a setting of 0 overrides the default
+specified in the Persistence-Manager or the PersistenceManagerFactory).
+ */
+
+public class QueryTimeout extends QueryTest {
+
+ /** */
+ private static final String ASSERTION_FAILED =
+ "Assertion A14.6.1-7 (QueryTimeout) failed: ";
+
+ /** Single String JDOQL Query. */
+ private static String SSJDOQL =
+ "select avg (this.x + point2.y) " +
+ "from PCPoint " +
+ "where this.y >= 0 && point2.x >= 0 " +
+ "variables PCPoint2 point2 " +
+ "import org.apache.jdo.tck.pc.mylib.PCPoint; " +
+ "import org.apache.jdo.tck.pc.mylib.PCPoint2; ";
+
+ /** Timeout value. */
+ private static Integer TIMEOUT_MILLIS = new Integer(10);
+
+ /**
+ * The <code>main</code> is called when the class
+ * is directly executed from the command line.
+ * @param args The arguments passed to the program.
+ */
+ public static void main(String[] args) {
+ BatchTestRunner.run(QueryTimeout.class);
+ }
+
+ /** Method to test setting query timeout. */
+ public void testTimeout() throws Exception {
+ PersistenceManager pm = getPM();
+ Transaction tx = pm.currentTransaction();
+
+ try {
+ tx.begin();
+ Query query = pm.newQuery(SSJDOQL);
+ query.setTimeoutMillis(TIMEOUT_MILLIS);
+ Object result = query.execute();
+ tx.commit();
+ tx = null;
+ fail(ASSERTION_FAILED,
+ "Query.execute should result in a JDOQueryTimeoutException.");
+ }
+ catch (JDOQueryTimeoutException ex) {
+ // expected exception
+ if (debug) {
+ logger.debug("caught expected exception " + ex);
+ }
+ }
+ finally {
+ if ((tx != null) && tx.isActive())
+ tx.rollback();
+ }
+ }
+
+ /** Method to test setting query timeout to 0. */
+ public void testZeroTimeout() throws Exception {
+ PersistenceManager pm = getPM();
+ Transaction tx = pm.currentTransaction();
+
+ try {
+ tx.begin();
+ Query query = pm.newQuery(SSJDOQL);
+ query.setTimeoutMillis(0);
+ Object result = query.execute();
+ tx.commit();
+ tx = null;
+ }
+ catch (JDOQueryTimeoutException ex) {
+ // setting the timeout to 0 should not result in an exception
+ fail(ASSERTION_FAILED,
+ "Query.execute should not result in a JDOQueryTimeoutException.");
+ }
+ finally {
+ if ((tx != null) && tx.isActive())
+ tx.rollback();
+ }
+ }
+
+ /**
+ * @see JDO_Test#localSetUp()
+ */
+ protected void localSetUp() {
+ addTearDownClass(PCPoint.class);
+ addTearDownClass(PCPoint2.class);
+
+ // create PCPoint and PCPoint2 instances
+ PersistenceManager pm = getPM();
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ for (int i = 0; i < 1000; i++) {
+ PCPoint obj = new PCPoint(i, i);
+ pm.makePersistent(obj);
+ }
+ for (int i = 0; i < 1000; i++) {
+ PCPoint2 obj = new PCPoint2(i, i);
+ pm.makePersistent(obj);
+ }
+ tx.commit();
+ tx = null;
+ }
+ finally {
+ if ((tx != null) && tx.isActive())
+ tx.rollback();
+ }
+ }
+}
+
Propchange: db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryTimeout.java
------------------------------------------------------------------------------
svn:eol-style = LF