Skip to content

Commit

Permalink
Update async await
Browse files Browse the repository at this point in the history
  • Loading branch information
ngallagher committed Jan 26, 2019
1 parent 84d6a48 commit 8e571ad
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.snapscript.compile;

public class AwaitCascadeTest extends ScriptTestCase {

private static final String SOURCE_1 =
"async func one(){\n"+
" println(`one(): ` +Thread.currentThread().getName());\n"+
" let a1 = await two();\n"+
" let b1 = await two();\n"+
" return `one: ${a1}---${b1}`;\n"+
"}\n"+
"async func two(){\n"+
" println(`two(): ` +Thread.currentThread().getName());\n"+
" let a2 = await three();\n"+
" return `two: ${a2}`;\n"+
"}\n"+
"async func three(){\n"+
" println(`three(): ` +Thread.currentThread().getName());\n"+
" let a3 = await 'foo'.toUpperCase();\n"+
" return `three: ${a3}`;\n"+
"}\n"+
"one().join().success(this::println);\n"+
"one().join().success(result -> {\n"+
" assert result == 'one: two: three: FOO---two: three: FOO';\n"+
"});\n";

private static final String SOURCE_2 =
"async func one(n){\n"+
" if(n > 0) {\n"+
" println(`two(${n}): ` +Thread.currentThread().getName());\n"+
" await two(n-1);\n"+
" }\n"+
" return 'one';\n"+
"}\n"+
"\n"+
"async func two(n){\n"+
" if(n > 0) {\n"+
" println(`one(${n}): ` +Thread.currentThread().getName());\n"+
" await one(n-1);\n"+
" }\n"+
" return 'two';\n"+
"}\n"+
"one(100).join().success(this::println);\n";

public void testAwaitCascade() throws Exception {
assertScriptExecutes(SOURCE_1);
assertScriptExecutes(SOURCE_2);
}

@Override
public boolean isThreadPool() {
return true;
}
}
16 changes: 8 additions & 8 deletions snap-compile/src/test/java/org/snapscript/compile/AwaitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public class AwaitTest extends ScriptTestCase {
"}\n"+
"let x = fun(1);\n"+
"println(x.class);\n"+
"assert x.thenAccept(y -> {\n"+
"assert x.success(y -> {\n"+
" println(`RESULT=${y}`);\n"+
" assert y == 'result for foo()';\n" +
"}).get() == 'result for foo()';\n";
Expand All @@ -143,7 +143,7 @@ public class AwaitTest extends ScriptTestCase {
"}\n"+
"let x = Mod.fun(1);\n"+
"println(x.class);\n"+
"assert x.thenAccept(y -> {\n"+
"assert x.success(y -> {\n"+
" println(`RESULT=${y}`);\n"+
" assert y == 'result for foo()';\n" +
"}).get() == 'result for foo()';\n";
Expand All @@ -167,7 +167,7 @@ public class AwaitTest extends ScriptTestCase {
"}\n"+
"let x = Typ.fun(1);\n"+
"println(x.class);\n"+
"assert x.thenAccept(y -> {\n"+
"assert x.success(y -> {\n"+
" println(`RESULT=${y}`);\n"+
" assert y == 'result for foo()';\n" +
"}).get() == 'result for foo()';\n";
Expand All @@ -191,7 +191,7 @@ public class AwaitTest extends ScriptTestCase {
"}\n"+
"let x = new Typ().fun(1);\n"+
"println(x.class);\n"+
"assert x.thenAccept(y -> {\n"+
"assert x.success(y -> {\n"+
" println(`RESULT=${y}`);\n"+
" assert y == 'result for foo()';\n" +
"}).get() == 'result for foo()';\n";
Expand All @@ -215,7 +215,7 @@ public class AwaitTest extends ScriptTestCase {
"}\n"+
"let x = new Typ().fun(1);\n"+
"println(x.class);\n"+
"assert x.thenAccept(y -> {\n"+
"assert x.success(y -> {\n"+
" println(`RESULT=${y}`);\n"+
" assert y == 'result for foo()';\n" +
"}).get() == 'result for foo()';\n";
Expand All @@ -239,7 +239,7 @@ public class AwaitTest extends ScriptTestCase {
"}\n"+
"let x = new Typ().fun(1);\n"+
"println(x.class);\n"+
"assert x.thenAccept(y -> {\n"+
"assert x.success(y -> {\n"+
" println(`RESULT=${y}`);\n"+
" assert y == 'result for foo()';\n" +
"}).get() == 'result for foo()';\n";
Expand All @@ -257,7 +257,7 @@ public class AwaitTest extends ScriptTestCase {
"}\n"+
"let x = new Typ().fun(1);\n"+
"println(x.class);\n"+
"assert x.thenAccept(y -> {\n"+
"assert x.success(y -> {\n"+
" println(`RESULT=${y}`);\n"+
" assert y == 'result for fun(1)';\n" +
"}).get() == 'result for fun(1)';\n";
Expand All @@ -279,7 +279,7 @@ public class AwaitTest extends ScriptTestCase {
"}\n"+
"let x = new Typ().fun(1);\n"+
"println(x.class);\n"+
"x.thenCatch(y -> {\n"+
"x.failure(y -> {\n"+
" y.printStackTrace();\n"+
" println(`ERROR=${y}`);\n"+
" println(`ERROR=${y.class}`);\n"+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class ClosureTest extends TestCase {
" return await System.currentTimeMillis();\n"+
"};\n" +
"println(x.class);\n"+
"x(1).thenAccept(y -> println(y)).join();\n";
"x(1).success(y -> println(y)).join();\n";

public void testClosure() throws Exception {
DecimalFormat format = new DecimalFormat("###,###,###,###,###");
Expand Down
6 changes: 6 additions & 0 deletions snap-core/src/main/java/org/snapscript/core/Answer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.snapscript.core;

public interface Answer {
void success(Object value);
void failure(Throwable cause);
}
Loading

0 comments on commit 8e571ad

Please sign in to comment.