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