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   */
52  public class ReflectionToStringBuilderConcurrencyTest extends AbstractLangTest {
53  
54      static class CollectionHolder<T extends Collection<?>> {
55          T collection;
56  
57          CollectionHolder(final T collection) {
58              this.collection = collection;
59          }
60      }
61  
62      private static final int DATA_SIZE = 100000;
63      private static final int REPEAT = 100;
64  
65      @Test
66      @Disabled
67      public void testArrayList() throws InterruptedException {
68          this.testConcurrency(new CollectionHolder<>(new ArrayList<>()));
69      }
70  
71      private void testConcurrency(final CollectionHolder<List<Integer>> holder) throws InterruptedException {
72          final List<Integer> list = holder.collection;
73          // make a big array that takes a long time to toString()
74          for (int i = 0; i < DATA_SIZE; i++) {
75              list.add(Integer.valueOf(i));
76          }
77          // Create a thread pool with two threads to cause the most contention on the underlying resource.
78          final ExecutorService threadPool = Executors.newFixedThreadPool(2);
79          try {
80              // Consumes toStrings
81              final Callable<Integer> consumer = () -> {
82                  for (int i = 0; i < REPEAT; i++) {
83                      final String s = ReflectionToStringBuilder.toString(holder);
84                      assertNotNull(s);
85                  }
86                  return Integer.valueOf(REPEAT);
87              };
88              // Produces changes in the list
89              final Callable<Integer> producer = () -> {
90                  for (int i = 0; i < DATA_SIZE; i++) {
91                      list.remove(list.get(0));
92                  }
93                  return Integer.valueOf(REPEAT);
94              };
95              final Collection<Callable<Integer>> tasks = new ArrayList<>();
96              tasks.add(consumer);
97              tasks.add(producer);
98              final List<Future<Integer>> futures = threadPool.invokeAll(tasks);
99              UncheckedFuture.on(futures).forEach(f -> assertEquals(REPEAT, f.get().intValue()));
100         } finally {
101             threadPool.shutdown();
102             threadPool.awaitTermination(1, TimeUnit.SECONDS);
103         }
104     }
105 
106     @Test
107     @Disabled
108     public void testCopyOnWriteArrayList() throws InterruptedException {
109         this.testConcurrency(new CollectionHolder<>(new CopyOnWriteArrayList<>()));
110     }
111 
112     @Test
113     @Disabled
114     public void testLinkedList() throws InterruptedException {
115         this.testConcurrency(new CollectionHolder<>(new LinkedList<>()));
116     }
117 }