You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2007/03/15 02:01:18 UTC

svn commit: r518406 - in /db/ojb/trunk/src/test/org/apache/ojb/broker: AllTests.java locking/LockManagerTest.java

Author: arminw
Date: Wed Mar 14 18:01:17 2007
New Revision: 518406

URL: http://svn.apache.org/viewvc?view=rev&rev=518406
Log:
add new test class (locking tests)

Added:
    db/ojb/trunk/src/test/org/apache/ojb/broker/locking/LockManagerTest.java
Modified:
    db/ojb/trunk/src/test/org/apache/ojb/broker/AllTests.java

Modified: db/ojb/trunk/src/test/org/apache/ojb/broker/AllTests.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/src/test/org/apache/ojb/broker/AllTests.java?view=diff&rev=518406&r1=518405&r2=518406
==============================================================================
--- db/ojb/trunk/src/test/org/apache/ojb/broker/AllTests.java (original)
+++ db/ojb/trunk/src/test/org/apache/ojb/broker/AllTests.java Wed Mar 14 18:01:17 2007
@@ -31,6 +31,7 @@
 import org.apache.ojb.broker.locking.LockTestRepeatableReads;
 import org.apache.ojb.broker.locking.LockTestSerializable;
 import org.apache.ojb.broker.locking.LockTestUncommitedReads;
+import org.apache.ojb.broker.locking.LockManagerTest;
 import org.apache.ojb.broker.metadata.CustomAttributesTest;
 import org.apache.ojb.broker.metadata.MetadataMultithreadedTest;
 import org.apache.ojb.broker.metadata.MetadataTest;
@@ -143,6 +144,7 @@
         suite.addTestSuite(CommonsLockTestCommittedReads.class);
         suite.addTestSuite(CommonsLockTestUncommittedReads.class);
         suite.addTestSuite(OptimisticLockingMultithreadedTest.class);
+        suite.addTestSuite(LockManagerTest.class);
         suite.addTestSuite(NoPkReferenceTest.class);
         suite.addTestSuite(UnwrapHelperTest.class);
         suite.addTestSuite(PaginationTest.class);

Added: db/ojb/trunk/src/test/org/apache/ojb/broker/locking/LockManagerTest.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/src/test/org/apache/ojb/broker/locking/LockManagerTest.java?view=auto&rev=518406
==============================================================================
--- db/ojb/trunk/src/test/org/apache/ojb/broker/locking/LockManagerTest.java (added)
+++ db/ojb/trunk/src/test/org/apache/ojb/broker/locking/LockManagerTest.java Wed Mar 14 18:01:17 2007
@@ -0,0 +1,184 @@
+package org.apache.ojb.broker.locking;
+
+/* Copyright 2002-2007 The Apache Software Foundation
+ *
+ * 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.
+ */
+
+import org.apache.ojb.junit.OJBTestCase;
+
+/**
+ * This class tests {@link LockManager} implementations
+ * and can be used to detect memory leaks.
+ *
+ * @version $Id$
+ */
+public class LockManagerTest extends OJBTestCase
+{
+    public static void main(String[] args)
+    {
+        String[] arr = {LockManagerTest.class.getName()};
+        junit.textui.TestRunner.main(arr);
+    }
+
+    public LockManagerTest()
+    {
+    }
+
+    public LockManagerTest(String name)
+    {
+        super(name);
+    }
+
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+    }
+
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+    }
+
+    public void testLockTimeout() throws Exception
+    {
+        LockManager lm = LockManagerHelper.getLockManagerSpecifiedByConfiguration(this.ojb);
+        performLockTimeout(lm);
+    }
+
+    void performLockTimeout(LockManager lm) throws Exception
+    {
+        long timestamp = System.currentTimeMillis();
+        lm.setBlockTimeout(100);
+        String owner_1 = "owner_1_" + timestamp;
+        String owner_2 = "owner_2_" + timestamp;
+        String resource_1 = "resource_1_" + timestamp;
+        String resource_2 = "resource_2_" + timestamp;
+        long old_cleanup = 0;
+
+        try
+        {
+            if(lm instanceof LockManagerInMemoryImpl)
+            {
+                ((LockManagerInMemoryImpl) lm).setCleanupFrequency(50);
+            }
+            lm.setLockTimeout(1000);
+            assertTrue(lm.writeLock(owner_1, resource_1, IsolationLevels.READ_COMMITTED));
+            lm.setLockTimeout(2000);
+            assertTrue(lm.writeLock(owner_2, resource_2, IsolationLevels.READ_COMMITTED));
+            // not allowed to lock same 'resource' by other 'key'
+            assertFalse(lm.writeLock(owner_2, resource_1, IsolationLevels.READ_COMMITTED));
+
+            Thread.sleep(1600);
+            // expect that the lock on resource_1 by owner_1 was timed out
+            assertTrue(lm.writeLock(owner_2, resource_1, IsolationLevels.READ_COMMITTED));
+            // release locks of owner_1, otherwise the reuse of owner_1 is not guaranteed
+            lm.releaseLocks(owner_1);
+            Thread.sleep(200);
+            // resource_2 is still locked by owner_2
+            assertFalse(lm.writeLock(owner_1, resource_2, IsolationLevels.READ_COMMITTED));
+
+            Thread.sleep(1700);
+            // now resource_2 locked by owner_2 was timed out, thus owner_1 should able to lock it
+            assertTrue(lm.writeLock(owner_1, resource_2, IsolationLevels.READ_COMMITTED));
+        }
+        finally
+        {
+            lm.releaseLocks(owner_1);
+            lm.releaseLocks(owner_2);
+            if(lm instanceof LockManagerInMemoryImpl)
+            {
+                if(old_cleanup > 0) ((LockManagerInMemoryImpl) lm).setCleanupFrequency(old_cleanup);
+            }
+        }
+
+        System.out.println("LM-info: " + lm.getLockInfo());
+    }
+
+    /**
+     * Test to reproduce memory leak in LockManager implementation
+     */
+    public void testLockTimeoutLostOwner() throws Exception
+    {
+        LockManager lm = LockManagerHelper.getLockManagerSpecifiedByConfiguration(this.ojb);
+        performLockTimeoutLostOwner(lm);
+    }
+
+    public void performLockTimeoutLostOwner(LockManager lm) throws Exception
+    {
+        long timestamp = System.currentTimeMillis();
+        lm.setBlockTimeout(100);
+        long old_cleanup = 0;
+
+        StringBuffer tmp = new StringBuffer();
+        for(int i=0; i<10; i++)
+        {
+            tmp.append("_big_test_string_");
+        }
+        String appendix = tmp.toString();
+
+        try
+        {
+            if(lm instanceof LockManagerInMemoryImpl)
+            {
+                ((LockManagerInMemoryImpl) lm).setCleanupFrequency(10);
+            }
+            lm.setLockTimeout(50);
+
+            for(int i=0; i < 10000; i++)
+            {
+                String owner_1 = "owner_a_" + i + "_" + timestamp + appendix;
+                String owner_2 = "owner_b_" + i + "_" + timestamp + appendix;
+                String resource_1 = "resource_a_" + i + "_" + timestamp + appendix;
+                String resource_2 = "resource_b_" + i + "_" + timestamp + appendix;
+
+                lm.readLock(owner_1, resource_1, IsolationLevels.READ_COMMITTED);
+                if(i%10 != 9) lm.writeLock(owner_2, resource_2, IsolationLevels.READ_COMMITTED);
+            }
+            for(int i=0; i<20; i++)
+            {
+                lm.hasRead("test", "test");
+                Thread.sleep(100);
+                System.gc();
+            }
+
+            System.out.println("LM-info: " + lm.getLockInfo());
+
+            timestamp = System.currentTimeMillis();
+            for(int i=0; i < 10000; i++)
+            {
+                String owner_1 = "owner_a_" + i + "_" + timestamp + appendix;
+                String owner_2 = "owner_b_" + i + "_" + timestamp + appendix;
+                String resource_1 = "resource_a_" + i + "_" + timestamp + appendix;
+                String resource_2 = "resource_b_" + i + "_" + timestamp + appendix;
+
+                lm.readLock(owner_1, resource_1, IsolationLevels.READ_COMMITTED);
+                if(i%10 != 9) lm.writeLock(owner_2, resource_2, IsolationLevels.READ_COMMITTED);
+            }
+            for(int i=0; i<20; i++)
+            {
+                lm.hasRead("test", "test");
+                Thread.sleep(100);
+                System.gc();
+            }
+        }
+        finally
+        {
+            if(lm instanceof LockManagerInMemoryImpl)
+            {
+                if(old_cleanup > 0) ((LockManagerInMemoryImpl) lm).setCleanupFrequency(old_cleanup);
+            }
+        }
+        System.out.println("LM-info: " + lm.getLockInfo());
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org