1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
25
26
27
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 }