takari / io.takari.incrementalbuild

Eclipse Public License 1.0
16 stars 12 forks source link

ForkJoinPool behavior change in Java 11 breaks ComposableSecurityManagerPolicyTest #36

Closed guw closed 4 years ago

guw commented 4 years ago

ComposableSecurityManagerPolicyTest.testParallelStreamsThreadingWithSecurityManager fails with Java 11. It looks like a workaround for ForkJoinPool (in ComposableSecurityManagerPolicy) no longer works properly.

I found a Stack Overflow post which seems to confirm a change between JDK 8 and JDK 11.

Here is the error output from running the test with Java 11:

testParallelStreamsThreadingWithSecurityManager(io.takari.builder.enforcer.ComposableSecurityManagerPolicyTest)  Time elapsed: 0.019 s  <<< FAILURE!
java.lang.AssertionError:

Expecting:
  <["access denied ("java.util.PropertyPermission" "not.allowed" "read")",
    "access denied ("java.util.PropertyPermission" "allowed" "read")",
    "access denied ("java.util.PropertyPermission" "not.allowed" "read")",
    null,
    "access denied ("java.util.PropertyPermission" "not.allowed" "read")",
    "access denied ("java.util.PropertyPermission" "allowed" "read")"]>
to contain exactly (and in same order):
  <["nope", null, "nope", null, "nope", null]>
but some elements were not found:
  <["nope", "nope", null, "nope", null]>
and others were not expected:
  <["access denied ("java.util.PropertyPermission" "not.allowed" "read")",
    "access denied ("java.util.PropertyPermission" "allowed" "read")",
    "access denied ("java.util.PropertyPermission" "not.allowed" "read")",
    "access denied ("java.util.PropertyPermission" "not.allowed" "read")",
    "access denied ("java.util.PropertyPermission" "allowed" "read")"]>

    at io.takari.builder.enforcer.ComposableSecurityManagerPolicyTest.testParallelStreamsThreadingWithSecurityManager(ComposableSecurityManagerPolicyTest.java:115)

A potential fix for the test is to simply wrap any async execution with a PrivilegedAction and run it within the outer's AccessControlContext.

testParallelStreamsThreadingWithSecurityManager (line 108):

      AccessControlContext accessControlContext = AccessController.getContext();
      List<String> exs1 = work.parallelStream().map(x -> {
        return AccessController.doPrivileged((PrivilegedAction<Exception>) () -> {
          try {
            return x.call();
          } catch (Exception e) {
            return e;
          }
        }, accessControlContext);
      }).map(e -> e != null ? e.getMessage() : null).collect(Collectors.toList());