Return-Path: Delivered-To: apmail-db-ojb-dev-archive@www.apache.org Received: (qmail 75129 invoked from network); 15 Mar 2007 01:01:41 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 15 Mar 2007 01:01:41 -0000 Received: (qmail 36436 invoked by uid 500); 15 Mar 2007 01:01:49 -0000 Delivered-To: apmail-db-ojb-dev-archive@db.apache.org Received: (qmail 36409 invoked by uid 500); 15 Mar 2007 01:01:49 -0000 Mailing-List: contact ojb-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "OJB Developers List" Reply-To: "OJB Developers List" Delivered-To: mailing list ojb-dev@db.apache.org Received: (qmail 36398 invoked by uid 500); 15 Mar 2007 01:01:49 -0000 Received: (qmail 36395 invoked by uid 99); 15 Mar 2007 01:01:49 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 14 Mar 2007 18:01:49 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 14 Mar 2007 18:01:40 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id ECFD01A9842; Wed, 14 Mar 2007 18:01:18 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r518406 - in /db/ojb/trunk/src/test/org/apache/ojb/broker: AllTests.java locking/LockManagerTest.java Date: Thu, 15 Mar 2007 01:01:18 -0000 To: ojb-commits@db.apache.org From: arminw@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070315010118.ECFD01A9842@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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