From 44cd641b4ecea28adc112ddf668380773cff9bb5 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Wed, 9 Nov 2011 14:34:43 +0000 Subject: [PATCH] [LANG-762] Handle or document ReflectionToStringBuilder and ToStringBuilder for collections that are not thread safe. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1199773 13f79535-47bb-0310-9956-ffa450edef68 --- .../builder/ToStringStyleConcurrencyTest.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java diff --git a/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java b/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java new file mode 100644 index 000000000..6975175a8 --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java @@ -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(); + } + } +}