< 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.Joiner;
  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.where(name, "x").call(() -> {
-             try (var scope = new StructuredTaskScope<String>(null, factory)) {
+             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 +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 = new StructuredTaskScope<String>(null, factory)) {
+             try (var scope1 = StructuredTaskScope.open(Joiner.awaitAll(),
+                                                        cf -> cf.withThreadFactory(factory))) {
                  Subtask<String> subtask1 = scope1.fork(() -> {
-                     try (var scope2 = new StructuredTaskScope<String>(null, factory)) {
+                     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 +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 = new StructuredTaskScope<String>(null, factory)) {
+             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 = new StructuredTaskScope<String>(null, factory)) {
+                         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 +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.where(name, "x").run(() -> {
-                     box.scope = new StructuredTaskScope<Object>();
+                     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> 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,12 +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 = new StructuredTaskScope<String>()) {
-                 ScopedValue.where(name, "x").run(() -> {
+         try (var scope = StructuredTaskScope.open(Joiner.awaitAll())) {
+             ScopedValue.where(name, "x").run(() -> {
                  assertThrows(StructureViolationException.class, scope::close);
              });
          }
      }
  

@@ -177,12 +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 = new StructuredTaskScope<String>()) {
-                 ScopedValue.where(name, "x").run(() -> {
+         try (var scope = StructuredTaskScope.open(Joiner.awaitAll())) {
+             ScopedValue.where(name, "x").run(() -> {
                  assertThrows(StructureViolationException.class,
                          () -> scope.fork(() -> "foo"));
              });
          }
      }

@@ -195,22 +201,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(() -> {
+             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 = new StructuredTaskScope<String>()) {
-                     ScopedValue.where(name2, "y").run(() -> {
+             try (var scope = StructuredTaskScope.open(Joiner.awaitAll())) {
+                 ScopedValue.where(name2, "y").run(() -> {
                      assertThrows(StructureViolationException.class,
                              () -> scope.fork(() -> "foo"));
                  });
              }
          });
< prev index next >