From d5580806783cd70247de5ed06d74ab5f8b602405 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Tue, 18 Oct 2022 15:20:18 +0530 Subject: [PATCH] Add tests for async send action as expression --- .../BasicWorkerActionsNegativeTest.java | 15 +------ .../test/worker/BasicWorkerTest.java | 18 ++++++++ .../worker_async_send_as_expression.bal | 43 ++++++++++++++++++- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/BasicWorkerActionsNegativeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/BasicWorkerActionsNegativeTest.java index 922f1789e830..6eaf9b90c43a 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/BasicWorkerActionsNegativeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/BasicWorkerActionsNegativeTest.java @@ -31,8 +31,8 @@ public class BasicWorkerActionsNegativeTest { public void testWorkerActionsSemanticsNegative() { int index = 0; CompileResult resultSemanticsNegative = BCompileUtil.compile("test-src/workers/actions-semantics-negative.bal"); - Assert.assertEquals(resultSemanticsNegative.getErrorCount(), 10, "Worker actions semantics negative test error" + - " count"); + Assert.assertEquals(resultSemanticsNegative.getErrorCount(), 10, "Worker actions semantics negative test " + + "error count"); BAssertUtil.validateError(resultSemanticsNegative, index++, "invalid type for worker send 'Person', expected value:Cloneable", 42, 9); BAssertUtil.validateError(resultSemanticsNegative, index++, @@ -133,15 +133,4 @@ private String formatMessage(String workerName) { return String.format( "multiple references to a named worker '%s' as a variable reference is not allowed", workerName); } - - @Test(enabled = false) - public void testAsyncSendAsExpression() { - // TODO: support async send as expression issue #24849 - CompileResult compileResult = BCompileUtil.compile("test-src/workers/worker_async_send_as_expression.bal"); - int index = 0; - BAssertUtil.validateError(compileResult, index++, "async send action not yet supported as expression", - 19, 16); - - Assert.assertEquals(compileResult.getErrorCount(), index); - } } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/BasicWorkerTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/BasicWorkerTest.java index 7c3c69ae608e..31fef1d87b6f 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/BasicWorkerTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/BasicWorkerTest.java @@ -35,11 +35,16 @@ public class BasicWorkerTest { private CompileResult result; + private CompileResult asyncSendResult; @BeforeClass public void setup() { this.result = BCompileUtil.compile("test-src/workers/basic-worker-actions.bal"); Assert.assertEquals(result.getErrorCount(), 0, Arrays.asList(result.getDiagnostics()).toString()); + + this.asyncSendResult = + BCompileUtil.compile("test-src/workers/worker_async_send_as_expression.bal"); + Assert.assertEquals(asyncSendResult.getErrorCount(), 0, Arrays.asList(result.getDiagnostics()).toString()); } @Test @@ -129,6 +134,19 @@ public Object[] workerMessagePassingFunctions() { }; } + @Test(dataProvider = "asyncSendAsExpressionFunctions") + public void testAsyncSendAsExpression(String funcName) { + BRunUtil.invoke(asyncSendResult, funcName); + } + + @DataProvider(name = "asyncSendAsExpressionFunctions") + public Object[] asyncSendAsExpressionFunctions() { + return new Object[]{ + "testAsyncSendAsExpressionReturnType", + "testAsyncSendAsExpressionWithPanic" + }; + } + @AfterClass public void tearDown() { result = null; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/workers/worker_async_send_as_expression.bal b/tests/jballerina-unit-test/src/test/resources/test-src/workers/worker_async_send_as_expression.bal index 614e6d05432d..a1dfa75f2168 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/workers/worker_async_send_as_expression.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/workers/worker_async_send_as_expression.bal @@ -14,12 +14,51 @@ // specific language governing permissions and limitations // under the License. -function foo() { +function testAsyncSendAsExpressionReturnType() { worker w1 { () a = 5 -> w2; + assertValueEquality(a, ()); + + var b = "Foo" -> w2; + assertValueEquality(b, ()); } worker w2 { - int a = <- w1; + int x = <- w1; + assertValueEquality(5, x); + + string y = <- w1; + assertValueEquality("Foo", y); + } + + _ = wait {w1, w2}; +} + +function testAsyncSendAsExpressionWithPanic() { + worker w1 { + () a = 5 -> w2; + () b = 15 -> w2; + assertValueEquality(b, ()); + } + + worker w2 returns error? { + int x = <- w1; + if (x == 5) { + return error("This is an error"); + } + x = <- w1; + } + + map mapResult = wait {w1, w2}; +} + +type AssertionError distinct error; +const ASSERTION_ERROR_REASON = "AssertionError"; + +function assertValueEquality(anydata expected, anydata actual) { + if expected == actual { + return; } + panic error(ASSERTION_ERROR_REASON, + message = "expected '" + expected.toString() + "', found '" + actual.toString () + "'"); }