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