[jboss-svn-commits] JBL Code SVN: r37227 - in labs/jbosstm/workspace/mlittle/STM-Arjuna/src: test/java/org/jboss/stm and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Jul 4 14:51:42 EDT 2011


Author: mark.little at jboss.com
Date: 2011-07-04 14:51:42 -0400 (Mon, 04 Jul 2011)
New Revision: 37227

Added:
   labs/jbosstm/workspace/mlittle/STM-Arjuna/src/main/java/org/jboss/stm/types/AtomicLinkedList.java
   labs/jbosstm/workspace/mlittle/STM-Arjuna/src/test/java/org/jboss/stm/TimeoutRetryUnitTest.java
   labs/jbosstm/workspace/mlittle/STM-Arjuna/src/test/java/org/jboss/stm/types/AtomicLinkedListUnitTest.java
Log:
Added Timeout and Retry as well as LinkedList support.

Added: labs/jbosstm/workspace/mlittle/STM-Arjuna/src/main/java/org/jboss/stm/types/AtomicLinkedList.java
===================================================================
--- labs/jbosstm/workspace/mlittle/STM-Arjuna/src/main/java/org/jboss/stm/types/AtomicLinkedList.java	                        (rev 0)
+++ labs/jbosstm/workspace/mlittle/STM-Arjuna/src/main/java/org/jboss/stm/types/AtomicLinkedList.java	2011-07-04 18:51:42 UTC (rev 37227)
@@ -0,0 +1,42 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss Inc., and others contributors as indicated 
+ * by the @authors tag. All rights reserved. 
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors. 
+ * 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, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A 
+ * 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,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+ * MA  02110-1301, USA.
+ * 
+ * (C) 2009,
+ * @author mark.little at jboss.com
+ */
+
+package org.jboss.stm.types;
+
+import org.jboss.stm.annotations.ReadLock;
+import org.jboss.stm.annotations.Transactional;
+import org.jboss.stm.annotations.WriteLock;
+
+ at Transactional
+public interface AtomicLinkedList
+{
+    @WriteLock
+    public void setPrev (AtomicLinkedList n);
+    
+    @ReadLock
+    public AtomicLinkedList getPrev ();
+    
+    @WriteLock
+    public void setNext (AtomicLinkedList n);
+    
+    @ReadLock
+    public AtomicLinkedList getNext ();
+}
\ No newline at end of file

Added: labs/jbosstm/workspace/mlittle/STM-Arjuna/src/test/java/org/jboss/stm/TimeoutRetryUnitTest.java
===================================================================
--- labs/jbosstm/workspace/mlittle/STM-Arjuna/src/test/java/org/jboss/stm/TimeoutRetryUnitTest.java	                        (rev 0)
+++ labs/jbosstm/workspace/mlittle/STM-Arjuna/src/test/java/org/jboss/stm/TimeoutRetryUnitTest.java	2011-07-04 18:51:42 UTC (rev 37227)
@@ -0,0 +1,183 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.stm;
+
+import java.util.Random;
+
+import org.jboss.stm.annotations.Retry;
+import org.jboss.stm.annotations.Timeout;
+import org.jboss.stm.annotations.Transactional;
+import org.jboss.stm.annotations.ReadLock;
+import org.jboss.stm.annotations.State;
+import org.jboss.stm.annotations.WriteLock;
+
+import com.arjuna.ats.arjuna.AtomicAction;
+
+import junit.framework.TestCase;
+
+/**
+ * Hammer equivalent test.
+ * 
+ * @author Mark Little
+ */
+
+public class TimeoutRetryUnitTest extends TestCase
+{   
+    @Transactional
+    public interface Sample
+    {
+       public void increment ();
+       public void decrement ();
+       
+       public int value ();
+    }
+    
+    @Transactional
+    public class SampleLockable implements Sample
+    {
+        public SampleLockable (int init)
+        {
+            _isState = init;
+        }
+        
+        @ReadLock
+        @Timeout(period=0)
+        @Retry(count=0)
+        public int value ()
+        {
+            return _isState;
+        }
+
+        @WriteLock
+        @Timeout(period=0)
+        @Retry(count=0)
+        public void increment ()
+        {
+            _isState++;
+        }
+        
+        @WriteLock
+        @Timeout(period=0)
+        @Retry(count=0)
+        public void decrement ()
+        {
+            _isState--;
+        }
+
+        @State
+        private int _isState;
+    }
+    
+    public class Worker extends Thread
+    {
+        public Worker (Sample obj1, Sample obj2)
+        {
+            _obj1 = obj1;
+            _obj2 = obj2;
+        }
+        
+        public void run ()
+        {
+            Random rand = new Random();
+
+            for (int i = 0; i < 5; i++)
+            {
+                AtomicAction A = new AtomicAction();
+                boolean doCommit = true;
+                
+                A.begin();
+                
+                try
+                {
+                    // always keep the two objects in sync.
+                    
+                    _obj1.increment();
+                    _obj2.decrement();
+                    
+                    Thread.sleep(1000);
+                }
+                catch (final Throwable ex)
+                {
+                    doCommit = false;
+                }
+                
+                if (rand.nextInt() % 2 == 0)
+                    doCommit = false;
+                
+                if (doCommit)
+                    A.commit();
+                else
+                    A.abort();
+            }
+        }
+        
+        private Sample _obj1;
+        private Sample _obj2;
+    }
+
+    public void testRecoverableHammer ()
+    {
+        RecoverableContainer<Sample> theContainer = new RecoverableContainer<Sample>();
+        Sample obj1 = theContainer.enlist(new SampleLockable(10));
+        Sample obj2 = theContainer.enlist(new SampleLockable(10));
+        Worker worker1 = new Worker(obj1, obj2);
+        Worker worker2 = new Worker(obj1, obj2);
+        
+        worker1.start();
+        worker2.start();
+        
+        try
+        {
+            worker1.join();
+            worker2.join();
+        }
+        catch (final Throwable ex)
+        {
+        }
+        
+        assertEquals(obj1.value()+obj2.value(), 20);
+    }
+    
+    public void testPersistentHammer ()
+    {
+        PersistentContainer<Sample> theContainer = new PersistentContainer<Sample>();
+        Sample obj1 = theContainer.enlist(new SampleLockable(10));
+        Sample obj2 = theContainer.enlist(new SampleLockable(10));
+        Worker worker1 = new Worker(obj1, obj2);
+        Worker worker2 = new Worker(obj1, obj2);
+        
+        worker1.start();
+        worker2.start();
+        
+        try
+        {
+            worker1.join();
+            worker2.join();
+        }
+        catch (final Throwable ex)
+        {
+        }
+        
+        assertEquals(obj1.value()+obj2.value(), 20);
+    }
+}

Added: labs/jbosstm/workspace/mlittle/STM-Arjuna/src/test/java/org/jboss/stm/types/AtomicLinkedListUnitTest.java
===================================================================
--- labs/jbosstm/workspace/mlittle/STM-Arjuna/src/test/java/org/jboss/stm/types/AtomicLinkedListUnitTest.java	                        (rev 0)
+++ labs/jbosstm/workspace/mlittle/STM-Arjuna/src/test/java/org/jboss/stm/types/AtomicLinkedListUnitTest.java	2011-07-04 18:51:42 UTC (rev 37227)
@@ -0,0 +1,143 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.stm.types;
+
+import java.io.IOException;
+import java.util.Hashtable;
+
+import org.jboss.stm.RecoverableContainer;
+import org.jboss.stm.annotations.RestoreState;
+import org.jboss.stm.annotations.SaveState;
+import org.jboss.stm.annotations.State;
+import org.jboss.stm.annotations.Transactional;
+import org.jboss.stm.annotations.ReadLock;
+import org.jboss.stm.annotations.WriteLock;
+
+import com.arjuna.ats.arjuna.AtomicAction;
+import com.arjuna.ats.arjuna.ObjectType;
+import com.arjuna.ats.arjuna.common.Uid;
+import com.arjuna.ats.arjuna.coordinator.ActionStatus;
+import com.arjuna.ats.arjuna.coordinator.BasicAction;
+import com.arjuna.ats.arjuna.state.InputObjectState;
+import com.arjuna.ats.arjuna.state.OutputObjectState;
+import com.arjuna.ats.txoj.Lock;
+import com.arjuna.ats.txoj.LockManager;
+import com.arjuna.ats.txoj.LockMode;
+import com.arjuna.ats.txoj.LockResult;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for the Class class.
+ * 
+ * @author Mark Little
+ */
+
+public class AtomicLinkedListUnitTest extends TestCase
+{
+    @Transactional
+    public interface ExtendedAtomicLinkedList extends AtomicLinkedList
+    {
+        @ReadLock
+        public String nodeName ();
+    }
+    
+    @Transactional
+    public class LinkedListEntry implements ExtendedAtomicLinkedList
+    {
+        public LinkedListEntry (final String name)
+        {
+            _nodeName = name;
+        }
+        
+        public final String nodeName ()
+        {
+            return _nodeName;
+        }
+        
+        @Override
+        public AtomicLinkedList getNext ()
+        {
+            return _next;
+        }
+
+        @Override
+        public AtomicLinkedList getPrev ()
+        {
+            return _prev;
+        }
+
+        @Override
+        public void setNext (AtomicLinkedList n)
+        {
+            _next = n;
+        }
+
+        @Override
+        public void setPrev (AtomicLinkedList n)
+        {
+            _prev = n;
+        }
+        
+        @State
+        private AtomicLinkedList _prev;
+        
+        @State
+        private AtomicLinkedList _next;
+        
+        @State
+        private String _nodeName = "";
+    }
+    
+    public void testLinkedList () throws Exception
+    {
+        ExtendedAtomicLinkedList ni1 = new LinkedListEntry("one");
+        ExtendedAtomicLinkedList ni2 = new LinkedListEntry("two");
+        ExtendedAtomicLinkedList ni3 = new LinkedListEntry("three");
+        AtomicAction A = new AtomicAction();
+        
+        ExtendedAtomicLinkedList h1 = theContainer.enlist(ni1);
+        ExtendedAtomicLinkedList h2 = theContainer.enlist(ni2);
+        ExtendedAtomicLinkedList h3 = theContainer.enlist(ni3);
+        
+        h1.setNext(h2);
+        h2.setPrev(h1);
+        
+        assertEquals(h1.getPrev(), null);
+        assertEquals(((ExtendedAtomicLinkedList)h2.getPrev()).nodeName(), h1.nodeName());
+        
+        A.begin();
+        
+        h1.setNext(h3);
+        h2.setPrev(null);
+        h3.setPrev(h1);
+        
+        A.abort();
+        
+        assertEquals(((ExtendedAtomicLinkedList)h1.getNext()).nodeName(), h2.nodeName());
+        assertEquals(h1.getPrev(), null);
+        assertEquals(((ExtendedAtomicLinkedList)h2.getPrev()).nodeName(), h1.nodeName());
+    }
+    
+    public RecoverableContainer<ExtendedAtomicLinkedList> theContainer = new RecoverableContainer<ExtendedAtomicLinkedList>();
+}



More information about the jboss-svn-commits mailing list