LantaoJin commented on code in PR #5348:
URL: https://github.com/apache/calcite/pull/5348#discussion_r4333921527


##########
core/src/test/java/org/apache/calcite/rel/RelCollationTest.java:
##########
@@ -220,6 +228,71 @@ class RelCollationTest {
     assertThat(CLUSTERED.reverse(), is(CLUSTERED));
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7872";>[CALCITE-7872]
+   * RelCollations and RelCollationImpl can deadlock during class
+   * initialization</a>.
+   *
+   * <p>Tests that the deprecated constants {@code RelCollationImpl.EMPTY} and
+   * {@code RelCollationImpl.PRESERVE} are the same objects as
+   * {@link RelCollations#EMPTY} and {@link RelCollations#PRESERVE}, whichever
+   * of the two classes is initialized first. They used to be null if
+   * {@code RelCollations} was initialized first. */
+  @Test void testDeprecatedConstantsInEitherInitializationOrder()
+      throws Exception {
+    final List<Class<?>> classes =
+        Arrays.asList(RelCollations.class, RelCollationImpl.class);
+    for (List<Class<?>> order : Arrays.asList(classes, 
Lists.reverse(classes))) {
+      final ClassLoader loader = new IsolatingClassLoader();
+      for (Class<?> c : order) {
+        Class.forName(c.getName(), true, loader);
+      }
+      final Class<?> relCollations =
+          loader.loadClass(RelCollations.class.getName());
+      final Class<?> relCollationImpl =
+          loader.loadClass(RelCollationImpl.class.getName());
+      for (String name : Arrays.asList("EMPTY", "PRESERVE")) {
+        final Object expected = relCollations.getField(name).get(null);
+        assertThat(expected, notNullValue());
+        assertThat(relCollationImpl.getField(name).get(null),
+            sameInstance(expected));
+      }
+    }
+  }
+
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7872";>[CALCITE-7872]
+   * RelCollations and RelCollationImpl can deadlock during class
+   * initialization</a>.
+   *
+   * <p>Tests that two threads can initialize {@link RelCollations} and
+   * {@link RelCollationImpl} at the same time. The static initializers of the
+   * two classes used to depend on each other, so the threads deadlocked. */
+  @Test void testConcurrentClassInitialization() throws Exception {

Review Comment:
    With the fix, it is deterministic. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to