From 470900c8bea7b3f9d13624433aa9da0f906c23a3 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Fri, 18 Jul 2008 01:56:53 +0000 Subject: Moved transaction support classes to txn package. --- .../com/amazon/carbonado/txn/TestCursorList.java | 224 +++++++++++++++++++++ .../carbonado/txn/TestTransactionManager.java | 160 +++++++++++++++ 2 files changed, 384 insertions(+) create mode 100644 src/test/java/com/amazon/carbonado/txn/TestCursorList.java create mode 100644 src/test/java/com/amazon/carbonado/txn/TestTransactionManager.java (limited to 'src/test/java/com/amazon/carbonado/txn') diff --git a/src/test/java/com/amazon/carbonado/txn/TestCursorList.java b/src/test/java/com/amazon/carbonado/txn/TestCursorList.java new file mode 100644 index 0000000..ed2f04f --- /dev/null +++ b/src/test/java/com/amazon/carbonado/txn/TestCursorList.java @@ -0,0 +1,224 @@ +/* + * Copyright 2006 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. All rights reserved. + * + * Licensed 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.amazon.carbonado.txn; + +import com.amazon.carbonado.Cursor; + +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import com.amazon.carbonado.cursor.EmptyCursorFactory; + +/** + * Test case for TransactionManager.CursorList. + * + * @author Brian S O'Neill + */ +public class TestCursorList extends TestCase { + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static TestSuite suite() { + return new TestSuite(TestCursorList.class); + } + + TransactionScope.CursorList mList; + + public TestCursorList(String name) { + super(name); + } + + protected void setUp() { + mList = new TransactionScope.CursorList(); + } + + public void testRegisterFew() { + assertEquals(0, mList.size()); + + { + Cursor cursor = EmptyCursorFactory.newEmptyCursor(); + mList.register(cursor, null); + assertEquals(1, mList.size()); + assertEquals(cursor, mList.getCursor(0)); + assertEquals(null, mList.getValue(0)); + Object value = mList.unregister(cursor); + assertEquals(0, mList.size()); + assertEquals(null, value); + } + + { + Cursor cursor_1 = EmptyCursorFactory.newEmptyCursor(); + Cursor cursor_2 = EmptyCursorFactory.newEmptyCursor(); + mList.register(cursor_1, null); + assertEquals(1, mList.size()); + mList.register(cursor_2, null); + assertEquals(2, mList.size()); + assertEquals(cursor_1, mList.getCursor(0)); + assertEquals(cursor_2, mList.getCursor(1)); + assertEquals(null, mList.getValue(0)); + assertEquals(null, mList.getValue(1)); + + Object value = mList.unregister(cursor_2); + assertEquals(1, mList.size()); + assertEquals(cursor_1, mList.getCursor(0)); + assertEquals(null, value); + mList.unregister(cursor_2); + assertEquals(1, mList.size()); + mList.unregister(cursor_1); + assertEquals(0, mList.size()); + } + + // unregister in reverse + { + Cursor cursor_1 = EmptyCursorFactory.newEmptyCursor(); + Cursor cursor_2 = EmptyCursorFactory.newEmptyCursor(); + mList.register(cursor_1, null); + mList.register(cursor_2, null); + + mList.unregister(cursor_1); + assertEquals(1, mList.size()); + assertEquals(cursor_2, mList.getCursor(0)); + + mList.unregister(cursor_1); + assertEquals(1, mList.size()); + mList.unregister(cursor_2); + assertEquals(0, mList.size()); + } + } + + public void testRegisterFewValue() { + Cursor cursor_1 = EmptyCursorFactory.newEmptyCursor(); + Cursor cursor_2 = EmptyCursorFactory.newEmptyCursor(); + String value_1 = "1"; + String value_2 = "2"; + + mList.register(cursor_1, value_1); + assertEquals(1, mList.size()); + assertEquals(cursor_1, mList.getCursor(0)); + assertEquals(value_1, mList.getValue(0)); + + mList.register(cursor_2, value_2); + assertEquals(2, mList.size()); + assertEquals(cursor_1, mList.getCursor(0)); + assertEquals(value_1, mList.getValue(0)); + assertEquals(cursor_2, mList.getCursor(1)); + assertEquals(value_2, mList.getValue(1)); + + Object value = mList.unregister(cursor_2); + assertEquals(1, mList.size()); + assertEquals(cursor_1, mList.getCursor(0)); + assertEquals(value_1, mList.getValue(0)); + assertEquals(value_2, value); + + value = mList.unregister(cursor_2); + assertEquals(1, mList.size()); + assertEquals(null, value); + value = mList.unregister(cursor_1); + assertEquals(0, mList.size()); + assertEquals(value_1, value); + } + + // Tests that the array expands properly. + public void testRegisterMany() { + final int count = 50; + Cursor[] cursors = new Cursor[count]; + for (int i=0; i() { + public Transaction call() { + return mTxnMgr.localScope().enter(null); + } + }).get(); + + try { + txn2.attach(); + fail(); + } catch (IllegalStateException e) { + } + + try { + txn2.detach(); + fail(); + } catch (IllegalStateException e) { + } + + txn.detach(); + + try { + txn2.attach(); + fail(); + } catch (IllegalStateException e) { + } + + es.submit(new Runnable() { + public void run() { + txn2.detach(); + } + }).get(); + + txn2.attach(); + txn2.detach(); + txn2.attach(); + + try { + txn.attach(); + fail(); + } catch (IllegalStateException e) { + } + + txn.detach(); + + txn2.exit(); + + try { + txn.attach(); + fail(); + } catch (IllegalStateException e) { + } + + txn.detach(); + + txn2.detach(); + + txn.detach(); + txn.attach(); + txn.detach(); + + es.shutdown(); + } + + private static class Txn { + } + + private static class TM extends TransactionManager { + protected IsolationLevel selectIsolationLevel(Transaction parent, IsolationLevel level) { + return IsolationLevel.READ_UNCOMMITTED; + } + + protected boolean supportsForUpdate() { + return true; + } + + protected Txn createTxn(Txn parent, IsolationLevel level) { + return new Txn(); + } + + protected boolean commitTxn(Txn txn) { + return false; + } + + protected void abortTxn(Txn txn) { + } + } +} -- cgit v1.2.3