Return-Path: X-Original-To: apmail-directory-commits-archive@www.apache.org Delivered-To: apmail-directory-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9147911947 for ; Thu, 31 Jul 2014 13:44:53 +0000 (UTC) Received: (qmail 98798 invoked by uid 500); 31 Jul 2014 13:44:53 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 98731 invoked by uid 500); 31 Jul 2014 13:44:53 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 98722 invoked by uid 99); 31 Jul 2014 13:44:53 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 31 Jul 2014 13:44:53 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 31 Jul 2014 13:44:54 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id EA3712389154; Thu, 31 Jul 2014 13:44:28 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1614888 - /directory/apacheds/trunk/server-integ/src/test/java/MultiThreadedReadWriteTest.java Date: Thu, 31 Jul 2014 13:44:28 -0000 To: commits@directory.apache.org From: kayyagari@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140731134428.EA3712389154@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kayyagari Date: Thu Jul 31 13:44:27 2014 New Revision: 1614888 URL: http://svn.apache.org/r1614888 Log: test for DIRSERVER-1992 Added: directory/apacheds/trunk/server-integ/src/test/java/MultiThreadedReadWriteTest.java Added: directory/apacheds/trunk/server-integ/src/test/java/MultiThreadedReadWriteTest.java URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-integ/src/test/java/MultiThreadedReadWriteTest.java?rev=1614888&view=auto ============================================================================== --- directory/apacheds/trunk/server-integ/src/test/java/MultiThreadedReadWriteTest.java (added) +++ directory/apacheds/trunk/server-integ/src/test/java/MultiThreadedReadWriteTest.java Thu Jul 31 13:44:27 2014 @@ -0,0 +1,181 @@ +/* + * 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. + * + */ +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.apache.directory.api.ldap.model.cursor.EntryCursor; +import org.apache.directory.api.ldap.model.entry.DefaultEntry; +import org.apache.directory.api.ldap.model.message.SearchScope; +import org.apache.directory.ldap.client.api.LdapNetworkConnection; + +/** + * A class to test DIRSERVER-1992. + * + * + * @author Apache Directory Project + */ +public class MultiThreadedReadWriteTest +{ + + private volatile boolean stop = false; + + private Runnable reader = new Runnable() + { + + @Override + public void run() + { + try + { + LdapNetworkConnection connection = getConnection(); + while ( !stop ) + { + read( connection ); + } + + connection.close(); + } + catch( Exception e ) + { + System.out.println( "Reader " + Thread.currentThread().getId() + " failed" ); + e.printStackTrace(); + System.exit( 0 ); + } + } + }; + + private Runnable writer = new Runnable() + { + + @Override + public void run() + { + try + { + sync(); + } + catch( Exception e ) + { + System.out.println( "Sync failed" ); + e.printStackTrace(); + System.exit( 0 ); + } + } + }; + + private void read( LdapNetworkConnection connection ) throws Exception + { + EntryCursor cursor = connection.search( "ou=users,ou=system", "(objectClass=*)", SearchScope.ONELEVEL, "*" ); + int count = 0; + + while( cursor.next() ) + { + cursor.get(); + count++; + } + + System.out.println( "Read " + count + " entries" ); + } + + private void sync() throws Exception + { + LdapNetworkConnection connection = getConnection(); + String dn = "uid={uid},ou=users,ou=system"; + + String personTemplate = "objectClass: top\n" + + "objectClass: person\n" + + "objectClass: organizationalPerson\n" + + "objectClass: inetOrgPerson\n" + + "givenName: {uid}_{uid}\n" + + "sn: {uid}_sn\n" + + "cn: {uid}_cn\n" + + "uid: {uid}\n\n"; + + int total = 2500; + + System.out.println( "writing " ); + for( int i =0; i< total; i++ ) + { + String uid = "user"+ i; + String userDn = dn.replace( "{uid}", uid ); + String user = personTemplate.replace( "{uid}", uid ); + + DefaultEntry entry = new DefaultEntry( userDn, user ); + + connection.add( entry ); + } + + System.out.println( "deleting " ); + for( int i =0; i< total; i++ ) + { + String uid = "user"+ i; + String userDn = dn.replace( "{uid}", uid ); + + connection.delete( userDn ); + } + + connection.close(); + } + + + public static String getStackTrace( Throwable t ) + { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter( sw, true ); + t.printStackTrace( pw ); + pw.flush(); + sw.flush(); + return sw.toString(); + } + + + private LdapNetworkConnection getConnection() throws Exception + { + LdapNetworkConnection connection = new LdapNetworkConnection( "localhost", 10389 ); + connection.bind( "uid=admin,ou=system", "secret" ); + connection.setTimeOut( Long.MAX_VALUE ); + + return connection; + } + + + /** + * @param args + */ + public static void main( String[] args ) throws Exception + { + MultiThreadedReadWriteTest mtrwt = new MultiThreadedReadWriteTest(); + + Thread writer = new Thread( mtrwt.writer ); + writer.start(); + + for ( int i = 0; i < 5; ++i ) + { + Thread reader = new Thread( mtrwt.reader ); + reader.start(); + } + + System.out.println( "waiting for writer to stop" ); + writer.join(); + + mtrwt.stop = true; + } + +}