Return-Path: Delivered-To: apmail-db-jdo-commits-archive@www.apache.org Received: (qmail 78111 invoked from network); 20 Mar 2010 20:57:01 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 20 Mar 2010 20:57:01 -0000 Received: (qmail 62749 invoked by uid 500); 20 Mar 2010 20:57:00 -0000 Mailing-List: contact jdo-commits-help@db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: jdo-dev@db.apache.org Delivered-To: mailing list jdo-commits@db.apache.org Received: (qmail 62742 invoked by uid 99); 20 Mar 2010 20:57:00 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 20 Mar 2010 20:57:00 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 20 Mar 2010 20:56:58 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 5A35923888CE; Sat, 20 Mar 2010 20:56:36 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r925680 - /db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryCancel.java Date: Sat, 20 Mar 2010 20:56:36 -0000 To: jdo-commits@db.apache.org From: mbo@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100320205636.5A35923888CE@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: mbo Date: Sat Mar 20 20:56:36 2010 New Revision: 925680 URL: http://svn.apache.org/viewvc?rev=925680&view=rev Log: JDO-623: added barrier to synchronize the threads Modified: db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryCancel.java Modified: 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=925680&r1=925679&r2=925680&view=diff ============================================================================== --- db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryCancel.java (original) +++ db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/api/QueryCancel.java Sat Mar 20 20:56:36 2010 @@ -17,6 +17,8 @@ package org.apache.jdo.tck.query.api; +import java.util.concurrent.CyclicBarrier; + import junit.framework.AssertionFailedError; import javax.jdo.JDOFatalException; @@ -50,7 +52,10 @@ supporting cancel) then JDOUnsupportedOp public class QueryCancel extends QueryTest { /** Time for the main thread to sleep after starting a parallel thread. */ - private static int MAIN_SLEEP_MILLIS = 1000; + private static int MAIN_SLEEP_MILLIS = 40; + + /** Number of instances to be created. */ + private static int NO_OF_INSTANCES = 5000; /** */ private static final String ASSERTION_FAILED = @@ -77,18 +82,24 @@ public class QueryCancel extends QueryTe /** */ public void testCancel() throws Exception { PersistenceManager pm = getPM(); + // Test query Query query = pm.newQuery(SSJDOQL); + query.compile(); // Thread executing the query + CyclicBarrier barrier = new CyclicBarrier(2); ThreadExceptionHandler group = new ThreadExceptionHandler(); - QueryExecutor runnable = new QueryExecutor(pm, query); + QueryExecutor runnable = new QueryExecutor(pm, query, barrier); Thread t = new Thread(group, runnable, "Query Executor"); t.start(); - // Wait for a second such that the other thread can execute the query - Thread.sleep(MAIN_SLEEP_MILLIS); - try { + // Wait for the other thread + barrier.await(); + + // Wait a couple of millis such that the other thread can start query execution + Thread.sleep(MAIN_SLEEP_MILLIS); + // cancel query query.cancel(t); if (!isQueryCancelSupported()) { @@ -119,19 +130,25 @@ public class QueryCancel extends QueryTe /** */ public void testCancelAll() throws Exception { PersistenceManager pm = getPM(); + // Test query Query query = pm.newQuery(SSJDOQL); + query.compile(); // Thread executing the query + CyclicBarrier barrier = new CyclicBarrier(2); ThreadExceptionHandler group = new ThreadExceptionHandler(); - QueryExecutor runnable = new QueryExecutor(pm, query); + QueryExecutor runnable = new QueryExecutor(pm, query, barrier); Thread t = new Thread(group, runnable, "Query Executor"); t.start(); - // Wait for a second such that the other thread can execute the query - Thread.sleep(MAIN_SLEEP_MILLIS); - try { // cancel query + // Wait for the other thread + barrier.await(); + + // Wait a couple of millis such that the other thread can start query execution + Thread.sleep(MAIN_SLEEP_MILLIS); + query.cancelAll(); if (!isQueryCancelSupported()) { fail(ASSERTION_FAILED, @@ -162,17 +179,23 @@ public class QueryCancel extends QueryTe class QueryExecutor implements Runnable { PersistenceManager pm; + CyclicBarrier barrier; Query query; - QueryExecutor(PersistenceManager pm, Query query) { + QueryExecutor(PersistenceManager pm, Query query, CyclicBarrier barrier) { this.pm = pm; this.query = query; + this.barrier = barrier; } public void run() { Transaction tx = pm.currentTransaction(); try { tx.begin(); + + // wait for the other thread + barrier.await(); + Object result = query.execute(); tx.commit(); tx = null; @@ -189,6 +212,9 @@ public class QueryCancel extends QueryTe "if query canceling is not supported."); } } + catch (Exception ex) { + throw new RuntimeException(ex); + } finally { if ((tx != null) && tx.isActive()) tx.rollback(); @@ -208,11 +234,11 @@ public class QueryCancel extends QueryTe Transaction tx = pm.currentTransaction(); try { tx.begin(); - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < NO_OF_INSTANCES; i++) { PCPoint obj = new PCPoint(i, i); pm.makePersistent(obj); } - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < NO_OF_INSTANCES; i++) { PCPoint2 obj = new PCPoint2(i, i); pm.makePersistent(obj); }