1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @summary Nested continuations test
  27  * 
  28  * @run testng/othervm -Xint -XX:-UseContinuationLazyCopy Scoped
  29  * @run testng/othervm -Xint -XX:+UseContinuationLazyCopy Scoped
  30  * @run testng/othervm -Xcomp -XX:CompileOnly=java/lang/Continuation,Scoped -XX:-UseContinuationLazyCopy Scoped
  31  * @run testng/othervm -Xcomp -XX:CompileOnly=java/lang/Continuation,Scoped -XX:+UseContinuationLazyCopy Scoped
  32  */
  33 
  34 import java.util.Arrays;
  35 import java.util.EnumSet;
  36 import java.util.List;
  37 import java.util.stream.Collectors;
  38 import org.testng.annotations.Test;
  39 import org.testng.annotations.DataProvider;
  40 import static org.testng.Assert.*;
  41 
  42 import java.util.concurrent.atomic.AtomicInteger;
  43 
  44 @Test
  45 public class Scoped {
  46     static final ContinuationScope A = new ContinuationScope("A") {};
  47     static final ContinuationScope B = new ContinuationScope("B") {};
  48     static final ContinuationScope C = new ContinuationScope("C") {};
  49     static final ContinuationScope K = new ContinuationScope("K") {};
  50 
  51     public void test1() {
  52                 final AtomicInteger res = new AtomicInteger(0);
  53                 Continuation cont = new Continuation(A, ()-> {
  54                         double r = 0;
  55                         for (int k = 1; k < 2; k++) {
  56                                 int x = 3;
  57                                 String s = "abc";
  58                                 r += foo(k);
  59                         }
  60                         res.set((int)r);        
  61                 });
  62 
  63                 List<String> frames;
  64                 frames = cont.stackWalker().walk(fs -> fs.map(StackWalker.StackFrame::getMethodName).collect(Collectors.toList()));
  65                 // System.out.println("No scope (before start): " + frames);
  66                 assertEquals(frames, List.of());
  67 
  68                 while (!cont.isDone()) {
  69                         cont.run();
  70                         System.gc();
  71 
  72                         frames = cont.stackWalker().walk(fs -> fs.map(StackWalker.StackFrame::getMethodName).collect(Collectors.toList()));
  73                         // System.out.println("No scope: " + frames);
  74                         assertEquals(frames, cont.isDone() ? List.of() : Arrays.asList("yield0", "yield", "lambda$bar$14", "enter", "run", "bar", "lambda$foo$8", "enter", "run", "foo", "lambda$test1$0", "enter"));
  75 
  76                         frames = cont.stackWalker(EnumSet.noneOf(StackWalker.Option.class), A).walk(fs -> fs.map(StackWalker.StackFrame::getMethodName).collect(Collectors.toList()));
  77                         // System.out.println("A: " + frames);
  78                         assertEquals(frames, cont.isDone() ? List.of() : Arrays.asList("yield0", "yield", "lambda$bar$14", "enter", "run", "bar", "lambda$foo$8", "enter", "run", "foo", "lambda$test1$0", "enter"));
  79 
  80                         frames = cont.stackWalker(EnumSet.noneOf(StackWalker.Option.class), B).walk(fs -> fs.map(StackWalker.StackFrame::getMethodName).collect(Collectors.toList()));
  81                         // System.out.println("B: " + frames);
  82                         assertEquals(frames, cont.isDone() ? List.of() : Arrays.asList("yield0", "yield", "lambda$bar$14", "enter", "run", "bar", "lambda$foo$8", "enter"));
  83 
  84                         frames = cont.stackWalker(EnumSet.noneOf(StackWalker.Option.class), C).walk(fs -> fs.map(StackWalker.StackFrame::getMethodName).collect(Collectors.toList()));
  85                         // System.out.println("C: " + frames);
  86                         assertEquals(frames, cont.isDone() ? List.of() : Arrays.asList("yield0", "yield", "lambda$bar$14", "enter"));
  87 
  88                         frames = cont.stackWalker(EnumSet.noneOf(StackWalker.Option.class), K).walk(fs -> fs.map(StackWalker.StackFrame::getMethodName).collect(Collectors.toList()));
  89                         // System.out.println("K: " + frames);
  90                         assertEquals(frames, cont.isDone() ? List.of() : Arrays.asList("yield0", "yield", "lambda$bar$14", "enter", "run", "bar", "lambda$foo$8", "enter", "run", "foo", "lambda$test1$0", "enter"));
  91 
  92                         frames = cont.stackWalker(EnumSet.noneOf(StackWalker.Option.class), null).walk(fs -> fs.map(StackWalker.StackFrame::getMethodName).collect(Collectors.toList()));
  93                         // System.out.println("null: " + frames);
  94                         assertEquals(frames, cont.isDone() ? List.of() : Arrays.asList("yield0", "yield", "lambda$bar$14", "enter", "run", "bar", "lambda$foo$8", "enter", "run", "foo", "lambda$test1$0", "enter"));
  95                 }
  96                 assertEquals(res.get(), 2);
  97         }
  98 
  99     static double foo(int a) {
 100                 long x = 8;
 101                 String s = "yyy";
 102                 String r = null;
 103                 Continuation cont = new Continuation(B, ()-> {
 104                         bar(a + 1);
 105                 });
 106                 cont.run();
 107                 return 2; // Integer.parseInt(r)+1;
 108         }
 109 
 110         static String bar(long b) {
 111                 double x = 9.99;
 112                 String s = "zzz";
 113                 // Thread.dumpStack();
 114                 Continuation cont = new Continuation(C, ()-> {
 115                         Continuation.yield(A);
 116 
 117                         List<String> frames = StackWalker.getInstance().walk(fs -> fs.map(StackWalker.StackFrame::getMethodName).collect(Collectors.toList()));
 118                 
 119                         assertEquals(frames.subList(0, 12), Arrays.asList("lambda$bar$14", "enter", "run", "bar", "lambda$foo$8", "enter", "run", "foo", "lambda$test1$0", "enter", "run", "test1"));
 120                 
 121                         frames = StackWalker.getInstance(C).walk(fs -> fs.map(StackWalker.StackFrame::getMethodName).collect(Collectors.toList()));
 122                 
 123                         assertEquals(frames, Arrays.asList("lambda$bar$14", "enter"));
 124 
 125                         frames = StackWalker.getInstance(B).walk(fs -> fs.map(StackWalker.StackFrame::getMethodName).collect(Collectors.toList()));
 126                 
 127                         assertEquals(frames, Arrays.asList("lambda$bar$14", "enter", "run", "bar", "lambda$foo$8", "enter"));
 128 
 129                         frames = StackWalker.getInstance(A).walk(fs -> fs.map(StackWalker.StackFrame::getMethodName).collect(Collectors.toList()));
 130                 
 131                         assertEquals(frames, Arrays.asList("lambda$bar$14", "enter", "run", "bar", "lambda$foo$8", "enter", "run", "foo", "lambda$test1$0", "enter"));
 132 
 133                         frames = StackWalker.getInstance(K).walk(fs -> fs.map(StackWalker.StackFrame::getMethodName).collect(Collectors.toList()));
 134                 
 135                         assertEquals(frames.subList(0, 12), Arrays.asList("lambda$bar$14", "enter", "run", "bar", "lambda$foo$8", "enter", "run", "foo", "lambda$test1$0", "enter", "run", "test1"));
 136 
 137                         long r = b+1;
 138                 });
 139                 cont.run();
 140                 return "" + 3;
 141         }
 142 }