11package mytest ;
22
3+ import net .openhft .compiler .CachedCompiler ;
34import net .openhft .compiler .CompilerUtils ;
45import org .junit .Test ;
56
67import java .net .URL ;
78import 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 ;
919import static org .junit .Assert .fail ;
1020
1121public 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
0 commit comments