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 static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.LinkedList;
26  import java.util.List;
27  import java.util.concurrent.Callable;
28  import java.util.concurrent.CopyOnWriteArrayList;
29  import java.util.concurrent.ExecutorService;
30  import java.util.concurrent.Executors;
31  import java.util.concurrent.Future;
32  import java.util.concurrent.TimeUnit;
33  
34  import org.apache.commons.lang3.AbstractLangTest;
35  import org.apache.commons.lang3.concurrent.UncheckedFuture;
36  import org.junit.jupiter.api.Disabled;
37  import org.junit.jupiter.api.Test;
38  
39  /**
40   * Tests concurrent access for {@link ReflectionToStringBuilder}.
41   * <p>
42   * The {@link ToStringStyle} class includes a registry to avoid infinite loops for objects with circular references. We
43   * want to make sure that we do not get concurrency exceptions accessing this registry.
44   * </p>
45   * <p>
46   * The tests on the non-thread-safe collections do not pass.
47   * </p>
48   *
49   * @see <a href="https://issues.apache.org/jira/browse/LANG-762">[LANG-762] Handle or document ReflectionToStringBuilder
50   *      and ToStringBuilder for collections that are not thread safe</a>
51   * @since 3.1
52   */
53  public class ReflectionToStringBuilderConcurrencyTest extends AbstractLangTest {
54  
55      static class CollectionHolder<T extends Collection<?>> {
56          T collection;
57  
58          CollectionHolder(final T collection) {
59              this.collection = collection;
60          }
61      }
62  
63      private static final int DATA_SIZE = 100000;
64      private static final int REPEAT = 100;
65  
66      @Test
67      @Disabled
68      public void testArrayList() throws InterruptedException {
69          this.testConcurrency(new CollectionHolder<>(new ArrayList<>()));
70      }
71  
72      private void testConcurrency(final CollectionHolder<List<Integer>> holder) throws InterruptedException {
73          final List<Integer> list = holder.collection;
74          // make a big array that takes a long time to toString()
75          for (int i = 0; i < DATA_SIZE; i++) {
76              list.add(Integer.valueOf(i));
77          }
78          // Create a thread pool with two threads to cause the most contention on the underlying resource.
79          final ExecutorService threadPool = Executors.newFixedThreadPool(2);
80          try {
81              // Consumes toStrings
82              final Callable<Integer> consumer = () -> {
83                  for (int i = 0; i < REPEAT; i++) {
84                      final String s = ReflectionToStringBuilder.toString(holder);
85                      assertNotNull(s);
86                  }
87                  return Integer.valueOf(REPEAT);
88              };
89              // Produces changes in the list
90              final Callable<Integer> producer = () -> {
91                  for (int i = 0; i < DATA_SIZE; i++) {
92                      list.remove(list.get(0));
93                  }
94                  return Integer.valueOf(REPEAT);
95              };
96              final Collection<Callable<Integer>> tasks = new ArrayList<>();
97              tasks.add(consumer);
98              tasks.add(producer);
99              final List<Future<Integer>> futures = threadPool.invokeAll(tasks);
100             UncheckedFuture.on(futures).forEach(f -> assertEquals(REPEAT, f.get().intValue()));
101         } finally {
102             threadPool.shutdown();
103             threadPool.awaitTermination(1, TimeUnit.SECONDS);
104         }
105     }
106 
107     @Test
108     @Disabled
109     public void testCopyOnWriteArrayList() throws InterruptedException {
110         this.testConcurrency(new CollectionHolder<>(new CopyOnWriteArrayList<>()));
111     }
112 
113     @Test
114     @Disabled
115     public void testLinkedList() throws InterruptedException {
116         this.testConcurrency(new CollectionHolder<>(new LinkedList<>()));
117     }
118 }