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.jxpath.ri;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.fail;
22  
23  import org.apache.commons.jxpath.JXPathContext;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test thread safety.
28   */
29  public class StressTest {
30  
31      private static final class StressRunnable implements Runnable {
32  
33          @Override
34          public void run() {
35              for (int j = 0; j < THREAD_DURATION && exception == null; j++) {
36                  try {
37                      final double random = 1 + Math.random();
38                      final double sum = ((Double) context.getValue("/ + " + random)).doubleValue();
39                      assertEquals(0.0001, sum, 100 + random);
40                      synchronized (context) {
41                          count++;
42                      }
43                  } catch (final Throwable t) {
44                      exception = t;
45                  }
46              }
47          }
48      }
49  
50      private static final int THREAD_COUNT = 50;
51      private static final int THREAD_DURATION = 1000;
52      private static JXPathContext context;
53      private static int count;
54      private static Throwable exception;
55  
56      @Test
57      public void testThreads() throws Throwable {
58          context = JXPathContext.newContext(null, Double.valueOf(100));
59          final Thread[] threadArray = new Thread[THREAD_COUNT];
60          for (int i = 0; i < THREAD_COUNT; i++) {
61              threadArray[i] = new Thread(new StressRunnable());
62          }
63          for (final Thread element : threadArray) {
64              element.start();
65          }
66          for (final Thread element : threadArray) {
67              try {
68                  element.join();
69              } catch (final InterruptedException e) {
70                  fail("Interrupted");
71              }
72          }
73          if (exception != null) {
74              throw exception;
75          }
76          assertEquals(THREAD_COUNT * THREAD_DURATION, count, "Test count");
77      }
78  }