Modified: river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/DestroyTest.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/DestroyTest.java?rev=1634322&r1=1634321&r2=1634322&view=diff ============================================================================== --- river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/DestroyTest.java (original) +++ river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/DestroyTest.java Sun Oct 26 13:17:28 2014 @@ -1,111 +1,113 @@ -/* - * 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 com.sun.jini.test.impl.outrigger.transaction; - -import java.util.logging.Level; - -// Test harness specific classes -import com.sun.jini.qa.harness.TestException; - -// All other imports -import net.jini.space.JavaSpace; -import net.jini.core.transaction.Transaction; -import net.jini.core.transaction.TransactionFactory; -import net.jini.core.transaction.TransactionException; -import net.jini.core.transaction.server.ServerTransaction; -import net.jini.core.lease.Lease; -import net.jini.admin.Administrable; -import com.sun.jini.admin.DestroyAdmin; -import java.rmi.RemoteException; - - -/** - * <code>RecoveryTest</code> tests basic transactional features. - * This test writes an entry under a transaction, then - * <ul> - * <li> try to read/take it with no transaction, and make sure it fails. - * <li> try to read/take it with the same transaction, and make sure it works. - * <li> try to read/take it with another transaction, and make sure it fails. - * </ul> - * - * Test steps are as follows: - * <ol> - * <li> Writes an entry under a transaction. - * <li> Read/take it with no/same/different transaction. - * <li> Check results. - * </ol> - * - * @author H.Fukuda - */ -public class DestroyTest extends TransactionTestBase { - - public void run() throws Exception { - simpleSetup(); - - // create an entry - SimpleEntry wentry = new SimpleEntry(); - wentry.string = "foo"; - wentry.stage = new Integer(1); - wentry.id = new Integer(10); - - // create a template - SimpleEntry template = new SimpleEntry(); - template.string = "foo"; - - // create a transaction object to write - pass("step-1: create a transaction object"); - Transaction txn = null; - - Transaction.Created tc = TransactionFactory.create(getTxmgr(), - Lease.FOREVER); - txn = tc.transaction; - - // write an entry under the transaction - pass("step-2: write an entry with the transaction."); - writeEntry(txn, wentry); - - // shutdown transaction manager. - pass("step-3: destroy transaction manager."); - - DestroyAdmin admin = (DestroyAdmin) ((Administrable) - getTxmgr()).getAdmin(); - admin = (DestroyAdmin) getConfig().prepare("test.mahaloAdminPreparer", - admin); - - admin.destroy(); - - // wait for a while - int destroyDelay = getConfig().getIntConfigVal( - "com.sun.jini.qa.harness.destroy.delay", 10); - - try { - logger.log(Level.INFO, "Destroying worked, sleeping for " - + destroyDelay + " seconds..."); - Thread.sleep(destroyDelay * 1000); - logger.log(Level.INFO, "...awake"); - } catch (InterruptedException e) {} - - // Check abort/commit cause RemoteException - try { - txn.commit(); - throw new TestException("Expected exception was NOT thrown."); - } catch (RemoteException e) { - pass("step-4: RemoteException was thrown, as expected."); - } - } -} +/* + * 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 com.sun.jini.test.impl.outrigger.transaction; + +import java.util.logging.Level; + +// Test harness specific classes +import com.sun.jini.qa.harness.TestException; + +// All other imports +import net.jini.space.JavaSpace; +import net.jini.core.transaction.Transaction; +import net.jini.core.transaction.TransactionFactory; +import net.jini.core.transaction.TransactionException; +import net.jini.core.transaction.server.ServerTransaction; +import net.jini.core.lease.Lease; +import net.jini.admin.Administrable; +import com.sun.jini.admin.DestroyAdmin; +import java.rmi.RemoteException; + + +/** + * <code>RecoveryTest</code> tests basic transactional features. + * This test writes an entry under a transaction, then + * <ul> + * <li> try to read/take it with no transaction, and make sure it fails. + * <li> try to read/take it with the same transaction, and make sure it works. + * <li> try to read/take it with another transaction, and make sure it fails. + * </ul> + * + * Test steps are as follows: + * <ol> + * <li> Writes an entry under a transaction. + * <li> Read/take it with no/same/different transaction. + * <li> Check results. + * </ol> + * + * @author H.Fukuda + */ +public class DestroyTest extends TransactionTestBase { + + public void run() throws Exception { + simpleSetup(); + + // create an entry + SimpleEntry wentry = new SimpleEntry(); + wentry.string = "foo"; + wentry.stage = new Integer(1); + wentry.id = new Integer(10); + + // create a template + SimpleEntry template = new SimpleEntry(); + template.string = "foo"; + + // create a transaction object to write + pass("step-1: create a transaction object"); + Transaction txn = null; + + Transaction.Created tc = TransactionFactory.create(getTxmgr(), + Lease.FOREVER); + txn = tc.transaction; + + // write an entry under the transaction + pass("step-2: write an entry with the transaction."); + writeEntry(txn, wentry); + + // shutdown transaction manager. + pass("step-3: destroy transaction manager."); + + DestroyAdmin admin = (DestroyAdmin) ((Administrable) + getTxmgr()).getAdmin(); + admin = (DestroyAdmin) getConfig().prepare("test.mahaloAdminPreparer", + admin); + + admin.destroy(); + + // wait for a while + int destroyDelay = getConfig().getIntConfigVal( + "com.sun.jini.qa.harness.destroy.delay", 10); + + try { + logger.log(Level.INFO, "Destroying worked, sleeping for " + + destroyDelay + " seconds..."); + Thread.sleep(destroyDelay * 1000); + logger.log(Level.INFO, "...awake"); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + + // Check abort/commit cause RemoteException + try { + txn.commit(); + throw new TestException("Expected exception was NOT thrown."); + } catch (RemoteException e) { + pass("step-4: RemoteException was thrown, as expected."); + } + } +}
Modified: river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/ExceptionTest2.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/ExceptionTest2.java?rev=1634322&r1=1634321&r2=1634322&view=diff ============================================================================== --- river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/ExceptionTest2.java (original) +++ river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/ExceptionTest2.java Sun Oct 26 13:17:28 2014 @@ -1,165 +1,169 @@ -/* - * 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 com.sun.jini.test.impl.outrigger.transaction; - -import java.util.logging.Level; - -// Test harness specific classes -import com.sun.jini.qa.harness.TestException; - -// All other imports -import net.jini.space.JavaSpace; -import net.jini.core.entry.Entry; -import net.jini.core.transaction.Transaction; -import net.jini.core.transaction.TransactionException; - -/** - * Force to generate <tt>TransactionException</tt>s - * by aborting the transaction while <tt>space.read</tt> or <tt>space.take</tt> - * is in progress. - * - * <ol> - * <li> Thread #1 tries to read/take an entry with a transaction. - * A space is empty, and this thread will be blocked. - * <li> Thread #2 attempts to abort the transaction. - * <li> Thread #1 will be thrown <tt>TransactionException</tt>. - * </ol> - * - * @author H.Fukuda - */ -public class ExceptionTest2 extends TransactionTestBase { - - // commonly used entry & template - SimpleEntry wentry; - SimpleEntry template; - - /* - * Var to detect that EntryGetter failed. - * Has been added during porting. - */ - private Exception failException = null; - - public void run() throws Exception { - simpleSetup(); - - // create an entry - wentry = new SimpleEntry(); - wentry.string = "foo"; - wentry.stage = new Integer(1); - wentry.id = new Integer(8); - - // create a template - template = new SimpleEntry(); - template.string = "foo"; - - // read/abort/TransactionException useRead IfExists Transaction_type - testAbortTransaction(new SpaceOperation(true, false, - SpaceOperation.USE_SAME)); - testAbortTransaction(new SpaceOperation(false, false, - SpaceOperation.USE_SAME)); - } - - private void testAbortTransaction(SpaceOperation ope) - throws Exception { - - // cleanup the space - scrubSpaces(); - - // create a transaction object to write - Transaction txn = createTransaction(); - - // invoke read/take thread - EntryGetter getter = new EntryGetter(getSpace(), ope, txn, this); - getter.start(); - - // wait for a while, - try { - Thread.sleep(1000); - } catch (InterruptedException ie) {} - - // then commit/abort the transaction - pass("step-2: thread #1: abort the transaction"); - abortTransaction(txn); - - // wait for the thread stops - try { - getter.join(); - } catch (InterruptedException ie) {} - - if (failException != null) { - throw failException; - } - - // check TransactionException has occurred - if (!getter.isTxnExceptionOccurred()) { - throw new TestException("TransactionException was not " - + "thrown as expected. [" + ope + "]"); - } else { - pass("TransactionException was thrown as expected. [" + ope + "]"); - } - } - - - class EntryGetter extends Thread { - private JavaSpace space; - private SpaceOperation ope; - private Transaction txn; - private TransactionTestBase parent; - private boolean exceptionOccurred = false; - private Entry entry; - - public EntryGetter(JavaSpace space, SpaceOperation ope, Transaction txn, - TransactionTestBase parent) { - this.space = space; - this.ope = ope; - this.txn = txn; - this.parent = parent; - this.entry = null; - } - - public synchronized void run() { - try { - - // read/take access to empty JavaSpace - parent.pass("step-1: thread #2: start read/take with a" - + " transaction (blocked)"); - - try { - entry = ope.get(space, template, txn, 10000); - } catch (TransactionException te) { - exceptionOccurred = true; - parent.pass("step-3: thread #2: TransactionException" - + " occurred as expected. [" + ope + "]"); - } catch (Exception e) { - parent.fail("Unexpected exception has been thrown", e); - } - - /* - * parent.pass("[Thread #2]: got entry is [" + entry + "] [" - * + ope + "]"); - */ - } catch (Exception e) { - failException = e; - } - } - - public synchronized boolean isTxnExceptionOccurred() { - return exceptionOccurred; - } - } -} +/* + * 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 com.sun.jini.test.impl.outrigger.transaction; + +import java.util.logging.Level; + +// Test harness specific classes +import com.sun.jini.qa.harness.TestException; + +// All other imports +import net.jini.space.JavaSpace; +import net.jini.core.entry.Entry; +import net.jini.core.transaction.Transaction; +import net.jini.core.transaction.TransactionException; + +/** + * Force to generate <tt>TransactionException</tt>s + * by aborting the transaction while <tt>space.read</tt> or <tt>space.take</tt> + * is in progress. + * + * <ol> + * <li> Thread #1 tries to read/take an entry with a transaction. + * A space is empty, and this thread will be blocked. + * <li> Thread #2 attempts to abort the transaction. + * <li> Thread #1 will be thrown <tt>TransactionException</tt>. + * </ol> + * + * @author H.Fukuda + */ +public class ExceptionTest2 extends TransactionTestBase { + + // commonly used entry & template + SimpleEntry wentry; + SimpleEntry template; + + /* + * Var to detect that EntryGetter failed. + * Has been added during porting. + */ + private Exception failException = null; + + public void run() throws Exception { + simpleSetup(); + + // create an entry + wentry = new SimpleEntry(); + wentry.string = "foo"; + wentry.stage = new Integer(1); + wentry.id = new Integer(8); + + // create a template + template = new SimpleEntry(); + template.string = "foo"; + + // read/abort/TransactionException useRead IfExists Transaction_type + testAbortTransaction(new SpaceOperation(true, false, + SpaceOperation.USE_SAME)); + testAbortTransaction(new SpaceOperation(false, false, + SpaceOperation.USE_SAME)); + } + + private void testAbortTransaction(SpaceOperation ope) + throws Exception { + + // cleanup the space + scrubSpaces(); + + // create a transaction object to write + Transaction txn = createTransaction(); + + // invoke read/take thread + EntryGetter getter = new EntryGetter(getSpace(), ope, txn, this); + getter.start(); + + // wait for a while, + try { + Thread.sleep(1000); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + + // then commit/abort the transaction + pass("step-2: thread #1: abort the transaction"); + abortTransaction(txn); + + // wait for the thread stops + try { + getter.join(); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + + if (failException != null) { + throw failException; + } + + // check TransactionException has occurred + if (!getter.isTxnExceptionOccurred()) { + throw new TestException("TransactionException was not " + + "thrown as expected. [" + ope + "]"); + } else { + pass("TransactionException was thrown as expected. [" + ope + "]"); + } + } + + + class EntryGetter extends Thread { + private JavaSpace space; + private SpaceOperation ope; + private Transaction txn; + private TransactionTestBase parent; + private boolean exceptionOccurred = false; + private Entry entry; + + public EntryGetter(JavaSpace space, SpaceOperation ope, Transaction txn, + TransactionTestBase parent) { + this.space = space; + this.ope = ope; + this.txn = txn; + this.parent = parent; + this.entry = null; + } + + public synchronized void run() { + try { + + // read/take access to empty JavaSpace + parent.pass("step-1: thread #2: start read/take with a" + + " transaction (blocked)"); + + try { + entry = ope.get(space, template, txn, 10000); + } catch (TransactionException te) { + exceptionOccurred = true; + parent.pass("step-3: thread #2: TransactionException" + + " occurred as expected. [" + ope + "]"); + } catch (Exception e) { + parent.fail("Unexpected exception has been thrown", e); + } + + /* + * parent.pass("[Thread #2]: got entry is [" + entry + "] [" + * + ope + "]"); + */ + } catch (Exception e) { + failException = e; + } + } + + public synchronized boolean isTxnExceptionOccurred() { + return exceptionOccurred; + } + } +} Modified: river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/GetBlockTest.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/GetBlockTest.java?rev=1634322&r1=1634321&r2=1634322&view=diff ============================================================================== --- river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/GetBlockTest.java (original) +++ river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/GetBlockTest.java Sun Oct 26 13:17:28 2014 @@ -1,263 +1,267 @@ -/* - * 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 com.sun.jini.test.impl.outrigger.transaction; - -import java.util.logging.Level; - -// Test harness specific classes -import com.sun.jini.qa.harness.TestException; - -// All other imports -import net.jini.space.JavaSpace; -import net.jini.core.entry.Entry; -import net.jini.core.transaction.Transaction; - - -/** - * Checks if read/take works well when a matching entry is written during the - * operations. In this test, <tt>read</tt> and <tt>readIfExists</tt> should - * work equally. - * - * <table border=1 bgcolor="honeydew"> - * <tr> - * <td width=30%> - * <td width=20% align=center><tt>read</tt> - * <td width=20% align=center><tt>readIfExists</tt> - * <tr> - * <td>No matching entry exists - * <td align=center>wait - * <td align=center>null - * <tr bgcolor="orange"> - * <td>Matching entry under transaction - * <td align=center><b>wait</b> - * <td align=center><b>wait</b> - * <tr> - * <td>Matching entry available - * <td align=center>entry - * <td align=center>entry - * </table> - * - * <ol> - * <li> Thread #1 writes an entry into a space under a transaction. - * <li> Thread #2 tries to read/take the entry with no/another transaction. - * This thread will be blocked. - * <li> Thread #1 commits/aborts the transaction. - * This termination makes the entry visible from the outside of the - * transaction. - * <li> Read/take operation of thread #2 returns. - * In the case of abort, read/take gives up and null will be returned. - * <li> Check results. - * </ol> - * - * @author H.Fukuda - */ -public class GetBlockTest extends TransactionTestBase { - - // commonly used entry & template - SimpleEntry wentry; - SimpleEntry template; - - /* - * Var to detect that EntryGetter failed. - * Has been added during porting. - */ - private boolean isFailed = false; - private String failMsg = ""; - - public void run() throws Exception { - simpleSetup(); - - // create an entry - wentry = new SimpleEntry(); - wentry.string = "foo"; - wentry.stage = new Integer(1); - wentry.id = new Integer(5); - - // create a template - template = new SimpleEntry(); - template.string = "foo"; - - // useRead IfExists Transaction_type useAbort - testIfExists(new SpaceOperation(true, false, - SpaceOperation.USE_NULL), true); - testIfExists(new SpaceOperation(true, true, - SpaceOperation.USE_NULL), true); - testIfExists(new SpaceOperation(false, false, - SpaceOperation.USE_NULL), true); - testIfExists(new SpaceOperation(false, true, - SpaceOperation.USE_NULL), true); - testIfExists(new SpaceOperation(true, false, - SpaceOperation.USE_NULL), false); - testIfExists(new SpaceOperation(true, true, - SpaceOperation.USE_NULL), false); - testIfExists(new SpaceOperation(false, false, - SpaceOperation.USE_NULL), false); - testIfExists(new SpaceOperation(false, true, - SpaceOperation.USE_NULL), false); - testIfExists(new SpaceOperation(true, false, - SpaceOperation.USE_DIFF), true); - testIfExists(new SpaceOperation(true, true, - SpaceOperation.USE_DIFF), true); - testIfExists(new SpaceOperation(false, false, - SpaceOperation.USE_DIFF), true); - testIfExists(new SpaceOperation(false, true, - SpaceOperation.USE_DIFF), true); - testIfExists(new SpaceOperation(true, false, - SpaceOperation.USE_DIFF), false); - testIfExists(new SpaceOperation(true, true, - SpaceOperation.USE_DIFF), false); - testIfExists(new SpaceOperation(false, false, - SpaceOperation.USE_DIFF), false); - testIfExists(new SpaceOperation(false, true, - SpaceOperation.USE_DIFF), false); - } - - private void testIfExists(SpaceOperation ope, boolean useAbort) - throws TestException { - - // cleanup the space - scrubSpaces(); - - // create a transaction object to write - Transaction txn = createTransaction(); - - // write entry under this transaction - pass("step-1: thread #1: write an entry under a transaction"); - writeEntry(txn, wentry); - - // create another transaction object - Transaction txn2 = null; - - if (ope.getTxnType() == SpaceOperation.USE_DIFF) { - txn2 = createTransaction(); - } - - // invoke read/take thread - EntryGetter getter = new EntryGetter(getSpace(), ope, txn2, this); - getter.start(); - - // wait for a while, - try { - Thread.sleep(1000); - } catch (InterruptedException ie) {} - - // commit/abort the transaction - if (useAbort) { - pass("step-3: thread #1: abort the transaction"); - abortTransaction(txn); - } else { - pass("step-3: thread #1: commit the transaction"); - commitTransaction(txn); - } - - // wait for threads - try { - pass("step-4: thread #1: wait until thread #2 die."); - getter.join(); - } catch (InterruptedException ie) {} - - if (isFailed) { - fail("EntryGetter failed with exception" + failMsg); - } - - // check the result - if (useAbort) { - - // in case of abort, getter returns by timeout and get null - if (!getter.isNullEntry()) { - fail("Non-null entry has returned after aborted transaction. [" - + ope + "]"); - } else { - pass("[Pass]: abort for [" + ope + "]"); - } - } else { - - // in case of commit, getter gets a non-null entry - if (getter.isNullEntry()) { - fail("Null entry has returned after committed transaction. [" - + ope + "]"); - } else { - pass("[Pass]: commit for [" + ope + "]"); - } - } - - // abort dummy transaction - if (txn2 != null) { - abortTransaction(txn2); - } - } - - - class EntryGetter extends Thread { - private JavaSpace space; - private SpaceOperation ope; - private Transaction txn; - private TransactionTestBase parent; - private boolean exceptionOccurred = false; - private Entry gotEntry = null; - - public EntryGetter(JavaSpace space, SpaceOperation ope, Transaction txn, - TransactionTestBase parent) { - this.space = space; - this.ope = ope; - this.txn = txn; - this.parent = parent; - } - - public void run() { - try { - - // read/take access to empty JavaSpace - if (txn == null) { - parent.pass("step-2: thread #2: try to get the entry" - + " without transcation. [blocked]"); - } else { - parent.pass("step-2: thread #2: try to get the entry with" - + " another transcation. [blocked]"); - } - - try { - gotEntry = ope.get(space, template, txn, 10000); - } catch (Exception e) { - parent.fail("Unexpected exception has thrown", e); - } - parent.pass("step-5: thread #2: end of the thread."); - } catch (Exception e) { - isFailed = true; - failMsg = e.toString(); - } - } - - public boolean isNullEntry() { - if (gotEntry == null) { - return true; - } else { - return false; - } - } - } - - /** - * Return a String which describes this test - */ - public String getDescription() { - return "Test Name = GetBlockTest : \n" - + "Checks if read/take works well when a matching entry is\n" - + "written during the operations."; - } -} +/* + * 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 com.sun.jini.test.impl.outrigger.transaction; + +import java.util.logging.Level; + +// Test harness specific classes +import com.sun.jini.qa.harness.TestException; + +// All other imports +import net.jini.space.JavaSpace; +import net.jini.core.entry.Entry; +import net.jini.core.transaction.Transaction; + + +/** + * Checks if read/take works well when a matching entry is written during the + * operations. In this test, <tt>read</tt> and <tt>readIfExists</tt> should + * work equally. + * + * <table border=1 bgcolor="honeydew"> + * <tr> + * <td width=30%> + * <td width=20% align=center><tt>read</tt> + * <td width=20% align=center><tt>readIfExists</tt> + * <tr> + * <td>No matching entry exists + * <td align=center>wait + * <td align=center>null + * <tr bgcolor="orange"> + * <td>Matching entry under transaction + * <td align=center><b>wait</b> + * <td align=center><b>wait</b> + * <tr> + * <td>Matching entry available + * <td align=center>entry + * <td align=center>entry + * </table> + * + * <ol> + * <li> Thread #1 writes an entry into a space under a transaction. + * <li> Thread #2 tries to read/take the entry with no/another transaction. + * This thread will be blocked. + * <li> Thread #1 commits/aborts the transaction. + * This termination makes the entry visible from the outside of the + * transaction. + * <li> Read/take operation of thread #2 returns. + * In the case of abort, read/take gives up and null will be returned. + * <li> Check results. + * </ol> + * + * @author H.Fukuda + */ +public class GetBlockTest extends TransactionTestBase { + + // commonly used entry & template + SimpleEntry wentry; + SimpleEntry template; + + /* + * Var to detect that EntryGetter failed. + * Has been added during porting. + */ + private boolean isFailed = false; + private String failMsg = ""; + + public void run() throws Exception { + simpleSetup(); + + // create an entry + wentry = new SimpleEntry(); + wentry.string = "foo"; + wentry.stage = new Integer(1); + wentry.id = new Integer(5); + + // create a template + template = new SimpleEntry(); + template.string = "foo"; + + // useRead IfExists Transaction_type useAbort + testIfExists(new SpaceOperation(true, false, + SpaceOperation.USE_NULL), true); + testIfExists(new SpaceOperation(true, true, + SpaceOperation.USE_NULL), true); + testIfExists(new SpaceOperation(false, false, + SpaceOperation.USE_NULL), true); + testIfExists(new SpaceOperation(false, true, + SpaceOperation.USE_NULL), true); + testIfExists(new SpaceOperation(true, false, + SpaceOperation.USE_NULL), false); + testIfExists(new SpaceOperation(true, true, + SpaceOperation.USE_NULL), false); + testIfExists(new SpaceOperation(false, false, + SpaceOperation.USE_NULL), false); + testIfExists(new SpaceOperation(false, true, + SpaceOperation.USE_NULL), false); + testIfExists(new SpaceOperation(true, false, + SpaceOperation.USE_DIFF), true); + testIfExists(new SpaceOperation(true, true, + SpaceOperation.USE_DIFF), true); + testIfExists(new SpaceOperation(false, false, + SpaceOperation.USE_DIFF), true); + testIfExists(new SpaceOperation(false, true, + SpaceOperation.USE_DIFF), true); + testIfExists(new SpaceOperation(true, false, + SpaceOperation.USE_DIFF), false); + testIfExists(new SpaceOperation(true, true, + SpaceOperation.USE_DIFF), false); + testIfExists(new SpaceOperation(false, false, + SpaceOperation.USE_DIFF), false); + testIfExists(new SpaceOperation(false, true, + SpaceOperation.USE_DIFF), false); + } + + private void testIfExists(SpaceOperation ope, boolean useAbort) + throws TestException { + + // cleanup the space + scrubSpaces(); + + // create a transaction object to write + Transaction txn = createTransaction(); + + // write entry under this transaction + pass("step-1: thread #1: write an entry under a transaction"); + writeEntry(txn, wentry); + + // create another transaction object + Transaction txn2 = null; + + if (ope.getTxnType() == SpaceOperation.USE_DIFF) { + txn2 = createTransaction(); + } + + // invoke read/take thread + EntryGetter getter = new EntryGetter(getSpace(), ope, txn2, this); + getter.start(); + + // wait for a while, + try { + Thread.sleep(1000); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + + // commit/abort the transaction + if (useAbort) { + pass("step-3: thread #1: abort the transaction"); + abortTransaction(txn); + } else { + pass("step-3: thread #1: commit the transaction"); + commitTransaction(txn); + } + + // wait for threads + try { + pass("step-4: thread #1: wait until thread #2 die."); + getter.join(); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + + if (isFailed) { + fail("EntryGetter failed with exception" + failMsg); + } + + // check the result + if (useAbort) { + + // in case of abort, getter returns by timeout and get null + if (!getter.isNullEntry()) { + fail("Non-null entry has returned after aborted transaction. [" + + ope + "]"); + } else { + pass("[Pass]: abort for [" + ope + "]"); + } + } else { + + // in case of commit, getter gets a non-null entry + if (getter.isNullEntry()) { + fail("Null entry has returned after committed transaction. [" + + ope + "]"); + } else { + pass("[Pass]: commit for [" + ope + "]"); + } + } + + // abort dummy transaction + if (txn2 != null) { + abortTransaction(txn2); + } + } + + + class EntryGetter extends Thread { + private JavaSpace space; + private SpaceOperation ope; + private Transaction txn; + private TransactionTestBase parent; + private boolean exceptionOccurred = false; + private Entry gotEntry = null; + + public EntryGetter(JavaSpace space, SpaceOperation ope, Transaction txn, + TransactionTestBase parent) { + this.space = space; + this.ope = ope; + this.txn = txn; + this.parent = parent; + } + + public void run() { + try { + + // read/take access to empty JavaSpace + if (txn == null) { + parent.pass("step-2: thread #2: try to get the entry" + + " without transcation. [blocked]"); + } else { + parent.pass("step-2: thread #2: try to get the entry with" + + " another transcation. [blocked]"); + } + + try { + gotEntry = ope.get(space, template, txn, 10000); + } catch (Exception e) { + parent.fail("Unexpected exception has thrown", e); + } + parent.pass("step-5: thread #2: end of the thread."); + } catch (Exception e) { + isFailed = true; + failMsg = e.toString(); + } + } + + public boolean isNullEntry() { + if (gotEntry == null) { + return true; + } else { + return false; + } + } + } + + /** + * Return a String which describes this test + */ + public String getDescription() { + return "Test Name = GetBlockTest : \n" + + "Checks if read/take works well when a matching entry is\n" + + "written during the operations."; + } +} Modified: river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/IfExistsTest.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/IfExistsTest.java?rev=1634322&r1=1634321&r2=1634322&view=diff ============================================================================== --- river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/IfExistsTest.java (original) +++ river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/IfExistsTest.java Sun Oct 26 13:17:28 2014 @@ -1,242 +1,246 @@ -/* - * 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 com.sun.jini.test.impl.outrigger.transaction; - -import java.util.logging.Level; - -// Test harness specific classes -import com.sun.jini.qa.harness.TestException; - -// All other imports -import net.jini.space.JavaSpace; -import net.jini.core.entry.Entry; -import net.jini.core.transaction.Transaction; - - -/** - * Checks if read/take works well when a matching entry is written during the - * operations. In this test, <tt>read</tt> and <tt>readIfExists</tt> should - * work equally. - * - * <table border=1 bgcolor="honeydew"> - * <tr> - * <td width=30%> - * <td width=20% align=center><tt>read</tt> - * <td width=20% align=center><tt>readIfExists</tt> - * <tr> - * <td>No matching entry exists - * <td align=center>wait - * <td align=center>null - * <tr bgcolor="orange"> - * <td>Matching entry under transaction - * <td align=center><b>wait</b> - * <td align=center><b>wait</b> - * <tr> - * <td>Matching entry available - * <td align=center>entry - * <td align=center>entry - * </table> - * - * <ol> - * <li> Thread #1 writes an entry into a space under a transaction. - * <li> Thread #2 tries to read/take the entry with no/another transaction. - * This thread will be blocked. - * <li> Thread #1 commits/aborts the transaction. - * This termination makes the entry visible from the outside of the - * transaction. - * <li> Read/take operation of thread #2 returns. - * In the case of abort, read/take gives up and null will be returned. - * <li> Check results. - * </ol> - * - * @author H.Fukuda - */ -public class IfExistsTest extends TransactionTestBase { - - // commonly used entry & template - SimpleEntry wentry; - SimpleEntry template; - - /* - * Var to detect that EntryGetter failed. - * Has been added during porting. - */ - private boolean isFailed = false; - private String failMsg = ""; - - public void run() throws Exception { - simpleSetup(); - - // create an entry - wentry = new SimpleEntry(); - wentry.string = "foo"; - wentry.stage = new Integer(1); - wentry.id = new Integer(5); - - // create a template - template = new SimpleEntry(); - template.string = "foo"; - - // useRead IfExists Transaction_type - testIfExists(new SpaceOperation(true, false, - SpaceOperation.USE_NULL)); - testIfExists(new SpaceOperation(true, true, - SpaceOperation.USE_NULL)); - testIfExists(new SpaceOperation(false, false, - SpaceOperation.USE_NULL)); - testIfExists(new SpaceOperation(false, true, - SpaceOperation.USE_NULL)); - testIfExists(new SpaceOperation(true, false, - SpaceOperation.USE_DIFF)); - testIfExists(new SpaceOperation(true, true, - SpaceOperation.USE_DIFF)); - testIfExists(new SpaceOperation(false, false, - SpaceOperation.USE_DIFF)); - testIfExists(new SpaceOperation(false, true, - SpaceOperation.USE_DIFF)); - } - - private void testIfExists(SpaceOperation ope) throws TestException { - - // cleanup the space - scrubSpaces(); - - // create another transaction object - Transaction txn = null; - - if (ope.getTxnType() == SpaceOperation.USE_DIFF) { - txn = createTransaction(); - } - - // invoke read/take thread - EntryGetter getter = new EntryGetter(getSpace(), ope, txn, this); - getter.start(); - - // wait for a while, - try { - Thread.sleep(1000); - } catch (InterruptedException ie) {} - - // write matching entry - if (ope.isIfExists()) { - pass("step-3: thread #1: write a matching entry."); - } else { - pass("step-2: thread #1: write a matching entry."); - } - writeEntry(null, wentry); - - // wait for threads - try { - if (ope.isIfExists()) { - pass("step-4: thread #1: wait until thread #2 die."); - } else { - pass("step-3: thread #1: wait until thread #2 die."); - } - getter.join(); - } catch (InterruptedException ie) {} - - if (isFailed) { - fail("EntryGetter failed with exception" + failMsg); - } - - // check the result - if (ope.isIfExists()) { - - // null should be retured for "IfExists" operation - if (getter.isNullEntry()) { - pass("[Pass]: " + ope); - } else { - fail("Non-null entry has returned. [" + ope + "]"); - } - } else { - if (getter.isNullEntry()) { - fail("Null entry has returned. [" + ope + "]"); - } else { - pass("[Pass]: " + ope); - } - } - - if (txn != null) { - abortTransaction(txn); - } - } - - - class EntryGetter extends Thread { - private JavaSpace space; - private SpaceOperation ope; - private Transaction txn; - private TransactionTestBase parent; - private boolean exceptionOccurred = false; - private Entry gotEntry = null; - - public EntryGetter(JavaSpace space, SpaceOperation ope, Transaction txn, - TransactionTestBase parent) { - this.space = space; - this.ope = ope; - this.txn = txn; - this.parent = parent; - } - - public void run() { - try { - - // read/take access to empty JavaSpace - if (txn == null) { - parent.pass("step-1: thread #2: try to get an entry without" - + " transcation."); - } else { - parent.pass("step-1: thread #2: try to get an entry with a" - + " transcation."); - } - - try { - gotEntry = ope.get(space, template, txn, 5000); - } catch (Exception e) { - parent.fail("Unexpected exception has thrown", e); - } - - if (ope.isIfExists()) { - parent.pass("step-2: thread #2: end of the thread."); - } else { - parent.pass("step-4: thread #2: end of the thread."); - } - } catch (Exception e) { - isFailed = true; - failMsg = e.toString(); - } - } - - public boolean isNullEntry() { - if (gotEntry == null) { - return true; - } else { - return false; - } - } - } - - /** - * Return a String which describes this test - */ - public String getDescription() { - return "Test Name = IfExistsTest : \n" - + "Checks if read/take works well when a matching entry is\n" - + "written during the operations."; - } -} +/* + * 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 com.sun.jini.test.impl.outrigger.transaction; + +import java.util.logging.Level; + +// Test harness specific classes +import com.sun.jini.qa.harness.TestException; + +// All other imports +import net.jini.space.JavaSpace; +import net.jini.core.entry.Entry; +import net.jini.core.transaction.Transaction; + + +/** + * Checks if read/take works well when a matching entry is written during the + * operations. In this test, <tt>read</tt> and <tt>readIfExists</tt> should + * work equally. + * + * <table border=1 bgcolor="honeydew"> + * <tr> + * <td width=30%> + * <td width=20% align=center><tt>read</tt> + * <td width=20% align=center><tt>readIfExists</tt> + * <tr> + * <td>No matching entry exists + * <td align=center>wait + * <td align=center>null + * <tr bgcolor="orange"> + * <td>Matching entry under transaction + * <td align=center><b>wait</b> + * <td align=center><b>wait</b> + * <tr> + * <td>Matching entry available + * <td align=center>entry + * <td align=center>entry + * </table> + * + * <ol> + * <li> Thread #1 writes an entry into a space under a transaction. + * <li> Thread #2 tries to read/take the entry with no/another transaction. + * This thread will be blocked. + * <li> Thread #1 commits/aborts the transaction. + * This termination makes the entry visible from the outside of the + * transaction. + * <li> Read/take operation of thread #2 returns. + * In the case of abort, read/take gives up and null will be returned. + * <li> Check results. + * </ol> + * + * @author H.Fukuda + */ +public class IfExistsTest extends TransactionTestBase { + + // commonly used entry & template + SimpleEntry wentry; + SimpleEntry template; + + /* + * Var to detect that EntryGetter failed. + * Has been added during porting. + */ + private boolean isFailed = false; + private String failMsg = ""; + + public void run() throws Exception { + simpleSetup(); + + // create an entry + wentry = new SimpleEntry(); + wentry.string = "foo"; + wentry.stage = new Integer(1); + wentry.id = new Integer(5); + + // create a template + template = new SimpleEntry(); + template.string = "foo"; + + // useRead IfExists Transaction_type + testIfExists(new SpaceOperation(true, false, + SpaceOperation.USE_NULL)); + testIfExists(new SpaceOperation(true, true, + SpaceOperation.USE_NULL)); + testIfExists(new SpaceOperation(false, false, + SpaceOperation.USE_NULL)); + testIfExists(new SpaceOperation(false, true, + SpaceOperation.USE_NULL)); + testIfExists(new SpaceOperation(true, false, + SpaceOperation.USE_DIFF)); + testIfExists(new SpaceOperation(true, true, + SpaceOperation.USE_DIFF)); + testIfExists(new SpaceOperation(false, false, + SpaceOperation.USE_DIFF)); + testIfExists(new SpaceOperation(false, true, + SpaceOperation.USE_DIFF)); + } + + private void testIfExists(SpaceOperation ope) throws TestException { + + // cleanup the space + scrubSpaces(); + + // create another transaction object + Transaction txn = null; + + if (ope.getTxnType() == SpaceOperation.USE_DIFF) { + txn = createTransaction(); + } + + // invoke read/take thread + EntryGetter getter = new EntryGetter(getSpace(), ope, txn, this); + getter.start(); + + // wait for a while, + try { + Thread.sleep(1000); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + + // write matching entry + if (ope.isIfExists()) { + pass("step-3: thread #1: write a matching entry."); + } else { + pass("step-2: thread #1: write a matching entry."); + } + writeEntry(null, wentry); + + // wait for threads + try { + if (ope.isIfExists()) { + pass("step-4: thread #1: wait until thread #2 die."); + } else { + pass("step-3: thread #1: wait until thread #2 die."); + } + getter.join(); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + + if (isFailed) { + fail("EntryGetter failed with exception" + failMsg); + } + + // check the result + if (ope.isIfExists()) { + + // null should be retured for "IfExists" operation + if (getter.isNullEntry()) { + pass("[Pass]: " + ope); + } else { + fail("Non-null entry has returned. [" + ope + "]"); + } + } else { + if (getter.isNullEntry()) { + fail("Null entry has returned. [" + ope + "]"); + } else { + pass("[Pass]: " + ope); + } + } + + if (txn != null) { + abortTransaction(txn); + } + } + + + class EntryGetter extends Thread { + private JavaSpace space; + private SpaceOperation ope; + private Transaction txn; + private TransactionTestBase parent; + private boolean exceptionOccurred = false; + private Entry gotEntry = null; + + public EntryGetter(JavaSpace space, SpaceOperation ope, Transaction txn, + TransactionTestBase parent) { + this.space = space; + this.ope = ope; + this.txn = txn; + this.parent = parent; + } + + public void run() { + try { + + // read/take access to empty JavaSpace + if (txn == null) { + parent.pass("step-1: thread #2: try to get an entry without" + + " transcation."); + } else { + parent.pass("step-1: thread #2: try to get an entry with a" + + " transcation."); + } + + try { + gotEntry = ope.get(space, template, txn, 5000); + } catch (Exception e) { + parent.fail("Unexpected exception has thrown", e); + } + + if (ope.isIfExists()) { + parent.pass("step-2: thread #2: end of the thread."); + } else { + parent.pass("step-4: thread #2: end of the thread."); + } + } catch (Exception e) { + isFailed = true; + failMsg = e.toString(); + } + } + + public boolean isNullEntry() { + if (gotEntry == null) { + return true; + } else { + return false; + } + } + } + + /** + * Return a String which describes this test + */ + public String getDescription() { + return "Test Name = IfExistsTest : \n" + + "Checks if read/take works well when a matching entry is\n" + + "written during the operations."; + } +} Modified: river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/MultipleAccessTest.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/MultipleAccessTest.java?rev=1634322&r1=1634321&r2=1634322&view=diff ============================================================================== --- river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/MultipleAccessTest.java (original) +++ river/jtsk/skunk/qa_refactor/trunk/qa/src/com/sun/jini/test/impl/outrigger/transaction/MultipleAccessTest.java Sun Oct 26 13:17:28 2014 @@ -1,340 +1,348 @@ -/* - * 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 com.sun.jini.test.impl.outrigger.transaction; - -import java.util.logging.Level; - -// Test harness specific classes -import com.sun.jini.qa.harness.TestException; - -// All other imports -import net.jini.core.lease.Lease; -import net.jini.space.JavaSpace; -import net.jini.core.transaction.Transaction; -import java.rmi.UnmarshalException; -import java.rmi.RemoteException; - - -/** - * Three (or more) threads pass entries each other under a transaction. - * These thread work independently, and accesses to the space will be done - * simultaneously. While these accesses, another illegal client try to read - * entries. - * <ul> - * <li> Make sure that the illegal client can't read any entries. - * <li> Make sure all entries are passed correctly. - * </ul> - * - * @author H.Fukuda - */ -public class MultipleAccessTest extends TransactionTestBase { - private final static int NUM_WORKERS = 4; - private final static int NUM_ENTRIES = 20; - - /* - * Vars to detect that Peeker/Worker failed. - * Has been added during porting. - */ - private volatile boolean peekerFailed = false; - private volatile boolean workerFailed = false; - private volatile String peekerFailMsg = ""; - private volatile String workerFailMsg = ""; - - public void run() throws Exception { - simpleSetup(); - - // create a transaction - Transaction txn = createTransaction(); - - // generate & write entries - writeEntries(txn); - - // start illegal access thread - Peeker peeker = new Peeker(getSpace(), this); - peeker.start(); - - // create & start worker threads - Worker[] workers = new Worker[NUM_WORKERS]; - - for (int i = 0; i < NUM_WORKERS; i++) { - workers[i] = new Worker(getSpace(), txn, i, i + 1, NUM_ENTRIES, - this); - workers[i].start(); - } - - // wait until the job has done - try { - for (int i = 0; i < NUM_WORKERS; i++) { - workers[i].join(); - } - } catch (InterruptedException e) {} - - // stop illegal access thread and check the result - peeker.exit(); - - try { - peeker.join(); - } catch (InterruptedException e) {} - - if (peeker.getReadCount() > 0) { - throw new TestException( - "Some entries are visible from the outside of" - + " transaction."); - } - - // commit the transaction - commitTransaction(txn); - - // check if workers pass all entries - for (int i = 0; i < NUM_WORKERS; i++) { - if (workers[i].getWriteCount() != NUM_ENTRIES) { - throw new TestException( - "Some entries are missing."); - } - } - - if (peekerFailed) { - throw new TestException( - "Peeker failed with exception" + peekerFailMsg); - } - - if (workerFailed) { - throw new TestException( - "Worker failed with exception" + workerFailMsg); - } - - // check entries - checkEntries(); - } - - private void writeEntries(Transaction txn) throws TestException { - SimpleEntry entry = new SimpleEntry(); - entry.string = "foo"; - entry.stage = new Integer(0); // initial stage is "0" - - for (int i = 0; i < NUM_ENTRIES; i++) { - entry.id = new Integer(i); - - // write an entry under the transaction - writeEntry(txn, entry); - pass("[writeEntries]: wrote an initial entry (" + entry + ")"); - } - } - - private void checkEntries() throws TestException { - SimpleEntry entry = new SimpleEntry(); - SimpleEntry template = new SimpleEntry(); - template.string = "foo"; - template.stage = new Integer(NUM_WORKERS); - - for (int i = 0; i < NUM_ENTRIES; i++) { - template.id = new Integer(i); - - try { - entry = (SimpleEntry) getSpace().takeIfExists(template, null, - JavaSpace.NO_WAIT); - } catch (Exception e) { - fail("[checkEntries]: Exception thrown while try to read an" - + " entry", e); - } - - if (entry == null) { - fail("Entry [" + template + "] is missing."); - } - } - } - - - class Peeker extends Thread { - private final JavaSpace space; - private final TransactionTestBase parent; - private int readCount = 0; - private volatile boolean shouldStop = false; - - public Peeker(JavaSpace space, TransactionTestBase parent) { - this.space = space; - this.parent = parent; - } - - public void run() { - SimpleEntry entry = null; - SimpleEntry template = new SimpleEntry(); - template.string = "foo"; - - try { - while (!shouldStop) { - try { - entry = (SimpleEntry) space.readIfExists(template, null, - JavaSpace.NO_WAIT); - } catch (Exception e) { - parent.fail("[Peeker]: Exception thrown while trying to" - + " read an entry", e); - } - - if (entry != null) { - synchronized (this){ - readCount++; - } - } - - try { - sleep(400); - } catch (InterruptedException ie) { - break; - } - } - } catch (Exception e) { - e.printStackTrace(); - peekerFailed = true; - peekerFailMsg = e.toString(); - } - } - - /** - * Stops the thread. - */ - public void exit() { - shouldStop = true; - } - - public int getReadCount() { - synchronized (this){ - parent.pass("[Peeker]: read " + readCount + " entries"); - return readCount; - } - } - } - - - class Worker extends Thread { - private final JavaSpace space; - private final Transaction txn; - private final int fromStage, toStage; - private volatile int writeCount = 0; - private final int expectedEntries; - private final TransactionTestBase parent; - private final int MAX_WAIT = 80000; // 8sec. - private final int WAIT_UNIT = 400; // 400 msec. - private volatile boolean completed = false; - private final Object lock = new Object(); - - public Worker(JavaSpace space, Transaction txn, int fromStage, - int toStage, int expectedEntries, TransactionTestBase parent) { - this.space = space; - this.txn = txn; - this.fromStage = fromStage; - this.toStage = toStage; - this.expectedEntries = expectedEntries; - this.parent = parent; - } - - public void run() { - SimpleEntry entry = null; - SimpleEntry template = new SimpleEntry(); - template.string = "foo"; - template.stage = new Integer(fromStage); - - try { - parent.pass("[Worker" + fromStage + "]: start running"); - int waitCount = 0; - - while (true) { - try { - entry = (SimpleEntry) space.takeIfExists(template, txn, - JavaSpace.NO_WAIT); - - /* - * entry = (SimpleEntry) space.readIfExists(template, - * txn, JavaSpace.NO_WAIT); - */ - } catch (Exception e) { - parent.fail("[Worker]: Exception thrown while reading" - + " an entry", e); - } - - if (entry == null) { - try { - sleep(WAIT_UNIT); - } catch (InterruptedException ie) {} - - if (++waitCount > (MAX_WAIT / WAIT_UNIT)) { - synchronized(lock) { - completed = true; - lock.notifyAll(); - } - break; // timeout - } - } else { - - // got an entry - parent.pass("[Worker" + fromStage + "]: got an entry (" - + entry + ")"); - - // clear wait count - waitCount = 0; - - // change ID - entry.stage = new Integer(toStage); - - // write back to the space - try { - space.write(entry, txn, Lease.ANY); - } catch (Exception e) { - parent.fail("[Worker]: Exception thrown while" - + " writing an entry", e); - } - parent.pass("[Worker" + fromStage + "]: wrote an entry" - + " (" + entry + ")"); - synchronized(lock) { - if (++writeCount >= expectedEntries) { - completed = true; - lock.notifyAll(); - break; // ends thread - } - } - } - } - } catch (Exception e) { - e.printStackTrace(); - workerFailed = true; - workerFailMsg = e.toString(); - } - } - - public int getWriteCount() { - int retry = 0; - while ((completed!=true)&&(retry<3)) { - synchronized(lock) { - try { - lock.wait(MAX_WAIT); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - return writeCount; - } - } - - /** - * Return a String which describes this test - */ - public String getDescription() { - return "Test Name = MultipleAccessTest : \n" + "tests multiple access."; - } -} +/* + * 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 com.sun.jini.test.impl.outrigger.transaction; + +import java.util.logging.Level; + +// Test harness specific classes +import com.sun.jini.qa.harness.TestException; + +// All other imports +import net.jini.core.lease.Lease; +import net.jini.space.JavaSpace; +import net.jini.core.transaction.Transaction; +import java.rmi.UnmarshalException; +import java.rmi.RemoteException; + + +/** + * Three (or more) threads pass entries each other under a transaction. + * These thread work independently, and accesses to the space will be done + * simultaneously. While these accesses, another illegal client try to read + * entries. + * <ul> + * <li> Make sure that the illegal client can't read any entries. + * <li> Make sure all entries are passed correctly. + * </ul> + * + * @author H.Fukuda + */ +public class MultipleAccessTest extends TransactionTestBase { + private final static int NUM_WORKERS = 4; + private final static int NUM_ENTRIES = 20; + + /* + * Vars to detect that Peeker/Worker failed. + * Has been added during porting. + */ + private volatile boolean peekerFailed = false; + private volatile boolean workerFailed = false; + private volatile String peekerFailMsg = ""; + private volatile String workerFailMsg = ""; + + public void run() throws Exception { + simpleSetup(); + + // create a transaction + Transaction txn = createTransaction(); + + // generate & write entries + writeEntries(txn); + + // start illegal access thread + Peeker peeker = new Peeker(getSpace(), this); + peeker.start(); + + // create & start worker threads + Worker[] workers = new Worker[NUM_WORKERS]; + + for (int i = 0; i < NUM_WORKERS; i++) { + workers[i] = new Worker(getSpace(), txn, i, i + 1, NUM_ENTRIES, + this); + workers[i].start(); + } + + // wait until the job has done + try { + for (int i = 0; i < NUM_WORKERS; i++) { + workers[i].join(); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + + // stop illegal access thread and check the result + peeker.exit(); + + try { + peeker.join(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + + if (peeker.getReadCount() > 0) { + throw new TestException( + "Some entries are visible from the outside of" + + " transaction."); + } + + // commit the transaction + commitTransaction(txn); + + // check if workers pass all entries + for (int i = 0; i < NUM_WORKERS; i++) { + if (workers[i].getWriteCount() != NUM_ENTRIES) { + throw new TestException( + "Some entries are missing."); + } + } + + if (peekerFailed) { + throw new TestException( + "Peeker failed with exception" + peekerFailMsg); + } + + if (workerFailed) { + throw new TestException( + "Worker failed with exception" + workerFailMsg); + } + + // check entries + checkEntries(); + } + + private void writeEntries(Transaction txn) throws TestException { + SimpleEntry entry = new SimpleEntry(); + entry.string = "foo"; + entry.stage = new Integer(0); // initial stage is "0" + + for (int i = 0; i < NUM_ENTRIES; i++) { + entry.id = new Integer(i); + + // write an entry under the transaction + writeEntry(txn, entry); + pass("[writeEntries]: wrote an initial entry (" + entry + ")"); + } + } + + private void checkEntries() throws TestException { + SimpleEntry entry = new SimpleEntry(); + SimpleEntry template = new SimpleEntry(); + template.string = "foo"; + template.stage = new Integer(NUM_WORKERS); + + for (int i = 0; i < NUM_ENTRIES; i++) { + template.id = new Integer(i); + + try { + entry = (SimpleEntry) getSpace().takeIfExists(template, null, + JavaSpace.NO_WAIT); + } catch (Exception e) { + fail("[checkEntries]: Exception thrown while try to read an" + + " entry", e); + } + + if (entry == null) { + fail("Entry [" + template + "] is missing."); + } + } + } + + + class Peeker extends Thread { + private final JavaSpace space; + private final TransactionTestBase parent; + private int readCount = 0; + private volatile boolean shouldStop = false; + + public Peeker(JavaSpace space, TransactionTestBase parent) { + this.space = space; + this.parent = parent; + } + + public void run() { + SimpleEntry entry = null; + SimpleEntry template = new SimpleEntry(); + template.string = "foo"; + + try { + while (!shouldStop) { + try { + entry = (SimpleEntry) space.readIfExists(template, null, + JavaSpace.NO_WAIT); + } catch (Exception e) { + parent.fail("[Peeker]: Exception thrown while trying to" + + " read an entry", e); + } + + if (entry != null) { + synchronized (this){ + readCount++; + } + } + + try { + sleep(400); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + break; + } + } + } catch (Exception e) { + e.printStackTrace(); + peekerFailed = true; + peekerFailMsg = e.toString(); + } + } + + /** + * Stops the thread. + */ + public void exit() { + shouldStop = true; + } + + public int getReadCount() { + synchronized (this){ + parent.pass("[Peeker]: read " + readCount + " entries"); + return readCount; + } + } + } + + + class Worker extends Thread { + private final JavaSpace space; + private final Transaction txn; + private final int fromStage, toStage; + private volatile int writeCount = 0; + private final int expectedEntries; + private final TransactionTestBase parent; + private final int MAX_WAIT = 80000; // 8sec. + private final int WAIT_UNIT = 400; // 400 msec. + private volatile boolean completed = false; + private final Object lock = new Object(); + + public Worker(JavaSpace space, Transaction txn, int fromStage, + int toStage, int expectedEntries, TransactionTestBase parent) { + this.space = space; + this.txn = txn; + this.fromStage = fromStage; + this.toStage = toStage; + this.expectedEntries = expectedEntries; + this.parent = parent; + } + + public void run() { + SimpleEntry entry = null; + SimpleEntry template = new SimpleEntry(); + template.string = "foo"; + template.stage = new Integer(fromStage); + + try { + parent.pass("[Worker" + fromStage + "]: start running"); + int waitCount = 0; + + while (true) { + try { + entry = (SimpleEntry) space.takeIfExists(template, txn, + JavaSpace.NO_WAIT); + + /* + * entry = (SimpleEntry) space.readIfExists(template, + * txn, JavaSpace.NO_WAIT); + */ + } catch (Exception e) { + parent.fail("[Worker]: Exception thrown while reading" + + " an entry", e); + } + + if (entry == null) { + try { + sleep(WAIT_UNIT); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + + if (++waitCount > (MAX_WAIT / WAIT_UNIT)) { + synchronized(lock) { + completed = true; + lock.notifyAll(); + } + break; // timeout + } + } else { + + // got an entry + parent.pass("[Worker" + fromStage + "]: got an entry (" + + entry + ")"); + + // clear wait count + waitCount = 0; + + // change ID + entry.stage = new Integer(toStage); + + // write back to the space + try { + space.write(entry, txn, Lease.ANY); + } catch (Exception e) { + parent.fail("[Worker]: Exception thrown while" + + " writing an entry", e); + } + parent.pass("[Worker" + fromStage + "]: wrote an entry" + + " (" + entry + ")"); + synchronized(lock) { + if (++writeCount >= expectedEntries) { + completed = true; + lock.notifyAll(); + break; // ends thread + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + workerFailed = true; + workerFailMsg = e.toString(); + } + } + + public int getWriteCount() { + int retry = 0; + while ((completed!=true)&&(retry<3)) { + synchronized(lock) { + try { + lock.wait(MAX_WAIT); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + } + } + return writeCount; + } + } + + /** + * Return a String which describes this test + */ + public String getDescription() { + return "Test Name = MultipleAccessTest : \n" + "tests multiple access."; + } +}
