Skip to content

Commit 9a4ae75

Browse files
committed
Ugly test for ConcurrentModificationException when running with Java 17 #73
1 parent 2bbfd6d commit 9a4ae75

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

src/test/java/mytest/RuntimeCompileTest.java

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
package mytest;
22

3+
import net.openhft.compiler.CachedCompiler;
34
import net.openhft.compiler.CompilerUtils;
45
import org.junit.Test;
56

67
import java.net.URL;
78
import java.net.URLClassLoader;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
13+
import java.util.concurrent.Future;
14+
import java.util.concurrent.TimeUnit;
15+
import java.util.concurrent.atomic.AtomicInteger;
16+
import java.util.function.IntSupplier;
817

18+
import static org.junit.Assert.assertEquals;
919
import static org.junit.Assert.fail;
1020

1121
public class RuntimeCompileTest {
@@ -18,17 +28,64 @@ public class RuntimeCompileTest {
1828
"}\n";
1929

2030
@Test
21-
public void outOfBounds() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
31+
public void outOfBounds() throws Exception {
2232
ClassLoader cl = new URLClassLoader(new URL[0]);
23-
Class aClass = CompilerUtils.CACHED_COMPILER.
33+
Class<?> aClass = CompilerUtils.CACHED_COMPILER.
2434
loadFromJava(cl, "mytest.Test", code);
25-
IntConsumer consumer = (IntConsumer) aClass.newInstance();
35+
IntConsumer consumer = (IntConsumer) aClass.getDeclaredConstructor().newInstance();
2636
consumer.accept(1); // ok
2737
try {
2838
consumer.accept(128); // no ok
2939
fail();
3040
} catch (IllegalArgumentException expected) {
3141
}
3242
}
43+
44+
@Test
45+
public void testMultiThread() throws Exception {
46+
StringBuilder largeClass = new StringBuilder("package mytest;\n" +
47+
"public class Test2 implements IntConsumer, java.util.function.IntSupplier {\n" +
48+
" static final java.util.concurrent.atomic.AtomicInteger called = new java.util.concurrent.atomic.AtomicInteger(0);\n" +
49+
" public int getAsInt() { return called.get(); }\n" +
50+
" public void accept(int num) {\n" +
51+
" called.incrementAndGet();\n" +
52+
" }\n");
53+
for (int j=0;j<1_000;j++) {
54+
largeClass.append(" public void accept"+j+"(int num) {\n" +
55+
" if ((byte) num != num)\n" +
56+
" throw new IllegalArgumentException();\n" +
57+
" }\n");
58+
}
59+
largeClass.append("}\n");
60+
final String code2 = largeClass.toString();
61+
62+
final ClassLoader cl = new URLClassLoader(new URL[0]);
63+
final CachedCompiler cc = new CachedCompiler(null, null);
64+
final int nThreads = Runtime.getRuntime().availableProcessors();
65+
final AtomicInteger started = new AtomicInteger(0);
66+
final ExecutorService executor = Executors.newFixedThreadPool(nThreads);
67+
final List<Future<?>> futures = new ArrayList<>();
68+
for (int i=0; i<nThreads; i++) {
69+
final int value = i;
70+
futures.add(executor.submit(() -> {
71+
started.incrementAndGet();
72+
while (started.get() < nThreads)
73+
;
74+
try {
75+
Class<?> aClass = cc.loadFromJava(cl, "mytest.Test2", code2);
76+
IntConsumer consumer = (IntConsumer) aClass.getDeclaredConstructor().newInstance();
77+
consumer.accept(value);
78+
} catch (Exception e) {
79+
throw new RuntimeException(e);
80+
}
81+
}));
82+
}
83+
executor.shutdown();
84+
for (Future<?> f : futures)
85+
f.get(10, TimeUnit.SECONDS);
86+
Class<?> aClass = cc.loadFromJava(cl, "mytest.Test2", code2);
87+
IntSupplier consumer = (IntSupplier) aClass.getDeclaredConstructor().newInstance();
88+
assertEquals(nThreads, consumer.getAsInt());
89+
}
3390
}
3491

src/test/java/net/openhft/compiler/CompilerTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class CompilerTest extends TestCase {
4747
}
4848
}
4949

50-
public void test_compiler() throws ClassNotFoundException {
50+
public void test_compiler() {
5151
try {
5252
// CompilerUtils.setDebug(true);
5353
// added so the test passes in Maven.
@@ -283,26 +283,27 @@ public void test_compilerErrorsDoNotBreakNextCompilations() throws Exception {
283283
"import java.util.concurrent.Callable; " +
284284
"public class OtherClass implements Callable<String> {" +
285285
"public String call() { return S.s; }}")
286+
.getDeclaredConstructor()
286287
.newInstance();
287288

288289
assertEquals("S", testClass.getName());
289290
assertEquals("ok", callable.call());
290291
}
291292

292293
@Test
293-
public void testNewCompiler() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
294+
public void testNewCompiler() throws Exception {
294295
for (int i = 1; i <= 3; i++) {
295296
ClassLoader classLoader = new ClassLoader() {
296297
};
297298
CachedCompiler cc = new CachedCompiler(null, null);
298299
Class a = cc.loadFromJava(classLoader, "A", "public class A { static int i = " + i + "; }");
299300
Class b = cc.loadFromJava(classLoader, "B", "public class B implements net.openhft.compiler.MyIntSupplier { public int get() { return A.i; } }");
300-
MyIntSupplier bi = (MyIntSupplier) b.newInstance();
301+
MyIntSupplier bi = (MyIntSupplier) b.getDeclaredConstructor().newInstance();
301302
assertEquals(i, bi.get());
302303
}
303304
}
304305

305-
public static void main(String[] args) throws ClassNotFoundException {
306+
public static void main(String[] args) {
306307
new CompilerTest().test_compiler();
307308
}
308309
}

0 commit comments

Comments
 (0)