changeset 407:f761bd60f8e2

7902023: Shuffle tasks between actors to better randomize the test
author shade
date Thu, 31 Aug 2017 16:32:40 +0200
parents 12d706400981
children 9e1246a9d0d0
files jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java tests-custom/src/main/java/org/openjdk/jcstress/tests/future/ThreadNames.java
diffstat 2 files changed, 71 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java	Thu Aug 31 11:54:10 2017 +0200
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java	Thu Aug 31 16:32:40 2017 +0200
@@ -367,8 +367,14 @@
         pw.println("        }");
 
         if (info.getArbiter() != null) {
-            pw.print("        ");
-            emitMethod(pw, info.getArbiter(), (isStateItself ? "s." : "t.") + info.getArbiter().getSimpleName(), "s", "r", true);
+            pw.println("        try {");
+            pw.print("            pool.submit(() ->");
+            emitMethod(pw, info.getArbiter(), (isStateItself ? "s." : "t.") + info.getArbiter().getSimpleName(), "s", "r", false);
+            pw.println(").get();");
+            pw.println("        } catch (ExecutionException e) {");
+            pw.println("            throw e.getCause();");
+            pw.println("        }");
+
         }
         pw.println("        counter.record(r);");
         pw.println("    }");
@@ -415,12 +421,18 @@
 
         pw.println();
         pw.println("        control.isStopped = false;");
-        pw.println("        Collection<Future<Counter<" + r + ">>> tasks = new ArrayList<>();");
+        pw.println();
+        pw.println("        List<Callable<Counter<" + r + ">>> tasks = new ArrayList<>();");
 
         for (ExecutableElement a : info.getActors()) {
-            pw.println("        tasks.add(pool.submit(this::" + a.getSimpleName() + "));");
+            pw.println("        tasks.add(this::" + a.getSimpleName() + ");");
         }
-
+        pw.println("        Collections.shuffle(tasks);");
+        pw.println();
+        pw.println("        Collection<Future<Counter<" + r + ">>> results = new ArrayList<>();");
+        pw.println("        for (Callable<Counter<" + r + ">> task : tasks) {");
+        pw.println("            results.add(pool.submit(task));");
+        pw.println("        }");
         pw.println();
         pw.println("        try {");
         pw.println("            TimeUnit.MILLISECONDS.sleep(config.time);");
@@ -429,10 +441,10 @@
         pw.println();
         pw.println("        control.isStopped = true;");
         pw.println();
-        pw.println("        waitFor(tasks);");
+        pw.println("        waitFor(results);");
         pw.println();
         pw.println("        Counter<" + r + "> counter = new Counter<>();");
-        pw.println("        for (Future<Counter<" + r + ">> f : tasks) {");
+        pw.println("        for (Future<Counter<" + r + ">> f : results) {");
         pw.println("            try {");
         pw.println("                counter.merge(f.get());");
         pw.println("            } catch (Throwable e) {");
@@ -907,7 +919,8 @@
                 ExecutorService.class, Future.class, TimeUnit.class,
                 TestConfig.class, TestResultCollector.class,
                 Runner.class, StateHolder.class, Counter.class,
-                WhiteBoxSupport.class, ExecutionException.class
+                WhiteBoxSupport.class, ExecutionException.class,
+                Callable.class, Collections.class, List.class,
         };
 
         for (Class<?> c : imports) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests-custom/src/main/java/org/openjdk/jcstress/tests/future/ThreadNames.java	Thu Aug 31 16:32:40 2017 +0200
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2017, Red Hat Inc. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jcstress.tests.future;
+
+import org.openjdk.jcstress.annotations.*;
+import org.openjdk.jcstress.infra.results.LLL_Result;
+
+@JCStressTest
+@Outcome(expect = Expect.ACCEPTABLE, desc = "acceptable")
+@State
+public class ThreadNames {
+
+    @Actor
+    public void actor1(LLL_Result r) {
+        r.r1 = Thread.currentThread().getName();
+    }
+
+    @Actor
+    public void actor2(LLL_Result r) {
+        r.r2 = Thread.currentThread().getName();
+    }
+
+    @Arbiter
+    public void arbiter(LLL_Result r) {
+        r.r3 = Thread.currentThread().getName();
+    }
+
+}