View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3.builder;
19  
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.LinkedList;
23  import java.util.List;
24  import java.util.concurrent.Callable;
25  import java.util.concurrent.CopyOnWriteArrayList;
26  import java.util.concurrent.ExecutorService;
27  import java.util.concurrent.Executors;
28  import java.util.concurrent.Future;
29  import java.util.concurrent.TimeUnit;
30  
31  import org.apache.commons.lang3.AbstractLangTest;
32  import org.apache.commons.lang3.concurrent.UncheckedFuture;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Tests concurrent access for the default {@link ToStringStyle}.
37   * <p>
38   * The {@link ToStringStyle} class includes a registry to avoid infinite loops for objects with circular references. We
39   * want to make sure that we do not get concurrency exceptions accessing this registry.
40   * </p>
41   * <p>
42   * This test passes but only tests one aspect of the issue.
43   * </p>
44   *
45   * @see <a href="https://issues.apache.org/jira/browse/LANG-762">[LANG-762] Handle or document ReflectionToStringBuilder
46   *      and ToStringBuilder for collections that are not thread safe</a>
47   * @since 3.1
48   */
49  public class ToStringStyleConcurrencyTest extends AbstractLangTest {
50  
51      static class CollectionHolder<T extends Collection<?>> {
52          T collection;
53  
54          CollectionHolder(final T collection) {
55              this.collection = collection;
56          }
57      }
58  
59      private static final List<Integer> LIST;
60      private static final int LIST_SIZE = 100000;
61      private static final int REPEAT = 100;
62  
63      static {
64          LIST = new ArrayList<>(LIST_SIZE);
65          for (int i = 0; i < LIST_SIZE; i++) {
66              LIST.add(Integer.valueOf(i));
67          }
68      }
69  
70      @Test
71      public void testArrayList() throws InterruptedException {
72          this.testConcurrency(new CollectionHolder<>(new ArrayList<>()));
73      }
74  
75      private void testConcurrency(final CollectionHolder<List<Integer>> holder) throws InterruptedException {
76          final List<Integer> list = holder.collection;
77          // make a big array that takes a long time to toString()
78          list.addAll(LIST);
79          // Create a thread pool with two threads to cause the most contention on the underlying resource.
80          final ExecutorService threadPool = Executors.newFixedThreadPool(2);
81          try {
82              // Consumes toStrings
83              final Callable<Integer> consumer = () -> {
84                  for (int i = 0; i < REPEAT; i++) {
85                      // Calls ToStringStyle
86                      new ToStringBuilder(holder).append(holder.collection);
87                  }
88                  return Integer.valueOf(REPEAT);
89              };
90              final Collection<Callable<Integer>> tasks = new ArrayList<>();
91              tasks.add(consumer);
92              tasks.add(consumer);
93              final List<Future<Integer>> futures = threadPool.invokeAll(tasks);
94              UncheckedFuture.on(futures).forEach(UncheckedFuture::get);
95          } finally {
96              threadPool.shutdown();
97              threadPool.awaitTermination(1, TimeUnit.SECONDS);
98          }
99      }
100 
101     @Test
102     public void testCopyOnWriteArrayList() throws InterruptedException {
103         this.testConcurrency(new CollectionHolder<>(new CopyOnWriteArrayList<>()));
104     }
105 
106     @Test
107     public void testLinkedList() throws InterruptedException {
108         this.testConcurrency(new CollectionHolder<>(new LinkedList<>()));
109     }
110 }