Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4922C7DFE for ; Wed, 9 Nov 2011 14:35:06 +0000 (UTC) Received: (qmail 79103 invoked by uid 500); 9 Nov 2011 14:35:05 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 79016 invoked by uid 500); 9 Nov 2011 14:35:05 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 79009 invoked by uid 99); 9 Nov 2011 14:35:05 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 09 Nov 2011 14:35:05 +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; Wed, 09 Nov 2011 14:35:04 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 5E66223889FA for ; Wed, 9 Nov 2011 14:34:44 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1199773 - /commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java Date: Wed, 09 Nov 2011 14:34:44 -0000 To: commits@commons.apache.org From: ggregory@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111109143444.5E66223889FA@eris.apache.org> Author: ggregory Date: Wed Nov 9 14:34:43 2011 New Revision: 1199773 URL: http://svn.apache.org/viewvc?rev=1199773&view=rev Log: [LANG-762] Handle or document ReflectionToStringBuilder and ToStringBuilder for collections that are not thread safe. Added: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java Added: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java?rev=1199773&view=auto ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java (added) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java Wed Nov 9 14:34:43 2011 @@ -0,0 +1,108 @@ +/* + * 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. + */ + +package org.apache.commons.lang3.builder; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.Test; + +/** + * Tests concurrent access for the default {@link ToStringStyle}. + *

+ * The {@link ToStringStyle} class includes a registry to avoid infinite loops for objects with circular references. We + * want to make sure that we do not get concurrency exceptions accessing this registry. + *

+ *

+ * This test passes but only tests one aspect of the issue. + *

+ * + * @see [LANG-762] Handle or document ReflectionToStringBuilder + * and ToStringBuilder for collections that are not thread safe + * @since 3.0.2 + */ +public class ToStringStyleConcurrencyTest { + + static class CollectionHolder> { + T collection; + + CollectionHolder(T collection) { + this.collection = collection; + } + } + + private static final List LIST; + private static final int LIST_SIZE = 100000; + private static final int REPEAT = 100; + + static { + LIST = new ArrayList(LIST_SIZE); + for (int i = 0; i < LIST_SIZE; i++) { + LIST.add(Integer.valueOf(i)); + } + } + + @Test + public void testLinkedList() throws InterruptedException, ExecutionException { + this.testConcurrency(new CollectionHolder>(new LinkedList())); + } + + @Test + public void testArrayList() throws InterruptedException, ExecutionException { + this.testConcurrency(new CollectionHolder>(new ArrayList())); + } + + @Test + public void testCopyOnWriteArrayList() throws InterruptedException, ExecutionException { + this.testConcurrency(new CollectionHolder>(new CopyOnWriteArrayList())); + } + + private void testConcurrency(final CollectionHolder> holder) throws InterruptedException, + ExecutionException { + final List list = holder.collection; + // make a big array that takes a long time to toString() + list.addAll(LIST); + // Create a thread pool with two threads to cause the most contention on the underlying resource. + final ExecutorService threadPool = Executors.newFixedThreadPool(2); + // Consumes toStrings + Callable consumer = new Callable() { + public Integer call() { + for (int i = 0; i < REPEAT; i++) { + // Calls ToStringStyle + new ToStringBuilder(holder).append(holder.collection); + } + return REPEAT; + } + }; + Collection> tasks = new ArrayList>(); + tasks.add(consumer); + tasks.add(consumer); + final List> futures = threadPool.invokeAll(tasks); + for (Future future : futures) { + future.get(); + } + } +}