< prev index next >

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

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

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

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

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

*** 129,33 ***
       */
      @Test
      void testStructureViolation1() throws Exception {
          ScopedValue<String> name = ScopedValue.newInstance();
          class Box {
!             StructuredTaskScope<Object> scope;
          }
          var box = new Box();
          try {
              try {
                  ScopedValue.where(name, "x").run(() -> {
!                     box.scope = new StructuredTaskScope<Object>();
                  });
                  fail();
              } catch (StructureViolationException expected) { }
  
              // underlying flock should be closed and fork should fail to start a thread
!             StructuredTaskScope<Object> 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;
              if (scope != null) {
                  scope.close();
              }
          }
      }
--- 135,33 ---
       */
      @Test
      void testStructureViolation1() throws Exception {
          ScopedValue<String> name = ScopedValue.newInstance();
          class Box {
!             StructuredTaskScope<Object, Void> scope;
          }
          var box = new Box();
          try {
              try {
                  ScopedValue.where(name, "x").run(() -> {
!                     box.scope = StructuredTaskScope.open(Joiner.awaitAll());
                  });
                  fail();
              } catch (StructureViolationException expected) { }
  
              // underlying flock should be closed and fork should fail to start a thread
!             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, Void> scope = box.scope;
              if (scope != null) {
                  scope.close();
              }
          }
      }

*** 164,12 ***
       * 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>()) {
!                 ScopedValue.where(name, "x").run(() -> {
                  assertThrows(StructureViolationException.class, scope::close);
              });
          }
      }
  
--- 170,12 ---
       * Test closing a StructuredTaskScope while executing in a dynamic scope.
       */
      @Test
      void testStructureViolation2() throws Exception {
          ScopedValue<String> name = ScopedValue.newInstance();
!         try (var scope = StructuredTaskScope.open(Joiner.awaitAll())) {
!             ScopedValue.where(name, "x").run(() -> {
                  assertThrows(StructureViolationException.class, scope::close);
              });
          }
      }
  

*** 177,12 ***
       * 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>()) {
!                 ScopedValue.where(name, "x").run(() -> {
                  assertThrows(StructureViolationException.class,
                          () -> scope.fork(() -> "foo"));
              });
          }
      }
--- 183,12 ---
       * 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 = StructuredTaskScope.open(Joiner.awaitAll())) {
!             ScopedValue.where(name, "x").run(() -> {
                  assertThrows(StructureViolationException.class,
                          () -> scope.fork(() -> "foo"));
              });
          }
      }

*** 195,22 ***
          ScopedValue<String> name1 = ScopedValue.newInstance();
          ScopedValue<String> name2 = ScopedValue.newInstance();
  
          // rebind
          ScopedValue.where(name1, "x").run(() -> {
!             try (var scope = new StructuredTaskScope<String>()) {
!                     ScopedValue.where(name1, "y").run(() -> {
                      assertThrows(StructureViolationException.class,
                              () -> scope.fork(() -> "foo"));
                  });
              }
          });
  
          // new binding
          ScopedValue.where(name1, "x").run(() -> {
!             try (var scope = new StructuredTaskScope<String>()) {
!                     ScopedValue.where(name2, "y").run(() -> {
                      assertThrows(StructureViolationException.class,
                              () -> scope.fork(() -> "foo"));
                  });
              }
          });
--- 201,22 ---
          ScopedValue<String> name1 = ScopedValue.newInstance();
          ScopedValue<String> name2 = ScopedValue.newInstance();
  
          // rebind
          ScopedValue.where(name1, "x").run(() -> {
!             try (var scope = StructuredTaskScope.open(Joiner.awaitAll())) {
!                 ScopedValue.where(name1, "y").run(() -> {
                      assertThrows(StructureViolationException.class,
                              () -> scope.fork(() -> "foo"));
                  });
              }
          });
  
          // new binding
          ScopedValue.where(name1, "x").run(() -> {
!             try (var scope = StructuredTaskScope.open(Joiner.awaitAll())) {
!                 ScopedValue.where(name2, "y").run(() -> {
                      assertThrows(StructureViolationException.class,
                              () -> scope.fork(() -> "foo"));
                  });
              }
          });
< prev index next >