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    *      https://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  package org.apache.commons.beanutils2.bugs;
18  
19  import static org.junit.jupiter.api.Assertions.assertNotNull;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  import java.util.concurrent.ExecutorService;
31  import java.util.concurrent.Executors;
32  import java.util.concurrent.TimeUnit;
33  
34  import org.apache.commons.beanutils2.WrapDynaClass;
35  import org.apache.commons.lang3.concurrent.BasicThreadFactory;
36  import org.junit.jupiter.api.Disabled;
37  import org.junit.jupiter.api.Test;
38  import org.junit.jupiter.api.Timeout;
39  
40  /**
41   * Tests Jira issue BEANUTILS-509.
42   *
43   * The bug causes an infinite loop in WeakHashMap.get which was not accessed in a thread safe manner. This originates with WrapDynaClass.createDynaClass().
44   */
45  @Disabled("https://issues.apache.org/jira/browse/BEANUTILS-509.")
46  public class Jira509Test {
47  
48      protected int random(final int max) {
49          return (int) (Math.random() * 100_000) % max;
50      }
51  
52      /**
53       * The bug makes the {@link WrapDynaClass#createDynaClass} method run in an infinite loop and acquire locks. The test case adds a timeout. The test case may
54       * pass even without this fix because this is a rare scenario.
55       */
56      @Timeout(value = 60, unit = TimeUnit.SECONDS)
57      @Test
58      public void testConcurrent() throws InterruptedException {
59          final List<Class<?>> classList = Arrays.asList(Map.class, HashMap.class, Collections.class, Arrays.class, Collection.class, Set.class, ArrayList.class,
60                  List.class, HashSet.class);
61          // All daemon threads.
62          final ExecutorService executor = Executors.newFixedThreadPool(100, new BasicThreadFactory.Builder().daemon(true).build());
63          try {
64              // Loop _may_ hang without fix.
65              for (int i = 1; i < 10_000_000; i++) {
66                  executor.submit(new Runnable() {
67  
68                      final Class<?> clazz = classList.get(random(classList.size()));
69  
70                      @Override
71                      public void run() {
72                          final WrapDynaClass w = WrapDynaClass.createDynaClass(clazz);
73                          assertNotNull(w);
74                      }
75  
76                  });
77              }
78          } finally {
79              executor.shutdown();
80              executor.awaitTermination(3500, TimeUnit.MILLISECONDS);
81          }
82      }
83  
84  }