aboutsummaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
authormichalkurka <michalk@h2o.ai>2019-01-25 11:28:06 -0800
committermichalkurka <michalk@h2o.ai>2019-01-25 11:28:06 -0800
commitd64a1c8ab01215236790b6601c6ce06dc324a05c (patch)
tree7fd74a90e2262c874859c3164560147e788fb9bc /src/test
parent88a9d9b572be7e419538739456599e50525e8326 (diff)
downloadjavassist-d64a1c8ab01215236790b6601c6ce06dc324a05c.tar.gz
javassist-d64a1c8ab01215236790b6601c6ce06dc324a05c.zip
JAVASSIST-242: Demonstrates a race condition in DefineClassHelper
Diffstat (limited to 'src/test')
-rw-r--r--src/test/javassist/ConcurrentClassDefinitionTest.java75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/test/javassist/ConcurrentClassDefinitionTest.java b/src/test/javassist/ConcurrentClassDefinitionTest.java
new file mode 100644
index 00000000..c26360c4
--- /dev/null
+++ b/src/test/javassist/ConcurrentClassDefinitionTest.java
@@ -0,0 +1,75 @@
+package javassist;
+
+import javassist.bytecode.ClassFile;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class ConcurrentClassDefinitionTest {
+
+ @Parameterized.Parameter
+ public int N;
+
+ @Parameterized.Parameters
+ public static Object[] data() {
+ return new Object[] {
+ 1, // single threaded - should pass
+ Runtime.getRuntime().availableProcessors() * 2
+ };
+ }
+
+ @Test
+ public void showDefineClassRaceCondition() throws Exception{
+ Worker[] workers = new Worker[N];
+ for (int i = 0; i < N; i++) {
+ workers[i] = new Worker(N + "_ " + i, 100);
+ workers[i].start();
+ }
+ for (Worker w : workers) {
+ w.join();
+ }
+ for (Worker w : workers) {
+ if (w.e != null) {
+ throw w.e;
+ }
+ }
+ }
+
+ private static class Worker extends Thread {
+ String id;
+ int count;
+ Exception e;
+
+ Worker(String id, int count) {
+ this.id = id;
+ this.count = count;
+ }
+
+ @Override
+ public void run() {
+ try {
+ for (int i = 0; i < count; i++) {
+ Class c = makeClass(id + "_" + i);
+ assert c != null;
+ }
+ } catch (Exception e) {
+ this.e = e;
+ }
+ }
+
+ @Override
+ public void interrupt() {
+ super.interrupt();
+ }
+ }
+
+ private static Class makeClass(String id) throws Exception {
+ ClassFile cf = new ClassFile(
+ false, "com.example.JavassistGeneratedClass_" + id, null);
+ ClassPool classPool = ClassPool.getDefault();
+ return classPool.makeClass(cf).toClass();
+ }
+
+
+}