< prev index next >

test/jdk/java/util/concurrent/StructuredTaskScope/WithScopedValue.java

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
+  * Copyright (c) 2022, 2024, Oracle and/or its affiliates. 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.

@@ -29,10 +29,11 @@
   * @run junit WithScopedValue
   */
  
  import java.util.concurrent.StructuredTaskScope;
  import java.util.concurrent.StructuredTaskScope.Subtask;
+ import java.util.concurrent.StructuredTaskScope.JoinPolicy;
  import java.util.concurrent.StructureViolationException;
  import java.util.concurrent.ThreadFactory;
  import java.util.concurrent.atomic.AtomicBoolean;
  import java.util.stream.Stream;
  

@@ -53,11 +54,12 @@
      @ParameterizedTest
      @MethodSource("factories")
      void testForkInheritsScopedValue1(ThreadFactory factory) throws Exception {
          ScopedValue<String> name = ScopedValue.newInstance();
          String value = ScopedValue.callWhere(name, "x", () -> {
-             try (var scope = new StructuredTaskScope<String>(null, factory)) {
+             try (var scope = StructuredTaskScope.open(JoinPolicy.ignoreAll(),
+                                                       cf -> cf.withThreadFactory(factory))) {
                  Subtask<String> subtask = scope.fork(() -> {
                      return name.get(); // child should read "x"
                  });
                  scope.join();
                  return subtask.get();

@@ -72,13 +74,15 @@
      @ParameterizedTest
      @MethodSource("factories")
      void testForkInheritsScopedValue2(ThreadFactory factory) throws Exception {
          ScopedValue<String> name = ScopedValue.newInstance();
          String value = ScopedValue.callWhere(name, "x", () -> {
-             try (var scope1 = new StructuredTaskScope<String>(null, factory)) {
+             try (var scope1 = StructuredTaskScope.open(JoinPolicy.ignoreAll(),
+                                                        cf -> cf.withThreadFactory(factory))) {
                  Subtask<String> subtask1 = scope1.fork(() -> {
-                     try (var scope2 = new StructuredTaskScope<String>(null, factory)) {
+                     try (var scope2 = StructuredTaskScope.open(JoinPolicy.ignoreAll(),
+                                                                cf -> cf.withThreadFactory(factory))) {
                          Subtask<String> subtask2 = scope2.fork(() -> {
                              return name.get(); // grandchild should read "x"
                          });
                          scope2.join();
                          return subtask2.get();

@@ -97,17 +101,19 @@
      @ParameterizedTest
      @MethodSource("factories")
      void testForkInheritsScopedValue3(ThreadFactory factory) throws Exception {
          ScopedValue<String> name = ScopedValue.newInstance();
          String value = ScopedValue.callWhere(name, "x", () -> {
-             try (var scope1 = new StructuredTaskScope<String>(null, factory)) {
+             try (var scope1 = StructuredTaskScope.open(JoinPolicy.ignoreAll(),
+                                                        cf -> cf.withThreadFactory(factory))) {
                  Subtask<String> subtask1 = scope1.fork(() -> {
                      assertEquals(name.get(), "x");  // child should read "x"
  
                      // rebind name to "y"
                      String grandchildValue = ScopedValue.callWhere(name, "y", () -> {
-                         try (var scope2 = new StructuredTaskScope<String>(null, factory)) {
+                         try (var scope2 = StructuredTaskScope.open(JoinPolicy.ignoreAll(),
+                                                                    cf -> cf.withThreadFactory(factory))) {
                              Subtask<String> subtask2 = scope2.fork(() -> {
                                  return name.get(); // grandchild should read "y"
                              });
                              scope2.join();
                              return subtask2.get();

@@ -129,33 +135,33 @@
       */
      @Test
      void testStructureViolation1() throws Exception {
          ScopedValue<String> name = ScopedValue.newInstance();
          class Box {
-             StructuredTaskScope<Object> scope;
+             StructuredTaskScope<Object, Void> scope;
          }
          var box = new Box();
          try {
              try {
                  ScopedValue.runWhere(name, "x", () -> {
-                     box.scope = new StructuredTaskScope<Object>();
+                     box.scope = StructuredTaskScope.open(JoinPolicy.ignoreAll());
                  });
                  fail();
              } catch (StructureViolationException expected) { }
  
              // underlying flock should be closed and fork should fail to start a thread
-             StructuredTaskScope<Object> scope = box.scope;
+             StructuredTaskScope<Object, Void> scope = box.scope;
              AtomicBoolean ran = new AtomicBoolean();
              Subtask<Object> subtask = scope.fork(() -> {
                  ran.set(true);
                  return null;
              });
              scope.join();
              assertEquals(Subtask.State.UNAVAILABLE, subtask.state());
              assertFalse(ran.get());
          } finally {
-             StructuredTaskScope<Object> scope = box.scope;
+             StructuredTaskScope<Object, Void> scope = box.scope;
              if (scope != null) {
                  scope.close();
              }
          }
      }

@@ -164,11 +170,11 @@
       * Test closing a StructuredTaskScope while executing in a dynamic scope.
       */
      @Test
      void testStructureViolation2() throws Exception {
          ScopedValue<String> name = ScopedValue.newInstance();
-         try (var scope = new StructuredTaskScope<String>()) {
+         try (var scope = StructuredTaskScope.open(JoinPolicy.ignoreAll())) {
              ScopedValue.runWhere(name, "x", () -> {
                  assertThrows(StructureViolationException.class, scope::close);
              });
          }
      }

@@ -177,11 +183,11 @@
       * Test fork when a scoped value is bound after a StructuredTaskScope is created.
       */
      @Test
      void testStructureViolation3() throws Exception {
          ScopedValue<String> name = ScopedValue.newInstance();
-         try (var scope = new StructuredTaskScope<String>()) {
+         try (var scope = StructuredTaskScope.open(JoinPolicy.ignoreAll())) {
              ScopedValue.runWhere(name, "x", () -> {
                  assertThrows(StructureViolationException.class,
                          () -> scope.fork(() -> "foo"));
              });
          }

@@ -195,21 +201,21 @@
          ScopedValue<String> name1 = ScopedValue.newInstance();
          ScopedValue<String> name2 = ScopedValue.newInstance();
  
          // rebind
          ScopedValue.runWhere(name1, "x", () -> {
-             try (var scope = new StructuredTaskScope<String>()) {
+             try (var scope = StructuredTaskScope.open(JoinPolicy.ignoreAll())) {
                  ScopedValue.runWhere(name1, "y", () -> {
                      assertThrows(StructureViolationException.class,
                              () -> scope.fork(() -> "foo"));
                  });
              }
          });
  
          // new binding
          ScopedValue.runWhere(name1, "x", () -> {
-             try (var scope = new StructuredTaskScope<String>()) {
+             try (var scope = StructuredTaskScope.open(JoinPolicy.ignoreAll())) {
                  ScopedValue.runWhere(name2, "y", () -> {
                      assertThrows(StructureViolationException.class,
                              () -> scope.fork(() -> "foo"));
                  });
              }
< prev index next >